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

KDocTools

meinproc.cpp

Go to the documentation of this file.
00001 #ifndef _WIN32
00002 extern "C" int xmlLoadExtDtdDefaultValue;
00003 #endif
00004 
00005 #include <config-kdoctools.h>
00006 #include <config.h>
00007 #include <string.h>
00008 #include <sys/time.h>
00009 #include <unistd.h>
00010 #include <libxml/xmlversion.h>
00011 #include <libxml/xmlmemory.h>
00012 #include <libxml/debugXML.h>
00013 #include <libxml/HTMLtree.h>
00014 #include <libxml/xmlIO.h>
00015 #include <libxml/parserInternals.h>
00016 #include <libxslt/xsltconfig.h>
00017 #include <libxslt/xsltInternals.h>
00018 #include <libxslt/transform.h>
00019 #include <libxslt/xsltutils.h>
00020 #include <QtCore/QString>
00021 #include <kstandarddirs.h>
00022 #include <kcomponentdata.h>
00023 #include "xslt.h"
00024 #include <QtCore/QFile>
00025 #include <QtCore/QDir>
00026 #include <kcmdlineargs.h>
00027 #include <klocale.h>
00028 #include <kaboutdata.h>
00029 #include <stdlib.h>
00030 #include <kdebug.h>
00031 #include <QtCore/QTextCodec>
00032 #include <QtCore/QFileInfo>
00033 #include <kshell.h>
00034 #include <kurl.h>
00035 #include <QtCore/QList>
00036 
00037 class MyPair {
00038 public:
00039     QString word;
00040     int base;};
00041 
00042 typedef QList<MyPair> PairList;
00043 
00044 void parseEntry(PairList &list, xmlNodePtr cur, int base)
00045 {
00046     if ( !cur )
00047         return;
00048 
00049     base += atoi( ( const char* )xmlGetProp(cur, ( const xmlChar* )"header") );
00050     if ( base > 10 ) // 10 is the maximum
00051         base = 10;
00052 
00053     /* We don't care what the top level element name is */
00054     cur = cur->xmlChildrenNode;
00055     while (cur != NULL) {
00056 
00057         if ( cur->type == XML_TEXT_NODE ) {
00058             QString words = QString::fromUtf8( ( char* )cur->content );
00059             const QStringList wlist = words.simplified().split( ' ',QString::SkipEmptyParts );
00060             for ( QStringList::ConstIterator it = wlist.begin();
00061                   it != wlist.end(); ++it )
00062             {
00063                 MyPair m;
00064                 m.word = *it;
00065                 m.base = base;
00066                 list.append( m );
00067             }
00068         } else if ( !xmlStrcmp( cur->name, (const xmlChar *) "entry") )
00069             parseEntry( list, cur, base );
00070 
00071         cur = cur->next;
00072     }
00073 
00074 }
00075 
00076 int main(int argc, char **argv) {
00077 
00078     // xsltSetGenericDebugFunc(stderr, NULL);
00079 
00080     KCmdLineOptions options;
00081     options.add("stylesheet <xsl>", ki18n("Stylesheet to use"));
00082     options.add("stdout", ki18n("Output whole document to stdout"));
00083     options.add("o");
00084     options.add("output <file>", ki18n("Output whole document to file"));
00085     options.add("htdig", ki18n("Create a ht://dig compatible index"));
00086     options.add("check", ki18n("Check the document for validity"));
00087     options.add("cache <file>", ki18n("Create a cache file for the document"));
00088     options.add("srcdir <dir>", ki18n("Set the srcdir, for kdelibs"));
00089     options.add("param <key>=<value>", ki18n("Parameters to pass to the stylesheet"));
00090     options.add("+xml", ki18n("The file to transform"));
00091 
00092     KAboutData aboutData( "meinproc4", "kio_help4", ki18n("XML-Translator" ),
00093     "$Revision: 874548 $",
00094     ki18n("KDE Translator for XML"));
00095 
00096     KCmdLineArgs::init(argc, argv, &aboutData, KCmdLineArgs::CmdLineArgKDE);
00097     KCmdLineArgs::addCmdLineOptions( options );
00098 
00099     KComponentData ins("kio_help4");
00100     KGlobal::locale();
00101 
00102 
00103     KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
00104     if ( args->count() != 1 ) {
00105         args->usage();
00106         return ( 1 );
00107     }
00108 
00109     // Need to set SRCDIR before calling fillInstance
00110     QString srcdir;
00111     if ( args->isSet( "srcdir" ) )
00112         srcdir = QDir( args->getOption( "srcdir" ) ).absolutePath();
00113     fillInstance(ins,srcdir);
00114 
00115     LIBXML_TEST_VERSION
00116 
00117     QString checkFilename = args->arg( 0 );
00118     QFileInfo checkFile(checkFilename);
00119     if (!checkFile.exists())
00120     {
00121         kError() << "File '" << checkFilename << "' does not exist." << endl;
00122         return ( 2 );
00123     }
00124     if (!checkFile.isFile())
00125     {
00126         kError() << "'" << checkFilename << "' is not a file." << endl;
00127         return ( 2 );
00128     }
00129     if (!checkFile.isReadable())
00130     {
00131         kError() << "File '" << checkFilename << "' is not readable." << endl;
00132         return ( 2 );
00133     }
00134 
00135     if ( args->isSet( "check" ) ) {
00136         QString pwd_buffer = QDir::currentPath();
00137         QFileInfo file( args->arg( 0 ) );
00138 
00139         QByteArray catalogs;
00140         catalogs += KUrl::fromLocalFile( KStandardDirs::locate( "dtd", "customization/catalog.xml" ) ).toEncoded();
00141         catalogs += ' ';
00142         catalogs += KUrl::fromLocalFile( KStandardDirs::locate( "dtd", "docbook/xml-dtd-4.1.2/catalog.xml" ) ).toEncoded();
00143 
00144         setenv( "XML_CATALOG_FILES", catalogs.constData(), 1 );
00145         QString exe;
00146 #if defined( XMLLINT )
00147         exe = XMLLINT;
00148 #endif
00149         if ( !QFileInfo( exe ).isExecutable() ) {
00150             exe = KStandardDirs::findExe( "xmllint" );
00151             if (exe.isEmpty())
00152                 exe = KStandardDirs::locate( "exe", "xmllint" );
00153         }
00154         if ( QFileInfo( exe ).isExecutable() ) {
00155             QDir::setCurrent( file.absolutePath() );
00156             QString cmd = exe;
00157             cmd += " --valid --noout ";
00158 #ifdef Q_OS_WIN
00159             cmd += file.fileName();
00160 #else
00161             cmd += KShell::quoteArg(file.fileName());
00162 #endif
00163             cmd += " 2>&1";
00164             FILE *xmllint = popen( QFile::encodeName( cmd ).constData(), "r" );
00165             char buf[ 512 ];
00166             bool noout = true;
00167             unsigned int n;
00168             while ( ( n = fread(buf, 1, sizeof( buf ) - 1, xmllint ) ) ) {
00169                 noout = false;
00170                 buf[ n ] = '\0';
00171                 fputs( buf, stderr );
00172             }
00173             pclose( xmllint );
00174             QDir::setCurrent( pwd_buffer );
00175             if ( !noout )
00176                 return 1;
00177         } else {
00178             kWarning() << "couldn't find xmllint";
00179         }
00180     }
00181 
00182     xmlSubstituteEntitiesDefault(1);
00183     xmlLoadExtDtdDefaultValue = 1;
00184 
00185     QVector<const char *> params;
00186 #ifndef Q_WS_WIN 
00187     // libxslt parses the path given to outputFile as XPath expression which fails 
00188     // see libxslt/xsltEvalUserParams
00189     // this parameter is used only by share/apps/ksgmltools2/docbook/xsl/html/math.xsl 
00190     // and is not supported on windows yet
00191     if (args->isSet( "output" ) ) {
00192         params.append( qstrdup( "outputFile" ) );
00193         params.append( qstrdup( args->getOption( "output" ).toLocal8Bit() ) );
00194     }
00195 #endif
00196     {
00197         const QStringList paramList = args->getOptionList( "param" );
00198         QStringList::ConstIterator it = paramList.begin();
00199         QStringList::ConstIterator end = paramList.end();
00200         for ( ; it != end; ++it ) {
00201             const QString tuple = *it;
00202             const int ch = tuple.indexOf( '=' );
00203             if ( ch == -1 ) {
00204                 kError() << "Key-Value tuple '" << tuple << "' lacks a '='!" << endl;
00205                 return( 2 );
00206             }
00207             params.append( qstrdup( tuple.left( ch ).toUtf8() ) );
00208             params.append( qstrdup( tuple.mid( ch + 1 ).toUtf8() )  );
00209         }
00210     }
00211     params.append( NULL );
00212 
00213     bool index = args->isSet( "htdig" );
00214     QString tss = args->getOption( "stylesheet" );
00215     if ( tss.isEmpty() )
00216         tss =  "customization/kde-chunk.xsl";
00217     if ( index )
00218         tss = "customization/htdig_index.xsl" ;
00219 
00220     tss = KStandardDirs::locate( "dtd", tss );
00221 
00222     if ( index ) {
00223         xsltStylesheetPtr style_sheet =
00224             xsltParseStylesheetFile((const xmlChar *)tss.toLatin1().data());
00225 
00226         if (style_sheet != NULL) {
00227 
00228             xmlDocPtr doc = xmlParseFile( QFile::encodeName( args->arg( 0 ) ).constData() );
00229 
00230             xmlDocPtr res = xsltApplyStylesheet(style_sheet, doc, &params[0]);
00231 
00232             xmlFreeDoc(doc);
00233             xsltFreeStylesheet(style_sheet);
00234             if (res != NULL) {
00235                 xmlNodePtr cur = xmlDocGetRootElement(res);
00236                 if (!cur || xmlStrcmp(cur->name, (const xmlChar *) "entry")) {
00237                     fprintf(stderr,"document of the wrong type, root node != entry");
00238                     xmlFreeDoc(res);
00239                     return(1);
00240                 }
00241                 PairList list;
00242                 parseEntry( list, cur, 0 );
00243                 int wi = 0;
00244                 for ( PairList::ConstIterator it = list.constBegin(); it != list.constEnd();
00245                       ++it, ++wi )
00246                     fprintf( stdout, "w\t%s\t%d\t%d\n", ( *it ).word.toUtf8().data(),
00247                              1000*wi/list.count(), ( *it ).base );
00248 
00249                 xmlFreeDoc(res);
00250             } else {
00251                 kDebug() << "couldn't parse document " << args->arg( 0 );
00252             }
00253         } else {
00254             kDebug() << "couldn't parse style sheet " << tss;
00255         }
00256 
00257     } else {
00258         QString output = transform(args->arg( 0 ) , tss, params);
00259         if (output.isEmpty()) {
00260             fprintf(stderr, "unable to parse %s\n", args->arg( 0 ).toLocal8Bit().data());
00261             return(1);
00262         }
00263 
00264         QString cache = args->getOption( "cache" );
00265         if ( !cache.isEmpty() ) {
00266             if ( !saveToCache( output, cache ) ) {
00267                 kError() << i18n( "Could not write to cache file %1." ,  cache ) << endl;
00268             }
00269             goto end;
00270         }
00271 
00272         if (output.indexOf( "<FILENAME " ) == -1 || args->isSet( "stdout" ) || args->isSet("output") )
00273         {
00274             QFile file;
00275             if (args->isSet( "stdout" ) ) {
00276                 file.open( stdout, QIODevice::WriteOnly );
00277             } else {
00278                 if (args->isSet( "output" ) )
00279                    file.setFileName( args->getOption( "output" ));
00280                 else
00281                    file.setFileName( "index.html" );
00282                 file.open(QIODevice::WriteOnly);
00283             }
00284             replaceCharsetHeader( output );
00285 
00286             QByteArray data = output.toLocal8Bit();
00287             file.write(data.data(), data.length());
00288             file.close();
00289         } else {
00290             int index = 0;
00291             while (true) {
00292                 index = output.indexOf("<FILENAME ", index);
00293                 if (index == -1)
00294                     break;
00295                 int filename_index = index + strlen("<FILENAME filename=\"");
00296 
00297                 QString filename = output.mid(filename_index,
00298                                               output.indexOf("\"", filename_index) -
00299                                               filename_index);
00300 
00301                 QString filedata = splitOut(output, index);
00302                 QFile file(filename);
00303                 file.open(QIODevice::WriteOnly);
00304                 replaceCharsetHeader( filedata );
00305                 QByteArray data = fromUnicode( filedata );
00306                 file.write(data.data(), data.length());
00307                 file.close();
00308 
00309                 index += 8;
00310             }
00311         }
00312     }
00313  end:
00314     xmlCleanupParser();
00315     xmlMemoryDump();
00316     return(0);
00317 }
00318 

KDocTools

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal