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 )
00047 base = 10;
00048
00049
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
00085 };
00086
00087
00088
00089
00090 int main(int argc, char **argv) {
00091
00092
00093
00094 KAboutData aboutData( "meinproc", I18N_NOOP("XML-Translator" ),
00095 "$Revision: 497203 $",
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
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 if ( !getcwd( pwd_buffer, sizeof(pwd_buffer) ) ) {
00142 kdError() << "getcwd failed." << endl;
00143 return 2;
00144 }
00145
00146 chdir( QFile::encodeName( file.dirPath( true ) ) );
00147
00148 QString catalogs;
00149 catalogs += locate( "dtd", "customization/catalog" );
00150 catalogs += " ";
00151 catalogs += locate( "dtd", "docbook/xml-dtd-4.1.2/docbook.cat" );
00152
00153 setenv( "SGML_CATALOG_FILES", QFile::encodeName( catalogs ).data(), 1);
00154 QString exe;
00155 #if defined( XMLLINT )
00156 exe = XMLLINT;
00157 #endif
00158 if ( ::access( QFile::encodeName( exe ), X_OK ) ) {
00159 exe = KStandardDirs::findExe( "xmllint" );
00160 if (exe.isEmpty())
00161 exe = locate( "exe", "xmllint" );
00162 }
00163 if ( !::access( QFile::encodeName( exe ), X_OK ) ) {
00164 QString cmd = exe;
00165 cmd += " --catalogs --valid --noout ";
00166 cmd += KProcess::quote(file.fileName());
00167 cmd += " 2>&1";
00168 FILE *xmllint = popen( QFile::encodeName( cmd ), "r");
00169 char buf[ 512 ];
00170 bool noout = true;
00171 unsigned int n;
00172 while ( ( n = fread(buf, 1, sizeof( buf ), xmllint ) ) ) {
00173 noout = false;
00174 buf[ n ] = '\0';
00175 fputs( buf, stderr );
00176 }
00177 pclose( xmllint );
00178 chdir( pwd_buffer );
00179 if ( !noout )
00180 return 1;
00181 } else {
00182 kdWarning() << "couldn't find xmllint" << endl;
00183 }
00184 }
00185
00186 xmlSubstituteEntitiesDefault(1);
00187 xmlLoadExtDtdDefaultValue = 1;
00188
00189 QValueVector<const char *> params;
00190 if (args->isSet( "output" ) ) {
00191 params.append( qstrdup( "outputFile" ) );
00192 params.append( qstrdup( QFile::decodeName( args->getOption( "output" ) ).latin1() ) );
00193 }
00194 {
00195 const QCStringList paramList = args->getOptionList( "param" );
00196 QCStringList::ConstIterator it = paramList.begin();
00197 QCStringList::ConstIterator end = paramList.end();
00198 for ( ; it != end; ++it ) {
00199 const QCString tuple = *it;
00200 const int ch = tuple.find( '=' );
00201 if ( ch == -1 ) {
00202 kdError() << "Key-Value tuple '" << tuple << "' lacks a '='!" << endl;
00203 return( 2 );
00204 }
00205 params.append( qstrdup( tuple.left( ch ) ) );
00206 params.append( qstrdup( tuple.mid( ch + 1 ) ) );
00207 }
00208 }
00209 params.append( NULL );
00210
00211 bool index = args->isSet( "htdig" );
00212 QString tss = args->getOption( "stylesheet" );
00213 if ( tss.isEmpty() )
00214 tss = "customization/kde-chunk.xsl";
00215 if ( index )
00216 tss = "customization/htdig_index.xsl" ;
00217
00218 tss = locate( "dtd", tss );
00219
00220 if ( index ) {
00221 xsltStylesheetPtr style_sheet =
00222 xsltParseStylesheetFile((const xmlChar *)tss.latin1());
00223
00224 if (style_sheet != NULL) {
00225
00226 xmlDocPtr doc = xmlParseFile( QFile::encodeName( args->arg( 0 ) ) );
00227
00228 xmlDocPtr res = xsltApplyStylesheet(style_sheet, doc, ¶ms[0]);
00229
00230 xmlFreeDoc(doc);
00231 xsltFreeStylesheet(style_sheet);
00232 if (res != NULL) {
00233 xmlNodePtr cur = xmlDocGetRootElement(res);
00234 if (!cur || xmlStrcmp(cur->name, (const xmlChar *) "entry")) {
00235 fprintf(stderr,"document of the wrong type, root node != entry");
00236 xmlFreeDoc(res);
00237 return(1);
00238 }
00239 PairList list;
00240 parseEntry( list, cur, 0 );
00241 int wi = 0;
00242 for ( PairList::ConstIterator it = list.begin(); it != list.end();
00243 ++it, ++wi )
00244 fprintf( stdout, "w\t%s\t%d\t%d\n", ( *it ).word.utf8().data(),
00245 1000*wi/list.count(), ( *it ).base );
00246
00247 xmlFreeDoc(res);
00248 } else {
00249 kdDebug() << "couldn't parse document " << args->arg( 0 ) << endl;
00250 }
00251 } else {
00252 kdDebug() << "couldn't parse style sheet " << tss << endl;
00253 }
00254
00255 } else {
00256 QString output = transform(args->arg( 0 ) , tss, params);
00257 if (output.isEmpty()) {
00258 fprintf(stderr, "unable to parse %s\n", args->arg( 0 ));
00259 return(1);
00260 }
00261
00262 QString cache = args->getOption( "cache" );
00263 if ( !cache.isEmpty() ) {
00264 if ( !saveToCache( output, cache ) ) {
00265 kdError() << i18n( "Could not write to cache file %1." ).arg( cache ) << endl;
00266 }
00267 goto end;
00268 }
00269
00270 if (output.find( "<FILENAME " ) == -1 || args->isSet( "stdout" ) || args->isSet("output") )
00271 {
00272 QFile file;
00273 if (args->isSet( "stdout" ) ) {
00274 file.open( IO_WriteOnly, stdout );
00275 } else {
00276 if (args->isSet( "output" ) )
00277 file.setName( QFile::decodeName(args->getOption( "output" )));
00278 else
00279 file.setName( "index.html" );
00280 file.open(IO_WriteOnly);
00281 }
00282 replaceCharsetHeader( output );
00283
00284 QCString data = output.local8Bit();
00285 file.writeBlock(data.data(), data.length());
00286 file.close();
00287 } else {
00288 int index = 0;
00289 while (true) {
00290 index = output.find("<FILENAME ", index);
00291 if (index == -1)
00292 break;
00293 int filename_index = index + strlen("<FILENAME filename=\"");
00294
00295 QString filename = output.mid(filename_index,
00296 output.find("\"", filename_index) -
00297 filename_index);
00298
00299 QString filedata = splitOut(output, index);
00300 QFile file(filename);
00301 file.open(IO_WriteOnly);
00302 replaceCharsetHeader( filedata );
00303 QCString data = fromUnicode( filedata );
00304 file.writeBlock(data.data(), data.length());
00305 file.close();
00306
00307 index += 8;
00308 }
00309 }
00310 }
00311 end:
00312 xmlCleanupParser();
00313 xmlMemoryDump();
00314 return(0);
00315 }
00316