Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

xmlTools.cpp

Go to the documentation of this file.
00001 //==============================================
00002 //  copyright            : (C) 2003 by Will Stokes
00003 //==============================================
00004 //  This program is free software; you can redistribute it 
00005 //  and/or modify it under the terms of the GNU General 
00006 //  Public License as published by the Free Software 
00007 //  Foundation; either version 2 of the License, or  
00008 //  (at your option) any later version.         
00009 //
00010 //  As a special exception, Will Stokes gives permission to 
00011 //  link this program with Qt non-commercial edition, and 
00012 //  distribute the resulting executable, without including the 
00013 //  source code for the Qt non-commercial edition in the 
00014 //  source distribution. 
00015 //==============================================
00016 
00017 //Systemwide includes
00018 #include <qstring.h>
00019 #include <qdir.h>
00020 
00021 #include <string>
00022 #include <libxml/xmlmemory.h>
00023 #include <libxml/debugXML.h>
00024 #include <libxml/HTMLtree.h>
00025 #include <libxml/xmlIO.h>
00026 #include <libxml/DOCBparser.h>
00027 #include <libxml/xinclude.h>
00028 #include <libxml/catalog.h>
00029 #include <libxslt/xslt.h>
00030 #include <libxslt/xsltInternals.h>
00031 #include <libxslt/transform.h>
00032 #include <libxslt/xsltutils.h>
00033 #include <stdio.h>
00034 
00035 //Projectwide includes
00036 #include "xmlTools.h"
00037 #include "../config.h"
00038 
00039 //==============================================
00040 QString fixXMLString( QString text )
00041 {
00042   //the following checks are necessary before exporting
00043   //strings to XML. see http://hdf.ncsa.uiuc.edu/HDF5/XML/xml_escape_chars.html for details
00044   text.replace("&", "&amp;");
00045   text.replace("\"","&quot;");
00046   text.replace("'", "&apos;");
00047   text.replace("<", "&lt;");
00048   text.replace(">", "&gt;");
00049   return text;
00050 }
00051 //==============================================
00052 void transformXMLtoHTML( QString outputPath, 
00053                                        QString theme)
00054 {
00055   xmlSubstituteEntitiesDefault(1);
00056   xmlLoadExtDtdDefaultValue = 1;
00057   xsltStylesheetPtr cur = xsltParseStylesheetFile( (const xmlChar *) QString(THEMES_PATH + theme + "/theme.xsl").ascii() );
00058   xmlDocPtr doc = xmlParseFile( QString(outputPath + "/Album.xml").ascii() );
00059   const char* params[3];
00060   params[0] = "outputPath";
00061   QString quotedPath = outputPath;
00062   params[1] = quotedPath.prepend('\"').append('\"').ascii();
00063   params[2] = NULL;
00064   xmlDocPtr res = xsltApplyStylesheet( cur, doc, params);
00065   xsltFreeStylesheet( cur );
00066   xmlFreeDoc( res );
00067   xmlFreeDoc( doc );
00068   xsltCleanupGlobals();
00069   xmlCleanupParser();
00070 }
00071 //==============================================
00072 void updateXML( QString inputPath )
00073 {
00074   xmlSubstituteEntitiesDefault(1);
00075   xmlLoadExtDtdDefaultValue = 1;
00076   
00077   xsltStylesheetPtr stylesheet;
00078   xmlDocPtr inputDoc, outputDoc;
00079   
00080   stylesheet = xsltParseStylesheetFile( (const xmlChar *) QString(XMLCONVERSION_PATH + "update.xsl").ascii() );
00081   
00082   inputDoc = xmlParseFile( QString(inputPath + "/Album.xml").ascii() );
00083   
00084   const char* params[3];
00085   params[0] = "outputPath";
00086   QString quotedPath = inputPath;
00087   params[1] = quotedPath.prepend('\"').append('\"').ascii();
00088   params[2] = NULL;
00089 
00090   //iterate until Album.updated file is created
00091   QDir workingDir( inputPath );
00092   
00093   int iterations = 0;
00094   while(true)
00095   {
00096     iterations++;
00097     
00098     //apply the stylesheet
00099     outputDoc = xsltApplyStylesheet( stylesheet, inputDoc, params );
00100 
00101     //if Album.updated file now exists we have already completed the last iteration, 
00102     //meaning the input document is the most up-to-date so break out of loop
00103     if(workingDir.exists( "Album.updated" ))
00104       break;
00105         
00106     //free input  doc
00107     xmlFreeDoc( inputDoc );
00108    
00109     //swap input and output
00110     inputDoc = outputDoc;   
00111   }
00112   
00113   //remove updated file
00114   workingDir.remove( "Album.updated" );
00115 
00116   //if we made more than one iteration then changes were applied
00117   if(iterations > 1)
00118   {
00119     //output updated xml file
00120     FILE* outfile = fopen( QString(inputPath + "/Album.xml").ascii(), "w" );
00121     xsltSaveResultToFile( outfile, inputDoc, stylesheet);
00122     fclose( outfile );
00123   }
00124   
00125   //memory
00126   xsltFreeStylesheet( stylesheet );
00127   xmlFreeDoc( inputDoc );
00128   xmlFreeDoc( outputDoc );
00129   xsltCleanupGlobals();
00130   xmlCleanupParser();
00131 }
00132 //==============================================
00133 
00134 

Generated on Thu Nov 13 00:10:55 2003 for AlbumShaper by doxygen 1.3.4