Annotation of parser3/src/classes/xdoc.C, revision 1.16

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.16    ! parser      7:        $Id: xdoc.C,v 1.15 2001/10/12 12:15:32 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.13      parser     23: #include "XalanTransformer2.hpp"
1.1       parser     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.16    ! parser     38: #include <XalanDOM/XalanDocumentFragment.hpp>
        !            39: #include <XalanDOM/XalanCDATASection.hpp>
        !            40: #include <XalanDOM/XalanEntityReference.hpp>
1.1       parser     41: 
                     42: // defines
                     43: 
                     44: #define XDOC_CLASS_NAME "xdoc"
                     45: 
                     46: #define XDOC_OUTPUT_METHOD_OPTION_NAME "method"
                     47: #define XDOC_OUTPUT_METHOD_OPTION_VALUE_XML "xml"
                     48: #define XDOC_OUTPUT_METHOD_OPTION_VALUE_HTML "html"
                     49: #define XDOC_OUTPUT_METHOD_OPTION_VALUE_TEXT "text"
                     50: 
                     51: #define XDOC_OUTPUT_ENCODING_OPTION_NAME "encoding"
                     52: 
                     53: #define XDOC_OUTPUT_DEFAULT_INDENT 4
                     54: 
                     55: // class
                     56: 
                     57: class MXdoc : public MXnode {
                     58: public: // VStateless_class
                     59:        Value *create_new_value(Pool& pool) { return new(pool) VXdoc(pool); }
                     60: 
                     61: public:
                     62:        MXdoc(Pool& pool);
                     63: 
                     64: public: // Methoded
                     65:        bool used_directly() { return true; }
1.5       parser     66:        void configure_admin(Request& r);
1.1       parser     67: };
                     68: 
                     69: // methods
                     70: 
1.16    ! parser     71: // Element createElement(in DOMString tagName) raises(DOMException);
        !            72: static void _createElement(Request& r, const String& method_name, MethodParams *params) {
        !            73:        Pool& pool=r.pool();
        !            74:        VXdoc& vdoc=*static_cast<VXdoc *>(r.self);
        !            75: 
        !            76:        const char *tagName=params->as_string(0, "tagName must not be code").cstr(String::UL_AS_IS);
        !            77: 
        !            78:        try {
        !            79:                XalanNode *node=
        !            80:                        vdoc.get_document(pool, &method_name).
        !            81:                        createElement(XalanDOMString(tagName));
        !            82:                // write out result
        !            83:                VXnode& result=*new(pool) VXnode(pool, node);
        !            84:                r.write_no_lang(result);
        !            85:        } catch(const XalanDOMException& e)     {
        !            86:                pool.exception()._throw(pool, &method_name, e);
        !            87:        }
        !            88: }
        !            89: 
        !            90: // DocumentFragment createDocumentFragment()
        !            91: static void _createDocumentFragment(Request& r, const String& method_name, MethodParams *params) {
        !            92:        Pool& pool=r.pool();
        !            93:        VXdoc& vdoc=*static_cast<VXdoc *>(r.self);
        !            94: 
        !            95:        XalanNode *node=
        !            96:                vdoc.get_document(pool, &method_name).
        !            97:                createDocumentFragment();
        !            98:        // write out result
        !            99:        VXnode& result=*new(pool) VXnode(pool, node);
        !           100:        r.write_no_lang(result);
        !           101: }
        !           102: 
        !           103: // Text createTextNode(in DOMString data);
        !           104: static void _createTextNode(Request& r, const String& method_name, MethodParams *params) {
        !           105:        Pool& pool=r.pool();
        !           106:        VXdoc& vdoc=*static_cast<VXdoc *>(r.self);
        !           107: 
        !           108:        const char *data=params->as_string(0, "data must not be code").cstr(String::UL_AS_IS);
        !           109: 
        !           110:        XalanNode *node=
        !           111:                vdoc.get_document(pool, &method_name).
        !           112:                createTextNode(XalanDOMString(data));
        !           113:        // write out result
        !           114:        VXnode& result=*new(pool) VXnode(pool, node);
        !           115:        r.write_no_lang(result);
        !           116: }
        !           117: 
        !           118: // Comment createComment(in DOMString data)
        !           119: static void _createComment(Request& r, const String& method_name, MethodParams *params) {
        !           120:        Pool& pool=r.pool();
        !           121:        VXdoc& vdoc=*static_cast<VXdoc *>(r.self);
        !           122: 
        !           123:        const char *data=params->as_string(0, "data must not be code").cstr(String::UL_AS_IS);
        !           124: 
        !           125:        XalanNode *node=
        !           126:                vdoc.get_document(pool, &method_name).
        !           127:                createComment(XalanDOMString(data));
        !           128:        // write out result
        !           129:        VXnode& result=*new(pool) VXnode(pool, node);
        !           130:        r.write_no_lang(result);
        !           131: }
        !           132: 
        !           133: // CDATASection createCDATASection(in DOMString data) raises(DOMException);
        !           134: static void _createCDATASection(Request& r, const String& method_name, MethodParams *params) {
        !           135:        Pool& pool=r.pool();
        !           136:        VXdoc& vdoc=*static_cast<VXdoc *>(r.self);
        !           137: 
        !           138:        const char *data=params->as_string(0, "data must not be code").cstr(String::UL_AS_IS);
        !           139: 
        !           140:        try {
        !           141:                XalanNode *node=
        !           142:                        vdoc.get_document(pool, &method_name).
        !           143:                        createCDATASection(XalanDOMString(data));
        !           144:                // write out result
        !           145:                VXnode& result=*new(pool) VXnode(pool, node);
        !           146:                r.write_no_lang(result);
        !           147:        } catch(const XalanDOMException& e)     {
        !           148:                pool.exception()._throw(pool, &method_name, e);
        !           149:        }
        !           150: }
        !           151: 
        !           152: // ProcessingInstruction createProcessingInstruction(in DOMString target,in DOMString data) raises(DOMException);
        !           153: static void _createProcessingInstruction(Request& r, const String& method_name, MethodParams *params) {
        !           154:        Pool& pool=r.pool();
        !           155:        VXdoc& vdoc=*static_cast<VXdoc *>(r.self);
        !           156: 
        !           157:        const char *target=params->as_string(0, "target must not be code").cstr(String::UL_AS_IS);
        !           158:        const char *data=params->as_string(1, "data must not be code").cstr(String::UL_AS_IS);
        !           159: 
        !           160:        try {
        !           161:                XalanNode *node=
        !           162:                        vdoc.get_document(pool, &method_name).
        !           163:                        createProcessingInstruction(XalanDOMString(target), XalanDOMString(data));
        !           164:                // write out result
        !           165:                VXnode& result=*new(pool) VXnode(pool, node);
        !           166:                r.write_no_lang(result);
        !           167:        } catch(const XalanDOMException& e)     {
        !           168:                pool.exception()._throw(pool, &method_name, e);
        !           169:        }
        !           170: }
        !           171: 
        !           172: // Attr createAttribute(in DOMString name) raises(DOMException);
        !           173: static void _createAttribute(Request& r, const String& method_name, MethodParams *params) {
        !           174:        Pool& pool=r.pool();
        !           175:        VXdoc& vdoc=*static_cast<VXdoc *>(r.self);
        !           176: 
        !           177:        const char *name=params->as_string(0, "name must not be code").cstr(String::UL_AS_IS);
        !           178: 
        !           179:        try {
        !           180:                XalanNode *node=
        !           181:                        vdoc.get_document(pool, &method_name).
        !           182:                        createAttribute(XalanDOMString(name));
        !           183:                // write out result
        !           184:                VXnode& result=*new(pool) VXnode(pool, node);
        !           185:                r.write_no_lang(result);
        !           186:        } catch(const XalanDOMException& e)     {
        !           187:                pool.exception()._throw(pool, &method_name, e);
        !           188:        }
        !           189: }
        !           190: // EntityReference createEntityReference(in DOMString name) raises(DOMException);
        !           191: static void _createEntityReference(Request& r, const String& method_name, MethodParams *params) {
        !           192:        Pool& pool=r.pool();
        !           193:        VXdoc& vdoc=*static_cast<VXdoc *>(r.self);
        !           194: 
        !           195:        const char *name=params->as_string(0, "name must not be code").cstr(String::UL_AS_IS);
        !           196: 
        !           197:        try {
        !           198:                XalanNode *node=
        !           199:                        vdoc.get_document(pool, &method_name).
        !           200:                        createEntityReference(XalanDOMString(name));
        !           201:                // write out result
        !           202:                VXnode& result=*new(pool) VXnode(pool, node);
        !           203:                r.write_no_lang(result);
        !           204:        } catch(const XalanDOMException& e)     {
        !           205:                pool.exception()._throw(pool, &method_name, e);
        !           206:        }
        !           207: }
        !           208: 
        !           209: /*
        !           210: static void _getElementsByTagName(Request& r, const String& method_name, MethodParams *params) {
        !           211:        Pool& pool=r.pool();
        !           212:        VXdoc& vdoc=*static_cast<VXdoc *>(r.self);
        !           213: 
        !           214:        // tagname
        !           215:        const char *name=params->as_string(0, "name must not be code").cstr(String::UL_AS_IS);
        !           216: 
        !           217:        VHash& result=*new(pool) VHash(pool);
        !           218:        if(const XalanNodeList *nodes=
        !           219:                vdoc.get_document(pool, &method_name).getElementsByTagName(XalanDOMString(name))) {
        !           220:                for(int i=0; i<nodes->getLength(); i++) {
        !           221:                        String& skey=*new(pool) String(pool);
        !           222:                        {
        !           223:                                char *buf=(char *)pool.malloc(MAX_NUMBER);
        !           224:                                snprintf(buf, MAX_NUMBER, "%d", i);
        !           225:                                skey << buf;
        !           226:                        }
        !           227: 
        !           228:                        result.hash().put(skey, new(pool) VXnode(pool, nodes->item(i)));
        !           229:                }
        !           230:        }
        !           231: 
        !           232:        // write out result
        !           233:        r.write_no_lang(result);
        !           234: }
        !           235: 
        !           236: static void _getElementsByTagNameNS(Request& r, const String& method_name, MethodParams *params) {
        !           237:        Pool& pool=r.pool();
        !           238:        VXdoc& vdoc=*static_cast<VXdoc *>(r.self);
        !           239: 
        !           240:        // namespaceURI;localName
        !           241:        const char *namespaceURI=params->as_string(0, "namespaceURI must not be code").cstr(String::UL_AS_IS);
        !           242:        const char *localName=params->as_string(0, "localName must not be code").cstr(String::UL_AS_IS);
        !           243: 
        !           244:        VHash& result=*new(pool) VHash(pool);
        !           245:        if(const XalanNodeList *nodes=
        !           246:                vdoc.get_document(pool, &method_name).getElementsByTagNameNS(
        !           247:                        XalanDOMString(namespaceURI), XalanDOMString(localName))) {
        !           248:                for(int i=0; i<nodes->getLength(); i++) {
        !           249:                        String& skey=*new(pool) String(pool);
        !           250:                        {
        !           251:                                char *buf=(char *)pool.malloc(MAX_NUMBER);
        !           252:                                snprintf(buf, MAX_NUMBER, "%d", i);
        !           253:                                skey << buf;
        !           254:                        }
        !           255: 
        !           256:                        result.hash().put(skey, new(pool) VXnode(pool, nodes->item(i)));
        !           257:                }
        !           258:        }
        !           259: 
        !           260:        // write out result
        !           261:        r.write_no_lang(result);
        !           262: }
        !           263: */
        !           264: 
        !           265: 
1.1       parser    266: class ParserStringXalanOutputStream: public XalanOutputStream {
                    267: public:
                    268:        
                    269:        explicit ParserStringXalanOutputStream(String& astring) : fstring(astring) {}
                    270: 
                    271: protected: // XalanOutputStream
                    272: 
                    273:        virtual void writeData(const char *theBuffer, unsigned long theBufferLength) {
                    274:                char *copy=(char *)fstring.malloc((size_t)theBufferLength);
                    275:                memcpy(copy, theBuffer, (size_t)theBufferLength);
1.2       parser    276:                fstring.APPEND_CLEAN(copy, (size_t)theBufferLength, "xdoc", 0);
1.1       parser    277:        }
                    278: 
                    279:        virtual void doFlush() {}
                    280: 
                    281: private:
                    282: 
                    283:        String& fstring;
                    284:        
                    285: };
                    286: 
1.9       parser    287: class XalanSourceTreeParserLiaison2: public XalanSourceTreeParserLiaison {
                    288: public:
1.16    ! parser    289:        XalanSourceTreeParserLiaison2(XalanSourceTreeDOMSupport&        theSupport) : XalanSourceTreeParserLiaison(theSupport), 
1.9       parser    290:                ferror_handler(new HandlerBase) {
                    291:        }
                    292: 
1.10      parser    293:        XalanDocument*
                    294:        parseXMLStream2(
1.16    ! parser    295:                                const InputSource&      inputSource) {
1.10      parser    296:                XalanSourceTreeContentHandler   theContentHandler(createXalanSourceTreeDocument());
                    297:                XalanAutoPtr<SAX2XMLReader>             theReader(XMLReaderFactory::createXMLReader());
                    298:                theReader->setContentHandler(&theContentHandler);
                    299:                theReader->setDTDHandler(&theContentHandler);
                    300:                theReader->setErrorHandler(ferror_handler); // disable stderr output
                    301:                theReader->setLexicalHandler(&theContentHandler);
                    302:                EntityResolver* const   theResolver = getEntityResolver();
                    303:                if (theResolver != 0) {
                    304:                        theReader->setEntityResolver(theResolver);
                    305:                }
                    306:                theReader->parse(inputSource);
                    307:                return theContentHandler.getDocument();
                    308:        }
                    309:        
1.9       parser    310:        ~XalanSourceTreeParserLiaison2() {
1.11      parser    311:                delete ferror_handler;
1.9       parser    312:        }
                    313: private:
                    314:        ErrorHandler *ferror_handler;
                    315: };
                    316: 
                    317: class XalanDefaultParsedSource2 : public XalanParsedSource
                    318: {
                    319: public:
                    320: 
1.16    ! parser    321:        XalanDefaultParsedSource2(const XSLTInputSource&        theInputSource);
1.9       parser    322: 
                    323:        virtual XalanDocument*
                    324:        getDocument() const;
                    325: 
                    326:        virtual XalanParsedSourceHelper*
                    327:        createHelper() const;
                    328: 
                    329: private:
                    330: 
                    331:        XalanSourceTreeDOMSupport               m_domSupport;
                    332: 
1.10      parser    333:        XalanSourceTreeParserLiaison2   m_parserLiaison2;
1.9       parser    334: 
                    335:        XalanSourceTreeDocument* const  m_parsedSource;
                    336: };
                    337: 
1.16    ! parser    338: XalanDefaultParsedSource2::XalanDefaultParsedSource2(const XSLTInputSource& theInputSource):
1.9       parser    339:        XalanParsedSource(),
                    340:        m_domSupport(),
1.10      parser    341:        m_parserLiaison2(m_domSupport),
                    342:        m_parsedSource(m_parserLiaison2.mapDocument(m_parserLiaison2.parseXMLStream2(theInputSource)))
1.9       parser    343: {
                    344:        assert(m_parsedSource != 0);
                    345: 
1.10      parser    346:        m_domSupport.setParserLiaison(&m_parserLiaison2);
1.9       parser    347: }
                    348: 
                    349: 
                    350: 
                    351: XalanDocument* 
                    352: XalanDefaultParsedSource2::getDocument() const
                    353: {
                    354:        return m_parsedSource;
                    355: }
                    356: 
                    357: 
                    358: 
                    359: XalanParsedSourceHelper*
                    360: XalanDefaultParsedSource2::createHelper() const
                    361: {
                    362:        return new XalanDefaultParsedSourceHelper(m_domSupport);
                    363: }
                    364: 
                    365: 
                    366: 
                    367: 
1.1       parser    368: static void create_optioned_listener(
                    369:                                                                         const char *& content_type, const char *& charset, FormatterListener *& listener, 
                    370:                                                                         Pool& pool, 
                    371:                                                                         const String& method_name, MethodParams *params, int index, Writer& writer) {
                    372:        // default encoding from pool
                    373:        const String *scharset=&pool.get_charset();
                    374:        const String *method=0;
                    375:        XalanDOMString xalan_encoding;
                    376: 
                    377:        if(params->size()>index) {
                    378:                Value& voptions=params->as_no_junction(index, "options must not be code");
                    379:                if(voptions.is_defined()) {
                    380:                        if(Hash *options=voptions.get_hash()) {
                    381:                                // $.method[xml|html|text]
                    382:                                if(Value *vmethod=static_cast<Value *>(options->get(*new(pool) 
                    383:                                        String(pool, XDOC_OUTPUT_METHOD_OPTION_NAME))))
                    384:                                        method=&vmethod->as_string();
                    385: 
                    386:                                // $.encoding[windows-1251|...]
                    387:                                if(Value *vencoding=static_cast<Value *>(options->get(*new(pool) 
                    388:                                        String(pool, XDOC_OUTPUT_ENCODING_OPTION_NAME)))) {
                    389:                                        scharset=&vencoding->as_string();
                    390:                                }
                    391:                        } else
                    392:                                PTHROW(0, 0,
                    393:                                        &method_name,
                    394:                                        "options must be hash");
                    395:                }
                    396:        }
                    397: 
                    398:        xalan_encoding.append(charset=scharset->cstr());
                    399:        if(!method/*default='xml'*/ || *method == XDOC_OUTPUT_METHOD_OPTION_VALUE_XML) {
                    400:                content_type="text/xml";
                    401:                listener=new FormatterToXML(writer,
                    402:                        XalanDOMString(),  // version
                    403:                        true, // doIndent
                    404:                        XDOC_OUTPUT_DEFAULT_INDENT, // indent 
                    405:                        xalan_encoding  // encoding
                    406:                );
                    407:        } else if(*method == XDOC_OUTPUT_METHOD_OPTION_VALUE_HTML) {
                    408:                content_type="text/html";
                    409:                listener=new FormatterToHTML(writer,
                    410:                        xalan_encoding,  // encoding
                    411:                        XalanDOMString(),  // mediaType 
                    412:                        XalanDOMString(),  // doctypeSystem; String to be printed at the top of the document 
                    413:                        XalanDOMString(),  // doctypePublic  
                    414:                        true, // doIndent 
                    415:                        XDOC_OUTPUT_DEFAULT_INDENT // indent 
                    416:                );
                    417:        } else if(*method == XDOC_OUTPUT_METHOD_OPTION_VALUE_TEXT) {
                    418:                content_type="text/plain";
                    419:                listener=new FormatterToText(writer,
                    420:                        xalan_encoding  // encoding
                    421:                );
                    422:        } else
                    423:                PTHROW(0, 0,
                    424:                        method,
                    425:                        XDOC_OUTPUT_METHOD_OPTION_NAME " option is invalid; valid methods are: "
                    426:                                "'" XDOC_OUTPUT_METHOD_OPTION_VALUE_XML "', "
                    427:                                "'" XDOC_OUTPUT_METHOD_OPTION_VALUE_HTML "', "
                    428:                                "'" XDOC_OUTPUT_METHOD_OPTION_VALUE_TEXT "'");                  
                    429: 
                    430:        // never reached
                    431: }
                    432: 
                    433: static void _save(Request& r, const String& method_name, MethodParams *params) {
                    434:        Pool& pool=r.pool();
                    435:        VXnode& vnode=*static_cast<VXnode *>(r.self);
                    436: 
                    437:        // filespec
                    438:        const String& file_name=params->as_string(0, "file name must not be code");
                    439:        const char *filespec=r.absolute(file_name).cstr(String::UL_FILE_SPEC);
                    440:        
                    441:        // node
                    442:        XalanNode& node=vnode.get_node(pool, &method_name);
                    443: 
                    444:        try {
                    445:                XalanFileOutputStream stream(XalanDOMString(filespec, strlen(filespec)));
                    446:                XalanOutputStreamPrintWriter writer(stream);
                    447:                const char *content_type, *charset;
                    448:                FormatterListener *formatterListener;
                    449:                create_optioned_listener(content_type, charset, formatterListener, 
                    450:                        pool, method_name, params, 1, writer);
                    451:                FormatterTreeWalker treeWalker(*formatterListener);
                    452:                treeWalker.traverse(&node); // Walk that node and produce the XML...
                    453:        } catch(const XSLException& e) {
1.14      parser    454:                pool.exception()._throw(pool, &method_name, e);
1.1       parser    455:        }
                    456: }
                    457: 
                    458: static void _string(Request& r, const String& method_name, MethodParams *params) {
                    459:        Pool& pool=r.pool();
                    460:        VXnode& vnode=*static_cast<VXnode *>(r.self);
                    461: 
                    462:        // node
                    463:        XalanNode& node=vnode.get_node(pool, &method_name);
                    464: 
                    465:        try {
                    466:                String parserString=*new(pool) String(pool);
                    467:                ParserStringXalanOutputStream stream(parserString);
                    468:                XalanOutputStreamPrintWriter writer(stream);
                    469:                const char *content_type, *charset;
                    470:                FormatterListener *formatterListener;
                    471:                create_optioned_listener(content_type, charset, formatterListener, 
                    472:                        pool, method_name, params, 0, writer);
                    473:                FormatterTreeWalker treeWalker(*formatterListener);
                    474:                treeWalker.traverse(&node); // Walk that node and produce the XML...
                    475: 
                    476:                // write out result
                    477:                r.write_no_lang(parserString);
                    478:        } catch(const XSLException& e) {
1.14      parser    479:                pool.exception()._throw(pool, &method_name, e);
1.1       parser    480:        }
                    481: }
                    482: 
                    483: 
                    484: static void _file(Request& r, const String& method_name, MethodParams *params) {
                    485:        Pool& pool=r.pool();
                    486:        VXnode& vnode=*static_cast<VXnode *>(r.self);
                    487: 
                    488:        // node
                    489:        XalanNode& node=vnode.get_node(pool, &method_name);
                    490: 
                    491:        try {
                    492:                String& parserString=*new(pool) String(pool);
                    493:                ParserStringXalanOutputStream stream(parserString);
                    494:                XalanOutputStreamPrintWriter writer(stream);
                    495:                const char *content_type, *charset;
                    496:                FormatterListener *formatterListener;
                    497:                create_optioned_listener(content_type, charset, formatterListener, 
                    498:                        pool, method_name, params, 0, writer);
                    499:                FormatterTreeWalker treeWalker(*formatterListener);
                    500:                treeWalker.traverse(&node); // Walk that node and produce the XML...
                    501: 
                    502:                // write out result
                    503:                VFile& vfile=*new(pool) VFile(pool);
                    504:                const char *cstr=parserString.cstr();
                    505:                String *scontent_type=new(pool) String(pool, content_type);
                    506:                Value *vcontent_type;
                    507:                if(charset) {
                    508:                        VHash *vhcontent_type=new(pool) VHash(pool);
                    509:                        vhcontent_type->hash().put(*value_name, new(pool) VString(*scontent_type));
                    510:                        String *scharset=new(pool) String(pool, charset);
                    511:                        vhcontent_type->hash().put(*new(pool) String(pool, "charset"), new(pool) VString(*scharset));
                    512:                        vcontent_type=vhcontent_type;
                    513:                } else
                    514:                        vcontent_type=new(pool) VString(*scontent_type);
                    515:                vfile.set(false/*tainted*/, cstr, strlen(cstr), 0/*file_name*/, vcontent_type);
                    516:                r.write_no_lang(vfile);
                    517:        } catch(const XSLException& e) {
1.14      parser    518:                pool.exception()._throw(pool, &method_name, e);
1.1       parser    519:        }
                    520: }
                    521: 
                    522: static void _set(Request& r, const String& method_name, MethodParams *params) {
1.15      parser    523:        //_asm int 3;
1.1       parser    524:        Pool& pool=r.pool();
1.5       parser    525:        VXdoc& vdoc=*static_cast<VXdoc *>(r.self);
1.1       parser    526: 
                    527:        Value& vxml=params->as_junction(0, "xml must be code");
                    528:        Temp_lang temp_lang(r, String::UL_XML);
                    529:        const String& xml=r.process(vxml).as_string();
                    530: 
                    531:        std::istrstream stream(xml.cstr());
                    532:        const XalanParsedSource* parsedSource;
1.9       parser    533: 
1.11      parser    534:        try {
                    535:                parsedSource = new XalanDefaultParsedSource2(&stream);
1.9       parser    536:        }
1.10      parser    537:        catch (XSLException& e) {
1.14      parser    538:                pool.exception()._throw(pool, &method_name, e);
1.10      parser    539:        }
                    540:        catch (SAXParseException& e)    {
1.14      parser    541:                pool.exception()._throw(pool, &method_name, e);
1.9       parser    542:        }
1.10      parser    543:        catch (SAXException& e) {
1.14      parser    544:                pool.exception()._throw(pool, &method_name, e);
1.9       parser    545:        }
1.10      parser    546:        catch (XMLException& e) {
1.14      parser    547:                pool.exception()._throw(pool, &method_name, e);
1.9       parser    548:        }
1.16    ! parser    549:        catch(const XalanDOMException& e)       {
1.14      parser    550:                pool.exception()._throw(pool, &method_name, e);
1.9       parser    551:        }
1.1       parser    552: 
                    553:        // replace any previous parsed source
1.5       parser    554:        vdoc.set_parsed_source(*parsedSource);
1.1       parser    555: }
                    556: 
                    557: static void _load(Request& r, const String& method_name, MethodParams *params) {
                    558:        Pool& pool=r.pool();
1.5       parser    559:        VXdoc& vdoc=*static_cast<VXdoc *>(r.self);
1.1       parser    560: 
                    561:        // filespec
                    562:        const String& file_name=params->as_string(0, "file name must not be code");
                    563:        const char *filespec=r.absolute(file_name).cstr(String::UL_FILE_SPEC);
                    564:        
                    565:        const XalanParsedSource* parsedSource;
1.12      parser    566:        try {
                    567:                parsedSource = new XalanDefaultParsedSource2(filespec);
                    568:        }
                    569:        catch (XSLException& e) {
1.14      parser    570:                pool.exception()._throw(pool, &method_name, e);
1.12      parser    571:        }
                    572:        catch (SAXParseException& e)    {
1.14      parser    573:                pool.exception()._throw(pool, &method_name, e);
1.12      parser    574:        }
                    575:        catch (SAXException& e) {
1.14      parser    576:                pool.exception()._throw(pool, &method_name, e);
1.12      parser    577:        }
                    578:        catch (XMLException& e) {
1.14      parser    579:                pool.exception()._throw(pool, &method_name, e);
1.12      parser    580:        }
1.16    ! parser    581:        catch(const XalanDOMException& e)       {
1.14      parser    582:                pool.exception()._throw(pool, &method_name, e);
1.12      parser    583:        }
1.1       parser    584: 
                    585:        // replace any previous parsed source
1.5       parser    586:        vdoc.set_parsed_source(*parsedSource);
1.1       parser    587: }
                    588: 
                    589: static void add_xslt_param(const Hash::Key& aattribute, Hash::Val *ameaning, 
                    590:                                                   void *info) {
1.13      parser    591:        XalanTransformer2& transformer=*static_cast<XalanTransformer2 *>(info);
1.1       parser    592:        const char *attribute_cstr=aattribute.cstr();
                    593:        const char *meaning_cstr=static_cast<Value *>(ameaning)->as_string().cstr();
                    594: 
                    595:        transformer.setStylesheetParam(
                    596:                XalanDOMString(attribute_cstr),  
                    597:                XalanDOMString(meaning_cstr));
                    598: }
1.8       parser    599: static void _transform(Request& r, const String& method_name, MethodParams *params) {
1.1       parser    600:        Pool& pool=r.pool();
1.5       parser    601:        VXdoc& vdoc=*static_cast<VXdoc *>(r.self);
1.1       parser    602: 
                    603:        // params
                    604:        if(params->size()>1) {
                    605:                Value& vparams=params->as_no_junction(1, "transform parameters parameter must not be code");
                    606:                if(vparams.is_defined())
                    607:                        if(Hash *params=vparams.get_hash())
1.5       parser    608:                                params->for_each(add_xslt_param, &vdoc.transformer());
1.1       parser    609:                        else
                    610:                                PTHROW(0, 0,
                    611:                                        &method_name,
                    612:                                        "transform parameters parameter must be hash");
                    613:        }
                    614: 
                    615:        // source
1.5       parser    616:        const XalanParsedSource &parsed_source=vdoc.get_parsed_source(pool, &method_name);
1.1       parser    617: 
                    618:        // stylesheet
                    619:        const String& stylesheet_file_name=params->as_string(0, "file name must not be code");
                    620:        const String& stylesheet_filespec=r.absolute(stylesheet_file_name);
                    621:        //_asm int 3;
                    622:        Stylesheet_connection& connection=XSLT_stylesheet_manager->get_connection(stylesheet_filespec);
                    623: 
                    624:        // target
1.5       parser    625:        XalanDocument* target=vdoc.parser_liaison().createDocument();
1.1       parser    626: 
                    627:        // transform
1.13      parser    628:        try {
                    629:                vdoc.transformer().transform2(
                    630:                        parsed_source, 
                    631:                        &connection.stylesheet(true/*nocache*/), 
                    632:                        target);
                    633:        }
                    634:        catch (XSLException& e) {
                    635:                connection.close();
1.14      parser    636:                pool.exception()._throw(pool, &stylesheet_file_name, e);
1.13      parser    637:        }
                    638:        catch (SAXParseException& e)    {
                    639:                connection.close();
1.14      parser    640:                pool.exception()._throw(pool, &stylesheet_file_name, e);
1.13      parser    641:        }
                    642:        catch (SAXException& e) {
                    643:                connection.close();
1.14      parser    644:                pool.exception()._throw(pool, &stylesheet_file_name, e);
1.13      parser    645:        }
                    646:        catch (XMLException& e) {
                    647:                connection.close();
1.14      parser    648:                pool.exception()._throw(pool, &stylesheet_file_name, e);
1.13      parser    649:        }
1.16    ! parser    650:        catch(const XalanDOMException& e)       {
1.13      parser    651:                connection.close();
1.14      parser    652:                pool.exception()._throw(pool, &stylesheet_file_name, e);
1.13      parser    653:        }
1.1       parser    654: 
                    655:        // write out result
                    656:        VXdoc& result=*new(pool) VXdoc(pool);
                    657:        result.set_document(*target);
                    658:        r.write_no_lang(result);
                    659: }
                    660: 
1.2       parser    661: static void _getElementById(Request& r, const String& method_name, MethodParams *params) {
                    662:        Pool& pool=r.pool();
                    663:        VXdoc& vdoc=*static_cast<VXdoc *>(r.self);
                    664: 
                    665:        // elementId
                    666:        const char *elementId=params->as_string(0, "elementID must not be code").cstr(String::UL_AS_IS);
                    667: 
1.16    ! parser    668:        if(XalanNode *node=
1.2       parser    669:                vdoc.get_document(pool, &method_name).getElementById(XalanDOMString(elementId))) {
                    670:                // write out result
1.16    ! parser    671:                VXnode& result=*new(pool) VXnode(pool, node);
1.2       parser    672:                r.write_no_lang(result);
                    673:        }
                    674: }
1.16    ! parser    675: // constructor
1.2       parser    676: 
1.16    ! parser    677: MXdoc::MXdoc(Pool& apool) : MXnode(apool) {
        !           678:        set_name(*NEW String(pool(), XDOC_CLASS_NAME));
1.2       parser    679: 
1.16    ! parser    680:        /// @test how to create empty type html?
1.2       parser    681: 
1.16    ! parser    682:        /// DOM1
1.2       parser    683: 
1.16    ! parser    684:        // Element createElement(in DOMString tagName) raises(DOMException);
        !           685:        add_native_method("createElement", Method::CT_DYNAMIC, _createElement, 1, 1);
        !           686:        // DocumentFragment createDocumentFragment(); 
        !           687:        add_native_method("createDocumentFragment", Method::CT_DYNAMIC, _createDocumentFragment, 0, 0);
        !           688:        // Text createTextNode(in DOMString data);
        !           689:        add_native_method("createTextNode", Method::CT_DYNAMIC, _createTextNode, 1, 1);
        !           690:        // Comment createComment(in DOMString data);
        !           691:        add_native_method("createComment", Method::CT_DYNAMIC, _createComment, 1, 1);
        !           692:        // CDATASection createCDATASection(in DOMString data) raises(DOMException);
        !           693:        add_native_method("createCDATASection", Method::CT_DYNAMIC, _createCDATASection, 1, 1);
        !           694:        // ProcessingInstruction createProcessingInstruction(in DOMString target, in DOMString data) raises(DOMException);
        !           695:        add_native_method("createProcessingInstruction", Method::CT_DYNAMIC, _createProcessingInstruction, 2, 2);
        !           696:        // Attr createAttribute(in DOMString name) raises(DOMException);
        !           697:        add_native_method("createAttribute", Method::CT_DYNAMIC, _createAttribute, 1, 1);
        !           698:        // EntityReference createEntityReference(in DOMString name) raises(DOMException);
        !           699:        add_native_method("createEntityReference", Method::CT_DYNAMIC, _createEntityReference, 1, 1);
        !           700:        // NodeList getElementsByTagName(in DOMString tagname);
        !           701:        /*      
        !           702:                // ^xdoc.getElementsByTagName[tagname]
        !           703:                add_native_method("getElementsByTagName", Method::CT_DYNAMIC, _getElementsByTagName, 1, 1);
        !           704: 
        !           705:                // ^xdoc.getElementsByTagNameNS[namespaceURI;localName] = array of nodes
        !           706:                add_native_method("getElementsByTagNameNS", Method::CT_DYNAMIC, _getElementsByTagNameNS, 2, 2);
        !           707:        */
1.2       parser    708: 
1.16    ! parser    709:        /// DOM2(?)
1.2       parser    710: 
1.16    ! parser    711:        // ^xdoc.getElementById[elementId]
        !           712:        add_native_method("getElementById", Method::CT_DYNAMIC, _getElementById, 1, 1);
1.1       parser    713: 
1.16    ! parser    714:        /// parser
        !           715:        
1.2       parser    716:        // ^xdoc.save[some.xml]
                    717:        // ^xdoc.save[some.xml;options hash]
1.1       parser    718:        add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2);
                    719: 
1.2       parser    720:        // ^xdoc.string[] <doc/>
                    721:        // ^xdoc.string[options hash] <doc/>
1.1       parser    722:        add_native_method("string", Method::CT_DYNAMIC, _string, 0, 1);
                    723: 
1.2       parser    724:        // ^xdoc.file[] file with "<doc/>"
                    725:        // ^xdoc.file[options hash] file with "<doc/>"
1.1       parser    726:        add_native_method("file", Method::CT_DYNAMIC, _file, 0, 1);
                    727: 
1.2       parser    728:        // ^xdoc::set[<some>xml</some>]
1.1       parser    729:        add_native_method("set", Method::CT_DYNAMIC, _set, 1, 1);
                    730: 
1.2       parser    731:        // ^xdoc::load[some.xml]
1.1       parser    732:        add_native_method("load", Method::CT_DYNAMIC, _load, 1, 1);
                    733: 
1.8       parser    734:        // ^xdoc.transform[stylesheet file_name]
                    735:        // ^xdoc.transform[stylesheet file_name;params hash]
                    736:        add_native_method("transform", Method::CT_DYNAMIC, _transform, 1, 2);
1.2       parser    737: 
1.1       parser    738: }
1.5       parser    739: 
                    740: void MXdoc::configure_admin(Request& r) {
                    741: }
                    742: 
1.1       parser    743: // global variable
                    744: 
                    745: Methoded *Xdoc_class;
                    746: 
                    747: // creator
                    748: 
                    749: #endif
                    750: 
                    751: Methoded *MXdoc_create(Pool& pool) {
                    752:        return 
                    753: #ifdef XML
                    754:                Xdoc_class=new(pool) MXdoc(pool);
                    755: #else
                    756:                0
                    757: #endif
                    758:        ;
                    759: }

E-mail: