Annotation of parser3/src/classes/xdoc.C, revision 1.195
1.1 parser 1: /** @file
1.2 parser 2: Parser: @b xdoc parser class.
1.1 parser 3:
1.192 moko 4: Copyright (c) 2001-2017 Art. Lebedev Studio (http://www.artlebedev.com)
1.85 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.96 paf 6: */
1.1 parser 7:
1.111 paf 8: #include "pa_config_includes.h"
9:
1.1 parser 10: #ifdef XML
1.96 paf 11:
1.69 paf 12: #include "libxml/tree.h"
1.133 paf 13: #include "libxml/HTMLtree.h"
1.69 paf 14: #include "libxslt/xsltInternals.h"
15: #include "libxslt/transform.h"
16: #include "libxslt/xsltutils.h"
1.90 paf 17: #include "libxslt/variables.h"
1.132 paf 18: #include "libxslt/imports.h"
1.111 paf 19:
20: #include "pa_vmethod_frame.h"
21:
22: #include "pa_stylesheet_manager.h"
23: #include "pa_request.h"
24: #include "pa_vxdoc.h"
25: #include "pa_charset.h"
26: #include "pa_vfile.h"
1.115 paf 27: #include "pa_xml_exception.h"
1.111 paf 28: #include "xnode.h"
1.171 misha 29: #include "pa_charsets.h"
1.111 paf 30:
1.195 ! moko 31: volatile const char * IDENT_XDOC_C="$Id: xdoc.C,v 1.194 2018/01/11 00:03:56 moko Exp $";
1.173 moko 32:
1.1 parser 33: // defines
34:
35: #define XDOC_CLASS_NAME "xdoc"
36:
37: // class
38:
1.111 paf 39: class MXdoc: public MXnode {
1.1 parser 40: public: // VStateless_class
1.164 misha 41: Value* create_new_value(Pool&) { return new VXdoc(); }
1.1 parser 42:
43: public:
1.111 paf 44: MXdoc();
1.1 parser 45:
46: };
47:
1.111 paf 48: // global variable
49:
1.186 moko 50: DECLARE_CLASS_VAR(xnode, new MXnode); // must be here as Xdoc is inherited from Xnode and should be inited before
1.184 moko 51: DECLARE_CLASS_VAR(xdoc, new MXdoc);
1.61 paf 52:
1.138 paf 53: // helper classes
54:
55: class xmlOutputBuffer_auto_ptr {
56: public:
57: explicit xmlOutputBuffer_auto_ptr(xmlOutputBuffer *_APtr = 0)
58: : _Owns(_APtr != 0), _Ptr(_APtr) {}
59: xmlOutputBuffer_auto_ptr(const xmlOutputBuffer_auto_ptr& _Y)
60: : _Owns(_Y._Owns), _Ptr(_Y.release()) {}
61: xmlOutputBuffer_auto_ptr& operator=(const xmlOutputBuffer_auto_ptr& _Y)
62: {if (this != &_Y)
63: {if (_Ptr != _Y.get())
64: {if (_Owns && _Ptr)
65: xmlOutputBufferClose(_Ptr);
66: _Owns = _Y._Owns; }
67: else if (_Y._Owns)
68: _Owns = true;
69: _Ptr = _Y.release(); }
70: return (*this); }
71: ~xmlOutputBuffer_auto_ptr()
72: {if (_Owns && _Ptr)
73: xmlOutputBufferClose(_Ptr); }
74: xmlOutputBuffer& operator*() const
75: {return (*get()); }
76: xmlOutputBuffer *operator->() const
77: {return (get()); }
78: xmlOutputBuffer *get() const
79: {return (_Ptr); }
80: xmlOutputBuffer *release() const
81: {((xmlOutputBuffer_auto_ptr *)this)->_Owns = false;
82: return (_Ptr); }
83: private:
84: bool _Owns;
85: xmlOutputBuffer *_Ptr;
86: };
87:
1.65 paf 88: class xsltTransformContext_auto_ptr {
89: public:
1.67 paf 90: explicit xsltTransformContext_auto_ptr(xsltTransformContext *_APtr = 0)
91: : _Owns(_APtr != 0), _Ptr(_APtr) {}
1.65 paf 92: xsltTransformContext_auto_ptr(const xsltTransformContext_auto_ptr& _Y)
93: : _Owns(_Y._Owns), _Ptr(_Y.release()) {}
94: xsltTransformContext_auto_ptr& operator=(const xsltTransformContext_auto_ptr& _Y)
95: {if (this != &_Y)
96: {if (_Ptr != _Y.get())
97: {if (_Owns && _Ptr)
98: xsltFreeTransformContext(_Ptr);
99: _Owns = _Y._Owns; }
100: else if (_Y._Owns)
101: _Owns = true;
102: _Ptr = _Y.release(); }
103: return (*this); }
104: ~xsltTransformContext_auto_ptr()
105: {if (_Owns && _Ptr)
106: xsltFreeTransformContext(_Ptr); }
107: xsltTransformContext& operator*() const
108: {return (*get()); }
109: xsltTransformContext *operator->() const
110: {return (get()); }
111: xsltTransformContext *get() const
112: {return (_Ptr); }
113: xsltTransformContext *release() const
114: {((xsltTransformContext_auto_ptr *)this)->_Owns = false;
115: return (_Ptr); }
116: private:
117: bool _Owns;
118: xsltTransformContext *_Ptr;
119: };
1.61 paf 120:
1.65 paf 121: class xsltStylesheet_auto_ptr {
122: public:
1.67 paf 123: explicit xsltStylesheet_auto_ptr(xsltStylesheet *_APtr = 0)
124: : _Owns(_APtr != 0), _Ptr(_APtr) {}
1.65 paf 125: xsltStylesheet_auto_ptr(const xsltStylesheet_auto_ptr& _Y)
126: : _Owns(_Y._Owns), _Ptr(_Y.release()) {}
127: xsltStylesheet_auto_ptr& operator=(const xsltStylesheet_auto_ptr& _Y)
128: {if (this != &_Y)
129: {if (_Ptr != _Y.get())
130: {if (_Owns && _Ptr)
131: xsltFreeStylesheet(_Ptr);
132: _Owns = _Y._Owns; }
133: else if (_Y._Owns)
134: _Owns = true;
135: _Ptr = _Y.release(); }
136: return (*this); }
137: ~xsltStylesheet_auto_ptr()
138: {if (_Owns && _Ptr)
139: xsltFreeStylesheet(_Ptr); }
140: xsltStylesheet& operator*() const
141: {return (*get()); }
142: xsltStylesheet *operator->() const
143: {return (get()); }
144: xsltStylesheet *get() const
145: {return (_Ptr); }
146: xsltStylesheet *release() const
147: {((xsltStylesheet_auto_ptr *)this)->_Owns = false;
148: return (_Ptr); }
149: private:
150: bool _Owns;
151: xsltStylesheet *_Ptr;
152: };
1.61 paf 153:
1.1 parser 154: // methods
155:
1.149 paf 156: static void writeNode(Request& r, VXdoc& xdoc, xmlNode* node) {
157: if(!node)
1.154 misha 158: throw Exception(PARSER_RUNTIME,
1.149 paf 159: 0,
160: "error creating node"); // OOM, bad name, things like that
1.54 paf 161:
162: // write out result
1.190 moko 163: r.write(xdoc.wrap(*node));
1.149 paf 164: }
165:
166: struct IdsIteratorInfo {
167: xmlChar *elementId;
168: xmlNode *element;
169: };
170:
171: /* Hash Scanner function for pa_getElementById */
172: extern "C" void // switching to calling convetion of libxml
173: idsHashScanner (void *payload, void *data, xmlChar *name) {
174: IdsIteratorInfo *priv = (IdsIteratorInfo *)data;
175:
176: if (priv->element == NULL && xmlStrEqual (name, priv->elementId))
177: {
178: xmlNode* parent=((xmlID *)payload)->attr->parent;
179: assert(parent);
180: priv->element=parent;
181: }
182: }
183:
184: static xmlNode*
185: pa_getElementById(xmlDoc& xmldoc, xmlChar* elementId) {
186: xmlHashTable *ids = (xmlHashTable *)xmldoc.ids;
187: IdsIteratorInfo iter={elementId, NULL};
188: xmlHashScan(ids, idsHashScanner, &iter);
189: return iter.element;
190: }
191:
192: /*
193: static xmlNode *
194: pa_importNode (xmlDoc& xmldoc, xmlNode& importedNode, bool deep) {
195: xmlNode *result = NULL;
196:
197: switch (importedNode.type) {
198: case XML_ATTRIBUTE_NODE:
199: result = (xmlNode *)xmlCopyProp(xmldoc, (xmlAttr *)importedNode);
200: result.parent=0; // no idea
201: break;
202: case XML_DOCUMENT_FRAG_NODE:
203: case XML_ELEMENT_NODE:
204: case XML_ENTITY_REF_NODE:
205: case XML_PI_NODE:
206: case XML_TEXT_NODE:
207: case XML_CDATA_SECTION_NODE:
208: case XML_COMMENT_NODE:
209: result = xmlCopyNode (importedNode->n, deep);
210: xmlSetTreeDoc (result, priv->n);
211: break;
212: default:
213: *exc = GDOME_NOT_SUPPORTED_ERR;
214: }
215:
216: return result;
1.54 paf 217: }
1.149 paf 218: */
1.54 paf 219:
1.16 parser 220: // Element createElement(in DOMString tagName) raises(DOMException);
1.111 paf 221: static void _createElement(Request& r, MethodParams& params) {
1.168 misha 222: xmlChar* tagName=as_xmlname(r, params, 0, "tagName must be string");
223:
1.111 paf 224: VXdoc& vdoc=GET_SELF(r, VXdoc);
1.149 paf 225: xmlDoc& xmldoc=vdoc.get_xmldoc();
1.16 parser 226:
1.149 paf 227: xmlNode *node=xmlNewDocNode(&xmldoc, NULL, tagName, NULL);
228: writeNode(r, vdoc, node);
1.16 parser 229: }
230:
1.113 paf 231: // Element createElementNS(in DOMString namespaceURI, in DOMString qualifiedName) raises(DOMException);
1.112 paf 232: static void _createElementNS(Request& r, MethodParams& params) {
1.168 misha 233: xmlChar* namespaceURI=as_xmlnsuri(r, params, 0);
234: xmlChar* qualifiedName=as_xmlqname(r, params, 1);
235:
1.112 paf 236: VXdoc& vdoc=GET_SELF(r, VXdoc);
1.149 paf 237: xmlDoc& xmldoc=vdoc.get_xmldoc();
1.112 paf 238:
1.149 paf 239: xmlChar* prefix=0;
240: xmlChar* localName=xmlSplitQName2(qualifiedName, &prefix);
1.112 paf 241:
1.149 paf 242: xmlNode *node;
243: if(localName) {
244: xmlNs& ns=pa_xmlMapNs(xmldoc, namespaceURI, prefix);
245: node=xmlNewDocNode(&xmldoc, &ns, localName, NULL);
246: } else
247: node=xmlNewDocNode(&xmldoc, NULL, qualifiedName/*unqualified, actually*/, NULL);
248: writeNode(r, vdoc, node);
1.112 paf 249: }
250:
1.16 parser 251: // DocumentFragment createDocumentFragment()
1.111 paf 252: static void _createDocumentFragment(Request& r, MethodParams&) {
253: VXdoc& vdoc=GET_SELF(r, VXdoc);
1.149 paf 254: xmlDoc& xmldoc=vdoc.get_xmldoc();
1.16 parser 255:
1.149 paf 256: xmlNode *node=xmlNewDocFragment(&xmldoc);
257: writeNode(r, vdoc, node);
1.16 parser 258: }
259:
260: // Text createTextNode(in DOMString data);
1.111 paf 261: static void _createTextNode(Request& r, MethodParams& params) {
1.168 misha 262: xmlChar* data=as_xmlchar(r, params, 0, XML_DATA_MUST_BE_STRING);
263:
1.111 paf 264: VXdoc& vdoc=GET_SELF(r, VXdoc);
1.149 paf 265: xmlDoc& xmldoc=vdoc.get_xmldoc();
1.16 parser 266:
1.149 paf 267: xmlNode *node=xmlNewDocText(&xmldoc, data);
268: writeNode(r, vdoc, node);
1.16 parser 269: }
270:
271: // Comment createComment(in DOMString data)
1.111 paf 272: static void _createComment(Request& r, MethodParams& params) {
1.168 misha 273: xmlChar* data=as_xmlchar(r, params, 0, XML_DATA_MUST_BE_STRING);
274:
1.111 paf 275: VXdoc& vdoc=GET_SELF(r, VXdoc);
1.16 parser 276:
1.149 paf 277: xmlNode *node=xmlNewComment(data);
278: writeNode(r, vdoc, node);
1.16 parser 279: }
280:
281: // CDATASection createCDATASection(in DOMString data) raises(DOMException);
1.111 paf 282: static void _createCDATASection(Request& r, MethodParams& params) {
1.168 misha 283: xmlChar* data=as_xmlchar(r, params, 0, XML_DATA_MUST_BE_STRING);
284:
1.111 paf 285: VXdoc& vdoc=GET_SELF(r, VXdoc);
1.149 paf 286: xmlDoc& xmldoc=vdoc.get_xmldoc();
1.16 parser 287:
1.149 paf 288: xmlNode *node=xmlNewCDataBlock(&xmldoc, data, strlen((const char*)data));
289: writeNode(r, vdoc, node);
1.16 parser 290: }
291:
292: // ProcessingInstruction createProcessingInstruction(in DOMString target,in DOMString data) raises(DOMException);
1.111 paf 293: static void _createProcessingInstruction(Request& r, MethodParams& params) {
1.168 misha 294: xmlChar* target=as_xmlchar(r, params, 0, XML_DATA_MUST_BE_STRING);
295: xmlChar* data=as_xmlchar(r, params, 1, XML_DATA_MUST_BE_STRING);
296:
1.111 paf 297: VXdoc& vdoc=GET_SELF(r, VXdoc);
1.149 paf 298: xmlDoc& xmldoc=vdoc.get_xmldoc();
1.16 parser 299:
1.149 paf 300: xmlNode *node=xmlNewDocPI(&xmldoc, target, data);
301: writeNode(r, vdoc, node);
1.16 parser 302: }
303:
304: // Attr createAttribute(in DOMString name) raises(DOMException);
1.111 paf 305: static void _createAttribute(Request& r, MethodParams& params) {
1.168 misha 306: xmlChar* name=as_xmlname(r, params, 0);
307:
1.111 paf 308: VXdoc& vdoc=GET_SELF(r, VXdoc);
1.149 paf 309: xmlDoc& xmldoc=vdoc.get_xmldoc();
1.16 parser 310:
1.149 paf 311: xmlNode *node=(xmlNode*)xmlNewDocProp(&xmldoc, name, 0);
312: writeNode(r, vdoc, node);
1.16 parser 313: }
1.112 paf 314:
1.113 paf 315: // Attr createAttributeNS(in DOMString namespaceURI, in DOMString qualifiedName) raises(DOMException);
1.112 paf 316: static void _createAttributeNS(Request& r, MethodParams& params) {
1.168 misha 317: xmlChar* namespaceURI=as_xmlnsuri(r, params, 0);
318: xmlChar* qualifiedName=as_xmlqname(r, params, 1);
319:
1.112 paf 320: VXdoc& vdoc=GET_SELF(r, VXdoc);
1.149 paf 321: xmlDoc& xmldoc=vdoc.get_xmldoc();
1.112 paf 322:
1.149 paf 323: xmlChar* prefix=0;
324: xmlChar* localName=xmlSplitQName2(qualifiedName, &prefix);
325:
326: xmlNode *node;
327: if(localName) {
328: xmlNs& ns=pa_xmlMapNs(xmldoc, namespaceURI, prefix);
329: node=(xmlNode*)xmlNewDocProp(&xmldoc, localName, NULL);
330: xmlSetNs(node, &ns);
331: } else
332: node=(xmlNode*)xmlNewDocProp(&xmldoc, qualifiedName/*unqualified, actually*/, NULL);
333: writeNode(r, vdoc, node);
1.112 paf 334: }
335:
1.16 parser 336: // EntityReference createEntityReference(in DOMString name) raises(DOMException);
1.111 paf 337: static void _createEntityReference(Request& r, MethodParams& params) {
1.168 misha 338: xmlChar* name=as_xmlname(r, params, 0);
339:
1.111 paf 340: VXdoc& vdoc=GET_SELF(r, VXdoc);
1.149 paf 341: xmlDoc& xmldoc=vdoc.get_xmldoc();
1.16 parser 342:
1.149 paf 343: xmlNode *node=xmlNewReference(&xmldoc, name);
344: writeNode(r, vdoc, node);
1.99 paf 345: }
346:
347:
1.111 paf 348: static void _getElementById(Request& r, MethodParams& params) {
1.168 misha 349: xmlChar* elementId=as_xmlname(r, params, 0, "elementID must be string");
350:
1.111 paf 351: VXdoc& vdoc=GET_SELF(r, VXdoc);
1.149 paf 352: xmlDoc& xmldoc=vdoc.get_xmldoc();
1.16 parser 353:
1.168 misha 354: if(xmlNode *node=pa_getElementById(xmldoc, elementId))
1.149 paf 355: writeNode(r, vdoc, node);
1.54 paf 356: }
1.79 paf 357:
1.111 paf 358: static void _importNode(Request& r, MethodParams& params) {
1.168 misha 359: xmlNode& importedNode=as_node(params, 0, "importedNode must be node");
360: bool deep=params.as_bool(1, "deep must be bool", r);
361:
1.111 paf 362: VXdoc& vdoc=GET_SELF(r, VXdoc);
1.149 paf 363: xmlDoc& xmldoc=vdoc.get_xmldoc();
1.79 paf 364:
1.149 paf 365: xmlNode *node=xmlDocCopyNode(&importedNode, &xmldoc, deep?1: 0);
366: writeNode(r, vdoc, node);
1.79 paf 367: }
1.168 misha 368:
1.195 ! moko 369: #define XML_PARSE_OPTIONS (XML_PARSE_DTDLOAD | XML_PARSE_NOENT | XML_PARSE_HUGE)
1.1 parser 370:
1.111 paf 371: static void _create(Request& r, MethodParams& params) {
372: Charset& source_charset=r.charsets.source();
373: VXdoc& vdoc=GET_SELF(r, VXdoc);
1.1 parser 374:
1.111 paf 375: Value& param=params[params.count()-1];
1.149 paf 376: xmlDoc* xmldoc;
1.111 paf 377: bool set_encoding=false;
1.95 paf 378: if(param.get_junction()) { // {<?xml?>...}
1.189 moko 379:
1.91 paf 380: const String& xml=r.process_to_string(param);
1.189 moko 381: String::Body sbody=xml.cstr_to_string_body_untaint(String::L_XML, r.connection(false), &r.charsets);
1.83 paf 382:
1.195 ! moko 383: xmldoc=xmlReadMemory(sbody.cstr(), sbody.length(), NULL, NULL, XML_PARSE_OPTIONS);
1.163 misha 384:
1.149 paf 385: if(!xmldoc || xmlHaveGenericErrors())
1.179 moko 386: throw XmlException(0, r);
1.111 paf 387:
388: // must be last action in if, see after if}
1.149 paf 389: } else { // [localName]
1.153 misha 390: if(const String* value = param.get_string()){
391: xmlChar* localName=r.transcode(*value);
1.168 misha 392: if(xmlValidateNCName(localName, 0) != 0)
393: throw XmlException(0, XML_INVALID_LOCAL_NAME, localName);
394:
1.151 misha 395: xmldoc=xmlNewDoc(0);
396: if(!xmldoc || xmlHaveGenericErrors())
1.179 moko 397: throw XmlException(0, r);
1.168 misha 398:
1.151 misha 399: xmlNode* node=xmlNewChild((xmlNode*)xmldoc, NULL, localName, NULL);
400: if(!node || xmlHaveGenericErrors())
1.179 moko 401: throw XmlException(0, r);
1.103 paf 402:
1.151 misha 403: set_encoding=true;
404: // must be last action in if, see after if}
1.153 misha 405: } else {
1.161 misha 406: VFile* vfile=param.as_vfile(String::L_AS_IS);
1.195 ! moko 407: xmldoc=xmlReadMemory(vfile->value_ptr(), vfile->value_size(), NULL, NULL, XML_PARSE_OPTIONS);
1.153 misha 408: if(!xmldoc || xmlHaveGenericErrors())
1.179 moko 409: throw XmlException(0, r);
1.151 misha 410: }
1.111 paf 411: }
412: // must be first action after if}
413: // replace any previous parsed source
1.149 paf 414: vdoc.set_xmldoc(r.charsets, *xmldoc);
1.95 paf 415:
416: // URI
1.111 paf 417: const char* URI_cstr;
418: if(params.count()>1) { // absolute(param)
419: const String& URI=params.as_string(0, "URI must be string");
1.149 paf 420: URI_cstr=r.absolute(URI).cstr();
1.95 paf 421: } else // default = disk path to requested document
1.111 paf 422: URI_cstr=r.request_info.path_translated;
1.95 paf 423: if(URI_cstr)
1.149 paf 424: xmldoc->URL=source_charset.transcode_buf2xchar(URI_cstr, strlen(URI_cstr));
1.95 paf 425:
1.111 paf 426: if(set_encoding) {
427: const char* source_charset_name=source_charset.NAME().cstr();
1.149 paf 428: xmldoc->encoding=source_charset.transcode_buf2xchar(source_charset_name, strlen(source_charset_name));
1.111 paf 429: }
1.9 parser 430: }
431:
1.111 paf 432: static void _load(Request& r, MethodParams& params) {
433: VXdoc& vdoc=GET_SELF(r, VXdoc);
1.9 parser 434:
1.54 paf 435: // filespec
1.156 misha 436: const String* uri=¶ms.as_string(0, "URI must be string");
1.122 paf 437: const char* uri_cstr;
438: if(uri->pos("://")==STRING_NOT_FOUND) // disk path
1.162 misha 439: uri_cstr=r.absolute(*uri).taint_cstr(String::L_FILE_SPEC);
1.122 paf 440: else // xxx://
1.162 misha 441: uri_cstr=uri->taint_cstr(String::L_AS_IS); // leave as-is for xmlParseFile to handle
1.105 paf 442:
1.157 misha 443: /// @todo!! add SAFE MODE!!
1.195 ! moko 444: xmlDoc* xmldoc=xmlReadFile(uri_cstr, NULL, XML_PARSE_OPTIONS);
1.149 paf 445: if(!xmldoc || xmlHaveGenericErrors())
1.179 moko 446: throw XmlException(uri, r);
1.149 paf 447:
1.111 paf 448: // must be first action after if}
449: // replace any previous parsed source
1.149 paf 450: vdoc.set_xmldoc(r.charsets, *xmldoc);
1.9 parser 451: }
452:
1.166 misha 453: String::C xdoc2buf(Request& r, VXdoc& vdoc,
454: XDocOutputOptions& oo,
455: const String* file_spec,
456: bool use_source_charset_to_render_and_client_charset_to_write_to_header=false) {
1.171 misha 457: Charset* render=0;
458: Charset* header=0;
1.140 paf 459: if(use_source_charset_to_render_and_client_charset_to_write_to_header) {
1.171 misha 460: render=&r.charsets.source();
461: header=&r.charsets.client();
1.140 paf 462: } else {
1.188 moko 463: header=render=&pa_charsets.get(*oo.encoding);
1.140 paf 464: }
1.171 misha 465: const char* render_encoding=render->NAME_CSTR();
466: const char* header_encoding=header->NAME_CSTR();
1.140 paf 467:
468: xmlCharEncodingHandler *renderer=xmlFindCharEncodingHandler(render_encoding);
469: // UTF-8 renderer contains empty input/output converters,
1.61 paf 470: // which is wrong for xmlOutputBufferCreateIO
1.140 paf 471: // while zero renderer goes perfectly
1.171 misha 472: if(render->isUTF8())
1.140 paf 473: renderer=0;
1.61 paf 474:
1.140 paf 475: xmlOutputBuffer_auto_ptr outputBuffer(xmlAllocOutputBuffer(renderer));
1.138 paf 476:
1.65 paf 477: xsltStylesheet_auto_ptr stylesheet(xsltNewStylesheet());
478: if(!stylesheet.get())
1.87 paf 479: throw Exception(0,
1.111 paf 480: 0,
1.61 paf 481: "xsltNewStylesheet failed");
482:
1.143 paf 483: #define OOSTRING2STYLE(name) \
1.149 paf 484: stylesheet->name=oo.name?BAD_CAST xmlMemStrdup((const char*)r.transcode(*oo.name)):0
1.143 paf 485: #define OOBOOL2STYLE(name) \
486: if(oo.name>=0) stylesheet->name=oo.name
1.61 paf 487:
1.143 paf 488: OOSTRING2STYLE(method);
489: OOSTRING2STYLE(encoding);
490: OOSTRING2STYLE(mediaType);
491: // OOSTRING2STYLE(doctypeSystem);
492: // OOSTRING2STYLE(doctypePublic);
493: OOBOOL2STYLE(indent);
494: OOSTRING2STYLE(version);
495: OOBOOL2STYLE(standalone);
496: OOBOOL2STYLE(omitXmlDeclaration);
1.61 paf 497:
1.149 paf 498: xmlDoc& xmldoc=vdoc.get_xmldoc();
499: xmldoc.encoding=BAD_CAST xmlMemStrdup(render_encoding);
1.142 paf 500: if(header_encoding)
501: stylesheet->encoding=BAD_CAST xmlMemStrdup(header_encoding);
1.149 paf 502: if(xsltSaveResultTo(outputBuffer.get(), &xmldoc, stylesheet.get())<0
503: || xmlHaveGenericErrors())
1.179 moko 504: throw XmlException(0, r);
1.61 paf 505:
1.138 paf 506: // write out result
1.166 misha 507: char *gnome_str;
508: size_t gnome_length;
1.180 moko 509: #ifdef LIBXML2_NEW_BUFFER
510: if(outputBuffer->conv) {
511: gnome_length=xmlBufUse(outputBuffer->conv);
512: gnome_str=(char *)xmlBufContent(outputBuffer->conv);
513: } else {
514: gnome_length=xmlOutputBufferGetSize(&(*outputBuffer));
515: gnome_str=(char *)xmlOutputBufferGetContent(&(*outputBuffer));
516: }
517: #else
1.138 paf 518: if(outputBuffer->conv) {
519: gnome_length=outputBuffer->conv->use;
520: gnome_str=(char *)outputBuffer->conv->content;
521: } else {
522: gnome_length=outputBuffer->buffer->use;
523: gnome_str=(char *)outputBuffer->buffer->content;
524: }
1.180 moko 525: #endif
1.138 paf 526:
1.166 misha 527: if(file_spec){
1.165 misha 528: file_write(r.charsets,
529: *file_spec,
530: gnome_str,
531: gnome_length,
1.111 paf 532: true/*as_text*/);
1.166 misha 533: return String::C(); // actually, we don't need this output at all
534: } else
535: return String::C(gnome_length ? pa_strdup(gnome_str, gnome_length) : 0, gnome_length);
536: }
1.111 paf 537:
1.166 misha 538: inline HashStringValue* get_options(MethodParams& params, size_t index){
539: return (params.count()>index) ? params.as_hash(index) : 0;
1.1 parser 540: }
541:
1.111 paf 542: static void _file(Request& r, MethodParams& params) {
543: VXdoc& vdoc=GET_SELF(r, VXdoc);
1.1 parser 544:
1.177 moko 545: XDocOutputOptions oo(vdoc.output_options);
546: oo.append(r, get_options(params, 0), true/* $.name[filename] could be specified by user */);
1.166 misha 547: String::C buf=xdoc2buf(r, vdoc, oo, 0/*file_name. not to file, to memory*/);
548:
1.111 paf 549: VFile& vfile=*new VFile;
550: VHash& vhcontent_type=*new VHash;
1.182 moko 551: vhcontent_type.hash().put(value_name, new VString(*oo.mediaType));
552: vhcontent_type.hash().put("charset", new VString(*oo.encoding));
1.111 paf 553:
1.176 moko 554: vfile.set_binary(false/*not tainted*/, buf.str?buf.str:""/*to distinguish from stat-ed file*/, buf.length, oo.filename, &vhcontent_type);
1.169 misha 555:
1.166 misha 556: // write out result
1.190 moko 557: r.write(vfile);
1.63 paf 558: }
559:
1.111 paf 560: static void _save(Request& r, MethodParams& params) {
561: VXdoc& vdoc=GET_SELF(r, VXdoc);
1.63 paf 562:
1.155 misha 563: const String& file_spec=r.absolute(params.as_string(0, FILE_NAME_MUST_BE_STRING));
1.63 paf 564:
1.177 moko 565: XDocOutputOptions oo(vdoc.output_options);
566: oo.append(r, get_options(params, 1));
1.166 misha 567: xdoc2buf(r, vdoc, oo, &file_spec);
1.63 paf 568: }
569:
1.111 paf 570: static void _string(Request& r, MethodParams& params) {
571: VXdoc& vdoc=GET_SELF(r, VXdoc);
1.166 misha 572:
1.177 moko 573: XDocOutputOptions oo(vdoc.output_options);
574: oo.append(r, get_options(params, 0));
1.166 misha 575: String::C buf=xdoc2buf(r, vdoc, oo,
576: 0/*file_name. not to file, to memory*/,
1.140 paf 577: true/*use source charset to render, client charset to put to header*/);
1.166 misha 578:
1.63 paf 579: // write out result
1.190 moko 580: r.write(String(buf, String::L_AS_IS));
1.1 parser 581: }
1.58 paf 582:
1.111 paf 583: #ifndef DOXYGEN
584: struct Add_xslt_param_info {
585: Request* r;
1.149 paf 586: Array<const xmlChar*>* strings;
587: const xmlChar** current_transform_param;
1.111 paf 588: };
589: #endif
590: static void add_xslt_param(
591: HashStringValue::key_type attribute,
592: HashStringValue::value_type meaning,
593: Add_xslt_param_info* info) {
1.149 paf 594: xmlChar* s;
595: *info->current_transform_param++=(s=info->r->transcode(attribute)); *info->strings+=s;
596: *info->current_transform_param++=(s=info->r->transcode(meaning->as_string())); *info->strings+=s;
1.111 paf 597: }
1.132 paf 598:
1.111 paf 599: static VXdoc& _transform(Request& r, const String* stylesheet_source,
1.149 paf 600: VXdoc& vdoc, xsltStylesheetPtr stylesheet, const xmlChar** transform_params)
1.132 paf 601: {
1.149 paf 602: xmlDoc& xmldoc=vdoc.get_xmldoc();
603:
1.65 paf 604: xsltTransformContext_auto_ptr transformContext(
1.149 paf 605: xsltNewTransformContext(stylesheet, &xmldoc));
1.90 paf 606: // make params literal
1.111 paf 607: if (transformContext->globalVars == NULL) // strangly not initialized by xsltNewTransformContext
1.90 paf 608: transformContext->globalVars = xmlHashCreate(20);
1.149 paf 609: xsltQuoteUserParams(transformContext.get(), (const char**)transform_params);
1.90 paf 610: // do transform
1.59 paf 611: xmlDoc *transformed=xsltApplyStylesheetUser(
612: stylesheet,
1.149 paf 613: &xmldoc,
1.90 paf 614: 0/*already quoted-inserted transform_params*/,
1.111 paf 615: 0/*const char* output*/,
1.59 paf 616: 0/*FILE *profile*/,
1.65 paf 617: transformContext.get());
1.149 paf 618: if(!transformed || xmlHaveGenericErrors())
1.179 moko 619: throw XmlException(stylesheet_source, r);
1.42 paf 620:
1.61 paf 621: //gdome_xml_doc_mkref dislikes XML_HTML_DOCUMENT_NODE type, fixing
622: transformed->type=XML_DOCUMENT_NODE;
1.177 moko 623: // constructing result
624: VXdoc& result=*new VXdoc(r.charsets, *transformed);
625: /* grabbing options
626:
627: <xsl:output
628: !method = "xml" | "html" | "text"
629: X| qname-but-not-ncname
630: !version = nmtoken
631: !encoding = string
632: !omit-xml-declaration = "yes" | "no"
633: !standalone = "yes" | "no"
634: !doctype-public = string
635: !doctype-system = string
636: Xcdata-section-elements = qnames
637: !indent = "yes" | "no"
638: !media-type = string />
639: */
640: XDocOutputOptions& oo=result.output_options;
641:
642: oo.method=stylesheet->method?&r.transcode(stylesheet->method):0;
643: oo.encoding=stylesheet->encoding?&r.transcode(stylesheet->encoding):0;
644: oo.mediaType=stylesheet->mediaType?&r.transcode(stylesheet->mediaType):0;
645: oo.indent=stylesheet->indent;
646: oo.version=stylesheet->version?&r.transcode(stylesheet->version):0;
647: oo.standalone=stylesheet->standalone;
648: oo.omitXmlDeclaration=stylesheet->omitXmlDeclaration;
1.167 misha 649:
1.177 moko 650: // return
651: return result;
1.93 paf 652: }
1.111 paf 653: static void _transform(Request& r, MethodParams& params) {
654: VXdoc& vdoc=GET_SELF(r, VXdoc);
1.93 paf 655:
656: // params
1.149 paf 657: Array<const xmlChar*> transform_strings;
658: const xmlChar** transform_params=0;
1.166 misha 659: if(params.count()>1)
1.174 misha 660: if(HashStringValue* hash=params.as_hash(1, "transform options")) {
1.193 moko 661: transform_params=new(PointerGC) const xmlChar*[hash->count()*2+1];
1.166 misha 662: Add_xslt_param_info info={
663: &r,
664: &transform_strings,
665: transform_params
666: };
667: hash->for_each<Add_xslt_param_info*>(add_xslt_param, &info);
668: transform_params[hash->count()*2]=0;
669: }
1.93 paf 670:
1.111 paf 671: VXdoc* result;
1.164 misha 672: if(Value *vxdoc=params[0].as(VXDOC_TYPE)) { // stylesheet (xdoc)
1.191 moko 673: VXdoc& vstylesheet=static_cast<VXdoc &>(*vxdoc);
674:
675: if(!vstylesheet.stylesheet){
676: xmlDoc& stylesheetdoc=vstylesheet.get_xmldoc();
677:
678: // compile xdoc stylesheet
679: vstylesheet.stylesheet=xsltParseStylesheetDoc(&stylesheetdoc);
680: if(xmlHaveGenericErrors())
681: throw XmlException(0, r);
682: if(!vstylesheet.stylesheet)
683: throw Exception("xml", 0, "stylesheet failed to compile");
684: }
1.93 paf 685:
686: // transform!
1.191 moko 687: result=&_transform(r, 0, vdoc, vstylesheet.stylesheet, transform_params);
1.93 paf 688: } else { // stylesheet (file name)
689: // extablish stylesheet connection
690: const String& stylesheet_filespec=
1.111 paf 691: r.absolute(params.as_string(0, "stylesheet must be file name (string) or DOM document (xdoc)"));
1.125 paf 692: Stylesheet_connection_ptr connection=stylesheet_manager->get_connection(stylesheet_filespec);
1.93 paf 693:
694: // load and compile file to stylesheet [or get cached if any]
695: // transform!
1.191 moko 696: result=&_transform(r, &stylesheet_filespec, vdoc, connection->stylesheet(), transform_params);
1.93 paf 697: }
698:
1.59 paf 699: // write out result
1.190 moko 700: r.write(*result);
1.1 parser 701: }
702:
1.16 parser 703: // constructor
1.2 parser 704:
1.92 paf 705: /// @test how to create empty type html?
1.185 moko 706: MXdoc::MXdoc(): MXnode(XDOC_CLASS_NAME) {
707: set_base(xnode_class);
708:
1.16 parser 709: /// DOM1
1.2 parser 710:
1.16 parser 711: // Element createElement(in DOMString tagName) raises(DOMException);
712: add_native_method("createElement", Method::CT_DYNAMIC, _createElement, 1, 1);
713: // DocumentFragment createDocumentFragment();
714: add_native_method("createDocumentFragment", Method::CT_DYNAMIC, _createDocumentFragment, 0, 0);
715: // Text createTextNode(in DOMString data);
716: add_native_method("createTextNode", Method::CT_DYNAMIC, _createTextNode, 1, 1);
717: // Comment createComment(in DOMString data);
718: add_native_method("createComment", Method::CT_DYNAMIC, _createComment, 1, 1);
719: // CDATASection createCDATASection(in DOMString data) raises(DOMException);
720: add_native_method("createCDATASection", Method::CT_DYNAMIC, _createCDATASection, 1, 1);
721: // ProcessingInstruction createProcessingInstruction(in DOMString target, in DOMString data) raises(DOMException);
722: add_native_method("createProcessingInstruction", Method::CT_DYNAMIC, _createProcessingInstruction, 2, 2);
723: // Attr createAttribute(in DOMString name) raises(DOMException);
724: add_native_method("createAttribute", Method::CT_DYNAMIC, _createAttribute, 1, 1);
725: // EntityReference createEntityReference(in DOMString name) raises(DOMException);
726: add_native_method("createEntityReference", Method::CT_DYNAMIC, _createEntityReference, 1, 1);
1.2 parser 727:
1.79 paf 728: /// DOM2
1.2 parser 729:
1.16 parser 730: // ^xdoc.getElementById[elementId]
731: add_native_method("getElementById", Method::CT_DYNAMIC, _getElementById, 1, 1);
1.79 paf 732:
733: // Node (in Node importedNode, in boolean deep) raises(DOMException)
734: add_native_method("importNode", Method::CT_DYNAMIC, _importNode, 2, 2);
1.112 paf 735:
1.113 paf 736: // Attr createAttributeNS(in DOMString namespaceURI, in DOMString qualifiedName) raises(DOMException);
1.112 paf 737: add_native_method("createAttributeNS", Method::CT_DYNAMIC, _createAttributeNS, 2, 2);
738:
1.113 paf 739: // Element createElementNS(in DOMString namespaceURI, in DOMString qualifiedName) raises(DOMException);
1.112 paf 740: add_native_method("createElementNS", Method::CT_DYNAMIC, _createElementNS, 2, 2);
1.99 paf 741:
1.16 parser 742: /// parser
743:
1.54 paf 744: // ^xdoc::create{qualifiedName}
1.95 paf 745: // ^xdoc::create[<some>xml</some>]
746: // ^xdoc::create[URI][<some>xml</some>]
747: add_native_method("create", Method::CT_DYNAMIC, _create, 1, 2);
1.83 paf 748: // for backward compatibility with <=v 1.82 2002/01/31 11:51:46 paf
749: add_native_method("set", Method::CT_DYNAMIC, _create, 1, 1);
1.54 paf 750:
751: // ^xdoc::load[some.xml]
1.122 paf 752: add_native_method("load", Method::CT_DYNAMIC, _load, 1, 1);
1.54 paf 753:
1.2 parser 754: // ^xdoc.save[some.xml]
755: // ^xdoc.save[some.xml;options hash]
1.1 parser 756: add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2);
757:
1.2 parser 758: // ^xdoc.string[] <doc/>
759: // ^xdoc.string[options hash] <doc/>
1.1 parser 760: add_native_method("string", Method::CT_DYNAMIC, _string, 0, 1);
761:
1.2 parser 762: // ^xdoc.file[] file with "<doc/>"
763: // ^xdoc.file[options hash] file with "<doc/>"
1.58 paf 764: add_native_method("file", Method::CT_DYNAMIC, _file, 0, 1);
1.1 parser 765:
1.98 paf 766: // ^xdoc.transform[stylesheet file_name/xdoc]
767: // ^xdoc.transform[stylesheet file_name/xdoc;params hash]
1.59 paf 768: add_native_method("transform", Method::CT_DYNAMIC, _transform, 1, 2);
1.2 parser 769:
1.5 parser 770: }
771:
1.111 paf 772: # else
773:
774: #include "classes.h"
775:
1.1 parser 776: // global variable
777:
1.186 moko 778: DECLARE_CLASS_VAR(xnode, 0); // fictive
1.184 moko 779: DECLARE_CLASS_VAR(xdoc, 0); // fictive
1.1 parser 780:
781: #endif
E-mail: