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

1.1       parser      1: /** @file
1.2       parser      2:        Parser: @b xdoc parser class.
1.1       parser      3: 
1.131     paf         4:        Copyright (c) 2001-2004 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.136   ! paf        12: static const char * const IDENT_XDOC_C="$Date: 2004/02/17 15:02:15 $";
1.82      paf        13: 
1.69      paf        14: #include "gdome.h"
                     15: #include "libxml/tree.h"
1.133     paf        16: #include "libxml/HTMLtree.h"
1.69      paf        17: #include "libxslt/xsltInternals.h"
                     18: #include "libxslt/transform.h"
                     19: #include "libxslt/xsltutils.h"
1.90      paf        20: #include "libxslt/variables.h"
1.132     paf        21: #include "libxslt/imports.h"
1.111     paf        22: 
                     23: #include "pa_vmethod_frame.h"
                     24: 
                     25: #include "pa_stylesheet_manager.h"
                     26: #include "pa_request.h"
                     27: #include "pa_vxdoc.h"
                     28: #include "pa_charset.h"
                     29: #include "pa_vfile.h"
1.115     paf        30: #include "pa_xml_exception.h"
1.111     paf        31: #include "xnode.h"
                     32: 
1.1       parser     33: // defines
                     34: 
                     35: #define XDOC_CLASS_NAME "xdoc"
                     36: 
                     37: #define XDOC_OUTPUT_METHOD_OPTION_NAME "method"
                     38: #define XDOC_OUTPUT_METHOD_OPTION_VALUE_XML "xml"
                     39: #define XDOC_OUTPUT_METHOD_OPTION_VALUE_HTML "html"
                     40: #define XDOC_OUTPUT_METHOD_OPTION_VALUE_TEXT "text"
                     41: 
                     42: // class
                     43: 
1.111     paf        44: class MXdoc: public MXnode {
1.1       parser     45: public: // VStateless_class
1.118     paf        46:        Value* create_new_value(Pool&) { return new VXdoc(0, 0); }
1.1       parser     47: 
                     48: public:
1.111     paf        49:        MXdoc();
1.1       parser     50: 
                     51: };
                     52: 
1.111     paf        53: // global variable
                     54: 
                     55: DECLARE_CLASS_VAR(xdoc, new MXdoc, 0);
                     56: 
1.61      paf        57: // helper classes
                     58: 
                     59: class xmlOutputBuffer_auto_ptr {
                     60: public:
1.67      paf        61:        explicit xmlOutputBuffer_auto_ptr(xmlOutputBuffer *_APtr = 0) 
                     62:                : _Owns(_APtr != 0), _Ptr(_APtr) {}
1.61      paf        63:        xmlOutputBuffer_auto_ptr(const xmlOutputBuffer_auto_ptr& _Y) 
                     64:                : _Owns(_Y._Owns), _Ptr(_Y.release()) {}
                     65:        xmlOutputBuffer_auto_ptr& operator=(const xmlOutputBuffer_auto_ptr& _Y) 
                     66:                {if (this != &_Y)
                     67:                        {if (_Ptr != _Y.get())
                     68:                                {if (_Owns && _Ptr)
                     69:                                        xmlOutputBufferClose(_Ptr);
                     70:                                _Owns = _Y._Owns; }
                     71:                        else if (_Y._Owns)
                     72:                                _Owns = true;
                     73:                        _Ptr = _Y.release(); }
                     74:                return (*this); }
                     75:        ~xmlOutputBuffer_auto_ptr()
                     76:                {if (_Owns && _Ptr)
                     77:                        xmlOutputBufferClose(_Ptr); }
                     78:        xmlOutputBuffer& operator*() const 
                     79:                {return (*get()); }
                     80:        xmlOutputBuffer *operator->() const 
                     81:                {return (get()); }
                     82:        xmlOutputBuffer *get() const 
                     83:                {return (_Ptr); }
                     84:        xmlOutputBuffer *release() const 
                     85:                {((xmlOutputBuffer_auto_ptr *)this)->_Owns = false;
                     86:                return (_Ptr); }
                     87: private:
                     88:        bool _Owns;
                     89:        xmlOutputBuffer *_Ptr;
                     90: };
                     91: 
1.65      paf        92: class xsltTransformContext_auto_ptr {
                     93: public:
1.67      paf        94:        explicit xsltTransformContext_auto_ptr(xsltTransformContext *_APtr = 0) 
                     95:                : _Owns(_APtr != 0), _Ptr(_APtr) {}
1.65      paf        96:        xsltTransformContext_auto_ptr(const xsltTransformContext_auto_ptr& _Y) 
                     97:                : _Owns(_Y._Owns), _Ptr(_Y.release()) {}
                     98:        xsltTransformContext_auto_ptr& operator=(const xsltTransformContext_auto_ptr& _Y) 
                     99:                {if (this != &_Y)
                    100:                        {if (_Ptr != _Y.get())
                    101:                                {if (_Owns && _Ptr)
                    102:                                        xsltFreeTransformContext(_Ptr);
                    103:                                _Owns = _Y._Owns; }
                    104:                        else if (_Y._Owns)
                    105:                                _Owns = true;
                    106:                        _Ptr = _Y.release(); }
                    107:                return (*this); }
                    108:        ~xsltTransformContext_auto_ptr()
                    109:                {if (_Owns && _Ptr)
                    110:                        xsltFreeTransformContext(_Ptr); }
                    111:        xsltTransformContext& operator*() const 
                    112:                {return (*get()); }
                    113:        xsltTransformContext *operator->() const 
                    114:                {return (get()); }
                    115:        xsltTransformContext *get() const 
                    116:                {return (_Ptr); }
                    117:        xsltTransformContext *release() const 
                    118:                {((xsltTransformContext_auto_ptr *)this)->_Owns = false;
                    119:                return (_Ptr); }
                    120: private:
                    121:        bool _Owns;
                    122:        xsltTransformContext *_Ptr;
                    123: };
1.61      paf       124: 
1.65      paf       125: class xsltStylesheet_auto_ptr {
                    126: public:
1.67      paf       127:        explicit xsltStylesheet_auto_ptr(xsltStylesheet *_APtr = 0) 
                    128:                : _Owns(_APtr != 0), _Ptr(_APtr) {}
1.65      paf       129:        xsltStylesheet_auto_ptr(const xsltStylesheet_auto_ptr& _Y) 
                    130:                : _Owns(_Y._Owns), _Ptr(_Y.release()) {}
                    131:        xsltStylesheet_auto_ptr& operator=(const xsltStylesheet_auto_ptr& _Y) 
                    132:                {if (this != &_Y)
                    133:                        {if (_Ptr != _Y.get())
                    134:                                {if (_Owns && _Ptr)
                    135:                                        xsltFreeStylesheet(_Ptr);
                    136:                                _Owns = _Y._Owns; }
                    137:                        else if (_Y._Owns)
                    138:                                _Owns = true;
                    139:                        _Ptr = _Y.release(); }
                    140:                return (*this); }
                    141:        ~xsltStylesheet_auto_ptr()
                    142:                {if (_Owns && _Ptr)
                    143:                        xsltFreeStylesheet(_Ptr); }
                    144:        xsltStylesheet& operator*() const 
                    145:                {return (*get()); }
                    146:        xsltStylesheet *operator->() const 
                    147:                {return (get()); }
                    148:        xsltStylesheet *get() const 
                    149:                {return (_Ptr); }
                    150:        xsltStylesheet *release() const 
                    151:                {((xsltStylesheet_auto_ptr *)this)->_Owns = false;
                    152:                return (_Ptr); }
                    153: private:
                    154:        bool _Owns;
                    155:        xsltStylesheet *_Ptr;
                    156: };
1.61      paf       157: 
1.1       parser    158: // methods
                    159: 
1.111     paf       160: static void writeNode(Request& r, GdomeNode *node, 
1.54      paf       161:                                          GdomeException exc) {
                    162:        if(!node || exc)
1.115     paf       163:                throw XmlException(0, exc);
1.54      paf       164: 
                    165:        // write out result
1.111     paf       166:        r.write_no_lang(*new VXnode(&r.charsets, node));
1.54      paf       167: }
                    168: 
1.16      parser    169: // Element createElement(in DOMString tagName) raises(DOMException);
1.111     paf       170: static void _createElement(Request& r, MethodParams& params) {
                    171:        VXdoc& vdoc=GET_SELF(r, VXdoc);
1.16      parser    172: 
1.111     paf       173:        const String& tagName=params.as_string(0, "tagName must be string");
1.16      parser    174: 
1.54      paf       175:        GdomeException exc;
                    176:        GdomeNode *node=
1.111     paf       177:                (GdomeNode *)gdome_doc_createElement(vdoc.get_document(), 
                    178:                r.transcode(tagName).use(),
1.54      paf       179:                &exc);
1.111     paf       180:        writeNode(r, node, exc);
1.16      parser    181: }
                    182: 
1.113     paf       183: // Element createElementNS(in DOMString namespaceURI, in DOMString qualifiedName) raises(DOMException);
1.112     paf       184: static void _createElementNS(Request& r, MethodParams& params) {
                    185:        VXdoc& vdoc=GET_SELF(r, VXdoc);
                    186: 
                    187:        // namespaceURI;localName
                    188:        const String& namespaceURI=params.as_string(0, "namespaceURI must be string");
1.113     paf       189:        const String& qualifiedName=params.as_string(1, "qualifiedName must be string");
1.112     paf       190: 
                    191:        GdomeException exc;
                    192:        GdomeNode *node=
                    193:                (GdomeNode *)gdome_doc_createElementNS(vdoc.get_document(), 
                    194:                r.transcode(namespaceURI).use(),
1.113     paf       195:                r.transcode(qualifiedName).use(),
1.112     paf       196:                &exc);
                    197:        writeNode(r, node, exc);
                    198: }
                    199: 
1.16      parser    200: // DocumentFragment createDocumentFragment()
1.111     paf       201: static void _createDocumentFragment(Request& r, MethodParams&) {
                    202:        VXdoc& vdoc=GET_SELF(r, VXdoc);
1.16      parser    203: 
1.54      paf       204:        GdomeException exc;
                    205:        GdomeNode *node=
                    206:                (GdomeNode *)gdome_doc_createDocumentFragment(
1.111     paf       207:                vdoc.get_document(),
1.54      paf       208:                &exc);
1.111     paf       209:        writeNode(r, node, exc);
1.16      parser    210: }
                    211: 
                    212: // Text createTextNode(in DOMString data);
1.111     paf       213: static void _createTextNode(Request& r, MethodParams& params) {
                    214:        VXdoc& vdoc=GET_SELF(r, VXdoc);
1.16      parser    215: 
1.111     paf       216:        const String& data=params.as_string(0, "data must be string");
1.16      parser    217: 
1.54      paf       218:        GdomeException exc;
                    219:        GdomeNode *node=(GdomeNode *)gdome_doc_createTextNode(
1.111     paf       220:                vdoc.get_document(),
                    221:                r.transcode(data).use(),
1.54      paf       222:                &exc);
1.111     paf       223:        writeNode(r, node, exc);
1.16      parser    224: }
                    225: 
                    226: // Comment createComment(in DOMString data)
1.111     paf       227: static void _createComment(Request& r, MethodParams& params) {
                    228:        VXdoc& vdoc=GET_SELF(r, VXdoc);
1.16      parser    229: 
1.111     paf       230:        const String& data=params.as_string(0, "data must be string");
1.16      parser    231: 
1.54      paf       232:        GdomeException exc;
                    233:        GdomeNode *node=(GdomeNode *)gdome_doc_createComment(
1.111     paf       234:                vdoc.get_document(),
                    235:                r.transcode(data).use(),
1.54      paf       236:                &exc);
1.111     paf       237:        writeNode(r, node, exc);
1.16      parser    238: }
                    239: 
                    240: // CDATASection createCDATASection(in DOMString data) raises(DOMException);
1.111     paf       241: static void _createCDATASection(Request& r, MethodParams& params) {
                    242:        VXdoc& vdoc=GET_SELF(r, VXdoc);
1.16      parser    243: 
1.111     paf       244:        const String& data=params.as_string(0, "data must be string");
1.16      parser    245: 
1.54      paf       246:        GdomeException exc;
                    247:        GdomeNode *node=(GdomeNode *)gdome_doc_createCDATASection(
1.111     paf       248:                vdoc.get_document(),
                    249:                r.transcode(data).use(),
1.54      paf       250:                &exc);
1.111     paf       251:        writeNode(r, node, exc);
1.16      parser    252: }
                    253: 
                    254: // ProcessingInstruction createProcessingInstruction(in DOMString target,in DOMString data) raises(DOMException);
1.111     paf       255: static void _createProcessingInstruction(Request& r, MethodParams& params) {
                    256:        VXdoc& vdoc=GET_SELF(r, VXdoc);
1.16      parser    257: 
1.111     paf       258:        const String& target=params.as_string(0, "data must be string");
                    259:        const String& data=params.as_string(1, "data must be string");
1.16      parser    260: 
1.54      paf       261:        GdomeException exc;
                    262:        GdomeNode *node=(GdomeNode *)gdome_doc_createProcessingInstruction(
1.111     paf       263:                vdoc.get_document(),
                    264:                r.transcode(target).use(), 
                    265:                r.transcode(data).use(),
1.54      paf       266:                &exc);
1.111     paf       267:        writeNode(r, node, exc);
1.16      parser    268: }
                    269: 
                    270: // Attr createAttribute(in DOMString name) raises(DOMException);
1.111     paf       271: static void _createAttribute(Request& r, MethodParams& params) {
                    272:        VXdoc& vdoc=GET_SELF(r, VXdoc);
1.16      parser    273: 
1.111     paf       274:        const String& name=params.as_string(0, "name must be string");
1.16      parser    275: 
1.54      paf       276:        GdomeException exc;
                    277:        GdomeNode *node=(GdomeNode *)gdome_doc_createAttribute(
1.111     paf       278:                vdoc.get_document(),
                    279:                r.transcode(name).use(),
1.54      paf       280:                &exc);
1.111     paf       281:        writeNode(r, node, exc);
1.16      parser    282: }
1.112     paf       283: 
1.113     paf       284: // Attr createAttributeNS(in DOMString namespaceURI, in DOMString qualifiedName) raises(DOMException);
1.112     paf       285: static void _createAttributeNS(Request& r, MethodParams& params) {
                    286:        VXdoc& vdoc=GET_SELF(r, VXdoc);
                    287: 
                    288:        // namespaceURI;qualifiedName
                    289:        const String& namespaceURI=params.as_string(0, "namespaceURI must be string");
                    290:        const String& qualifiedName=params.as_string(1, "name must be string");
                    291: 
                    292:        GdomeException exc;
                    293:        GdomeNode *node=(GdomeNode *)gdome_doc_createAttributeNS(
                    294:                vdoc.get_document(),
                    295:                r.transcode(namespaceURI).use(),
                    296:                r.transcode(qualifiedName).use(),
                    297:                &exc);
                    298:        writeNode(r, node, exc);
                    299: }
                    300: 
1.16      parser    301: // EntityReference createEntityReference(in DOMString name) raises(DOMException);
1.111     paf       302: static void _createEntityReference(Request& r, MethodParams& params) {
                    303:        VXdoc& vdoc=GET_SELF(r, VXdoc);
1.16      parser    304: 
1.111     paf       305:        const String& name=params.as_string(0, "name must be string");
1.16      parser    306: 
1.54      paf       307:        GdomeException exc;
                    308:        GdomeNode *node=(GdomeNode *)gdome_doc_createEntityReference(
1.111     paf       309:                vdoc.get_document(),
                    310:                r.transcode(name).use(),
1.54      paf       311:                &exc);
1.111     paf       312:        writeNode(r, node, exc);
1.16      parser    313: }
                    314: 
1.99      paf       315: // NodeList getElementsByTagName(in DOMString name);
1.111     paf       316: static void _getElementsByTagName(Request& r, MethodParams& params) {
                    317:        VXdoc& vdoc=GET_SELF(r, VXdoc);
1.99      paf       318: 
1.111     paf       319:        const String& name=params.as_string(0, "name must be string");
1.99      paf       320: 
1.111     paf       321:        VHash& result=*new VHash;
1.99      paf       322:        GdomeException exc;
                    323:        if(GdomeNodeList *nodes=
                    324:                gdome_doc_getElementsByTagName(
1.111     paf       325:                        vdoc.get_document(), 
                    326:                        r.transcode(name).use(), 
1.99      paf       327:                        &exc)) {
                    328:                gulong length=gdome_nl_length(nodes, &exc);
1.111     paf       329:                for(gulong i=0; i<length; i++)
                    330:                        result.hash().put(
1.114     paf       331:                                String::Body::Format(i), 
1.111     paf       332:                                new VXnode(&r.charsets, gdome_nl_item(nodes, i, &exc)));
1.99      paf       333:        } else if(exc)
1.116     paf       334:                throw XmlException(0, exc);
1.99      paf       335: 
                    336:        // write out result
                    337:        r.write_no_lang(result);
                    338: }
                    339: 
1.111     paf       340: static void _getElementsByTagNameNS(Request& r, MethodParams& params) {
                    341:        VXdoc& vdoc=GET_SELF(r, VXdoc);
1.99      paf       342: 
                    343:        // namespaceURI;localName
1.111     paf       344:        const String& namespaceURI=params.as_string(0, "namespaceURI must be string");
                    345:        const String& localName=params.as_string(1, "localName must be string");
1.99      paf       346: 
                    347:        GdomeException exc;
1.111     paf       348:        VHash& result=*new VHash;
1.99      paf       349:        if(GdomeNodeList *nodes=
                    350:                gdome_doc_getElementsByTagNameNS(
1.111     paf       351:                        vdoc.get_document(), 
                    352:                        r.transcode(namespaceURI).use(),
                    353:                        r.transcode(localName).use(),
1.99      paf       354:                        &exc)) {
                    355:                gulong length=gdome_nl_length(nodes, &exc);
1.111     paf       356:                for(gulong i=0; i<length; i++)
                    357:                        result.hash().put(
1.114     paf       358:                                String::Body::Format(i), 
1.111     paf       359:                                new VXnode(&r.charsets, gdome_nl_item(nodes, i, &exc)));
1.99      paf       360:        }
                    361: 
                    362:        // write out result
                    363:        r.write_no_lang(result);
                    364: }
                    365: 
1.111     paf       366: static void _getElementById(Request& r, MethodParams& params) {
                    367:        VXdoc& vdoc=GET_SELF(r, VXdoc);
1.16      parser    368: 
1.54      paf       369:        // elementId
1.111     paf       370:        const String& elementId=params.as_string(0, "elementID must be string");
1.1       parser    371: 
1.54      paf       372:        GdomeException exc;
                    373:        if(GdomeNode *node=(GdomeNode *)gdome_doc_getElementById(
1.111     paf       374:                vdoc.get_document(),
                    375:                r.transcode(elementId).use(),
1.54      paf       376:                &exc)) {
                    377:                // write out result
1.111     paf       378:                r.write_no_lang(*new VXnode(&r.charsets, node));
1.66      paf       379:        } else if(exc || xmlHaveGenericErrors())
1.116     paf       380:                throw XmlException(&elementId, exc);
1.54      paf       381: }
1.79      paf       382: 
1.111     paf       383: static void _importNode(Request& r, MethodParams& params) {
                    384:        VXdoc& vdoc=GET_SELF(r, VXdoc);
1.79      paf       385: 
                    386:        GdomeNode *importedNode=
1.111     paf       387:                as_node(params, 0, "importedNode must be node");
1.79      paf       388:        bool deep=
1.111     paf       389:                params.as_bool(1, "deep must be bool", r);
1.79      paf       390: 
                    391:        GdomeException exc;
1.111     paf       392:        GdomeNode *outputNode=gdome_doc_importNode(vdoc.get_document(), 
1.79      paf       393:                importedNode,
                    394:                deep, &exc);
                    395:        if(exc)
1.116     paf       396:                throw XmlException(0, exc);
1.79      paf       397: 
                    398:        // write out result
1.111     paf       399:        r.write_no_lang(*new VXnode(&r.charsets, outputNode));
1.79      paf       400: }
1.54      paf       401: /*
                    402: GdomeElement *gdome_doc_createElementNS (GdomeDocument *self, GdomeDOMString *namespaceURI, GdomeDOMString *qualifiedName, GdomeException *exc);
                    403: GdomeAttr *gdome_doc_createAttributeNS (GdomeDocument *self, GdomeDOMString *namespaceURI, GdomeDOMString *qualifiedName, GdomeException *exc);
                    404: */
1.1       parser    405: 
1.111     paf       406: static void _create(Request& r, MethodParams& params) {
                    407:        Charset& source_charset=r.charsets.source();
                    408:        VXdoc& vdoc=GET_SELF(r, VXdoc);
1.1       parser    409: 
1.111     paf       410:        Value& param=params[params.count()-1];
1.83      paf       411:        GdomeDocument *document;
1.111     paf       412:        bool set_encoding=false;
1.95      paf       413:        if(param.get_junction()) { // {<?xml?>...}
1.111     paf       414:                Temp_lang temp_lang(r, String::L_XML);
1.91      paf       415:                const String& xml=r.process_to_string(param);
1.83      paf       416: 
1.127     paf       417:                const char* cstr=xml.cstr(String::L_UNSPECIFIED, 0, &r.charsets);
1.83      paf       418:                document=(GdomeDocument *)
                    419:                        gdome_xml_n_mkref((xmlNode *)xmlParseMemory(
                    420:                                cstr, strlen(cstr)
                    421:                        ));
1.111     paf       422:                //printf("document=0x%p\n", document);
1.83      paf       423:                if(!document || xmlHaveGenericErrors()) {
                    424:                        GdomeException exc=0;
1.116     paf       425:                        throw XmlException(0, exc);
1.83      paf       426:                }
1.111     paf       427: 
                    428:                // must be last action in if, see after if}
1.83      paf       429:        } else { // [name]
                    430:                const String& qualifiedName=param.as_string();
1.1       parser    431: 
1.83      paf       432:                GdomeException exc;
1.120     paf       433: #if 0
1.83      paf       434:                GdomeDocumentType *documentType=gdome_di_createDocumentType (
                    435:                        docimpl, 
1.111     paf       436:                        r.transcode(qualifiedName), 
1.120     paf       437:                        0/*publicId*/, 
                    438:                        0/*systemId*/, 
1.83      paf       439:                        &exc);
                    440:                if(!documentType || exc || xmlHaveGenericErrors())
1.87      paf       441:                        throw Exception(
1.111     paf       442:                                method_name, 
1.83      paf       443:                                exc);
1.111     paf       444:                /// +xalan createXMLDecl ?
1.120     paf       445: #endif
                    446:                document=gdome_di_createDocument(domimpl, 
1.83      paf       447:                        0/*namespaceURI*/, 
1.111     paf       448:                        r.transcode(qualifiedName).use(), 
1.83      paf       449:                        0/*doctype*/, 
                    450:                        &exc);
                    451:                if(!document || exc || xmlHaveGenericErrors())
1.116     paf       452:                        throw XmlException(0, exc);
1.103     paf       453: 
1.111     paf       454:                set_encoding=true;
                    455:                // must be last action in if, see after if}
                    456:        }
                    457:        // must be first action after if}
                    458:        // replace any previous parsed source
                    459:        {
                    460:                vdoc.set_document(&r.charsets, document); 
                    461:                GdomeException exc;
                    462:                gdome_doc_unref(document, &exc);
1.73      paf       463:        }
1.95      paf       464:        
                    465:        // URI 
1.111     paf       466:        const char* URI_cstr;
                    467:        const char* URI_cstr_ptr;
                    468:        if(params.count()>1) { // absolute(param)
                    469:                const String& URI=params.as_string(0, "URI must be string");
                    470:                URI_cstr=URI_cstr_ptr=r.absolute(URI).cstr();
1.95      paf       471:        } else // default = disk path to requested document
1.111     paf       472:                URI_cstr=r.request_info.path_translated;
1.95      paf       473:        xmlDoc *doc=gdome_xml_doc_get_xmlDoc(document);
                    474:        if(URI_cstr)
1.111     paf       475:                doc->URL=source_charset.transcode_buf2xchar(URI_cstr, strlen(URI_cstr));
1.95      paf       476: 
1.111     paf       477:        if(set_encoding) {
                    478:                const char* source_charset_name=source_charset.NAME().cstr();
                    479:                doc->encoding=source_charset.transcode_buf2xchar(source_charset_name, strlen(source_charset_name));
                    480:        }
1.9       parser    481: }
                    482: 
1.111     paf       483: static void _load(Request& r, MethodParams& params) {
                    484:        VXdoc& vdoc=GET_SELF(r, VXdoc);
1.9       parser    485: 
1.54      paf       486:        // filespec
1.122     paf       487:        const String* uri=&params.as_string(0, "uri must be string");
                    488:        const char* uri_cstr;
                    489:        if(uri->pos("://")==STRING_NOT_FOUND) // disk path
                    490:                uri_cstr=r.absolute(*uri).cstr(String::L_FILE_SPEC);
                    491:        else // xxx:// 
                    492:                uri_cstr=uri->cstr(String::L_AS_IS); // leave as-is for xmlParseFile to handle
1.105     paf       493: 
1.122     paf       494:        /// todo!! add SAFE MODE!!
1.73      paf       495:        GdomeDocument *document=(GdomeDocument *)
1.123     paf       496:                gdome_xml_n_mkref((xmlNode *)xmlParseFile(uri_cstr));
1.73      paf       497:        if(!document || xmlHaveGenericErrors()) {
                    498:                GdomeException exc=0;
1.122     paf       499:                throw XmlException(uri, exc);
1.111     paf       500:        }
                    501:        // must be first action after if}
                    502:        // replace any previous parsed source
                    503:        {
                    504:                vdoc.set_document(&r.charsets, document); 
                    505:                GdomeException exc;
                    506:                gdome_doc_unref(document, &exc);
1.73      paf       507:        }
1.122     paf       508: /* xmlParseFile does that itself. old peace for xmlParseMemory
                    509:        const char* URI_cstr=uri->cstr();
1.107     paf       510:        xmlDoc *doc=gdome_xml_doc_get_xmlDoc(document);
                    511:        if(URI_cstr)
1.111     paf       512:                doc->URL=r.charsets.source().transcode_buf2xchar(URI_cstr, strlen(URI_cstr));
1.122     paf       513: */
1.9       parser    514: }
                    515: 
1.111     paf       516: static void param_option_over_output_option(
                    517:                                            HashStringValue& param_options, const char* option_name,
                    518:                                            const String*& output_option) {
1.114     paf       519:        if(Value* value=param_options.get(String::Body(option_name)))
1.61      paf       520:                output_option=&value->as_string();
1.46      paf       521: }
1.111     paf       522: static void param_option_over_output_option(
                    523:                                            HashStringValue& param_options, const char* option_name,
                    524:                                            bool& output_option) {
1.114     paf       525:        if(Value* value=param_options.get(String::Body(option_name))) {
1.46      paf       526:                const String& s=value->as_string();
                    527:                if(s=="yes")
                    528:                        output_option=true;
                    529:                else if(s=="no")
                    530:                        output_option=false;
                    531:                else
1.87      paf       532:                        throw Exception("parser.runtime",
1.46      paf       533:                                &s,
                    534:                                "%s must be either 'yes' or 'no'", option_name);
                    535:        }
                    536: }
                    537: 
1.100     paf       538: /// @test valid_options check
1.111     paf       539: static void prepare_output_options(Request& r,
                    540:                                   MethodParams& params, size_t index,
                    541:                                   VXdoc::Output_options& oo) {
1.46      paf       542: /*
                    543: <xsl:output
                    544:   !method = "xml" | "html" | "text" | qname-but-not-ncname 
                    545:   !version = nmtoken 
                    546:   !encoding = string 
                    547:   !omit-xml-declaration = "yes" | "no"
                    548:   !standalone = "yes" | "no"
                    549:   !doctype-public = string 
                    550:   !doctype-system = string 
                    551:   cdata-section-elements = qnames 
                    552:   !indent = "yes" | "no"
                    553:   !media-type = string /> 
1.61      paf       554: */
1.1       parser    555: 
1.46      paf       556:        // configuring with options from parameter...
1.111     paf       557:        if(params.count()>index) {
                    558:                Value& voptions=params.as_no_junction(index, "options must be string");
1.1       parser    559:                if(voptions.is_defined()) {
1.111     paf       560:                        if(HashStringValue *options=voptions.get_hash()) {
1.1       parser    561:                                // $.method[xml|html|text]
1.114     paf       562:                                if(Value* vmethod=options->get(String::Body(XDOC_OUTPUT_METHOD_OPTION_NAME)))
1.61      paf       563:                                        oo.method=&vmethod->as_string();
1.1       parser    564: 
1.46      paf       565:                                // $.version[1.0]
1.111     paf       566:                                param_option_over_output_option(*options, "version", oo.version);
1.1       parser    567:                                // $.encoding[windows-1251|...]
1.111     paf       568:                                param_option_over_output_option(*options, "encoding", oo.encoding);
1.46      paf       569:                                // $.omit-xml-declaration[yes|no]
1.111     paf       570:                                param_option_over_output_option(*options, "omit-xml-declaration", oo.omitXmlDeclaration);
1.46      paf       571:                                // $.standalone[yes|no]
1.111     paf       572:                                param_option_over_output_option(*options, "standalone", oo.standalone);
1.46      paf       573:                                // $.indent[yes|no]
1.111     paf       574:                                param_option_over_output_option(*options, "indent", oo.indent);
1.46      paf       575:                                // $.media-type[text/{html|xml|plain}]
1.111     paf       576:                                param_option_over_output_option(*options, "media-type", oo.mediaType);                           
1.19      parser    577:                        }
1.1       parser    578:                }
                    579:        }
                    580: 
1.46      paf       581:        // default encoding from pool
1.61      paf       582:        if(!oo.encoding)
1.111     paf       583:                oo.encoding=new String(r.charsets.source().NAME(), String::L_TAINTED);
1.46      paf       584:        // default method=xml
                    585:        if(!oo.method)
1.111     paf       586:                oo.method=new String(XDOC_OUTPUT_METHOD_OPTION_VALUE_XML);
1.61      paf       587:        // default mediaType = depending on method
                    588:        if(!oo.mediaType) {
                    589:                if(*oo.method==XDOC_OUTPUT_METHOD_OPTION_VALUE_XML)
1.111     paf       590:                        oo.mediaType=new String("text/xml");
1.61      paf       591:                else if(*oo.method==XDOC_OUTPUT_METHOD_OPTION_VALUE_HTML)
1.111     paf       592:                        oo.mediaType=new String("text/html");
1.61      paf       593:                else // XDOC_OUTPUT_METHOD_OPTION_VALUE_TEXT & all others
1.111     paf       594:                        oo.mediaType=new String("text/plain");
1.61      paf       595:        }
                    596: }
1.46      paf       597: 
1.133     paf       598: /// patching piece from libxslt not to set meta encoding
1.134     paf       599: static void
                    600: pa_xsltSaveResultToMem(        
                    601:                                           xmlChar*& doc_txt_ptr,       int& doc_txt_len,
                    602:                                           xmlDocPtr result,
                    603:                                           xsltStylesheetPtr style,
                    604:                                           xmlCharEncodingHandler* encoder) 
                    605: {
1.133     paf       606:     const xmlChar *encoding;
                    607:     int base;
                    608:     const xmlChar *method;
                    609:     int indent;
1.134     paf       610:        xmlOutputBufferPtr buf = 0;
1.133     paf       611: 
1.134     paf       612:     if ((result == NULL) || (style == NULL))
                    613:        return;
1.133     paf       614:     if ((result->children == NULL) ||
                    615:        ((result->children->type == XML_DTD_NODE) &&
                    616:         (result->children->next == NULL)))
1.134     paf       617:        return;
1.133     paf       618: 
                    619:     if ((style->methodURI != NULL) &&
                    620:        ((style->method == NULL) ||
                    621:         (!xmlStrEqual(style->method, (const xmlChar *) "xhtml")))) {
                    622:         xsltGenericError(xsltGenericErrorContext,
                    623:                "xsltSaveResultTo : unknown ouput method\n");
1.134     paf       624:         return;
1.133     paf       625:     }
                    626: 
                    627:     XSLT_GET_IMPORT_PTR(method, style, method)
                    628:     XSLT_GET_IMPORT_PTR(encoding, style, encoding)
                    629:     XSLT_GET_IMPORT_INT(indent, style, indent);
                    630: 
                    631:     if ((method == NULL) && (result->type == XML_HTML_DOCUMENT_NODE))
                    632:        method = (const xmlChar *) "html";
                    633: 
                    634:     if ((method != NULL) &&
1.134     paf       635:        (xmlStrEqual(method, (const xmlChar *) "html")
                    636:        ||xmlStrEqual(method, (const xmlChar *) "xhtml"))) {
1.133     paf       637:        if (indent == -1)
                    638:            indent = 1;
1.136   ! paf       639:        //
        !           640:        // * xmlDocDumpFormatMemoryEnc:
        !           641:        // Note it is up to the caller of this function to free the
        !           642:        // allocated memory with xmlFree()
        !           643:        // 
        !           644:        // we wont free anything, and wont copy that data anymore [already done inside and zeroterminated]
1.134     paf       645:        xmlDocDumpFormatMemoryEnc(result, &doc_txt_ptr, &doc_txt_len, (const char *) encoding,
1.133     paf       646:                                       indent);
                    647:     } else if ((method != NULL) &&
                    648:               (xmlStrEqual(method, (const xmlChar *) "text"))) {
                    649:        xmlNodePtr cur;
1.134     paf       650:        buf = xmlAllocOutputBuffer(encoder);
1.133     paf       651: 
                    652:        cur = result->children;
                    653:        while (cur != NULL) {
                    654:            if (cur->type == XML_TEXT_NODE)
                    655:                xmlOutputBufferWriteString(buf, (const char *) cur->content);
                    656: 
                    657:            /*
                    658:             * Skip to next node
                    659:             */
                    660:            if (cur->children != NULL) {
                    661:                if ((cur->children->type != XML_ENTITY_DECL) &&
                    662:                    (cur->children->type != XML_ENTITY_REF_NODE) &&
                    663:                    (cur->children->type != XML_ENTITY_NODE)) {
                    664:                    cur = cur->children;
                    665:                    continue;
                    666:                }
                    667:            }
                    668:            if (cur->next != NULL) {
                    669:                cur = cur->next;
                    670:                continue;
                    671:            }
                    672:            
                    673:            do {
                    674:                cur = cur->parent;
                    675:                if (cur == NULL)
                    676:                    break;
                    677:                if (cur == (xmlNodePtr) style->doc) {
                    678:                    cur = NULL;
                    679:                    break;
                    680:                }
                    681:                if (cur->next != NULL) {
                    682:                    cur = cur->next;
                    683:                    break;
                    684:                }
                    685:            } while (cur != NULL);
                    686:        }
                    687:     } else {
                    688:        int omitXmlDecl;
                    689:        int standalone;
1.134     paf       690:        buf = xmlAllocOutputBuffer(encoder);
1.133     paf       691: 
                    692:        XSLT_GET_IMPORT_INT(omitXmlDecl, style, omitXmlDeclaration);
                    693:        XSLT_GET_IMPORT_INT(standalone, style, standalone);
                    694: 
                    695:        if (omitXmlDecl != 1) {
                    696:            xmlOutputBufferWriteString(buf, "<?xml version=");
                    697:            if (result->version != NULL) 
                    698:                xmlBufferWriteQuotedString(buf->buffer, result->version);
                    699:            else
                    700:                xmlOutputBufferWriteString(buf, "\"1.0\"");
                    701:            if (encoding == NULL) {
                    702:                if (result->encoding != NULL)
                    703:                    encoding = result->encoding;
                    704:                else if (result->charset != XML_CHAR_ENCODING_UTF8)
                    705:                    encoding = (const xmlChar *)
                    706:                               xmlGetCharEncodingName((xmlCharEncoding)
                    707:                                                      result->charset);
                    708:            }
                    709:            if (encoding != NULL) {
                    710:                xmlOutputBufferWriteString(buf, " encoding=");
                    711:                xmlBufferWriteQuotedString(buf->buffer, (xmlChar *) encoding);
                    712:            }
                    713:            switch (standalone) {
                    714:                case 0:
                    715:                    xmlOutputBufferWriteString(buf, " standalone=\"no\"");
                    716:                    break;
                    717:                case 1:
                    718:                    xmlOutputBufferWriteString(buf, " standalone=\"yes\"");
                    719:                    break;
                    720:                default:
                    721:                    break;
                    722:            }
                    723:            xmlOutputBufferWriteString(buf, "?>\n");
                    724:        }
                    725:        if (result->children != NULL) {
                    726:            xmlNodePtr child = result->children;
                    727: 
                    728:            while (child != NULL) {
                    729:                xmlNodeDumpOutput(buf, result, child, 0, (indent == 1),
                    730:                                  (const char *) encoding);
                    731:                if (child->type == XML_DTD_NODE)
                    732:                    xmlOutputBufferWriteString(buf, "\n");
                    733:                child = child->next;
                    734:            }
                    735:            xmlOutputBufferWriteString(buf, "\n");
                    736:        }
                    737:     }
1.134     paf       738: 
                    739:        if(buf) {
                    740:                xmlOutputBufferFlush(buf);
                    741:                if(buf->conv) {
                    742:                        doc_txt_len=buf->conv->use;
                    743:                        doc_txt_ptr=buf->conv->content;
                    744:                } else {
                    745:                        doc_txt_len=buf->buffer->use;
                    746:                        doc_txt_ptr=buf->buffer->content;
                    747:                }
                    748: 
1.136   ! paf       749:                if(doc_txt_ptr && doc_txt_len)
        !           750:                        doc_txt_ptr=BAD_CAST pa_strdup((const char*)doc_txt_ptr, doc_txt_len);
1.134     paf       751: 
                    752:                xmlOutputBufferClose(buf);
1.136   ! paf       753:        }
1.133     paf       754: }
                    755: 
1.111     paf       756: struct Xdoc2buf_result {
                    757:        char* str;
                    758:        size_t length;
                    759: };
                    760: static Xdoc2buf_result xdoc2buf(Request& r, VXdoc& vdoc, 
                    761:                                         MethodParams& params, int index,
1.63      paf       762:                                         VXdoc::Output_options& oo,
1.111     paf       763:                                         const String* file_spec) {
                    764:        Xdoc2buf_result result;
                    765:        prepare_output_options(r, params, index,
1.61      paf       766:                oo);
                    767: 
1.111     paf       768:        const char* encoding_cstr=oo.encoding->cstr();
1.63      paf       769:        xmlCharEncodingHandler *encoder=xmlFindCharEncodingHandler(encoding_cstr);
                    770:        if(!encoder)
1.87      paf       771:                throw Exception("parser.runtime",
1.111     paf       772:                        0,
1.63      paf       773:                        "encoding '%s' not supported", encoding_cstr);
1.61      paf       774:        // UTF-8 encoder contains empty input/output converters, 
                    775:        // which is wrong for xmlOutputBufferCreateIO
                    776:        // while zero encoder goes perfectly 
1.130     paf       777:        const char* encoder_name=encoder->name;
                    778:        if(strcmp(encoder_name, "UTF-8")==0)
1.61      paf       779:                encoder=0;
                    780: 
1.65      paf       781:        xsltStylesheet_auto_ptr stylesheet(xsltNewStylesheet());
                    782:        if(!stylesheet.get())
1.87      paf       783:                throw Exception(0,
1.111     paf       784:                        0,
1.61      paf       785:                        "xsltNewStylesheet failed");
                    786: 
1.62      paf       787:        #define OOS2STYLE(name) \
1.111     paf       788:                stylesheet->name=oo.name?BAD_CAST xmlMemStrdup(r.transcode(*oo.name)->str):0
1.62      paf       789:        #define OOE2STYLE(name) \
                    790:                stylesheet->name=oo.name
1.61      paf       791: 
1.62      paf       792:        OOS2STYLE(method);
                    793:        OOS2STYLE(encoding);
                    794:        OOS2STYLE(mediaType);
1.124     paf       795: //     OOS2STYLE(doctypeSystem);
1.130     paf       796: //     OOS2STYLE(doctypePublic);
1.62      paf       797:        OOE2STYLE(indent);
                    798:        OOS2STYLE(version);
                    799:        OOE2STYLE(standalone);
                    800:        OOE2STYLE(omitXmlDeclaration);
1.61      paf       801: 
1.111     paf       802:        xmlDoc *document=gdome_xml_doc_get_xmlDoc(vdoc.get_document());
1.130     paf       803:        document->encoding=BAD_CAST xmlMemStrdup(encoder_name);
1.134     paf       804: 
1.135     paf       805:        xmlChar* doc_txt_ptr=0;
                    806:        int doc_txt_len=0;
1.134     paf       807:        pa_xsltSaveResultToMem(doc_txt_ptr, doc_txt_len, document, stylesheet.get(), encoder);
                    808:        if(xmlHaveGenericErrors()) {
1.75      paf       809:                GdomeException exc=0;
1.116     paf       810:                throw XmlException(0, exc);
1.75      paf       811:        }
1.61      paf       812: 
1.134     paf       813:        result.length=doc_txt_len;
                    814:        result.str=(char*)doc_txt_ptr;
1.118     paf       815: 
1.63      paf       816:        if(file_spec)
1.111     paf       817:                file_write(*file_spec,
1.134     paf       818:                result.str, result.length, 
1.111     paf       819:                        true/*as_text*/);
                    820: 
                    821:        return result;
1.1       parser    822: }
                    823: 
1.111     paf       824: static void _file(Request& r, MethodParams& params) {
                    825:        VXdoc& vdoc=GET_SELF(r, VXdoc);
1.63      paf       826:        VXdoc::Output_options oo(vdoc.output_options);
1.111     paf       827:        Xdoc2buf_result buf=xdoc2buf(r, vdoc, params, 0, 
1.63      paf       828:                oo,
1.111     paf       829:                0/*not to file, to memory*/);
1.63      paf       830:        // write out result
1.111     paf       831:        r.write_no_lang(String(buf.str, buf.length));
1.1       parser    832: 
1.58      paf       833:        // write out result
1.111     paf       834:        VFile& vfile=*new VFile;
                    835:        VHash& vhcontent_type=*new VHash;
                    836:        vhcontent_type.hash().put(
                    837:                value_name, 
                    838:                new VString(*oo.mediaType));
                    839:        vhcontent_type.hash().put(
1.126     paf       840:                String::Body("charset"), 
1.111     paf       841:                new VString(*oo.encoding));
                    842: 
                    843:        vfile.set(false/*tainted*/, buf.str?buf.str:""/*to distinguish from stat-ed file*/, buf.length, 
                    844:                0/*file_name*/, &vhcontent_type);
1.58      paf       845:        r.write_no_lang(vfile);
1.63      paf       846: }
                    847: 
1.111     paf       848: static void _save(Request& r, MethodParams& params) {
                    849:        VXdoc& vdoc=GET_SELF(r, VXdoc);
1.63      paf       850: 
1.111     paf       851:        const String& file_spec=r.absolute(params.as_string(0, "file name must be string"));
1.63      paf       852:        
                    853:        VXdoc::Output_options oo(vdoc.output_options);
1.111     paf       854:        xdoc2buf(r, vdoc, params, 1, 
1.63      paf       855:                oo,
1.111     paf       856:                &file_spec);
1.63      paf       857: }
                    858: 
1.111     paf       859: static void _string(Request& r, MethodParams& params) {
                    860:        VXdoc& vdoc=GET_SELF(r, VXdoc);
1.63      paf       861:        VXdoc::Output_options oo(vdoc.output_options);
1.111     paf       862:        Xdoc2buf_result buf=xdoc2buf(r, vdoc, params, 0, 
1.63      paf       863:                oo,
1.111     paf       864:                0/*not to file, to memory*/);
1.63      paf       865:        // write out result
1.111     paf       866:        r.write_no_lang(String(buf.str, buf.length));
1.1       parser    867: }
1.58      paf       868: 
1.111     paf       869: #ifndef DOXYGEN
                    870: struct Add_xslt_param_info {
                    871:        Request* r;
                    872:        Array<GdomeDOMString_auto_ptr>* strings;
                    873:        const char** current_transform_param;
                    874: };
                    875: #endif
                    876: static void add_xslt_param(
                    877:                           HashStringValue::key_type attribute, 
                    878:                           HashStringValue::value_type meaning, 
                    879:                           Add_xslt_param_info* info) {
                    880:        GdomeDOMString_auto_ptr s;
                    881:        *info->current_transform_param++=(s=info->r->transcode(attribute))->str; *info->strings+=s;
                    882:        *info->current_transform_param++=(s=info->r->transcode(meaning->as_string()))->str; *info->strings+=s;
                    883: }
1.132     paf       884: 
1.111     paf       885: static VXdoc& _transform(Request& r, const String* stylesheet_source, 
1.132     paf       886:                                                   VXdoc& vdoc, xsltStylesheetPtr stylesheet, const char** transform_params) 
                    887: {
1.111     paf       888:        xmlDoc *document=gdome_xml_doc_get_xmlDoc(vdoc.get_document());
1.65      paf       889:        xsltTransformContext_auto_ptr transformContext(
                    890:                xsltNewTransformContext(stylesheet, document));
1.90      paf       891:        // make params literal
1.111     paf       892:        if (transformContext->globalVars == NULL) // strangly not initialized by xsltNewTransformContext
1.90      paf       893:                transformContext->globalVars = xmlHashCreate(20);
                    894:        xsltQuoteUserParams(transformContext.get(), transform_params);
                    895:        // do transform
1.59      paf       896:        xmlDoc *transformed=xsltApplyStylesheetUser(
                    897:                stylesheet,
                    898:                document,
1.90      paf       899:                0/*already quoted-inserted  transform_params*/,
1.111     paf       900:                0/*const char* output*/,
1.59      paf       901:                0/*FILE *profile*/,
1.65      paf       902:                transformContext.get());
1.66      paf       903:        if(!transformed || xmlHaveGenericErrors()) {
                    904:                GdomeException exc=0;
1.116     paf       905:                throw XmlException(stylesheet_source, exc);
1.66      paf       906:        }
1.42      paf       907: 
1.61      paf       908:        //gdome_xml_doc_mkref dislikes XML_HTML_DOCUMENT_NODE  type, fixing
                    909:        transformed->type=XML_DOCUMENT_NODE;
1.59      paf       910:        // constructing result
                    911:        GdomeDocument *gdomeDocument=gdome_xml_doc_mkref(transformed);
                    912:        if(!gdomeDocument)
1.87      paf       913:                throw Exception(0,
1.111     paf       914:                        0,
1.59      paf       915:                        "gdome_xml_doc_mkref failed");
1.111     paf       916:        VXdoc& result=*new VXdoc(&r.charsets, gdomeDocument);
1.61      paf       917:        /* grabbing options
                    918: 
                    919:                <xsl:output
                    920:                !method = "xml" | "html" | "text"
                    921:                        X| qname-but-not-ncname 
                    922:                !version = nmtoken 
                    923:                !encoding = string 
                    924:                !omit-xml-declaration = "yes" | "no"
                    925:                !standalone = "yes" | "no"
                    926:                !doctype-public = string 
                    927:                !doctype-system = string 
                    928:                Xcdata-section-elements = qnames 
                    929:                !indent = "yes" | "no"
                    930:                !media-type = string /> 
1.59      paf       931:        */
                    932:        VXdoc::Output_options& oo=result.output_options;
                    933: 
1.111     paf       934:        oo.method=stylesheet->method?&r.transcode(stylesheet->method):0;
                    935:        oo.encoding=stylesheet->encoding?&r.transcode(stylesheet->encoding):0;
                    936:        oo.mediaType=stylesheet->mediaType?&r.transcode(stylesheet->mediaType):0;
1.59      paf       937:        oo.indent=stylesheet->indent!=0;
1.111     paf       938:        oo.version=stylesheet->version?&r.transcode(stylesheet->version):0;
1.59      paf       939:        oo.standalone=stylesheet->standalone!=0;
                    940:        oo.omitXmlDeclaration=stylesheet->omitXmlDeclaration!=0;
                    941: 
1.93      paf       942:        // return
                    943:        return result;
                    944: }
1.111     paf       945: static void _transform(Request& r, MethodParams& params) {
                    946:        VXdoc& vdoc=GET_SELF(r, VXdoc);
1.93      paf       947: 
                    948:        // params
1.111     paf       949:        Array<GdomeDOMString_auto_ptr> transform_strings;
                    950:        const char** transform_params=0;
                    951:        if(params.count()>1) {
                    952:                Value& vparams=params.as_no_junction(1, "transform parameters must be hash");
1.106     paf       953:                if(!vparams.is_string())
1.111     paf       954:                        if(HashStringValue* hash=vparams.get_hash()) {
                    955:                                transform_params=new(UseGC) const char*[hash->count()*2+1];
                    956:                                Add_xslt_param_info info={
                    957:                                        &r, 
                    958:                                        &transform_strings,
                    959:                                        transform_params
                    960:                                };
                    961:                                hash->for_each(add_xslt_param, &info);
                    962:                                transform_params[hash->count()*2]=0;                            
1.93      paf       963:                        } else
                    964:                                throw Exception("parser.runtime",
1.111     paf       965:                                        0,
1.93      paf       966:                                        "transform parameters parameter must be hash");
                    967:        }
                    968: 
1.111     paf       969:        VXdoc* result;
                    970:        if(Value *vxdoc=params[0].as(VXDOC_TYPE, false)) { // stylesheet (xdoc)
1.93      paf       971:                xmlDoc *document=gdome_xml_doc_get_xmlDoc(
1.111     paf       972:                        static_cast<VXdoc *>(vxdoc)->get_document());
1.93      paf       973:                // compile xdoc stylesheet
1.94      paf       974:                xsltStylesheet_auto_ptr stylesheet_ptr(xsltParseStylesheetDoc(document)); 
1.93      paf       975:                if(xmlHaveGenericErrors()) {
                    976:                        GdomeException exc=0;
1.116     paf       977:                        throw XmlException(0, exc);
1.93      paf       978:                }
                    979:                if(!stylesheet_ptr.get())
                    980:                        throw Exception("xml",
1.111     paf       981:                                0,
1.93      paf       982:                                "stylesheet failed to compile");
1.128     paf       983:                // strange thing - xsltParseStylesheetDoc records document and destroys it in stylesheet destructor
                    984:                // we don't need that
                    985:                stylesheet_ptr->doc=0;
1.93      paf       986: 
                    987:                // transform!
1.111     paf       988:                result=&_transform(r, 0,
1.93      paf       989:                        vdoc, stylesheet_ptr.get(),
                    990:                        transform_params);
                    991:        } else { // stylesheet (file name)
                    992:                // extablish stylesheet connection
                    993:                const String& stylesheet_filespec=
1.111     paf       994:                        r.absolute(params.as_string(0, "stylesheet must be file name (string) or DOM document (xdoc)"));
1.125     paf       995:                Stylesheet_connection_ptr connection=stylesheet_manager->get_connection(stylesheet_filespec);
1.93      paf       996: 
                    997:                // load and compile file to stylesheet [or get cached if any]
                    998:                // transform!
1.122     paf       999:                result=&_transform(r, &stylesheet_filespec, vdoc, connection->stylesheet(),
1.93      paf      1000:                        transform_params);
                   1001:        }
                   1002: 
1.59      paf      1003:        // write out result
1.93      paf      1004:        r.write_no_lang(*result);
1.1       parser   1005: }
                   1006: 
1.16      parser   1007: // constructor
1.2       parser   1008: 
1.92      paf      1009: /// @test how to create empty type html?
1.111     paf      1010: MXdoc::MXdoc(): MXnode(XDOC_CLASS_NAME, xnode_class) {
1.16      parser   1011:        /// DOM1
1.2       parser   1012: 
1.16      parser   1013:        // Element createElement(in DOMString tagName) raises(DOMException);
                   1014:        add_native_method("createElement", Method::CT_DYNAMIC, _createElement, 1, 1);
                   1015:        // DocumentFragment createDocumentFragment(); 
                   1016:        add_native_method("createDocumentFragment", Method::CT_DYNAMIC, _createDocumentFragment, 0, 0);
                   1017:        // Text createTextNode(in DOMString data);
                   1018:        add_native_method("createTextNode", Method::CT_DYNAMIC, _createTextNode, 1, 1);
                   1019:        // Comment createComment(in DOMString data);
                   1020:        add_native_method("createComment", Method::CT_DYNAMIC, _createComment, 1, 1);
                   1021:        // CDATASection createCDATASection(in DOMString data) raises(DOMException);
                   1022:        add_native_method("createCDATASection", Method::CT_DYNAMIC, _createCDATASection, 1, 1);
                   1023:        // ProcessingInstruction createProcessingInstruction(in DOMString target, in DOMString data) raises(DOMException);
                   1024:        add_native_method("createProcessingInstruction", Method::CT_DYNAMIC, _createProcessingInstruction, 2, 2);
                   1025:        // Attr createAttribute(in DOMString name) raises(DOMException);
                   1026:        add_native_method("createAttribute", Method::CT_DYNAMIC, _createAttribute, 1, 1);
                   1027:        // EntityReference createEntityReference(in DOMString name) raises(DOMException);
                   1028:        add_native_method("createEntityReference", Method::CT_DYNAMIC, _createEntityReference, 1, 1);
1.99      paf      1029:        // NodeList getElementsByTagName(in DOMString name);
                   1030:        add_native_method("getElementsByTagName", Method::CT_DYNAMIC, _getElementsByTagName, 1, 1);
1.2       parser   1031: 
1.79      paf      1032:        /// DOM2
1.2       parser   1033: 
1.16      parser   1034:        // ^xdoc.getElementById[elementId]
                   1035:        add_native_method("getElementById", Method::CT_DYNAMIC, _getElementById, 1, 1);
1.79      paf      1036: 
                   1037:     // Node (in Node importedNode, in boolean deep) raises(DOMException)
                   1038:        add_native_method("importNode", Method::CT_DYNAMIC, _importNode, 2, 2);
1.112     paf      1039: 
1.113     paf      1040:        // Attr createAttributeNS(in DOMString namespaceURI, in DOMString qualifiedName) raises(DOMException);
1.112     paf      1041:        add_native_method("createAttributeNS", Method::CT_DYNAMIC, _createAttributeNS, 2, 2);
                   1042: 
1.113     paf      1043:        // Element createElementNS(in DOMString namespaceURI, in DOMString qualifiedName) raises(DOMException);
1.112     paf      1044:        add_native_method("createElementNS", Method::CT_DYNAMIC, _createElementNS, 2, 2);
1.99      paf      1045: 
                   1046:        // NodeList getElementsByTagNameNS(in DOMString namespaceURI, in DOMString localName);
                   1047:        add_native_method("getElementsByTagNameNS", Method::CT_DYNAMIC, _getElementsByTagNameNS, 2, 2);
1.1       parser   1048: 
1.16      parser   1049:        /// parser
                   1050:        
1.54      paf      1051:        // ^xdoc::create{qualifiedName}
1.95      paf      1052:        // ^xdoc::create[<some>xml</some>]
                   1053:        // ^xdoc::create[URI][<some>xml</some>]
                   1054:        add_native_method("create", Method::CT_DYNAMIC, _create, 1, 2); 
1.83      paf      1055:        // for backward compatibility with <=v 1.82 2002/01/31 11:51:46 paf
                   1056:        add_native_method("set", Method::CT_DYNAMIC, _create, 1, 1);
1.54      paf      1057: 
                   1058:        // ^xdoc::load[some.xml]
1.122     paf      1059:        add_native_method("load", Method::CT_DYNAMIC, _load, 1, 1);
1.54      paf      1060: 
1.2       parser   1061:        // ^xdoc.save[some.xml]
                   1062:        // ^xdoc.save[some.xml;options hash]
1.1       parser   1063:        add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2);
                   1064: 
1.2       parser   1065:        // ^xdoc.string[] <doc/>
                   1066:        // ^xdoc.string[options hash] <doc/>
1.1       parser   1067:        add_native_method("string", Method::CT_DYNAMIC, _string, 0, 1);
                   1068: 
1.2       parser   1069:        // ^xdoc.file[] file with "<doc/>"
                   1070:        // ^xdoc.file[options hash] file with "<doc/>"
1.58      paf      1071:        add_native_method("file", Method::CT_DYNAMIC, _file, 0, 1);
1.1       parser   1072: 
1.98      paf      1073:        // ^xdoc.transform[stylesheet file_name/xdoc]
                   1074:        // ^xdoc.transform[stylesheet file_name/xdoc;params hash]
1.59      paf      1075:        add_native_method("transform", Method::CT_DYNAMIC, _transform, 1, 2);
1.2       parser   1076: 
1.5       parser   1077: }
                   1078: 
1.111     paf      1079: # else
                   1080: 
                   1081: #include "classes.h"
                   1082: 
1.1       parser   1083: // global variable
                   1084: 
1.111     paf      1085: DECLARE_CLASS_VAR(xdoc, 0, 0); // fictive
1.1       parser   1086: 
                   1087: #endif

E-mail: