Annotation of parser3/src/classes/xdoc.C, revision 1.170
1.1 parser 1: /** @file
1.2 parser 2: Parser: @b xdoc parser class.
1.1 parser 3:
1.157 misha 4: Copyright (c) 2001-2009 ArtLebedev Group (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.170 ! misha 12: static const char * const IDENT_XDOC_C="$Date: 2011-05-27 06:55:47 $";
1.82 paf 13:
1.69 paf 14: #include "libxml/tree.h"
1.133 paf 15: #include "libxml/HTMLtree.h"
1.69 paf 16: #include "libxslt/xsltInternals.h"
17: #include "libxslt/transform.h"
18: #include "libxslt/xsltutils.h"
1.90 paf 19: #include "libxslt/variables.h"
1.132 paf 20: #include "libxslt/imports.h"
1.111 paf 21:
22: #include "pa_vmethod_frame.h"
23:
24: #include "pa_stylesheet_manager.h"
25: #include "pa_request.h"
26: #include "pa_vxdoc.h"
27: #include "pa_charset.h"
28: #include "pa_vfile.h"
1.115 paf 29: #include "pa_xml_exception.h"
1.111 paf 30: #include "xnode.h"
31:
1.1 parser 32: // defines
33:
34: #define XDOC_CLASS_NAME "xdoc"
35:
36: // class
37:
1.111 paf 38: class MXdoc: public MXnode {
1.1 parser 39: public: // VStateless_class
1.164 misha 40: Value* create_new_value(Pool&) { return new VXdoc(); }
1.1 parser 41:
42: public:
1.111 paf 43: MXdoc();
1.1 parser 44:
45: };
46:
1.111 paf 47: // global variable
48:
49: DECLARE_CLASS_VAR(xdoc, new MXdoc, 0);
1.61 paf 50:
1.138 paf 51: // helper classes
52:
53: class xmlOutputBuffer_auto_ptr {
54: public:
55: explicit xmlOutputBuffer_auto_ptr(xmlOutputBuffer *_APtr = 0)
56: : _Owns(_APtr != 0), _Ptr(_APtr) {}
57: xmlOutputBuffer_auto_ptr(const xmlOutputBuffer_auto_ptr& _Y)
58: : _Owns(_Y._Owns), _Ptr(_Y.release()) {}
59: xmlOutputBuffer_auto_ptr& operator=(const xmlOutputBuffer_auto_ptr& _Y)
60: {if (this != &_Y)
61: {if (_Ptr != _Y.get())
62: {if (_Owns && _Ptr)
63: xmlOutputBufferClose(_Ptr);
64: _Owns = _Y._Owns; }
65: else if (_Y._Owns)
66: _Owns = true;
67: _Ptr = _Y.release(); }
68: return (*this); }
69: ~xmlOutputBuffer_auto_ptr()
70: {if (_Owns && _Ptr)
71: xmlOutputBufferClose(_Ptr); }
72: xmlOutputBuffer& operator*() const
73: {return (*get()); }
74: xmlOutputBuffer *operator->() const
75: {return (get()); }
76: xmlOutputBuffer *get() const
77: {return (_Ptr); }
78: xmlOutputBuffer *release() const
79: {((xmlOutputBuffer_auto_ptr *)this)->_Owns = false;
80: return (_Ptr); }
81: private:
82: bool _Owns;
83: xmlOutputBuffer *_Ptr;
84: };
85:
1.65 paf 86: class xsltTransformContext_auto_ptr {
87: public:
1.67 paf 88: explicit xsltTransformContext_auto_ptr(xsltTransformContext *_APtr = 0)
89: : _Owns(_APtr != 0), _Ptr(_APtr) {}
1.65 paf 90: xsltTransformContext_auto_ptr(const xsltTransformContext_auto_ptr& _Y)
91: : _Owns(_Y._Owns), _Ptr(_Y.release()) {}
92: xsltTransformContext_auto_ptr& operator=(const xsltTransformContext_auto_ptr& _Y)
93: {if (this != &_Y)
94: {if (_Ptr != _Y.get())
95: {if (_Owns && _Ptr)
96: xsltFreeTransformContext(_Ptr);
97: _Owns = _Y._Owns; }
98: else if (_Y._Owns)
99: _Owns = true;
100: _Ptr = _Y.release(); }
101: return (*this); }
102: ~xsltTransformContext_auto_ptr()
103: {if (_Owns && _Ptr)
104: xsltFreeTransformContext(_Ptr); }
105: xsltTransformContext& operator*() const
106: {return (*get()); }
107: xsltTransformContext *operator->() const
108: {return (get()); }
109: xsltTransformContext *get() const
110: {return (_Ptr); }
111: xsltTransformContext *release() const
112: {((xsltTransformContext_auto_ptr *)this)->_Owns = false;
113: return (_Ptr); }
114: private:
115: bool _Owns;
116: xsltTransformContext *_Ptr;
117: };
1.61 paf 118:
1.65 paf 119: class xsltStylesheet_auto_ptr {
120: public:
1.67 paf 121: explicit xsltStylesheet_auto_ptr(xsltStylesheet *_APtr = 0)
122: : _Owns(_APtr != 0), _Ptr(_APtr) {}
1.65 paf 123: xsltStylesheet_auto_ptr(const xsltStylesheet_auto_ptr& _Y)
124: : _Owns(_Y._Owns), _Ptr(_Y.release()) {}
125: xsltStylesheet_auto_ptr& operator=(const xsltStylesheet_auto_ptr& _Y)
126: {if (this != &_Y)
127: {if (_Ptr != _Y.get())
128: {if (_Owns && _Ptr)
129: xsltFreeStylesheet(_Ptr);
130: _Owns = _Y._Owns; }
131: else if (_Y._Owns)
132: _Owns = true;
133: _Ptr = _Y.release(); }
134: return (*this); }
135: ~xsltStylesheet_auto_ptr()
136: {if (_Owns && _Ptr)
137: xsltFreeStylesheet(_Ptr); }
138: xsltStylesheet& operator*() const
139: {return (*get()); }
140: xsltStylesheet *operator->() const
141: {return (get()); }
142: xsltStylesheet *get() const
143: {return (_Ptr); }
144: xsltStylesheet *release() const
145: {((xsltStylesheet_auto_ptr *)this)->_Owns = false;
146: return (_Ptr); }
147: private:
148: bool _Owns;
149: xsltStylesheet *_Ptr;
150: };
1.61 paf 151:
1.1 parser 152: // methods
153:
1.149 paf 154: static void writeNode(Request& r, VXdoc& xdoc, xmlNode* node) {
155: if(!node)
1.154 misha 156: throw Exception(PARSER_RUNTIME,
1.149 paf 157: 0,
158: "error creating node"); // OOM, bad name, things like that
1.54 paf 159:
160: // write out result
1.149 paf 161: r.write_no_lang(xdoc.wrap(*node));
162: }
163:
164: struct IdsIteratorInfo {
165: xmlChar *elementId;
166: xmlNode *element;
167: };
168:
169: /* Hash Scanner function for pa_getElementById */
170: extern "C" void // switching to calling convetion of libxml
171: idsHashScanner (void *payload, void *data, xmlChar *name) {
172: IdsIteratorInfo *priv = (IdsIteratorInfo *)data;
173:
174: if (priv->element == NULL && xmlStrEqual (name, priv->elementId))
175: {
176: xmlNode* parent=((xmlID *)payload)->attr->parent;
177: assert(parent);
178: priv->element=parent;
179: }
180: }
181:
182: static xmlNode*
183: pa_getElementById(xmlDoc& xmldoc, xmlChar* elementId) {
184: xmlHashTable *ids = (xmlHashTable *)xmldoc.ids;
185: IdsIteratorInfo iter={elementId, NULL};
186: xmlHashScan(ids, idsHashScanner, &iter);
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.54 paf 367: /*
368: GdomeElement *gdome_doc_createElementNS (GdomeDocument *self, GdomeDOMString *namespaceURI, GdomeDOMString *qualifiedName, GdomeException *exc);
369: GdomeAttr *gdome_doc_createAttributeNS (GdomeDocument *self, GdomeDOMString *namespaceURI, GdomeDOMString *qualifiedName, GdomeException *exc);
370: */
1.1 parser 371:
1.111 paf 372: static void _create(Request& r, MethodParams& params) {
373: Charset& source_charset=r.charsets.source();
374: VXdoc& vdoc=GET_SELF(r, VXdoc);
1.1 parser 375:
1.111 paf 376: Value& param=params[params.count()-1];
1.149 paf 377: xmlDoc* xmldoc;
1.111 paf 378: bool set_encoding=false;
1.95 paf 379: if(param.get_junction()) { // {<?xml?>...}
1.111 paf 380: Temp_lang temp_lang(r, String::L_XML);
1.91 paf 381: const String& xml=r.process_to_string(param);
1.83 paf 382:
1.163 misha 383: String::Body sbody=xml.cstr_to_string_body_untaint(r.flang, 0, &r.charsets);
384: xmldoc=xmlParseMemory(sbody.cstr(), sbody.length());
385:
1.111 paf 386: //printf("document=0x%p\n", document);
1.149 paf 387: if(!xmldoc || xmlHaveGenericErrors())
388: throw XmlException(0);
1.111 paf 389:
390: // must be last action in if, see after if}
1.149 paf 391: } else { // [localName]
1.153 misha 392: if(const String* value = param.get_string()){
393: xmlChar* localName=r.transcode(*value);
1.168 misha 394: if(xmlValidateNCName(localName, 0) != 0)
395: throw XmlException(0, XML_INVALID_LOCAL_NAME, localName);
396:
1.120 paf 397: #if 0
1.153 misha 398: GdomeDocumentType *documentType=gdome_di_createDocumentType (
399: docimpl,
400: r.transcode(qualifiedName),
401: 0/*publicId*/,
402: 0/*systemId*/,
403: &exc);
404: if(!documentType || exc || xmlHaveGenericErrors())
405: throw Exception(
406: method_name,
407: exc);
408: /// +xalan createXMLDecl ?
1.120 paf 409: #endif
1.151 misha 410: xmldoc=xmlNewDoc(0);
411: if(!xmldoc || xmlHaveGenericErrors())
412: throw XmlException(0);
1.168 misha 413:
1.151 misha 414: xmlNode* node=xmlNewChild((xmlNode*)xmldoc, NULL, localName, NULL);
415: if(!node || xmlHaveGenericErrors())
416: throw XmlException(0);
1.103 paf 417:
1.151 misha 418: set_encoding=true;
419: // must be last action in if, see after if}
1.153 misha 420: } else {
1.161 misha 421: VFile* vfile=param.as_vfile(String::L_AS_IS);
1.153 misha 422: xmldoc=xmlParseMemory(vfile->value_ptr(), vfile->value_size());
423: if(!xmldoc || xmlHaveGenericErrors())
424: throw XmlException(0);
1.151 misha 425: }
1.111 paf 426: }
427: // must be first action after if}
428: // replace any previous parsed source
1.149 paf 429: vdoc.set_xmldoc(r.charsets, *xmldoc);
1.95 paf 430:
431: // URI
1.111 paf 432: const char* URI_cstr;
433: if(params.count()>1) { // absolute(param)
434: const String& URI=params.as_string(0, "URI must be string");
1.149 paf 435: URI_cstr=r.absolute(URI).cstr();
1.95 paf 436: } else // default = disk path to requested document
1.111 paf 437: URI_cstr=r.request_info.path_translated;
1.95 paf 438: if(URI_cstr)
1.149 paf 439: xmldoc->URL=source_charset.transcode_buf2xchar(URI_cstr, strlen(URI_cstr));
1.95 paf 440:
1.111 paf 441: if(set_encoding) {
442: const char* source_charset_name=source_charset.NAME().cstr();
1.149 paf 443: xmldoc->encoding=source_charset.transcode_buf2xchar(source_charset_name, strlen(source_charset_name));
1.111 paf 444: }
1.9 parser 445: }
446:
1.111 paf 447: static void _load(Request& r, MethodParams& params) {
448: VXdoc& vdoc=GET_SELF(r, VXdoc);
1.9 parser 449:
1.54 paf 450: // filespec
1.156 misha 451: const String* uri=¶ms.as_string(0, "URI must be string");
1.122 paf 452: const char* uri_cstr;
453: if(uri->pos("://")==STRING_NOT_FOUND) // disk path
1.162 misha 454: uri_cstr=r.absolute(*uri).taint_cstr(String::L_FILE_SPEC);
1.122 paf 455: else // xxx://
1.162 misha 456: uri_cstr=uri->taint_cstr(String::L_AS_IS); // leave as-is for xmlParseFile to handle
1.105 paf 457:
1.157 misha 458: /// @todo!! add SAFE MODE!!
1.149 paf 459: xmlDoc* xmldoc=xmlParseFile(uri_cstr);
460: if(!xmldoc || xmlHaveGenericErrors())
461: throw XmlException(uri);
462:
1.111 paf 463: // must be first action after if}
464: // replace any previous parsed source
1.149 paf 465: vdoc.set_xmldoc(r.charsets, *xmldoc);
1.9 parser 466: }
467:
1.166 misha 468: String::C xdoc2buf(Request& r, VXdoc& vdoc,
469: XDocOutputOptions& oo,
470: const String* file_spec,
471: bool use_source_charset_to_render_and_client_charset_to_write_to_header=false) {
1.140 paf 472: const char* render_encoding;
473: const char* header_encoding;
474: if(use_source_charset_to_render_and_client_charset_to_write_to_header) {
475: render_encoding=r.charsets.source().NAME_CSTR();
476: header_encoding=r.charsets.client().NAME_CSTR();
477: } else {
478: header_encoding=render_encoding=oo.encoding->cstr();
479: }
480:
481: xmlCharEncodingHandler *renderer=xmlFindCharEncodingHandler(render_encoding);
482: if(!renderer)
1.154 misha 483: throw Exception(PARSER_RUNTIME,
1.111 paf 484: 0,
1.140 paf 485: "encoding '%s' not supported", render_encoding);
486: // UTF-8 renderer contains empty input/output converters,
1.61 paf 487: // which is wrong for xmlOutputBufferCreateIO
1.140 paf 488: // while zero renderer goes perfectly
489: if(strcmp(render_encoding, "UTF-8")==0)
490: renderer=0;
1.61 paf 491:
1.140 paf 492: xmlOutputBuffer_auto_ptr outputBuffer(xmlAllocOutputBuffer(renderer));
1.138 paf 493:
1.65 paf 494: xsltStylesheet_auto_ptr stylesheet(xsltNewStylesheet());
495: if(!stylesheet.get())
1.87 paf 496: throw Exception(0,
1.111 paf 497: 0,
1.61 paf 498: "xsltNewStylesheet failed");
499:
1.143 paf 500: #define OOSTRING2STYLE(name) \
1.149 paf 501: stylesheet->name=oo.name?BAD_CAST xmlMemStrdup((const char*)r.transcode(*oo.name)):0
1.143 paf 502: #define OOBOOL2STYLE(name) \
503: if(oo.name>=0) stylesheet->name=oo.name
1.61 paf 504:
1.143 paf 505: OOSTRING2STYLE(method);
506: OOSTRING2STYLE(encoding);
507: OOSTRING2STYLE(mediaType);
508: // OOSTRING2STYLE(doctypeSystem);
509: // OOSTRING2STYLE(doctypePublic);
510: OOBOOL2STYLE(indent);
511: OOSTRING2STYLE(version);
512: OOBOOL2STYLE(standalone);
513: OOBOOL2STYLE(omitXmlDeclaration);
1.61 paf 514:
1.149 paf 515: xmlDoc& xmldoc=vdoc.get_xmldoc();
516: xmldoc.encoding=BAD_CAST xmlMemStrdup(render_encoding);
1.142 paf 517: if(header_encoding)
518: stylesheet->encoding=BAD_CAST xmlMemStrdup(header_encoding);
1.149 paf 519: if(xsltSaveResultTo(outputBuffer.get(), &xmldoc, stylesheet.get())<0
520: || xmlHaveGenericErrors())
521: throw XmlException(0);
1.61 paf 522:
1.138 paf 523: // write out result
1.166 misha 524: char *gnome_str;
525: size_t gnome_length;
1.138 paf 526: if(outputBuffer->conv) {
527: gnome_length=outputBuffer->conv->use;
528: gnome_str=(char *)outputBuffer->conv->content;
529: } else {
530: gnome_length=outputBuffer->buffer->use;
531: gnome_str=(char *)outputBuffer->buffer->content;
532: }
533:
1.166 misha 534: if(file_spec){
1.165 misha 535: file_write(r.charsets,
536: *file_spec,
537: gnome_str,
538: gnome_length,
1.111 paf 539: true/*as_text*/);
1.166 misha 540: return String::C(); // actually, we don't need this output at all
541: } else
542: return String::C(gnome_length ? pa_strdup(gnome_str, gnome_length) : 0, gnome_length);
543: }
1.111 paf 544:
1.166 misha 545: inline HashStringValue* get_options(MethodParams& params, size_t index){
546: return (params.count()>index) ? params.as_hash(index) : 0;
1.1 parser 547: }
548:
1.111 paf 549: static void _file(Request& r, MethodParams& params) {
550: VXdoc& vdoc=GET_SELF(r, VXdoc);
1.1 parser 551:
1.170 ! misha 552: XDocOutputOptions oo(r, get_options(params, 0), true/* $.name[filename] could be specified by user */);
1.166 misha 553: String::C buf=xdoc2buf(r, vdoc, oo, 0/*file_name. not to file, to memory*/);
554:
1.111 paf 555: VFile& vfile=*new VFile;
556: VHash& vhcontent_type=*new VHash;
557: vhcontent_type.hash().put(
558: value_name,
559: new VString(*oo.mediaType));
560: vhcontent_type.hash().put(
1.126 paf 561: String::Body("charset"),
1.111 paf 562: new VString(*oo.encoding));
563:
564: vfile.set(false/*tainted*/, buf.str?buf.str:""/*to distinguish from stat-ed file*/, buf.length,
1.170 ! misha 565: (oo.filename)?oo.filename->taint_cstr(String::L_FILE_SPEC):0, &vhcontent_type);
1.166 misha 566:
1.169 misha 567: vfile.set_mode(true/*text*/);
568:
1.166 misha 569: // write out result
1.58 paf 570: r.write_no_lang(vfile);
1.63 paf 571: }
572:
1.111 paf 573: static void _save(Request& r, MethodParams& params) {
574: VXdoc& vdoc=GET_SELF(r, VXdoc);
1.63 paf 575:
1.155 misha 576: const String& file_spec=r.absolute(params.as_string(0, FILE_NAME_MUST_BE_STRING));
1.63 paf 577:
1.166 misha 578: XDocOutputOptions oo(r, get_options(params, 1));
579: xdoc2buf(r, vdoc, oo, &file_spec);
1.63 paf 580: }
581:
1.111 paf 582: static void _string(Request& r, MethodParams& params) {
583: VXdoc& vdoc=GET_SELF(r, VXdoc);
1.166 misha 584:
585: XDocOutputOptions oo(r, get_options(params, 0));
586: String::C buf=xdoc2buf(r, vdoc, oo,
587: 0/*file_name. not to file, to memory*/,
1.140 paf 588: true/*use source charset to render, client charset to put to header*/);
1.166 misha 589:
1.63 paf 590: // write out result
1.166 misha 591: r.write_no_lang(String(buf, String::L_AS_IS));
1.1 parser 592: }
1.58 paf 593:
1.111 paf 594: #ifndef DOXYGEN
595: struct Add_xslt_param_info {
596: Request* r;
1.149 paf 597: Array<const xmlChar*>* strings;
598: const xmlChar** current_transform_param;
1.111 paf 599: };
600: #endif
601: static void add_xslt_param(
602: HashStringValue::key_type attribute,
603: HashStringValue::value_type meaning,
604: Add_xslt_param_info* info) {
1.149 paf 605: xmlChar* s;
606: *info->current_transform_param++=(s=info->r->transcode(attribute)); *info->strings+=s;
607: *info->current_transform_param++=(s=info->r->transcode(meaning->as_string())); *info->strings+=s;
1.111 paf 608: }
1.132 paf 609:
1.111 paf 610: static VXdoc& _transform(Request& r, const String* stylesheet_source,
1.149 paf 611: VXdoc& vdoc, xsltStylesheetPtr stylesheet, const xmlChar** transform_params)
1.132 paf 612: {
1.149 paf 613: xmlDoc& xmldoc=vdoc.get_xmldoc();
614:
1.65 paf 615: xsltTransformContext_auto_ptr transformContext(
1.149 paf 616: xsltNewTransformContext(stylesheet, &xmldoc));
1.90 paf 617: // make params literal
1.111 paf 618: if (transformContext->globalVars == NULL) // strangly not initialized by xsltNewTransformContext
1.90 paf 619: transformContext->globalVars = xmlHashCreate(20);
1.149 paf 620: xsltQuoteUserParams(transformContext.get(), (const char**)transform_params);
1.90 paf 621: // do transform
1.59 paf 622: xmlDoc *transformed=xsltApplyStylesheetUser(
623: stylesheet,
1.149 paf 624: &xmldoc,
1.90 paf 625: 0/*already quoted-inserted transform_params*/,
1.111 paf 626: 0/*const char* output*/,
1.59 paf 627: 0/*FILE *profile*/,
1.65 paf 628: transformContext.get());
1.149 paf 629: if(!transformed || xmlHaveGenericErrors())
630: throw XmlException(stylesheet_source);
1.42 paf 631:
1.61 paf 632: //gdome_xml_doc_mkref dislikes XML_HTML_DOCUMENT_NODE type, fixing
633: transformed->type=XML_DOCUMENT_NODE;
1.167 misha 634:
1.59 paf 635: // constructing result
1.167 misha 636: return *new VXdoc(r.charsets, *transformed);
1.93 paf 637: }
1.111 paf 638: static void _transform(Request& r, MethodParams& params) {
639: VXdoc& vdoc=GET_SELF(r, VXdoc);
1.93 paf 640:
641: // params
1.149 paf 642: Array<const xmlChar*> transform_strings;
643: const xmlChar** transform_params=0;
1.166 misha 644: if(params.count()>1)
645: if(HashStringValue* hash=params.as_hash(1)) {
646: transform_params=new(UseGC) const xmlChar*[hash->count()*2+1];
647: Add_xslt_param_info info={
648: &r,
649: &transform_strings,
650: transform_params
651: };
652: hash->for_each<Add_xslt_param_info*>(add_xslt_param, &info);
653: transform_params[hash->count()*2]=0;
654: }
1.93 paf 655:
1.111 paf 656: VXdoc* result;
1.164 misha 657: if(Value *vxdoc=params[0].as(VXDOC_TYPE)) { // stylesheet (xdoc)
1.149 paf 658: xmlDoc& stylesheetdoc=static_cast<VXdoc *>(vxdoc)->get_xmldoc();
1.93 paf 659: // compile xdoc stylesheet
1.149 paf 660: xsltStylesheet_auto_ptr stylesheet_ptr(xsltParseStylesheetDoc(&stylesheetdoc));
661: if(xmlHaveGenericErrors())
662: throw XmlException(0);
1.93 paf 663: if(!stylesheet_ptr.get())
664: throw Exception("xml",
1.111 paf 665: 0,
1.93 paf 666: "stylesheet failed to compile");
1.128 paf 667: // strange thing - xsltParseStylesheetDoc records document and destroys it in stylesheet destructor
668: // we don't need that
669: stylesheet_ptr->doc=0;
1.93 paf 670:
671: // transform!
1.111 paf 672: result=&_transform(r, 0,
1.93 paf 673: vdoc, stylesheet_ptr.get(),
674: transform_params);
675: } else { // stylesheet (file name)
676: // extablish stylesheet connection
677: const String& stylesheet_filespec=
1.111 paf 678: r.absolute(params.as_string(0, "stylesheet must be file name (string) or DOM document (xdoc)"));
1.125 paf 679: Stylesheet_connection_ptr connection=stylesheet_manager->get_connection(stylesheet_filespec);
1.93 paf 680:
681: // load and compile file to stylesheet [or get cached if any]
682: // transform!
1.122 paf 683: result=&_transform(r, &stylesheet_filespec, vdoc, connection->stylesheet(),
1.93 paf 684: transform_params);
685: }
686:
1.59 paf 687: // write out result
1.93 paf 688: r.write_no_lang(*result);
1.1 parser 689: }
690:
1.16 parser 691: // constructor
1.2 parser 692:
1.92 paf 693: /// @test how to create empty type html?
1.111 paf 694: MXdoc::MXdoc(): MXnode(XDOC_CLASS_NAME, xnode_class) {
1.16 parser 695: /// DOM1
1.2 parser 696:
1.16 parser 697: // Element createElement(in DOMString tagName) raises(DOMException);
698: add_native_method("createElement", Method::CT_DYNAMIC, _createElement, 1, 1);
699: // DocumentFragment createDocumentFragment();
700: add_native_method("createDocumentFragment", Method::CT_DYNAMIC, _createDocumentFragment, 0, 0);
701: // Text createTextNode(in DOMString data);
702: add_native_method("createTextNode", Method::CT_DYNAMIC, _createTextNode, 1, 1);
703: // Comment createComment(in DOMString data);
704: add_native_method("createComment", Method::CT_DYNAMIC, _createComment, 1, 1);
705: // CDATASection createCDATASection(in DOMString data) raises(DOMException);
706: add_native_method("createCDATASection", Method::CT_DYNAMIC, _createCDATASection, 1, 1);
707: // ProcessingInstruction createProcessingInstruction(in DOMString target, in DOMString data) raises(DOMException);
708: add_native_method("createProcessingInstruction", Method::CT_DYNAMIC, _createProcessingInstruction, 2, 2);
709: // Attr createAttribute(in DOMString name) raises(DOMException);
710: add_native_method("createAttribute", Method::CT_DYNAMIC, _createAttribute, 1, 1);
711: // EntityReference createEntityReference(in DOMString name) raises(DOMException);
712: add_native_method("createEntityReference", Method::CT_DYNAMIC, _createEntityReference, 1, 1);
1.2 parser 713:
1.79 paf 714: /// DOM2
1.2 parser 715:
1.16 parser 716: // ^xdoc.getElementById[elementId]
717: add_native_method("getElementById", Method::CT_DYNAMIC, _getElementById, 1, 1);
1.79 paf 718:
719: // Node (in Node importedNode, in boolean deep) raises(DOMException)
720: add_native_method("importNode", Method::CT_DYNAMIC, _importNode, 2, 2);
1.112 paf 721:
1.113 paf 722: // Attr createAttributeNS(in DOMString namespaceURI, in DOMString qualifiedName) raises(DOMException);
1.112 paf 723: add_native_method("createAttributeNS", Method::CT_DYNAMIC, _createAttributeNS, 2, 2);
724:
1.113 paf 725: // Element createElementNS(in DOMString namespaceURI, in DOMString qualifiedName) raises(DOMException);
1.112 paf 726: add_native_method("createElementNS", Method::CT_DYNAMIC, _createElementNS, 2, 2);
1.99 paf 727:
1.16 parser 728: /// parser
729:
1.54 paf 730: // ^xdoc::create{qualifiedName}
1.95 paf 731: // ^xdoc::create[<some>xml</some>]
732: // ^xdoc::create[URI][<some>xml</some>]
733: add_native_method("create", Method::CT_DYNAMIC, _create, 1, 2);
1.83 paf 734: // for backward compatibility with <=v 1.82 2002/01/31 11:51:46 paf
735: add_native_method("set", Method::CT_DYNAMIC, _create, 1, 1);
1.54 paf 736:
737: // ^xdoc::load[some.xml]
1.122 paf 738: add_native_method("load", Method::CT_DYNAMIC, _load, 1, 1);
1.54 paf 739:
1.2 parser 740: // ^xdoc.save[some.xml]
741: // ^xdoc.save[some.xml;options hash]
1.1 parser 742: add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2);
743:
1.2 parser 744: // ^xdoc.string[] <doc/>
745: // ^xdoc.string[options hash] <doc/>
1.1 parser 746: add_native_method("string", Method::CT_DYNAMIC, _string, 0, 1);
747:
1.2 parser 748: // ^xdoc.file[] file with "<doc/>"
749: // ^xdoc.file[options hash] file with "<doc/>"
1.58 paf 750: add_native_method("file", Method::CT_DYNAMIC, _file, 0, 1);
1.1 parser 751:
1.98 paf 752: // ^xdoc.transform[stylesheet file_name/xdoc]
753: // ^xdoc.transform[stylesheet file_name/xdoc;params hash]
1.59 paf 754: add_native_method("transform", Method::CT_DYNAMIC, _transform, 1, 2);
1.2 parser 755:
1.5 parser 756: }
757:
1.111 paf 758: # else
759:
760: #include "classes.h"
761:
1.1 parser 762: // global variable
763:
1.111 paf 764: DECLARE_CLASS_VAR(xdoc, 0, 0); // fictive
1.1 parser 765:
766: #endif
E-mail: