AlbumShaper  1.0a3
Functions
xmlTools.cpp File Reference
#include <qstring.h>
#include <qdir.h>
#include <qfile.h>
#include <q3dragobject.h>
#include <iostream>
#include <string>
#include <libxml/xmlmemory.h>
#include <libxml/debugXML.h>
#include <libxml/HTMLtree.h>
#include <libxml/xmlIO.h>
#include <libxml/xinclude.h>
#include <libxml/catalog.h>
#include <libxslt/xslt.h>
#include <libxslt/xsltInternals.h>
#include <libxslt/transform.h>
#include <libxslt/xsltutils.h>
#include <stdio.h>
#include "xmlTools.h"
#include "../../config.h"
Include dependency graph for xmlTools.cpp:

Go to the source code of this file.

Functions

QString fixXMLString (QString text)
 Fix strings before exporting to XML such that & becomes &, etc... More...
 
void transformXMLtoHTML (QString outputPath, QString theme, bool smallWebExport)
 
void updateXML (QString inputPath)
 

Function Documentation

§ fixXMLString()

QString fixXMLString ( QString  text)

Fix strings before exporting to XML such that & becomes &, etc...

Definition at line 36 of file xmlTools.cpp.

Referenced by Subalbum::exportToXML(), Photo::exportToXML(), and Album::exportToXML().

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 }

§ transformXMLtoHTML()

void transformXMLtoHTML ( QString  outputPath,
QString  theme,
bool  smallWebExport 
)

Definition at line 50 of file xmlTools.cpp.

References THEMES_PATH.

Referenced by Album::exportCompressedWebAlbum(), and Album::exportToDisk().

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 }
QString THEMES_PATH
Definition: config.cpp:21

§ updateXML()

void updateXML ( QString  inputPath)

Definition at line 90 of file xmlTools.cpp.

References XMLCONVERSION_PATH.

Referenced by Album::importFromDisk().

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 }
QString XMLCONVERSION_PATH
Definition: config.cpp:22