meinproc.cpp

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