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

xmlTools.cpp

Go to the documentation of this file.
00001 //==============================================
00002 //  copyright            : (C) 2003-2005 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 
00011 //Systemwide includes
00012 #include <qstring.h>
00013 #include <qdir.h>
00014 #include <qfile.h>
00015 #include <qdragobject.h>
00016 
00017 #include <iostream>
00018 #include <string>
00019 #include <libxml/xmlmemory.h>
00020 #include <libxml/debugXML.h>
00021 #include <libxml/HTMLtree.h>
00022 #include <libxml/xmlIO.h>
00023 #include <libxml/xinclude.h>
00024 #include <libxml/catalog.h>
00025 #include <libxslt/xslt.h>
00026 #include <libxslt/xsltInternals.h>
00027 #include <libxslt/transform.h>
00028 #include <libxslt/xsltutils.h>
00029 #include <stdio.h>
00030 
00031 //Projectwide includes
00032 #include "xmlTools.h"
00033 #include "../../config.h"
00034 
00035 //==============================================
00036 QString fixXMLString( QString text )
00037 {
00038   //the following checks are necessary before exporting
00039   //strings to XML. see http://hdf.ncsa.uiuc.edu/HDF5/XML/xml_escape_chars.html for details
00040   text.replace("&", "&amp;");
00041   text.replace("\"","&quot;");
00042   text.replace("'", "&apos;");
00043   text.replace("<", "&lt;");
00044   text.replace(">", "&gt;");
00045   text.replace("\n", "&#10;");
00046   text.replace("\r", "&#13;");
00047   return text;
00048 }
00049 //==============================================
00050 void transformXMLtoHTML( QString outputPath, QString theme, bool smallWebExport)
00051 {
00052   xmlSubstituteEntitiesDefault(1);
00053   xmlLoadExtDtdDefaultValue = 1;
00054   xsltStylesheetPtr cur = xsltParseStylesheetFile( (const xmlChar *) QString(THEMES_PATH + theme + "/theme.xsl").ascii() );
00055 
00056   QString xmlFile = QString(outputPath + "/Album.xml");
00057   xmlDocPtr doc = xmlParseFile( QFile::encodeName(xmlFile) ); 
00058   
00059   const char* params[5];
00060   //--
00061   params[0] = "outputPath";
00062   QString quotedPath = outputPath;
00063 
00064   //For some reason libxslt has trouble handling filenames with spaces on Unix platforms (OSX,
00065   //Linux, FreeBSD?). this problem can be averted by converting the filename to a URI. Converting it
00066   //to a URI on windows using the qt method mangles the drive name though, so only convert to
00067   //URI on OSX. We need to nail this weirdness at some point and be consistant IMHO but for now
00068   //this works...
00069 #ifndef Q_OS_WIN
00070   quotedPath = QUriDrag::localFileToUri( quotedPath );  
00071 #endif
00072   
00073   params[1] = quotedPath.prepend('\"').append('\"').ascii();
00074   //--
00075   params[2] = "smallWebExport";
00076   if(smallWebExport)
00077     params[3] = "1";
00078   else
00079     params[3] = "0";
00080   //--
00081   params[4] = NULL;
00082   xmlDocPtr res = xsltApplyStylesheet( cur, doc, params);
00083   xsltFreeStylesheet( cur );
00084   xmlFreeDoc( res );
00085   xmlFreeDoc( doc );
00086   xsltCleanupGlobals();
00087   xmlCleanupParser();
00088 }
00089 //==============================================
00090 void updateXML( QString inputPath )
00091 {
00092   //skip updating the xml file if we can't find the update.xsl file
00093   QDir tmpDir;
00094   if( !tmpDir.exists( XMLCONVERSION_PATH + "update.xsl" ) )
00095   {
00096     std::cout << "Can't find update.xsl! Skipping auto-update!\n";
00097     return;
00098   }
00099   
00100   xmlSubstituteEntitiesDefault(1);
00101   xmlLoadExtDtdDefaultValue = 1;
00102 
00103   xsltStylesheetPtr stylesheet;
00104   xmlDocPtr inputDoc, outputDoc;
00105 
00106   stylesheet = xsltParseStylesheetFile( (const xmlChar *) QString(XMLCONVERSION_PATH + "update.xsl").ascii() );
00107 
00108   QString xmlFile = QString( inputPath + "/Album.xml" );
00109   xmlFile = QDir::convertSeparators( xmlFile );
00110   inputDoc = xmlParseFile( QFile::encodeName(xmlFile) ); 
00111 
00112   const char* params[3];
00113   params[0] = "outputPath";
00114 
00115   QString quotedPath = inputPath;
00116 
00117   //For some reason libxslt has trouble handling filenames with spaces on Unix platforms (OSX,
00118   //Linux, FreeBSD?). this problem can be averted by converting the filename to a URI. Converting it
00119   //to a URI on windows using the qt method mangles the drive name though, so only convert to
00120   //URI on OSX. We need to nail this weirdness at some point and be consistant IMHO but for now
00121   //this works...
00122   #ifndef Q_OS_WIN
00123   quotedPath = QUriDrag::localFileToUri( quotedPath );  
00124   #endif
00125 
00126 
00127   params[1] = quotedPath.prepend('\"').append('\"').ascii();
00128 
00129   params[2] = NULL;
00130 
00131   std::cout.flush();
00132 
00133   //iterate until Album.updated file is created
00134   QDir workingDir( inputPath );
00135 
00136   int iterations = 0;
00137   while(true)
00138   {
00139     iterations++;
00140 
00141     //apply the stylesheet
00142     outputDoc = xsltApplyStylesheet( stylesheet, inputDoc, params );
00143 
00144     //if Album.updated file now exists we have already completed the last iteration,
00145     //meaning the input document is the most up-to-date so break out of loop
00146     if(workingDir.exists( "Album.updated" ))
00147       break;
00148 
00149     //free input  doc
00150     xmlFreeDoc( inputDoc );
00151 
00152     //swap input and output
00153     inputDoc = outputDoc;
00154   }
00155 
00156   //remove updated file
00157   workingDir.remove( inputPath + "/Album.updated" );
00158 
00159   //if we made more than one iteration then changes were applied
00160   if(iterations > 1)
00161   {
00162     //output updated xml file
00163     FILE* outfile = fopen( QFile::encodeName(xmlFile), "w" );
00164     xsltSaveResultToFile( outfile, inputDoc, stylesheet);
00165     fclose( outfile );
00166   }
00167 
00168   //memory
00169   xsltFreeStylesheet( stylesheet );
00170   xmlFreeDoc( inputDoc );
00171   xmlFreeDoc( outputDoc );
00172   xsltCleanupGlobals();
00173   xmlCleanupParser();
00174 }
00175 //==============================================
00176 
00177 

Generated on Mon Apr 11 18:27:47 2005 for AlbumShaper by  doxygen 1.3.9.1