Annotation of parser3/src/classes/xdoc.C, revision 1.11
1.1 parser 1: /** @file
1.2 parser 2: Parser: @b xdoc parser class.
1.1 parser 3:
4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
5: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
6:
1.11 ! parser 7: $Id: xdoc.C,v 1.10 2001/10/05 17:33:50 parser Exp $
1.1 parser 8: */
9: #include "classes.h"
10: #ifdef XML
11:
12: #include "pa_request.h"
13: #include "pa_vxdoc.h"
14: #include "pa_xslt_stylesheet_manager.h"
15: #include "pa_stylesheet_connection.h"
16: #include "pa_vfile.h"
17: #include "xnode.h"
18:
19: #include <strstream>
20: #include <Include/PlatformDefinitions.hpp>
21: #include <util/PlatformUtils.hpp>
1.5 parser 22: #include <util/TransENameMap.hpp>
1.1 parser 23: #include <XalanTransformer/XalanTransformer.hpp>
24: #include <XalanTransformer/XalanParsedSource.hpp>
1.9 parser 25: # include <XalanTransformer/XalanDefaultParsedSource.hpp>
26: # include <XalanSourceTree/XalanSourceTreeDocument.hpp>
1.10 parser 27: # include <XalanSourceTree/XalanSourceTreeContentHandler.hpp>
28: # include <sax2/XMLReaderFactory.hpp>
1.1 parser 29: #include <XMLSupport/FormatterToXML.hpp>
30: #include <XMLSupport/FormatterToHTML.hpp>
31: #include <XMLSupport/FormatterToText.hpp>
32: #include <XMLSupport/FormatterTreeWalker.hpp>
33: #include <PlatformSupport/XalanFileOutputStream.hpp>
34: #include <PlatformSupport/XalanOutputStreamPrintWriter.hpp>
35: #include <PlatformSupport/DOMStringPrintWriter.hpp>
1.2 parser 36: #include <XalanDOM/XalanElement.hpp>
37: #include <XalanDOM/XalanNodeList.hpp>
1.1 parser 38:
39: // defines
40:
41: #define XDOC_CLASS_NAME "xdoc"
42:
43: #define XDOC_OUTPUT_METHOD_OPTION_NAME "method"
44: #define XDOC_OUTPUT_METHOD_OPTION_VALUE_XML "xml"
45: #define XDOC_OUTPUT_METHOD_OPTION_VALUE_HTML "html"
46: #define XDOC_OUTPUT_METHOD_OPTION_VALUE_TEXT "text"
47:
48: #define XDOC_OUTPUT_ENCODING_OPTION_NAME "encoding"
49:
50: #define XDOC_OUTPUT_DEFAULT_INDENT 4
51:
52: // class
53:
54: class MXdoc : public MXnode {
55: public: // VStateless_class
56: Value *create_new_value(Pool& pool) { return new(pool) VXdoc(pool); }
57:
58: public:
59: MXdoc(Pool& pool);
60:
61: public: // Methoded
62: bool used_directly() { return true; }
1.5 parser 63: void configure_admin(Request& r);
1.1 parser 64: };
65:
66: // methods
67:
68: class ParserStringXalanOutputStream: public XalanOutputStream {
69: public:
70:
71: explicit ParserStringXalanOutputStream(String& astring) : fstring(astring) {}
72:
73: protected: // XalanOutputStream
74:
75: virtual void writeData(const char *theBuffer, unsigned long theBufferLength) {
76: char *copy=(char *)fstring.malloc((size_t)theBufferLength);
77: memcpy(copy, theBuffer, (size_t)theBufferLength);
1.2 parser 78: fstring.APPEND_CLEAN(copy, (size_t)theBufferLength, "xdoc", 0);
1.1 parser 79: }
80:
81: virtual void doFlush() {}
82:
83: private:
84:
85: String& fstring;
86:
87: };
88:
1.9 parser 89: class XalanSourceTreeParserLiaison2: public XalanSourceTreeParserLiaison {
90: public:
91: XalanSourceTreeParserLiaison2(XalanSourceTreeDOMSupport& theSupport) : XalanSourceTreeParserLiaison(theSupport),
92: ferror_handler(new HandlerBase) {
93: }
94:
1.10 parser 95: XalanDocument*
96: parseXMLStream2(
97: const InputSource& inputSource) {
98: XalanSourceTreeContentHandler theContentHandler(createXalanSourceTreeDocument());
99: XalanAutoPtr<SAX2XMLReader> theReader(XMLReaderFactory::createXMLReader());
100: theReader->setContentHandler(&theContentHandler);
101: theReader->setDTDHandler(&theContentHandler);
102: theReader->setErrorHandler(ferror_handler); // disable stderr output
103: theReader->setLexicalHandler(&theContentHandler);
104: EntityResolver* const theResolver = getEntityResolver();
105: if (theResolver != 0) {
106: theReader->setEntityResolver(theResolver);
107: }
108: theReader->parse(inputSource);
109: return theContentHandler.getDocument();
110: }
111:
1.9 parser 112: ~XalanSourceTreeParserLiaison2() {
1.11 ! parser 113: delete ferror_handler;
1.9 parser 114: }
115: private:
116: ErrorHandler *ferror_handler;
117: };
118:
119: class XalanDefaultParsedSource2 : public XalanParsedSource
120: {
121: public:
122:
123: XalanDefaultParsedSource2(const XSLTInputSource& theInputSource);
124:
125: virtual XalanDocument*
126: getDocument() const;
127:
128: virtual XalanParsedSourceHelper*
129: createHelper() const;
130:
131: private:
132:
133: XalanSourceTreeDOMSupport m_domSupport;
134:
1.10 parser 135: XalanSourceTreeParserLiaison2 m_parserLiaison2;
1.9 parser 136:
137: XalanSourceTreeDocument* const m_parsedSource;
138: };
139:
140: XalanDefaultParsedSource2::XalanDefaultParsedSource2(const XSLTInputSource& theInputSource):
141: XalanParsedSource(),
142: m_domSupport(),
1.10 parser 143: m_parserLiaison2(m_domSupport),
144: m_parsedSource(m_parserLiaison2.mapDocument(m_parserLiaison2.parseXMLStream2(theInputSource)))
1.9 parser 145: {
146: assert(m_parsedSource != 0);
147:
1.10 parser 148: m_domSupport.setParserLiaison(&m_parserLiaison2);
1.9 parser 149: }
150:
151:
152:
153: XalanDocument*
154: XalanDefaultParsedSource2::getDocument() const
155: {
156: return m_parsedSource;
157: }
158:
159:
160:
161: XalanParsedSourceHelper*
162: XalanDefaultParsedSource2::createHelper() const
163: {
164: return new XalanDefaultParsedSourceHelper(m_domSupport);
165: }
166:
167:
168:
169:
1.1 parser 170: static void create_optioned_listener(
171: const char *& content_type, const char *& charset, FormatterListener *& listener,
172: Pool& pool,
173: const String& method_name, MethodParams *params, int index, Writer& writer) {
174: // default encoding from pool
175: const String *scharset=&pool.get_charset();
176: const String *method=0;
177: XalanDOMString xalan_encoding;
178:
179: if(params->size()>index) {
180: Value& voptions=params->as_no_junction(index, "options must not be code");
181: if(voptions.is_defined()) {
182: if(Hash *options=voptions.get_hash()) {
183: // $.method[xml|html|text]
184: if(Value *vmethod=static_cast<Value *>(options->get(*new(pool)
185: String(pool, XDOC_OUTPUT_METHOD_OPTION_NAME))))
186: method=&vmethod->as_string();
187:
188: // $.encoding[windows-1251|...]
189: if(Value *vencoding=static_cast<Value *>(options->get(*new(pool)
190: String(pool, XDOC_OUTPUT_ENCODING_OPTION_NAME)))) {
191: scharset=&vencoding->as_string();
192: }
193: } else
194: PTHROW(0, 0,
195: &method_name,
196: "options must be hash");
197: }
198: }
199:
200: xalan_encoding.append(charset=scharset->cstr());
201: if(!method/*default='xml'*/ || *method == XDOC_OUTPUT_METHOD_OPTION_VALUE_XML) {
202: content_type="text/xml";
203: listener=new FormatterToXML(writer,
204: XalanDOMString(), // version
205: true, // doIndent
206: XDOC_OUTPUT_DEFAULT_INDENT, // indent
207: xalan_encoding // encoding
208: );
209: } else if(*method == XDOC_OUTPUT_METHOD_OPTION_VALUE_HTML) {
210: content_type="text/html";
211: listener=new FormatterToHTML(writer,
212: xalan_encoding, // encoding
213: XalanDOMString(), // mediaType
214: XalanDOMString(), // doctypeSystem; String to be printed at the top of the document
215: XalanDOMString(), // doctypePublic
216: true, // doIndent
217: XDOC_OUTPUT_DEFAULT_INDENT // indent
218: );
219: } else if(*method == XDOC_OUTPUT_METHOD_OPTION_VALUE_TEXT) {
220: content_type="text/plain";
221: listener=new FormatterToText(writer,
222: xalan_encoding // encoding
223: );
224: } else
225: PTHROW(0, 0,
226: method,
227: XDOC_OUTPUT_METHOD_OPTION_NAME " option is invalid; valid methods are: "
228: "'" XDOC_OUTPUT_METHOD_OPTION_VALUE_XML "', "
229: "'" XDOC_OUTPUT_METHOD_OPTION_VALUE_HTML "', "
230: "'" XDOC_OUTPUT_METHOD_OPTION_VALUE_TEXT "'");
231:
232: // never reached
233: }
234:
235: static void _save(Request& r, const String& method_name, MethodParams *params) {
236: Pool& pool=r.pool();
237: VXnode& vnode=*static_cast<VXnode *>(r.self);
238:
239: // filespec
240: const String& file_name=params->as_string(0, "file name must not be code");
241: const char *filespec=r.absolute(file_name).cstr(String::UL_FILE_SPEC);
242:
243: // node
244: XalanNode& node=vnode.get_node(pool, &method_name);
245:
246: try {
247: XalanFileOutputStream stream(XalanDOMString(filespec, strlen(filespec)));
248: XalanOutputStreamPrintWriter writer(stream);
249: const char *content_type, *charset;
250: FormatterListener *formatterListener;
251: create_optioned_listener(content_type, charset, formatterListener,
252: pool, method_name, params, 1, writer);
253: FormatterTreeWalker treeWalker(*formatterListener);
254: treeWalker.traverse(&node); // Walk that node and produce the XML...
255: } catch(const XSLException& e) {
256: r._throw(&method_name, e);
257: }
258: }
259:
260: static void _string(Request& r, const String& method_name, MethodParams *params) {
261: Pool& pool=r.pool();
262: VXnode& vnode=*static_cast<VXnode *>(r.self);
263:
264: // node
265: XalanNode& node=vnode.get_node(pool, &method_name);
266:
267: try {
268: String parserString=*new(pool) String(pool);
269: ParserStringXalanOutputStream stream(parserString);
270: XalanOutputStreamPrintWriter writer(stream);
271: const char *content_type, *charset;
272: FormatterListener *formatterListener;
273: create_optioned_listener(content_type, charset, formatterListener,
274: pool, method_name, params, 0, writer);
275: FormatterTreeWalker treeWalker(*formatterListener);
276: treeWalker.traverse(&node); // Walk that node and produce the XML...
277:
278: // write out result
279: r.write_no_lang(parserString);
280: } catch(const XSLException& e) {
281: r._throw(&method_name, e);
282: }
283: }
284:
285:
286: static void _file(Request& r, const String& method_name, MethodParams *params) {
287: Pool& pool=r.pool();
288: VXnode& vnode=*static_cast<VXnode *>(r.self);
289:
290: // node
291: XalanNode& node=vnode.get_node(pool, &method_name);
292:
293: try {
294: String& parserString=*new(pool) String(pool);
295: ParserStringXalanOutputStream stream(parserString);
296: XalanOutputStreamPrintWriter writer(stream);
297: const char *content_type, *charset;
298: FormatterListener *formatterListener;
299: create_optioned_listener(content_type, charset, formatterListener,
300: pool, method_name, params, 0, writer);
301: FormatterTreeWalker treeWalker(*formatterListener);
302: treeWalker.traverse(&node); // Walk that node and produce the XML...
303:
304: // write out result
305: VFile& vfile=*new(pool) VFile(pool);
306: const char *cstr=parserString.cstr();
307: String *scontent_type=new(pool) String(pool, content_type);
308: Value *vcontent_type;
309: if(charset) {
310: VHash *vhcontent_type=new(pool) VHash(pool);
311: vhcontent_type->hash().put(*value_name, new(pool) VString(*scontent_type));
312: String *scharset=new(pool) String(pool, charset);
313: vhcontent_type->hash().put(*new(pool) String(pool, "charset"), new(pool) VString(*scharset));
314: vcontent_type=vhcontent_type;
315: } else
316: vcontent_type=new(pool) VString(*scontent_type);
317: vfile.set(false/*tainted*/, cstr, strlen(cstr), 0/*file_name*/, vcontent_type);
318: r.write_no_lang(vfile);
319: } catch(const XSLException& e) {
320: r._throw(&method_name, e);
321: }
322: }
323:
324: static void _set(Request& r, const String& method_name, MethodParams *params) {
325: Pool& pool=r.pool();
1.5 parser 326: VXdoc& vdoc=*static_cast<VXdoc *>(r.self);
1.1 parser 327:
328: Value& vxml=params->as_junction(0, "xml must be code");
329: Temp_lang temp_lang(r, String::UL_XML);
330: const String& xml=r.process(vxml).as_string();
331:
332: std::istrstream stream(xml.cstr());
333: const XalanParsedSource* parsedSource;
1.9 parser 334: /* int error=vdoc.transformer().parseSource(&stream, parsedSource);
1.1 parser 335:
336: if(error)
337: PTHROW(0, 0,
338: &method_name,
1.5 parser 339: vdoc.transformer().getLastError());
1.9 parser 340: */
341:
1.11 ! parser 342: try {
! 343: parsedSource = new XalanDefaultParsedSource2(&stream);
1.9 parser 344: }
1.10 parser 345: catch (XSLException& e) {
346: r._throw(&method_name, e);
347: }
348: catch (SAXParseException& e) {
349: r._throw(&method_name, e);
1.9 parser 350: }
1.10 parser 351: catch (SAXException& e) {
352: r._throw(&method_name, e);
1.9 parser 353: }
1.10 parser 354: catch (XMLException& e) {
355: r._throw(&method_name, e);
1.9 parser 356: }
1.10 parser 357: catch(const XalanDOMException& e) {
358: r._throw(&method_name, e);
1.9 parser 359: }
1.1 parser 360:
361: // replace any previous parsed source
1.5 parser 362: vdoc.set_parsed_source(*parsedSource);
1.1 parser 363: }
364:
365: static void _load(Request& r, const String& method_name, MethodParams *params) {
366: Pool& pool=r.pool();
1.5 parser 367: VXdoc& vdoc=*static_cast<VXdoc *>(r.self);
1.1 parser 368:
369: // filespec
370: const String& file_name=params->as_string(0, "file name must not be code");
371: const char *filespec=r.absolute(file_name).cstr(String::UL_FILE_SPEC);
372:
373: const XalanParsedSource* parsedSource;
1.5 parser 374: int error=vdoc.transformer().parseSource(filespec, parsedSource);
1.1 parser 375:
376: if(error)
377: PTHROW(0, 0,
378: &file_name,
1.5 parser 379: vdoc.transformer().getLastError());
1.1 parser 380:
381: // replace any previous parsed source
1.5 parser 382: vdoc.set_parsed_source(*parsedSource);
1.1 parser 383: }
384:
385: static void add_xslt_param(const Hash::Key& aattribute, Hash::Val *ameaning,
386: void *info) {
387: XalanTransformer& transformer=*static_cast<XalanTransformer *>(info);
388: const char *attribute_cstr=aattribute.cstr();
389: const char *meaning_cstr=static_cast<Value *>(ameaning)->as_string().cstr();
390:
391: transformer.setStylesheetParam(
392: XalanDOMString(attribute_cstr),
393: XalanDOMString(meaning_cstr));
394: }
1.8 parser 395: static void _transform(Request& r, const String& method_name, MethodParams *params) {
1.1 parser 396: Pool& pool=r.pool();
1.5 parser 397: VXdoc& vdoc=*static_cast<VXdoc *>(r.self);
1.1 parser 398:
399: // params
400: if(params->size()>1) {
401: Value& vparams=params->as_no_junction(1, "transform parameters parameter must not be code");
402: if(vparams.is_defined())
403: if(Hash *params=vparams.get_hash())
1.5 parser 404: params->for_each(add_xslt_param, &vdoc.transformer());
1.1 parser 405: else
406: PTHROW(0, 0,
407: &method_name,
408: "transform parameters parameter must be hash");
409: }
410:
411: // source
1.5 parser 412: const XalanParsedSource &parsed_source=vdoc.get_parsed_source(pool, &method_name);
1.1 parser 413:
414: // stylesheet
415: const String& stylesheet_file_name=params->as_string(0, "file name must not be code");
416: const String& stylesheet_filespec=r.absolute(stylesheet_file_name);
417: //_asm int 3;
418: Stylesheet_connection& connection=XSLT_stylesheet_manager->get_connection(stylesheet_filespec);
419:
420: // target
1.5 parser 421: XalanDocument* target=vdoc.parser_liaison().createDocument();
1.1 parser 422: XSLTResultTarget domResultTarget(target);
423:
424: // transform
1.5 parser 425: int error=vdoc.transformer().transform(
1.1 parser 426: parsed_source,
1.6 parser 427: &connection.stylesheet(true/*nocache*/),
1.1 parser 428: domResultTarget);
429: connection.close();
430: if(error)
431: PTHROW(0, 0,
432: &stylesheet_file_name,
1.5 parser 433: vdoc.transformer().getLastError());
1.1 parser 434:
435: // write out result
436: VXdoc& result=*new(pool) VXdoc(pool);
437: result.set_document(*target);
438: r.write_no_lang(result);
439: }
440:
1.2 parser 441: static void _getElementById(Request& r, const String& method_name, MethodParams *params) {
442: Pool& pool=r.pool();
443: VXdoc& vdoc=*static_cast<VXdoc *>(r.self);
444:
445: // elementId
446: const char *elementId=params->as_string(0, "elementID must not be code").cstr(String::UL_AS_IS);
447:
448: if(XalanElement *element=
449: vdoc.get_document(pool, &method_name).getElementById(XalanDOMString(elementId))) {
450: // write out result
451: VXnode& result=*new(pool) VXnode(pool, element);
452: r.write_no_lang(result);
453: }
454: }
1.4 parser 455: /*
1.2 parser 456: static void _getElementsByTagName(Request& r, const String& method_name, MethodParams *params) {
457: Pool& pool=r.pool();
458: VXdoc& vdoc=*static_cast<VXdoc *>(r.self);
459:
460: // tagname
461: const char *tagname=params->as_string(0, "tagname must not be code").cstr(String::UL_AS_IS);
462:
463: VHash& result=*new(pool) VHash(pool);
464: if(const XalanNodeList *nodes=
465: vdoc.get_document(pool, &method_name).getElementsByTagName(XalanDOMString(tagname))) {
466: for(int i=0; i<nodes->getLength(); i++) {
467: String& skey=*new(pool) String(pool);
468: {
469: char *buf=(char *)pool.malloc(MAX_NUMBER);
470: snprintf(buf, MAX_NUMBER, "%d", i);
471: skey << buf;
472: }
473:
474: result.hash().put(skey, new(pool) VXnode(pool, nodes->item(i)));
475: }
476: }
477:
478: // write out result
479: r.write_no_lang(result);
480: }
481:
482: static void _getElementsByTagNameNS(Request& r, const String& method_name, MethodParams *params) {
483: Pool& pool=r.pool();
484: VXdoc& vdoc=*static_cast<VXdoc *>(r.self);
485:
486: // namespaceURI;localName
487: const char *namespaceURI=params->as_string(0, "namespaceURI must not be code").cstr(String::UL_AS_IS);
488: const char *localName=params->as_string(0, "localName must not be code").cstr(String::UL_AS_IS);
489:
490: VHash& result=*new(pool) VHash(pool);
491: if(const XalanNodeList *nodes=
492: vdoc.get_document(pool, &method_name).getElementsByTagNameNS(
493: XalanDOMString(namespaceURI), XalanDOMString(localName))) {
494: for(int i=0; i<nodes->getLength(); i++) {
495: String& skey=*new(pool) String(pool);
496: {
497: char *buf=(char *)pool.malloc(MAX_NUMBER);
498: snprintf(buf, MAX_NUMBER, "%d", i);
499: skey << buf;
500: }
501:
502: result.hash().put(skey, new(pool) VXnode(pool, nodes->item(i)));
503: }
504: }
505:
506: // write out result
507: r.write_no_lang(result);
508: }
1.4 parser 509: */
1.1 parser 510: // constructor
511:
512: MXdoc::MXdoc(Pool& apool) : MXnode(apool) {
513: set_name(*NEW String(pool(), XDOC_CLASS_NAME));
514:
1.2 parser 515: // ^xdoc.save[some.xml]
516: // ^xdoc.save[some.xml;options hash]
1.1 parser 517: add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2);
518:
1.2 parser 519: // ^xdoc.string[] <doc/>
520: // ^xdoc.string[options hash] <doc/>
1.1 parser 521: add_native_method("string", Method::CT_DYNAMIC, _string, 0, 1);
522:
1.2 parser 523: // ^xdoc.file[] file with "<doc/>"
524: // ^xdoc.file[options hash] file with "<doc/>"
1.1 parser 525: add_native_method("file", Method::CT_DYNAMIC, _file, 0, 1);
526:
1.2 parser 527: // ^xdoc::set[<some>xml</some>]
1.1 parser 528: add_native_method("set", Method::CT_DYNAMIC, _set, 1, 1);
529:
1.2 parser 530: // ^xdoc::load[some.xml]
1.1 parser 531: add_native_method("load", Method::CT_DYNAMIC, _load, 1, 1);
532:
1.8 parser 533: // ^xdoc.transform[stylesheet file_name]
534: // ^xdoc.transform[stylesheet file_name;params hash]
535: add_native_method("transform", Method::CT_DYNAMIC, _transform, 1, 2);
1.2 parser 536:
537: // ^xdoc.getElementById[elementId]
538: add_native_method("getElementById", Method::CT_DYNAMIC, _getElementById, 1, 1);
1.4 parser 539: /*
1.3 parser 540: // ^xdoc.getElementsByTagName[tagname]
1.2 parser 541: add_native_method("getElementsByTagName", Method::CT_DYNAMIC, _getElementsByTagName, 1, 1);
542:
543: // ^xdoc.getElementsByTagNameNS[namespaceURI;localName] = array of nodes
544: add_native_method("getElementsByTagNameNS", Method::CT_DYNAMIC, _getElementsByTagNameNS, 2, 2);
1.4 parser 545: */
1.1 parser 546: }
1.5 parser 547:
548: void MXdoc::configure_admin(Request& r) {
549: }
550:
1.1 parser 551: // global variable
552:
553: Methoded *Xdoc_class;
554:
555: // creator
556:
557: #endif
558:
559: Methoded *MXdoc_create(Pool& pool) {
560: return
561: #ifdef XML
562: Xdoc_class=new(pool) MXdoc(pool);
563: #else
564: 0
565: #endif
566: ;
567: }
E-mail: