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