xmlutils.cpp

Go to the documentation of this file.
00001 // $Id: xmlutils.cpp 1082 2006-05-16 14:42:54Z alex $
00002 /* @@tag:xara-cn@@ DO NOT MODIFY THIS LINE
00003 ================================XARAHEADERSTART===========================
00004  
00005                Xara LX, a vector drawing and manipulation program.
00006                     Copyright (C) 1993-2006 Xara Group Ltd.
00007        Copyright on certain contributions may be held in joint with their
00008               respective authors. See AUTHORS file for details.
00009 
00010 LICENSE TO USE AND MODIFY SOFTWARE
00011 ----------------------------------
00012 
00013 This file is part of Xara LX.
00014 
00015 Xara LX is free software; you can redistribute it and/or modify it
00016 under the terms of the GNU General Public License version 2 as published
00017 by the Free Software Foundation.
00018 
00019 Xara LX and its component source files are distributed in the hope
00020 that it will be useful, but WITHOUT ANY WARRANTY; without even the
00021 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00022 See the GNU General Public License for more details.
00023 
00024 You should have received a copy of the GNU General Public License along
00025 with Xara LX (see the file GPL in the root directory of the
00026 distribution); if not, write to the Free Software Foundation, Inc., 51
00027 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00028 
00029 
00030 ADDITIONAL RIGHTS
00031 -----------------
00032 
00033 Conditional upon your continuing compliance with the GNU General Public
00034 License described above, Xara Group Ltd grants to you certain additional
00035 rights. 
00036 
00037 The additional rights are to use, modify, and distribute the software
00038 together with the wxWidgets library, the wxXtra library, and the "CDraw"
00039 library and any other such library that any version of Xara LX relased
00040 by Xara Group Ltd requires in order to compile and execute, including
00041 the static linking of that library to XaraLX. In the case of the
00042 "CDraw" library, you may satisfy obligation under the GNU General Public
00043 License to provide source code by providing a binary copy of the library
00044 concerned and a copy of the license accompanying it.
00045 
00046 Nothing in this section restricts any of the rights you have under
00047 the GNU General Public License.
00048 
00049 
00050 SCOPE OF LICENSE
00051 ----------------
00052 
00053 This license applies to this program (XaraLX) and its constituent source
00054 files only, and does not necessarily apply to other Xara products which may
00055 in part share the same code base, and are subject to their own licensing
00056 terms.
00057 
00058 This license does not apply to files in the wxXtra directory, which
00059 are built into a separate library, and are subject to the wxWindows
00060 license contained within that directory in the file "WXXTRA-LICENSE".
00061 
00062 This license does not apply to the binary libraries (if any) within
00063 the "libs" directory, which are subject to a separate license contained
00064 within that directory in the file "LIBS-LICENSE".
00065 
00066 
00067 ARRANGEMENTS FOR CONTRIBUTION OF MODIFICATIONS
00068 ----------------------------------------------
00069 
00070 Subject to the terms of the GNU Public License (see above), you are
00071 free to do whatever you like with your modifications. However, you may
00072 (at your option) wish contribute them to Xara's source tree. You can
00073 find details of how to do this at:
00074   http://www.xaraxtreme.org/developers/
00075 
00076 Prior to contributing your modifications, you will need to complete our
00077 contributor agreement. This can be found at:
00078   http://www.xaraxtreme.org/developers/contribute/
00079 
00080 Please note that Xara will not accept modifications which modify any of
00081 the text between the start and end of this header (marked
00082 XARAHEADERSTART and XARAHEADEREND).
00083 
00084 
00085 MARKS
00086 -----
00087 
00088 Xara, Xara LX, Xara X, Xara X/Xtreme, Xara Xtreme, the Xtreme and Xara
00089 designs are registered or unregistered trademarks, design-marks, and/or
00090 service marks of Xara Group Ltd. All rights in these marks are reserved.
00091 
00092 
00093       Xara Group Ltd, Gaddesden Place, Hemel Hempstead, HP2 6EX, UK.
00094                         http://www.xara.com/
00095 
00096 =================================XARAHEADEREND============================
00097  */
00098 
00099 // The module that controls the OIL side of import/export filters.
00100 
00101 
00102 #include "camtypes.h"
00103 
00104 #include "xmlutils.h"
00105 
00106 #include <stdlib.h>
00107 #include <stdio.h>
00108 #include <string.h>
00109 #include <assert.h>
00110 
00111 #include <libxml/tree.h>
00112 #include <libxml/parser.h>
00113 //#include <libxml/xpath.h>
00114 
00115 /************************************************************************************
00116 >   BOOL CXMLUtils::Initialise()
00117 
00118     Author:     Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
00119     Created:    08/May/2006
00120     Inputs:     -
00121     Outputs:    -
00122     Returns:    TRUE if succeeded
00123     Purpose:    Set up application wide XML support
00124 
00125 ************************************************************************************/
00126 BOOL CXMLUtils::Initialise()
00127 {
00128     xmlInitParser();
00129     LIBXML_TEST_VERSION
00130 
00131     return TRUE;
00132 }
00133 
00134 
00135 
00136 
00137 /************************************************************************************
00138 >   BOOL CXMLUtils::DeInitialise()
00139 
00140     Author:     Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
00141     Created:    08/May/2006
00142     Inputs:     -
00143     Outputs:    -
00144     Returns:    TRUE if succeeded
00145     Purpose:    Set up application wide XML support
00146 
00147 ************************************************************************************/
00148 BOOL CXMLUtils::DeInitialise()
00149 {
00150     xmlCleanupParser();
00151 
00152 #if _DEBUG
00153     /*
00154      * this is to debug memory for regression tests
00155      */
00156     xmlMemoryDump();
00157 #endif
00158 
00159     return TRUE;
00160 }
00161 
00162 
00163 
00164 
00165 /************************************************************************************
00166 >   static wxString CXMLUtils::ConvertToWXString(xmlChar* xmlString)
00167 
00168     Author:     Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
00169     Created:    08/May/2006
00170     Inputs:     -
00171     Outputs:    -
00172     Returns:    Pointer to newly allocated StringBase object (caller takes ownership)
00173     Purpose:    Set up application wide XML support
00174 
00175 ************************************************************************************/
00176 wxString CXMLUtils::ConvertToWXString(const xmlChar* xmlString)
00177 {
00178     // Use wx to convert from UTF8 to native
00179     wxString strTemp((const char*)xmlString, wxConvUTF8);
00180 
00181     return strTemp;
00182 }
00183 
00184 
00185 
00186 
00187 /************************************************************************************
00188 >   static void CXMLUtils::ConvertToXMLString(wxString str)
00189 
00190     Author:     Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
00191     Created:    09/May/2006
00192     Inputs:     str - wxString input
00193     Outputs:    pxmlstr - xmlChar* output
00194     Returns:    Pointer to newly allocated StringBase object (caller takes ownership)
00195     Purpose:    Set up application wide XML support
00196 
00197 ************************************************************************************/
00198 void CXMLUtils::ConvertToXMLString(wxString str, xmlChar** pxmlstr)
00199 {
00200 //  wxCharBuffer buf = str.mb_str(wxConvUTF8);
00201 //  wxStrdup from buf to pxmlstr?
00202 }
00203 
00204 
00205 
00206 
00207 #if _DEBUG
00208 #if defined(LIBXML_XPATH_ENABLED) && \
00209     defined(LIBXML_SAX1_ENABLED) && \
00210     defined(LIBXML_OUTPUT_ENABLED)
00211 
00212 /*
00213  * section:     XPath
00214  * synopsis:    Load a document, locate subelements with XPath, modify
00215  *              said elements and save the resulting document.
00216  * purpose:     Shows how to make a full round-trip from a load/edit/save
00217  * usage:   xpath2 <xml-file> <xpath-expr> <new-value>
00218  * test:    xpath2 test3.xml '//discarded' discarded > xpath2.tmp ; diff xpath2.tmp xpath2.res ; rm xpath2.tmp
00219  * author:  Aleksey Sanin and Daniel Veillard
00220  * copy:    see Copyright for the status of this software.
00221  */
00222 void CXMLUtils::Test()
00223 {
00224     /* Init libxml */
00225     xmlInitParser();
00226     LIBXML_TEST_VERSION
00227 
00228     /* Do the main job */
00229     example4("/tmp/testin.xml", (xmlChar*)"//fred", (xmlChar*)"jim");
00230 
00231     /* Shutdown libxml */
00232     xmlCleanupParser();
00233     
00234     /*
00235      * this is to debug memory for regression tests
00236      */
00237     xmlMemoryDump();
00238 }
00239 
00251 INT32 CXMLUtils::example4(const char* filename, const xmlChar* xpathExpr, const xmlChar* value)
00252 {
00253     xmlDocPtr doc;
00254     xmlXPathContextPtr xpathCtx; 
00255     xmlXPathObjectPtr xpathObj; 
00256     
00257     assert(filename);
00258     assert(xpathExpr);
00259     assert(value);
00260 
00261     /* Load XML document */
00262     doc = xmlParseFile(filename);
00263     if (doc == NULL) {
00264     fprintf(stderr, "Error: unable to parse file \"%s\"\n", filename);
00265     return(-1);
00266     }
00267 
00268     /* Create xpath evaluation context */
00269     xpathCtx = xmlXPathNewContext(doc);
00270     if(xpathCtx == NULL) {
00271         fprintf(stderr,"Error: unable to create new XPath context\n");
00272         xmlFreeDoc(doc); 
00273         return(-1);
00274     }
00275     
00276     /* Evaluate xpath expression */
00277     xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx);
00278     if(xpathObj == NULL) {
00279         fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", xpathExpr);
00280         xmlXPathFreeContext(xpathCtx); 
00281         xmlFreeDoc(doc); 
00282         return(-1);
00283     }
00284 
00285     /* update selected nodes */
00286     update_xpath_nodes(xpathObj->nodesetval, value);
00287 
00288     
00289     /* Cleanup of XPath data */
00290     xmlXPathFreeObject(xpathObj);
00291     xmlXPathFreeContext(xpathCtx); 
00292 
00293     /* dump the resulting document */
00294     xmlDocDump(stdout, doc);
00295 
00296     /* save the doc out to a temp file */
00297     xmlSaveFileEnc("/tmp/testout.xml", doc, "UTF-8");
00298 
00299     /* free the document */
00300     xmlFreeDoc(doc); 
00301     
00302     return(0);
00303 }
00304 
00312 void CXMLUtils::update_xpath_nodes(xmlNodeSetPtr nodes, const xmlChar* value)
00313 {
00314     INT32 size;
00315     INT32 i;
00316     
00317     assert(value);
00318     size = (nodes) ? nodes->nodeNr : 0;
00319     
00320     /*
00321      * NOTE: the nodes are processed in reverse order, i.e. reverse document
00322      *       order because xmlNodeSetContent can actually free up descendant
00323      *       of the node and such nodes may have been selected too ! Handling
00324      *       in reverse order ensure that descendant are accessed first, before
00325      *       they get removed. Mixing XPath and modifications on a tree must be
00326      *       done carefully !
00327      */
00328     for(i = size - 1; i >= 0; i--) {
00329     assert(nodes->nodeTab[i]);
00330     
00331     xmlNodeSetContent(nodes->nodeTab[i], value);
00332     /*
00333      * All the elements returned by an XPath query are pointers to
00334      * elements from the tree *except* namespace nodes where the XPath
00335      * semantic is different from the implementation in libxml2 tree.
00336      * As a result when a returned node set is freed when
00337      * xmlXPathFreeObject() is called, that routine must check the
00338      * element type. But node from the returned set may have been removed
00339      * by xmlNodeSetContent() resulting in access to freed data.
00340      * This can be exercised by running
00341      *       valgrind xpath2 test3.xml '//discarded' discarded
00342      * There is 2 ways around it:
00343      *   - make a copy of the pointers to the nodes from the result set 
00344      *     then call xmlXPathFreeObject() and then modify the nodes
00345      * or
00346      *   - remove the reference to the modified nodes from the node set
00347      *     as they are processed, if they are not namespace nodes.
00348      */
00349     if (nodes->nodeTab[i]->type != XML_NAMESPACE_DECL)
00350         nodes->nodeTab[i] = NULL;
00351     }
00352 }
00353 
00354 #else
00355     #pragma error "XPath support not compiled in"
00356 #endif
00357 
00358 #endif
00359 

Generated on Sat Nov 10 03:48:58 2007 for Camelot by  doxygen 1.4.4