Back to index

lightning-sunbird  0.9+nobinonly
transformiix.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is TransforMiiX XSLT processor code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * The MITRE Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "txStandaloneXSLTProcessor.h"
00039 #include "nsXPCOM.h"
00040 #include <fstream.h>
00041 #include "nsDoubleHashtable.h"
00042 #include "nsVoidArray.h"
00043 #ifdef MOZ_JPROF
00044 #include "jprof.h"
00045 #endif
00046 
00050 void printHelp()
00051 {
00052   cerr << "transfrmx [-h] [-i xml-file] [-s xslt-file] [-o output-file]" << endl << endl;
00053   cerr << "Options:";
00054   cerr << endl << endl;
00055   cerr << "\t-i  specify XML file to process" << endl;
00056   cerr << "\t-s  specify XSLT file to use for processing (default: use stylesheet" << endl
00057        << "\t\tspecified in XML file)" << endl;
00058   cerr << "\t-o  specify output file (default: write to stdout)" << endl;
00059   cerr << "\t-h  this help screen" << endl;
00060   cerr << endl;
00061   cerr << "You may use '-' in place of the output-file to explicitly specify" << endl;
00062   cerr << "standard output." << endl;
00063   cerr << endl;
00064 }
00065 
00069 void printUsage()
00070 {
00071   cerr << "transfrmx [-h] [-i xml-file] [-s xslt-file] [-o output-file]" << endl << endl;
00072   cerr << "For more infomation use the -h flag" << endl;
00073 }
00074 
00075 class txOptionEntry : public PLDHashCStringEntry
00076 {
00077 public:
00078     txOptionEntry(const void* aKey) : PLDHashCStringEntry(aKey)
00079     {
00080     }
00081     ~txOptionEntry()
00082     {
00083     }
00084     nsCStringArray mValues;
00085 };
00086 
00087 DECL_DHASH_WRAPPER(txOptions, txOptionEntry, nsACString&)
00088 DHASH_WRAPPER(txOptions, txOptionEntry, nsACString&)
00089 
00093 void parseCommandLine(int argc, char** argv, txOptions& aOptions)
00094 {
00095     nsCAutoString flag;
00096 
00097     for (int i = 1; i < argc; ++i) {
00098         nsDependentCString arg(argv[i]);
00099         if (*argv[i] == '-' && arg.Length() > 1) {
00100             // clean up previous flag
00101             if (!flag.IsEmpty()) {
00102                 aOptions.AddEntry(flag);
00103                 flag.Truncate();
00104             }
00105 
00106             // get next flag
00107             flag = Substring(arg, 1, arg.Length() - 1);
00108         }
00109         else {
00110             txOptionEntry* option = aOptions.AddEntry(flag);
00111             if (option) {
00112                 option->mValues.AppendCString(nsCString(arg));
00113             }
00114             flag.Truncate();
00115         }
00116     }
00117 
00118     if (!flag.IsEmpty()) {
00119         aOptions.AddEntry(flag);
00120     }
00121 }
00122 
00126 int main(int argc, char** argv)
00127 {
00128     using namespace std;
00129     nsresult rv;
00130 #ifdef MOZ_JPROF
00131     setupProfilingStuff();
00132 #endif
00133     rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
00134     NS_ENSURE_SUCCESS(rv, rv);
00135 
00136     if (!txXSLTProcessor::init())
00137         return 1;
00138 
00139     txOptions options;
00140     if (NS_FAILED(options.Init(4))) {
00141         return 1;
00142     }
00143     parseCommandLine(argc, argv, options);
00144 
00145     if (!options.GetEntry(NS_LITERAL_CSTRING("q"))) {
00146         NS_NAMED_LITERAL_CSTRING(copyright, "(C) 1999 The MITRE Corporation, Keith Visco, and contributors");
00147         cerr << "TransforMiiX ";
00148         cerr << MOZILLA_VERSION << endl;
00149         cerr << copyright.get() << endl;
00150         //-- print banner line
00151         PRUint32 fillSize = copyright.Length() + 1;
00152         PRUint32 counter;
00153         for (counter = 0; counter < fillSize; ++counter)
00154             cerr << '-';
00155         cerr << endl << endl;
00156     }
00157 
00158     if (options.GetEntry(NS_LITERAL_CSTRING("h"))) {
00159         printHelp();
00160         return 0;
00161     }
00162 
00163     //-- handle output stream
00164     ostream* resultOutput = &cout;
00165     ofstream resultFileStream;
00166 
00167     txOptionEntry* option = options.GetEntry(NS_LITERAL_CSTRING("o"));
00168     if (option &&
00169         option->mValues.Count() > 0 &&
00170         !option->mValues[0]->EqualsLiteral("-")) {
00171         resultFileStream.open(option->mValues[0]->get(), ios::out);
00172         if (!resultFileStream) {
00173             cerr << "error opening output file: ";
00174             cerr << option->mValues[0]->get() << endl;
00175             return -1;
00176         }
00177         resultOutput = &resultFileStream;
00178     }
00179 
00180     option = options.GetEntry(NS_LITERAL_CSTRING("i"));
00181     if (!option || option->mValues.Count() == 0) {
00182         cerr << "you must specify at least a source XML path" << endl;
00183         printUsage();
00184         return -1;
00185     }
00186 
00187     SimpleErrorObserver obs;
00188     txStandaloneXSLTProcessor proc;
00189 
00190     txOptionEntry* styleOption = options.GetEntry(NS_LITERAL_CSTRING("s"));
00191     if (!styleOption || styleOption->mValues.Count() == 0) {
00192         rv = proc.transform(*option->mValues[0], *resultOutput, obs);
00193     }
00194     else {
00195         // XXX TODO: Handle multiple stylesheets
00196         rv = proc.transform(*option->mValues[0], *styleOption->mValues[0],
00197                             *resultOutput, obs);
00198     }
00199 
00200     if (NS_FAILED(rv)) {
00201         cerr << "transformation failed with " << hex << rv << endl;
00202     }
00203 
00204     resultFileStream.close();
00205     txXSLTProcessor::shutdown();
00206     rv = NS_ShutdownXPCOM(nsnull);
00207     NS_ENSURE_SUCCESS(rv, rv);
00208     return 0;
00209 }