• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDocTools

  • sources
  • kde-4.12
  • kdelibs
  • kdoctools
meinproc.cpp
Go to the documentation of this file.
1 
2 #include <config-kdoctools.h>
3 #include <config.h>
4 #include "xslt.h"
5 #include "meinproc_common.h"
6 
7 #include <QCoreApplication>
8 #include <QtCore/QString>
9 #include <QtCore/QFile>
10 #include <QtCore/QDir>
11 #include <QtCore/QTextCodec>
12 #include <QtCore/QFileInfo>
13 #include <QtCore/QList>
14 
15 #include <kaboutdata.h>
16 #include <kcomponentdata.h>
17 #include <kcmdlineargs.h>
18 #include <kdebug.h>
19 #include <klocale.h>
20 #include <kstandarddirs.h>
21 #include <kshell.h>
22 #include <kurl.h>
23 
24 #include <libxml/xmlversion.h>
25 #include <libxml/xmlmemory.h>
26 #include <libxml/debugXML.h>
27 #include <libxml/HTMLtree.h>
28 #include <libxml/xmlIO.h>
29 #include <libxml/parserInternals.h>
30 #include <libxslt/xsltconfig.h>
31 #include <libxslt/xsltInternals.h>
32 #include <libxslt/transform.h>
33 #include <libxslt/xsltutils.h>
34 #include <libexslt/exslt.h>
35 
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/time.h>
39 #include <unistd.h>
40 
41 #ifndef _WIN32
42 extern "C" int xmlLoadExtDtdDefaultValue;
43 #endif
44 
45 class MyPair {
46 public:
47  QString word;
48  int base;};
49 
50 typedef QList<MyPair> PairList;
51 
52 void parseEntry(PairList &list, xmlNodePtr cur, int base)
53 {
54  if ( !cur )
55  return;
56 
57  base += atoi( ( const char* )xmlGetProp(cur, ( const xmlChar* )"header") );
58  if ( base > 10 ) // 10 is the maximum
59  base = 10;
60 
61  /* We don't care what the top level element name is */
62  cur = cur->xmlChildrenNode;
63  while (cur != NULL) {
64 
65  if ( cur->type == XML_TEXT_NODE ) {
66  QString words = QString::fromUtf8( ( char* )cur->content );
67  const QStringList wlist = words.simplified().split( ' ',QString::SkipEmptyParts );
68  for ( QStringList::ConstIterator it = wlist.begin();
69  it != wlist.end(); ++it )
70  {
71  MyPair m;
72  m.word = *it;
73  m.base = base;
74  list.append( m );
75  }
76  } else if ( !xmlStrcmp( cur->name, (const xmlChar *) "entry") )
77  parseEntry( list, cur, base );
78 
79  cur = cur->next;
80  }
81 
82 }
83 
84 int main(int argc, char **argv) {
85 
86  // xsltSetGenericDebugFunc(stderr, NULL);
87 
88  KCmdLineOptions options;
89  options.add("stylesheet <xsl>", ki18n("Stylesheet to use"));
90  options.add("stdout", ki18n("Output whole document to stdout"));
91  options.add("o");
92  options.add("output <file>", ki18n("Output whole document to file"));
93  options.add("htdig", ki18n("Create a ht://dig compatible index"));
94  options.add("check", ki18n("Check the document for validity"));
95  options.add("cache <file>", ki18n("Create a cache file for the document"));
96  options.add("srcdir <dir>", ki18n("Set the srcdir, for kdelibs"));
97  options.add("param <key>=<value>", ki18n("Parameters to pass to the stylesheet"));
98  options.add("+xml", ki18n("The file to transform"));
99 
100  KAboutData aboutData( "meinproc4", "kio_help4", ki18n("XML-Translator" ),
101  "$Revision$",
102  ki18n("KDE Translator for XML"));
103 
104  KCmdLineArgs::init(argc, argv, &aboutData, KCmdLineArgs::CmdLineArgKDE);
105  KCmdLineArgs::addCmdLineOptions( options );
106 
107  QCoreApplication app( argc, argv );
108  KComponentData ins("kio_help4");
109  KGlobal::locale();
110 
111  KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
112  if ( args->count() != 1 ) {
113  args->usage();
114  return ( 1 );
115  }
116 
117  exsltRegisterAll();
118 
119  // Need to set SRCDIR before calling fillInstance
120  QString srcdir;
121  if ( args->isSet( "srcdir" ) )
122  srcdir = QDir( args->getOption( "srcdir" ) ).absolutePath();
123  fillInstance(ins,srcdir);
124 
125  LIBXML_TEST_VERSION
126 
127  const QString checkFilename = args->arg( 0 );
128  CheckFileResult ckr = checkFile( checkFilename );
129  if ( ckr != CheckFileSuccess )
130  {
131  if ( ckr == CheckFileDoesNotExist ) kError() << "File '" << checkFilename << "' does not exist.";
132  else if ( ckr == CheckFileIsNotFile ) kError() << "'" << checkFilename << "' is not a file.";
133  else if ( ckr == CheckFileIsNotReadable ) kError() << "File '" << checkFilename << "' is not readable.";
134  return ( 2 );
135  }
136 
137  if ( args->isSet( "check" ) ) {
138 
139  QByteArray catalogs;
140  catalogs += KUrl::fromLocalFile( KStandardDirs::locate( "dtd", "customization/catalog.xml" ) ).toEncoded();
141 
142  QString exe;
143 #if defined( XMLLINT )
144  exe = XMLLINT;
145 #endif
146  if ( !QFileInfo( exe ).isExecutable() ) {
147  exe = KStandardDirs::findExe( "xmllint" );
148  if (exe.isEmpty())
149  exe = KStandardDirs::locate( "exe", "xmllint" );
150  }
151 
152  CheckResult cr = check( checkFilename, exe, catalogs );
153  if ( cr != CheckSuccess )
154  {
155  if ( cr == CheckNoXmllint ) kWarning() << "couldn't find xmllint";
156  return 1;
157  }
158  }
159 
160  xmlSubstituteEntitiesDefault(1);
161  xmlLoadExtDtdDefaultValue = 1;
162 
163  QVector<const char *> params;
164 #ifndef Q_WS_WIN
165  // libxslt parses the path given to outputFile as XPath expression which fails
166  // see libxslt/xsltEvalUserParams
167  // this parameter is used only by share/apps/ksgmltools2/docbook/xsl/html/math.xsl
168  // and is not supported on windows yet
169  if (args->isSet( "output" ) ) {
170  params.append( qstrdup( "outputFile" ) );
171  params.append( qstrdup( args->getOption( "output" ).toLocal8Bit() ) );
172  }
173 #endif
174  {
175  const QStringList paramList = args->getOptionList( "param" );
176  QStringList::ConstIterator it = paramList.begin();
177  QStringList::ConstIterator end = paramList.end();
178  for ( ; it != end; ++it ) {
179  const QString tuple = *it;
180  const int ch = tuple.indexOf( '=' );
181  if ( ch == -1 ) {
182  kError() << "Key-Value tuple '" << tuple << "' lacks a '='!" << endl;
183  return( 2 );
184  }
185  params.append( qstrdup( tuple.left( ch ).toUtf8() ) );
186  params.append( qstrdup( tuple.mid( ch + 1 ).toUtf8() ) );
187  }
188  }
189  params.append( NULL );
190 
191  bool index = args->isSet( "htdig" );
192  QString tss = args->getOption( "stylesheet" );
193  if ( tss.isEmpty() )
194  tss = "customization/kde-chunk.xsl";
195  if ( index )
196  tss = "customization/htdig_index.xsl" ;
197 
198  tss = KStandardDirs::locate( "dtd", tss );
199  const QString cache = args->getOption( "cache" );
200  const bool usingStdOut = args->isSet( "stdout" );
201  const bool usingOutput = args->isSet("output");
202  const QString outputOption = args->getOption( "output" );
203 
204  if ( index ) {
205  xsltStylesheetPtr style_sheet =
206  xsltParseStylesheetFile((const xmlChar *)tss.toLatin1().data());
207 
208  if (style_sheet != NULL) {
209 
210  xmlDocPtr doc = xmlParseFile( QFile::encodeName( checkFilename ).constData() );
211 
212  xmlDocPtr res = xsltApplyStylesheet(style_sheet, doc, &params[0]);
213 
214  xmlFreeDoc(doc);
215  xsltFreeStylesheet(style_sheet);
216  if (res != NULL) {
217  xmlNodePtr cur = xmlDocGetRootElement(res);
218  if (!cur || xmlStrcmp(cur->name, (const xmlChar *) "entry")) {
219  fprintf(stderr,"document of the wrong type, root node != entry");
220  xmlFreeDoc(res);
221  return(1);
222  }
223  PairList list;
224  parseEntry( list, cur, 0 );
225  int wi = 0;
226  for ( PairList::ConstIterator it = list.constBegin(); it != list.constEnd();
227  ++it, ++wi )
228  fprintf( stdout, "w\t%s\t%d\t%d\n", ( *it ).word.toUtf8().data(),
229  1000*wi/list.count(), ( *it ).base );
230 
231  xmlFreeDoc(res);
232  } else {
233  kDebug() << "couldn't parse document " << checkFilename;
234  }
235  } else {
236  kDebug() << "couldn't parse style sheet " << tss;
237  }
238 
239  } else {
240  QString output = transform(checkFilename , tss, params);
241  if (output.isEmpty()) {
242  fprintf(stderr, "unable to parse %s\n", checkFilename.toLocal8Bit().data());
243  return(1);
244  }
245 
246  if ( !cache.isEmpty() ) {
247  if ( !saveToCache( output, cache ) ) {
248  kError() << i18n( "Could not write to cache file %1." , cache ) << endl;
249  }
250  goto end;
251  }
252 
253  doOutput(output, usingStdOut, usingOutput, outputOption, true /* replaceCharset */);
254  }
255  end:
256  xmlCleanupParser();
257  xmlMemoryDump();
258  return(0);
259 }
260 
meinproc_common.h
transform
QString transform(const QString &pat, const QString &tss, const QVector< const char * > &params)
Definition: xslt.cpp:130
i18n
QString i18n(const char *text)
KCmdLineArgs::addCmdLineOptions
static void addCmdLineOptions(const KCmdLineOptions &options, const KLocalizedString &name=KLocalizedString(), const QByteArray &id=QByteArray(), const QByteArray &afterId=QByteArray())
KCmdLineArgs::getOptionList
QStringList getOptionList(const QByteArray &option) const
kdebug.h
kurl.h
KCmdLineOptions::add
KCmdLineOptions & add(const QByteArray &name, const KLocalizedString &description=KLocalizedString(), const QByteArray &defaultValue=QByteArray())
CheckFileSuccess
Definition: meinproc_common.h:10
ki18n
KLocalizedString ki18n(const char *msg)
CheckFileIsNotFile
Definition: meinproc_common.h:12
KCmdLineArgs::parsedArgs
static KCmdLineArgs * parsedArgs(const QByteArray &id=QByteArray())
KStandardDirs::locate
static QString locate(const char *type, const QString &filename, const KComponentData &cData=KGlobal::mainComponent())
KCmdLineArgs
checkFile
CheckFileResult checkFile(const QString &checkFilename)
Definition: meinproc_common.cpp:11
kshell.h
CheckFileDoesNotExist
Definition: meinproc_common.h:11
doOutput
void doOutput(QString output, bool usingStdOut, bool usingOutput, const QString &outputOption, bool replaceCharset)
Definition: meinproc_common.cpp:60
isExecutable
bool isExecutable(const QString &file)
kError
static QDebug kError(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
PairList
QList< MyPair > PairList
Definition: meinproc.cpp:50
QString
KCmdLineArgs::arg
QString arg(int n) const
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
klocale.h
KCmdLineArgs::isSet
bool isSet(const QByteArray &option) const
KCmdLineArgs::count
int count() const
saveToCache
bool saveToCache(const QString &contents, const QString &filename)
Definition: xslt_kde.cpp:48
output
void output(QList< Action > actions, QHash< QString, QString > domain)
CheckFileResult
CheckFileResult
Definition: meinproc_common.h:8
xslt.h
kcmdlineargs.h
QStringList
KAboutData
KCmdLineArgs::usage
static void usage(const QByteArray &id=QByteArray())
xmlLoadExtDtdDefaultValue
int xmlLoadExtDtdDefaultValue
Definition: meinproc.cpp:42
KCmdLineArgs::CmdLineArgKDE
CheckResult
CheckResult
Definition: meinproc_common.h:18
check
CheckResult check(const QString &checkFilename, const QString &exe, const QByteArray &catalogs)
Definition: meinproc_common.cpp:29
KGlobal::locale
KLocale * locale()
CheckFileIsNotReadable
Definition: meinproc_common.h:13
kstandarddirs.h
KStandardDirs::findExe
static QString findExe(const QString &appname, const QString &pathstr=QString(), SearchOptions options=NoSearchOptions)
CheckSuccess
Definition: meinproc_common.h:20
KCmdLineArgs::init
static void init(int argc, char **argv, const QByteArray &appname, const QByteArray &catalog, const KLocalizedString &programName, const QByteArray &version, const KLocalizedString &description=KLocalizedString(), StdCmdLineArgs stdargs=StdCmdLineArgs(CmdLineArgQt|CmdLineArgKDE))
kWarning
static QDebug kWarning(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
main
int main(int argc, char **argv)
Definition: meinproc.cpp:84
KCmdLineArgs::getOption
QString getOption(const QByteArray &option) const
parseEntry
void parseEntry(PairList &list, xmlNodePtr cur, int base)
Definition: meinproc.cpp:52
kaboutdata.h
kcomponentdata.h
end
const KShortcut & end()
KCmdLineOptions
CheckNoXmllint
Definition: meinproc_common.h:22
KComponentData
fillInstance
void fillInstance(KComponentData &ins, const QString &srcdir=QString())
Definition: xslt_kde.cpp:27
QList
list
QStringList list(const QString &fileClass)
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:51:03 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDocTools

Skip menu "KDocTools"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal