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

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

E-mail: