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

KDocTools

  • sources
  • kde-4.14
  • 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  QVector<const char *> params;
161  {
162  const QStringList paramList = args->getOptionList( "param" );
163  QStringList::ConstIterator it = paramList.begin();
164  QStringList::ConstIterator end = paramList.end();
165  for ( ; it != end; ++it ) {
166  const QString tuple = *it;
167  const int ch = tuple.indexOf( '=' );
168  if ( ch == -1 ) {
169  kError() << "Key-Value tuple '" << tuple << "' lacks a '='!" << endl;
170  return( 2 );
171  }
172  params.append( qstrdup( tuple.left( ch ).toUtf8() ) );
173  params.append( qstrdup( tuple.mid( ch + 1 ).toUtf8() ) );
174  }
175  }
176  params.append( NULL );
177 
178  bool index = args->isSet( "htdig" );
179  QString tss = args->getOption( "stylesheet" );
180  if ( tss.isEmpty() )
181  tss = "customization/kde-chunk.xsl";
182  if ( index )
183  tss = "customization/htdig_index.xsl" ;
184 
185  tss = KStandardDirs::locate( "dtd", tss );
186  const QString cache = args->getOption( "cache" );
187  const bool usingStdOut = args->isSet( "stdout" );
188  const bool usingOutput = args->isSet("output");
189  const QString outputOption = args->getOption( "output" );
190 
191  if ( index ) {
192  xsltStylesheetPtr style_sheet =
193  xsltParseStylesheetFile((const xmlChar *)tss.toLatin1().data());
194 
195  if (style_sheet != NULL) {
196 
197  xmlDocPtr doc = xmlReadFile( QFile::encodeName( checkFilename ).constData(),
198  NULL, XML_PARSE_NOENT|XML_PARSE_DTDLOAD|XML_PARSE_NONET );
199  xmlDocPtr res = xsltApplyStylesheet(style_sheet, doc, &params[0]);
200 
201  xmlFreeDoc(doc);
202  xsltFreeStylesheet(style_sheet);
203  if (res != NULL) {
204  xmlNodePtr cur = xmlDocGetRootElement(res);
205  if (!cur || xmlStrcmp(cur->name, (const xmlChar *) "entry")) {
206  fprintf(stderr,"document of the wrong type, root node != entry");
207  xmlFreeDoc(res);
208  return(1);
209  }
210  PairList list;
211  parseEntry( list, cur, 0 );
212  int wi = 0;
213  for ( PairList::ConstIterator it = list.constBegin(); it != list.constEnd();
214  ++it, ++wi )
215  fprintf( stdout, "w\t%s\t%d\t%d\n", ( *it ).word.toUtf8().data(),
216  1000*wi/list.count(), ( *it ).base );
217 
218  xmlFreeDoc(res);
219  } else {
220  kDebug() << "couldn't parse document " << checkFilename;
221  }
222  } else {
223  kDebug() << "couldn't parse style sheet " << tss;
224  }
225 
226  } else {
227  QString output = transform(checkFilename , tss, params);
228  if (output.isEmpty()) {
229  fprintf(stderr, "unable to parse %s\n", checkFilename.toLocal8Bit().data());
230  return(1);
231  }
232 
233  if ( !cache.isEmpty() ) {
234  if ( !saveToCache( output, cache ) ) {
235  kError() << i18n( "Could not write to cache file %1." , cache ) << endl;
236  }
237  goto end;
238  }
239 
240  doOutput(output, usingStdOut, usingOutput, outputOption, true /* replaceCharset */);
241  }
242  end:
243  xmlCleanupParser();
244  xmlMemoryDump();
245  return(0);
246 }
247 
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)
QString::indexOf
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
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
QVector::append
void append(const T &value)
KCmdLineOptions::add
KCmdLineOptions & add(const QByteArray &name, const KLocalizedString &description=KLocalizedString(), const QByteArray &defaultValue=QByteArray())
QByteArray
CheckFileSuccess
Definition: meinproc_common.h:10
ki18n
KLocalizedString ki18n(const char *msg)
CheckFileIsNotFile
Definition: meinproc_common.h:12
QString::split
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
KCmdLineArgs::parsedArgs
static KCmdLineArgs * parsedArgs(const QByteArray &id=QByteArray())
QCoreApplication
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)
QString::simplified
QString simplified() const
kError
static QDebug kError(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
PairList
QList< MyPair > PairList
Definition: meinproc.cpp:50
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
QList::count
int count(const T &value) const
QList::append
void append(const T &value)
QString::fromUtf8
QString fromUtf8(const char *str, int size)
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
QString::isEmpty
bool isEmpty() const
KAboutData
KCmdLineArgs::usage
static void usage(const QByteArray &id=QByteArray())
xmlLoadExtDtdDefaultValue
int xmlLoadExtDtdDefaultValue
Definition: meinproc.cpp:42
QString
QList
KCmdLineArgs::CmdLineArgKDE
QStringList
CheckResult
CheckResult
Definition: meinproc_common.h:18
QFileInfo
QList::end
iterator end()
check
CheckResult check(const QString &checkFilename, const QString &exe, const QByteArray &catalogs)
Definition: meinproc_common.cpp:29
KGlobal::locale
KLocale * locale()
QDir
QString::toLatin1
QByteArray toLatin1() const
CheckFileIsNotReadable
Definition: meinproc_common.h:13
QString::mid
QString mid(int position, int n) const
QVector
kstandarddirs.h
KStandardDirs::findExe
static QString findExe(const QString &appname, const QString &pathstr=QString(), SearchOptions options=NoSearchOptions)
QList::ConstIterator
typedef ConstIterator
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))
QByteArray::data
char * data()
kWarning
static QDebug kWarning(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
QString::left
QString left(int n) const
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
QList::constEnd
const_iterator constEnd() const
QList::constBegin
const_iterator constBegin() const
kcomponentdata.h
end
const KShortcut & end()
KCmdLineOptions
CheckNoXmllint
Definition: meinproc_common.h:22
QList::begin
iterator begin()
KComponentData
fillInstance
void fillInstance(KComponentData &ins, const QString &srcdir=QString())
Definition: xslt_kde.cpp:27
QUrl::toEncoded
QByteArray toEncoded(QFlags< QUrl::FormattingOption > options) const
QFile::encodeName
QByteArray encodeName(const QString &fileName)
QUrl::fromLocalFile
QUrl fromLocalFile(const QString &localFile)
list
QStringList list(const QString &fileClass)
QString::toUtf8
QByteArray toUtf8() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:26:01 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
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • 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