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

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.133   ! paf        12: static const char * const IDENT_XDOC_C="$Date: 2004/02/13 14:01:08 $";
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
        !           599: static int
        !           600: pa_xsltSaveResultTo(xmlOutputBufferPtr buf, xmlDocPtr result,
        !           601:               xsltStylesheetPtr style) {
        !           602:     const xmlChar *encoding;
        !           603:     int base;
        !           604:     const xmlChar *method;
        !           605:     int indent;
        !           606: 
        !           607:     if ((buf == NULL) || (result == NULL) || (style == NULL))
        !           608:        return(-1);
        !           609:     if ((result->children == NULL) ||
        !           610:        ((result->children->type == XML_DTD_NODE) &&
        !           611:         (result->children->next == NULL)))
        !           612:        return(0);
        !           613: 
        !           614:     if ((style->methodURI != NULL) &&
        !           615:        ((style->method == NULL) ||
        !           616:         (!xmlStrEqual(style->method, (const xmlChar *) "xhtml")))) {
        !           617:         xsltGenericError(xsltGenericErrorContext,
        !           618:                "xsltSaveResultTo : unknown ouput method\n");
        !           619:         return(-1);
        !           620:     }
        !           621: 
        !           622:     base = buf->written;
        !           623: 
        !           624:     XSLT_GET_IMPORT_PTR(method, style, method)
        !           625:     XSLT_GET_IMPORT_PTR(encoding, style, encoding)
        !           626:     XSLT_GET_IMPORT_INT(indent, style, indent);
        !           627: 
        !           628:     if ((method == NULL) && (result->type == XML_HTML_DOCUMENT_NODE))
        !           629:        method = (const xmlChar *) "html";
        !           630: 
        !           631:     if ((method != NULL) &&
        !           632:        (xmlStrEqual(method, (const xmlChar *) "html"))) {
        !           633:        if (indent == -1)
        !           634:            indent = 1;
        !           635:        htmlDocContentDumpFormatOutput(buf, result, (const char *) encoding,
        !           636:                                       indent);
        !           637:        xmlOutputBufferFlush(buf);
        !           638:     } else if ((method != NULL) &&
        !           639:        (xmlStrEqual(method, (const xmlChar *) "xhtml"))) {
        !           640:        htmlDocContentDumpOutput(buf, result, (const char *) encoding);
        !           641:        xmlOutputBufferFlush(buf);
        !           642:     } else if ((method != NULL) &&
        !           643:               (xmlStrEqual(method, (const xmlChar *) "text"))) {
        !           644:        xmlNodePtr cur;
        !           645: 
        !           646:        cur = result->children;
        !           647:        while (cur != NULL) {
        !           648:            if (cur->type == XML_TEXT_NODE)
        !           649:                xmlOutputBufferWriteString(buf, (const char *) cur->content);
        !           650: 
        !           651:            /*
        !           652:             * Skip to next node
        !           653:             */
        !           654:            if (cur->children != NULL) {
        !           655:                if ((cur->children->type != XML_ENTITY_DECL) &&
        !           656:                    (cur->children->type != XML_ENTITY_REF_NODE) &&
        !           657:                    (cur->children->type != XML_ENTITY_NODE)) {
        !           658:                    cur = cur->children;
        !           659:                    continue;
        !           660:                }
        !           661:            }
        !           662:            if (cur->next != NULL) {
        !           663:                cur = cur->next;
        !           664:                continue;
        !           665:            }
        !           666:            
        !           667:            do {
        !           668:                cur = cur->parent;
        !           669:                if (cur == NULL)
        !           670:                    break;
        !           671:                if (cur == (xmlNodePtr) style->doc) {
        !           672:                    cur = NULL;
        !           673:                    break;
        !           674:                }
        !           675:                if (cur->next != NULL) {
        !           676:                    cur = cur->next;
        !           677:                    break;
        !           678:                }
        !           679:            } while (cur != NULL);
        !           680:        }
        !           681:        xmlOutputBufferFlush(buf);
        !           682:     } else {
        !           683:        int omitXmlDecl;
        !           684:        int standalone;
        !           685: 
        !           686:        XSLT_GET_IMPORT_INT(omitXmlDecl, style, omitXmlDeclaration);
        !           687:        XSLT_GET_IMPORT_INT(standalone, style, standalone);
        !           688: 
        !           689:        if (omitXmlDecl != 1) {
        !           690:            xmlOutputBufferWriteString(buf, "<?xml version=");
        !           691:            if (result->version != NULL) 
        !           692:                xmlBufferWriteQuotedString(buf->buffer, result->version);
        !           693:            else
        !           694:                xmlOutputBufferWriteString(buf, "\"1.0\"");
        !           695:            if (encoding == NULL) {
        !           696:                if (result->encoding != NULL)
        !           697:                    encoding = result->encoding;
        !           698:                else if (result->charset != XML_CHAR_ENCODING_UTF8)
        !           699:                    encoding = (const xmlChar *)
        !           700:                               xmlGetCharEncodingName((xmlCharEncoding)
        !           701:                                                      result->charset);
        !           702:            }
        !           703:            if (encoding != NULL) {
        !           704:                xmlOutputBufferWriteString(buf, " encoding=");
        !           705:                xmlBufferWriteQuotedString(buf->buffer, (xmlChar *) encoding);
        !           706:            }
        !           707:            switch (standalone) {
        !           708:                case 0:
        !           709:                    xmlOutputBufferWriteString(buf, " standalone=\"no\"");
        !           710:                    break;
        !           711:                case 1:
        !           712:                    xmlOutputBufferWriteString(buf, " standalone=\"yes\"");
        !           713:                    break;
        !           714:                default:
        !           715:                    break;
        !           716:            }
        !           717:            xmlOutputBufferWriteString(buf, "?>\n");
        !           718:        }
        !           719:        if (result->children != NULL) {
        !           720:            xmlNodePtr child = result->children;
        !           721: 
        !           722:            while (child != NULL) {
        !           723:                xmlNodeDumpOutput(buf, result, child, 0, (indent == 1),
        !           724:                                  (const char *) encoding);
        !           725:                if (child->type == XML_DTD_NODE)
        !           726:                    xmlOutputBufferWriteString(buf, "\n");
        !           727:                child = child->next;
        !           728:            }
        !           729:            xmlOutputBufferWriteString(buf, "\n");
        !           730:        }
        !           731:        xmlOutputBufferFlush(buf);
        !           732:     }
        !           733:     return(buf->written - base);
        !           734: }
        !           735: 
        !           736: 
1.111     paf       737: struct Xdoc2buf_result {
                    738:        char* str;
                    739:        size_t length;
                    740: };
                    741: static Xdoc2buf_result xdoc2buf(Request& r, VXdoc& vdoc, 
                    742:                                         MethodParams& params, int index,
1.63      paf       743:                                         VXdoc::Output_options& oo,
1.111     paf       744:                                         const String* file_spec) {
                    745:        Xdoc2buf_result result;
                    746:        prepare_output_options(r, params, index,
1.61      paf       747:                oo);
                    748: 
1.111     paf       749:        const char* encoding_cstr=oo.encoding->cstr();
1.63      paf       750:        xmlCharEncodingHandler *encoder=xmlFindCharEncodingHandler(encoding_cstr);
                    751:        if(!encoder)
1.87      paf       752:                throw Exception("parser.runtime",
1.111     paf       753:                        0,
1.63      paf       754:                        "encoding '%s' not supported", encoding_cstr);
1.61      paf       755:        // UTF-8 encoder contains empty input/output converters, 
                    756:        // which is wrong for xmlOutputBufferCreateIO
                    757:        // while zero encoder goes perfectly 
1.130     paf       758:        const char* encoder_name=encoder->name;
                    759:        if(strcmp(encoder_name, "UTF-8")==0)
1.61      paf       760:                encoder=0;
                    761: 
1.62      paf       762:        xmlOutputBuffer_auto_ptr outputBuffer(xmlAllocOutputBuffer(encoder));
1.61      paf       763: 
1.65      paf       764:        xsltStylesheet_auto_ptr stylesheet(xsltNewStylesheet());
                    765:        if(!stylesheet.get())
1.87      paf       766:                throw Exception(0,
1.111     paf       767:                        0,
1.61      paf       768:                        "xsltNewStylesheet failed");
                    769: 
1.62      paf       770:        #define OOS2STYLE(name) \
1.111     paf       771:                stylesheet->name=oo.name?BAD_CAST xmlMemStrdup(r.transcode(*oo.name)->str):0
1.62      paf       772:        #define OOE2STYLE(name) \
                    773:                stylesheet->name=oo.name
1.61      paf       774: 
1.62      paf       775:        OOS2STYLE(method);
                    776:        OOS2STYLE(encoding);
                    777:        OOS2STYLE(mediaType);
1.124     paf       778: //     OOS2STYLE(doctypeSystem);
1.130     paf       779: //     OOS2STYLE(doctypePublic);
1.62      paf       780:        OOE2STYLE(indent);
                    781:        OOS2STYLE(version);
                    782:        OOE2STYLE(standalone);
                    783:        OOE2STYLE(omitXmlDeclaration);
1.61      paf       784: 
1.111     paf       785:        xmlDoc *document=gdome_xml_doc_get_xmlDoc(vdoc.get_document());
1.130     paf       786:        document->encoding=BAD_CAST xmlMemStrdup(encoder_name);
1.133   ! paf       787:        if(pa_xsltSaveResultTo(outputBuffer.get(), document, stylesheet.get())<0) {
1.75      paf       788:                GdomeException exc=0;
1.116     paf       789:                throw XmlException(0, exc);
1.75      paf       790:        }
1.61      paf       791: 
1.54      paf       792:        // write out result
1.111     paf       793:        char *gnome_str;  size_t gnome_length;
1.63      paf       794:        if(outputBuffer->conv) {
1.111     paf       795:                gnome_length=outputBuffer->conv->use;
                    796:                gnome_str=(char *)outputBuffer->conv->content;
1.63      paf       797:        } else {
1.111     paf       798:                gnome_length=outputBuffer->buffer->use;
                    799:                gnome_str=(char *)outputBuffer->buffer->content;
1.63      paf       800:        }
1.111     paf       801: 
1.121     paf       802:        if((result.length=gnome_length)) {
1.118     paf       803:                result.str=pa_strdup(gnome_str, gnome_length);
                    804:        } else
                    805:                result.str=0;
                    806: 
1.63      paf       807:        if(file_spec)
1.111     paf       808:                file_write(*file_spec,
                    809:                        gnome_str, gnome_length, 
                    810:                        true/*as_text*/);
                    811: 
                    812:        return result;
1.1       parser    813: }
                    814: 
1.111     paf       815: static void _file(Request& r, MethodParams& params) {
                    816:        VXdoc& vdoc=GET_SELF(r, VXdoc);
1.63      paf       817:        VXdoc::Output_options oo(vdoc.output_options);
1.111     paf       818:        Xdoc2buf_result buf=xdoc2buf(r, vdoc, params, 0, 
1.63      paf       819:                oo,
1.111     paf       820:                0/*not to file, to memory*/);
1.63      paf       821:        // write out result
1.111     paf       822:        r.write_no_lang(String(buf.str, buf.length));
1.1       parser    823: 
1.58      paf       824:        // write out result
1.111     paf       825:        VFile& vfile=*new VFile;
                    826:        VHash& vhcontent_type=*new VHash;
                    827:        vhcontent_type.hash().put(
                    828:                value_name, 
                    829:                new VString(*oo.mediaType));
                    830:        vhcontent_type.hash().put(
1.126     paf       831:                String::Body("charset"), 
1.111     paf       832:                new VString(*oo.encoding));
                    833: 
                    834:        vfile.set(false/*tainted*/, buf.str?buf.str:""/*to distinguish from stat-ed file*/, buf.length, 
                    835:                0/*file_name*/, &vhcontent_type);
1.58      paf       836:        r.write_no_lang(vfile);
1.63      paf       837: }
                    838: 
1.111     paf       839: static void _save(Request& r, MethodParams& params) {
                    840:        VXdoc& vdoc=GET_SELF(r, VXdoc);
1.63      paf       841: 
1.111     paf       842:        const String& file_spec=r.absolute(params.as_string(0, "file name must be string"));
1.63      paf       843:        
                    844:        VXdoc::Output_options oo(vdoc.output_options);
1.111     paf       845:        xdoc2buf(r, vdoc, params, 1, 
1.63      paf       846:                oo,
1.111     paf       847:                &file_spec);
1.63      paf       848: }
                    849: 
1.111     paf       850: static void _string(Request& r, MethodParams& params) {
                    851:        VXdoc& vdoc=GET_SELF(r, VXdoc);
1.63      paf       852:        VXdoc::Output_options oo(vdoc.output_options);
1.111     paf       853:        Xdoc2buf_result buf=xdoc2buf(r, vdoc, params, 0, 
1.63      paf       854:                oo,
1.111     paf       855:                0/*not to file, to memory*/);
1.63      paf       856:        // write out result
1.111     paf       857:        r.write_no_lang(String(buf.str, buf.length));
1.1       parser    858: }
1.58      paf       859: 
1.111     paf       860: #ifndef DOXYGEN
                    861: struct Add_xslt_param_info {
                    862:        Request* r;
                    863:        Array<GdomeDOMString_auto_ptr>* strings;
                    864:        const char** current_transform_param;
                    865: };
                    866: #endif
                    867: static void add_xslt_param(
                    868:                           HashStringValue::key_type attribute, 
                    869:                           HashStringValue::value_type meaning, 
                    870:                           Add_xslt_param_info* info) {
                    871:        GdomeDOMString_auto_ptr s;
                    872:        *info->current_transform_param++=(s=info->r->transcode(attribute))->str; *info->strings+=s;
                    873:        *info->current_transform_param++=(s=info->r->transcode(meaning->as_string()))->str; *info->strings+=s;
                    874: }
1.132     paf       875: 
1.111     paf       876: static VXdoc& _transform(Request& r, const String* stylesheet_source, 
1.132     paf       877:                                                   VXdoc& vdoc, xsltStylesheetPtr stylesheet, const char** transform_params) 
                    878: {
1.111     paf       879:        xmlDoc *document=gdome_xml_doc_get_xmlDoc(vdoc.get_document());
1.65      paf       880:        xsltTransformContext_auto_ptr transformContext(
                    881:                xsltNewTransformContext(stylesheet, document));
1.90      paf       882:        // make params literal
1.111     paf       883:        if (transformContext->globalVars == NULL) // strangly not initialized by xsltNewTransformContext
1.90      paf       884:                transformContext->globalVars = xmlHashCreate(20);
                    885:        xsltQuoteUserParams(transformContext.get(), transform_params);
                    886:        // do transform
1.59      paf       887:        xmlDoc *transformed=xsltApplyStylesheetUser(
                    888:                stylesheet,
                    889:                document,
1.90      paf       890:                0/*already quoted-inserted  transform_params*/,
1.111     paf       891:                0/*const char* output*/,
1.59      paf       892:                0/*FILE *profile*/,
1.65      paf       893:                transformContext.get());
1.66      paf       894:        if(!transformed || xmlHaveGenericErrors()) {
                    895:                GdomeException exc=0;
1.116     paf       896:                throw XmlException(stylesheet_source, exc);
1.66      paf       897:        }
1.42      paf       898: 
1.61      paf       899:        //gdome_xml_doc_mkref dislikes XML_HTML_DOCUMENT_NODE  type, fixing
                    900:        transformed->type=XML_DOCUMENT_NODE;
1.59      paf       901:        // constructing result
                    902:        GdomeDocument *gdomeDocument=gdome_xml_doc_mkref(transformed);
                    903:        if(!gdomeDocument)
1.87      paf       904:                throw Exception(0,
1.111     paf       905:                        0,
1.59      paf       906:                        "gdome_xml_doc_mkref failed");
1.111     paf       907:        VXdoc& result=*new VXdoc(&r.charsets, gdomeDocument);
1.61      paf       908:        /* grabbing options
                    909: 
                    910:                <xsl:output
                    911:                !method = "xml" | "html" | "text"
                    912:                        X| qname-but-not-ncname 
                    913:                !version = nmtoken 
                    914:                !encoding = string 
                    915:                !omit-xml-declaration = "yes" | "no"
                    916:                !standalone = "yes" | "no"
                    917:                !doctype-public = string 
                    918:                !doctype-system = string 
                    919:                Xcdata-section-elements = qnames 
                    920:                !indent = "yes" | "no"
                    921:                !media-type = string /> 
1.59      paf       922:        */
                    923:        VXdoc::Output_options& oo=result.output_options;
                    924: 
1.111     paf       925:        oo.method=stylesheet->method?&r.transcode(stylesheet->method):0;
                    926:        oo.encoding=stylesheet->encoding?&r.transcode(stylesheet->encoding):0;
                    927:        oo.mediaType=stylesheet->mediaType?&r.transcode(stylesheet->mediaType):0;
1.59      paf       928:        oo.indent=stylesheet->indent!=0;
1.111     paf       929:        oo.version=stylesheet->version?&r.transcode(stylesheet->version):0;
1.59      paf       930:        oo.standalone=stylesheet->standalone!=0;
                    931:        oo.omitXmlDeclaration=stylesheet->omitXmlDeclaration!=0;
                    932: 
1.93      paf       933:        // return
                    934:        return result;
                    935: }
1.111     paf       936: static void _transform(Request& r, MethodParams& params) {
                    937:        VXdoc& vdoc=GET_SELF(r, VXdoc);
1.93      paf       938: 
                    939:        // params
1.111     paf       940:        Array<GdomeDOMString_auto_ptr> transform_strings;
                    941:        const char** transform_params=0;
                    942:        if(params.count()>1) {
                    943:                Value& vparams=params.as_no_junction(1, "transform parameters must be hash");
1.106     paf       944:                if(!vparams.is_string())
1.111     paf       945:                        if(HashStringValue* hash=vparams.get_hash()) {
                    946:                                transform_params=new(UseGC) const char*[hash->count()*2+1];
                    947:                                Add_xslt_param_info info={
                    948:                                        &r, 
                    949:                                        &transform_strings,
                    950:                                        transform_params
                    951:                                };
                    952:                                hash->for_each(add_xslt_param, &info);
                    953:                                transform_params[hash->count()*2]=0;                            
1.93      paf       954:                        } else
                    955:                                throw Exception("parser.runtime",
1.111     paf       956:                                        0,
1.93      paf       957:                                        "transform parameters parameter must be hash");
                    958:        }
                    959: 
1.111     paf       960:        VXdoc* result;
                    961:        if(Value *vxdoc=params[0].as(VXDOC_TYPE, false)) { // stylesheet (xdoc)
1.93      paf       962:                xmlDoc *document=gdome_xml_doc_get_xmlDoc(
1.111     paf       963:                        static_cast<VXdoc *>(vxdoc)->get_document());
1.93      paf       964:                // compile xdoc stylesheet
1.94      paf       965:                xsltStylesheet_auto_ptr stylesheet_ptr(xsltParseStylesheetDoc(document)); 
1.93      paf       966:                if(xmlHaveGenericErrors()) {
                    967:                        GdomeException exc=0;
1.116     paf       968:                        throw XmlException(0, exc);
1.93      paf       969:                }
                    970:                if(!stylesheet_ptr.get())
                    971:                        throw Exception("xml",
1.111     paf       972:                                0,
1.93      paf       973:                                "stylesheet failed to compile");
1.128     paf       974:                // strange thing - xsltParseStylesheetDoc records document and destroys it in stylesheet destructor
                    975:                // we don't need that
                    976:                stylesheet_ptr->doc=0;
1.93      paf       977: 
                    978:                // transform!
1.111     paf       979:                result=&_transform(r, 0,
1.93      paf       980:                        vdoc, stylesheet_ptr.get(),
                    981:                        transform_params);
                    982:        } else { // stylesheet (file name)
                    983:                // extablish stylesheet connection
                    984:                const String& stylesheet_filespec=
1.111     paf       985:                        r.absolute(params.as_string(0, "stylesheet must be file name (string) or DOM document (xdoc)"));
1.125     paf       986:                Stylesheet_connection_ptr connection=stylesheet_manager->get_connection(stylesheet_filespec);
1.93      paf       987: 
                    988:                // load and compile file to stylesheet [or get cached if any]
                    989:                // transform!
1.122     paf       990:                result=&_transform(r, &stylesheet_filespec, vdoc, connection->stylesheet(),
1.93      paf       991:                        transform_params);
                    992:        }
                    993: 
1.59      paf       994:        // write out result
1.93      paf       995:        r.write_no_lang(*result);
1.1       parser    996: }
                    997: 
1.16      parser    998: // constructor
1.2       parser    999: 
1.92      paf      1000: /// @test how to create empty type html?
1.111     paf      1001: MXdoc::MXdoc(): MXnode(XDOC_CLASS_NAME, xnode_class) {
1.16      parser   1002:        /// DOM1
1.2       parser   1003: 
1.16      parser   1004:        // Element createElement(in DOMString tagName) raises(DOMException);
                   1005:        add_native_method("createElement", Method::CT_DYNAMIC, _createElement, 1, 1);
                   1006:        // DocumentFragment createDocumentFragment(); 
                   1007:        add_native_method("createDocumentFragment", Method::CT_DYNAMIC, _createDocumentFragment, 0, 0);
                   1008:        // Text createTextNode(in DOMString data);
                   1009:        add_native_method("createTextNode", Method::CT_DYNAMIC, _createTextNode, 1, 1);
                   1010:        // Comment createComment(in DOMString data);
                   1011:        add_native_method("createComment", Method::CT_DYNAMIC, _createComment, 1, 1);
                   1012:        // CDATASection createCDATASection(in DOMString data) raises(DOMException);
                   1013:        add_native_method("createCDATASection", Method::CT_DYNAMIC, _createCDATASection, 1, 1);
                   1014:        // ProcessingInstruction createProcessingInstruction(in DOMString target, in DOMString data) raises(DOMException);
                   1015:        add_native_method("createProcessingInstruction", Method::CT_DYNAMIC, _createProcessingInstruction, 2, 2);
                   1016:        // Attr createAttribute(in DOMString name) raises(DOMException);
                   1017:        add_native_method("createAttribute", Method::CT_DYNAMIC, _createAttribute, 1, 1);
                   1018:        // EntityReference createEntityReference(in DOMString name) raises(DOMException);
                   1019:        add_native_method("createEntityReference", Method::CT_DYNAMIC, _createEntityReference, 1, 1);
1.99      paf      1020:        // NodeList getElementsByTagName(in DOMString name);
                   1021:        add_native_method("getElementsByTagName", Method::CT_DYNAMIC, _getElementsByTagName, 1, 1);
1.2       parser   1022: 
1.79      paf      1023:        /// DOM2
1.2       parser   1024: 
1.16      parser   1025:        // ^xdoc.getElementById[elementId]
                   1026:        add_native_method("getElementById", Method::CT_DYNAMIC, _getElementById, 1, 1);
1.79      paf      1027: 
                   1028:     // Node (in Node importedNode, in boolean deep) raises(DOMException)
                   1029:        add_native_method("importNode", Method::CT_DYNAMIC, _importNode, 2, 2);
1.112     paf      1030: 
1.113     paf      1031:        // Attr createAttributeNS(in DOMString namespaceURI, in DOMString qualifiedName) raises(DOMException);
1.112     paf      1032:        add_native_method("createAttributeNS", Method::CT_DYNAMIC, _createAttributeNS, 2, 2);
                   1033: 
1.113     paf      1034:        // Element createElementNS(in DOMString namespaceURI, in DOMString qualifiedName) raises(DOMException);
1.112     paf      1035:        add_native_method("createElementNS", Method::CT_DYNAMIC, _createElementNS, 2, 2);
1.99      paf      1036: 
                   1037:        // NodeList getElementsByTagNameNS(in DOMString namespaceURI, in DOMString localName);
                   1038:        add_native_method("getElementsByTagNameNS", Method::CT_DYNAMIC, _getElementsByTagNameNS, 2, 2);
1.1       parser   1039: 
1.16      parser   1040:        /// parser
                   1041:        
1.54      paf      1042:        // ^xdoc::create{qualifiedName}
1.95      paf      1043:        // ^xdoc::create[<some>xml</some>]
                   1044:        // ^xdoc::create[URI][<some>xml</some>]
                   1045:        add_native_method("create", Method::CT_DYNAMIC, _create, 1, 2); 
1.83      paf      1046:        // for backward compatibility with <=v 1.82 2002/01/31 11:51:46 paf
                   1047:        add_native_method("set", Method::CT_DYNAMIC, _create, 1, 1);
1.54      paf      1048: 
                   1049:        // ^xdoc::load[some.xml]
1.122     paf      1050:        add_native_method("load", Method::CT_DYNAMIC, _load, 1, 1);
1.54      paf      1051: 
1.2       parser   1052:        // ^xdoc.save[some.xml]
                   1053:        // ^xdoc.save[some.xml;options hash]
1.1       parser   1054:        add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2);
                   1055: 
1.2       parser   1056:        // ^xdoc.string[] <doc/>
                   1057:        // ^xdoc.string[options hash] <doc/>
1.1       parser   1058:        add_native_method("string", Method::CT_DYNAMIC, _string, 0, 1);
                   1059: 
1.2       parser   1060:        // ^xdoc.file[] file with "<doc/>"
                   1061:        // ^xdoc.file[options hash] file with "<doc/>"
1.58      paf      1062:        add_native_method("file", Method::CT_DYNAMIC, _file, 0, 1);
1.1       parser   1063: 
1.98      paf      1064:        // ^xdoc.transform[stylesheet file_name/xdoc]
                   1065:        // ^xdoc.transform[stylesheet file_name/xdoc;params hash]
1.59      paf      1066:        add_native_method("transform", Method::CT_DYNAMIC, _transform, 1, 2);
1.2       parser   1067: 
1.5       parser   1068: }
                   1069: 
1.111     paf      1070: # else
                   1071: 
                   1072: #include "classes.h"
                   1073: 
1.1       parser   1074: // global variable
                   1075: 
1.111     paf      1076: DECLARE_CLASS_VAR(xdoc, 0, 0); // fictive
1.1       parser   1077: 
                   1078: #endif

E-mail: