AlbumShaper 1.0a3
xmlTools.cpp
Go to the documentation of this file.
1//==============================================
2// copyright : (C) 2003-2005 by Will Stokes
3//==============================================
4// This program is free software; you can redistribute it
5// and/or modify it under the terms of the GNU General
6// Public License as published by the Free Software
7// Foundation; either version 2 of the License, or
8// (at your option) any later version.
9//==============================================
10
11//Systemwide includes
12#include <qstring.h>
13#include <qdir.h>
14#include <qfile.h>
15#include <q3dragobject.h>
16
17#include <iostream>
18#include <string>
19#include <libxml/xmlmemory.h>
20#include <libxml/debugXML.h>
21#include <libxml/HTMLtree.h>
22#include <libxml/xmlIO.h>
23#include <libxml/xinclude.h>
24#include <libxml/catalog.h>
25#include <libxslt/xslt.h>
26#include <libxslt/xsltInternals.h>
27#include <libxslt/transform.h>
28#include <libxslt/xsltutils.h>
29#include <stdio.h>
30
31//Projectwide includes
32#include "xmlTools.h"
33#include "../../config.h"
34
35//==============================================
36QString fixXMLString( QString text )
37{
38 //the following checks are necessary before exporting
39 //strings to XML. see http://hdf.ncsa.uiuc.edu/HDF5/XML/xml_escape_chars.html for details
40 text.replace("&", "&amp;");
41 text.replace("\"","&quot;");
42 text.replace("'", "&apos;");
43 text.replace("<", "&lt;");
44 text.replace(">", "&gt;");
45 text.replace("\n", "&#10;");
46 text.replace("\r", "&#13;");
47 return text;
48}
49//==============================================
50void transformXMLtoHTML( QString outputPath, QString theme, bool smallWebExport)
51{
52 xmlSubstituteEntitiesDefault(1);
53 xmlLoadExtDtdDefaultValue = 1;
54 xsltStylesheetPtr cur = xsltParseStylesheetFile( (const xmlChar *) QString(THEMES_PATH + theme + "/theme.xsl").ascii() );
55
56 QString xmlFile = QString(outputPath + "/Album.xml");
57 xmlDocPtr doc = xmlParseFile( QFile::encodeName(xmlFile) );
58
59 const char* params[5];
60 //--
61 params[0] = "outputPath";
62 QString quotedPath = outputPath;
63
64 //For some reason libxslt has trouble handling filenames with spaces on Unix platforms (OSX,
65 //Linux, FreeBSD?). this problem can be averted by converting the filename to a URI. Converting it
66 //to a URI on windows using the qt method mangles the drive name though, so only convert to
67 //URI on OSX. We need to nail this weirdness at some point and be consistant IMHO but for now
68 //this works...
69#ifndef Q_OS_WIN
70 quotedPath = Q3UriDrag::localFileToUri( quotedPath );
71#endif
72
73 params[1] = quotedPath.prepend('\"').append('\"').ascii();
74 //--
75 params[2] = "smallWebExport";
76 if(smallWebExport)
77 params[3] = "1";
78 else
79 params[3] = "0";
80 //--
81 params[4] = NULL;
82 xmlDocPtr res = xsltApplyStylesheet( cur, doc, params);
83 xsltFreeStylesheet( cur );
84 xmlFreeDoc( res );
85 xmlFreeDoc( doc );
86 xsltCleanupGlobals();
87 xmlCleanupParser();
88}
89//==============================================
90void updateXML( QString inputPath )
91{
92 //skip updating the xml file if we can't find the update.xsl file
93 QDir tmpDir;
94 if( !tmpDir.exists( XMLCONVERSION_PATH + "update.xsl" ) )
95 {
96 std::cout << "Can't find update.xsl! Skipping auto-update!\n";
97 return;
98 }
99
100 xmlSubstituteEntitiesDefault(1);
101 xmlLoadExtDtdDefaultValue = 1;
102
103 xsltStylesheetPtr stylesheet;
104 xmlDocPtr inputDoc, outputDoc;
105
106 stylesheet = xsltParseStylesheetFile( (const xmlChar *) QString(XMLCONVERSION_PATH + "update.xsl").ascii() );
107
108 QString xmlFile = QString( inputPath + "/Album.xml" );
109 xmlFile = QDir::convertSeparators( xmlFile );
110 inputDoc = xmlParseFile( QFile::encodeName(xmlFile) );
111
112 const char* params[3];
113 params[0] = "outputPath";
114
115 QString quotedPath = inputPath;
116
117 //For some reason libxslt has trouble handling filenames with spaces on Unix platforms (OSX,
118 //Linux, FreeBSD?). this problem can be averted by converting the filename to a URI. Converting it
119 //to a URI on windows using the qt method mangles the drive name though, so only convert to
120 //URI on OSX. We need to nail this weirdness at some point and be consistant IMHO but for now
121 //this works...
122 #ifndef Q_OS_WIN
123 quotedPath = Q3UriDrag::localFileToUri( quotedPath );
124 #endif
125
126
127 params[1] = quotedPath.prepend('\"').append('\"').ascii();
128
129 params[2] = NULL;
130
131 std::cout.flush();
132
133 //iterate until Album.updated file is created
134 QDir workingDir( inputPath );
135
136 int iterations = 0;
137 while(true)
138 {
139 iterations++;
140
141 //apply the stylesheet
142 outputDoc = xsltApplyStylesheet( stylesheet, inputDoc, params );
143
144 //if Album.updated file now exists we have already completed the last iteration,
145 //meaning the input document is the most up-to-date so break out of loop
146 if(workingDir.exists( "Album.updated" ))
147 break;
148
149 //free input doc
150 xmlFreeDoc( inputDoc );
151
152 //swap input and output
153 inputDoc = outputDoc;
154 }
155
156 //remove updated file
157 workingDir.remove( inputPath + "/Album.updated" );
158
159 //if we made more than one iteration then changes were applied
160 if(iterations > 1)
161 {
162 //output updated xml file
163 FILE* outfile = fopen( QFile::encodeName(xmlFile), "w" );
164 xsltSaveResultToFile( outfile, inputDoc, stylesheet);
165 fclose( outfile );
166 }
167
168 //memory
169 xsltFreeStylesheet( stylesheet );
170 xmlFreeDoc( inputDoc );
171 xmlFreeDoc( outputDoc );
172 xsltCleanupGlobals();
173 xmlCleanupParser();
174}
175//==============================================
176
177
QString XMLCONVERSION_PATH
Definition config.cpp:22
QString THEMES_PATH
Definition config.cpp:21
void transformXMLtoHTML(QString outputPath, QString theme, bool smallWebExport)
Definition xmlTools.cpp:50
void updateXML(QString inputPath)
Definition xmlTools.cpp:90
QString fixXMLString(QString text)
Fix strings before exporting to XML such that & becomes &, etc...
Definition xmlTools.cpp:36