org.odftoolkit.odfxsltrunner.Main Maven / Gradle / Ivy
The newest version!
/************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
*
* Copyright 2008, 2010 Oracle and/or its affiliates. All rights reserved.
*
* Use is subject to license terms.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0. You can also
* obtain a copy of the License at http://odftoolkit.org/docs/license.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*
************************************************************************/
package org.odftoolkit.odfxsltrunner;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
/**
* This application applies an XSLT style sheet to an ODF file. To run this application from the command line without Ant, try:
* java -jar "[path/]odfxsltrunner.jar".
*/
public class Main {
private static final String VERSION="1.1.0";
/** Creates a new instance of Main */
public Main() {
}
/**
* @param aArgs the command line arguments
*/
public static void main(String[] aArgs) {
String aStyleSheetName = null;
String aInputName = null;
String aOutputName = null;
String aPath = "content.xml";
String aLogFileName = null;
String aTransformerFactoryClass = null;
int aInputMode = ODFXSLTRunner.INPUT_MODE_PACKAGE;
int aOutputMode = ODFXSLTRunner.OUTPUT_MODE_COPY_INPUT_PACKAGE;
Vector aParams = null;
Vector aExtractFileNames = null;
int nLogLevel = CommandLineLogger.ERROR;
boolean bCommandLineValid = true;
List aArgList = Arrays.asList(aArgs);
Iterator aArgIter = aArgList.iterator();
while( aArgIter.hasNext() && bCommandLineValid )
{
String aArg = aArgIter.next();
if( aArg.equals("-f") )
{
if( aArgIter.hasNext() )
aTransformerFactoryClass = aArgIter.next();
else
bCommandLineValid = false;
}
else if( aArg.equals("-i") )
{
aInputMode = ODFXSLTRunner.INPUT_MODE_FILE;
}
else if( aArg.equals("-l") )
{
if( aArgIter.hasNext() )
aLogFileName = aArgIter.next();
else
bCommandLineValid = false;
}
else if( aArg.equals("-o") )
{
if( aOutputMode == ODFXSLTRunner.OUTPUT_MODE_COPY_INPUT_PACKAGE )
aOutputMode = ODFXSLTRunner.OUTPUT_MODE_FILE;
else
bCommandLineValid = false;
}
else if( aArg.equals("-p") )
{
if( aArgIter.hasNext() )
aPath = aArgIter.next();
else
bCommandLineValid = false;
}
else if( aArg.equals("-r") )
{
if( aOutputMode == ODFXSLTRunner.OUTPUT_MODE_COPY_INPUT_PACKAGE )
aOutputMode = ODFXSLTRunner.OUTPUT_MODE_REPLACE_INPUT_PACKAGE;
else
bCommandLineValid = false;
}
else if( aArg.equals("-t") )
{
if( aOutputMode == ODFXSLTRunner.OUTPUT_MODE_COPY_INPUT_PACKAGE )
aOutputMode = ODFXSLTRunner.OUTPUT_MODE_TEMPLATE_PACKAGE;
else
bCommandLineValid = false;
}
else if( aArg.equals("-v") )
{
nLogLevel = CommandLineLogger.INFO;
}
else if( aArg.equals("-x") )
{
if( aArgIter.hasNext() )
{
if( aExtractFileNames == null )
aExtractFileNames = new Vector();
aExtractFileNames.add( aArgIter.next() );
}
else
bCommandLineValid = false;
}
else if( aArg.startsWith("-") )
{
System.out.print(aArg);
System.out.println(": unknown option, use '-' for help");
bCommandLineValid = false;
break;
}
else if( aStyleSheetName == null )
{
aStyleSheetName = aArg;
}
else if( aInputName == null )
{
aInputName = aArg;
}
else if( aOutputMode != ODFXSLTRunner.OUTPUT_MODE_REPLACE_INPUT_PACKAGE && aOutputName == null )
{
aOutputName = aArg;
}
else if( aArg.indexOf('=') != -1 )
{
if( aParams == null )
aParams = new Vector();
aParams.add(new XSLTCommandLineParameter(aArg) );
}
else
{
bCommandLineValid = false;
break;
}
}
if( aOutputMode == ODFXSLTRunner.OUTPUT_MODE_COPY_INPUT_PACKAGE &&
aOutputName == null )
aOutputMode = ODFXSLTRunner.OUTPUT_MODE_STDOUT;
// check usage
bCommandLineValid = aInputName != null && aStyleSheetName != null;
bCommandLineValid = bCommandLineValid &&
!(aOutputMode == ODFXSLTRunner.OUTPUT_MODE_REPLACE_INPUT_PACKAGE || aOutputMode == ODFXSLTRunner.OUTPUT_MODE_STDOUT) == (aOutputName != null);
// print help
if( !bCommandLineValid )
{
printUsage();
return;
}
PrintStream aLogStream = null;
if( aLogFileName != null )
{
try
{
aLogStream = new PrintStream( aLogFileName );
}
catch( FileNotFoundException e )
{
System.err.println(e.getMessage());
}
}
boolean bError = false;
try
{
ODFXSLTRunner aRunner = new ODFXSLTRunner();
Logger aLogger = new CommandLineLogger( aLogStream!=null ? aLogStream : System.err, nLogLevel );
bError = aRunner.runXSLT( aStyleSheetName, aParams, aInputName, aInputMode, aOutputName, aOutputMode, aPath, aTransformerFactoryClass, aExtractFileNames, aLogger );
}
catch( Exception e )
{
System.err.println(e.getMessage());
}
// if( bError )
// System.err.println("transformation failed");
if( aLogStream != null )
aLogStream.close();
}
private static void printUsage()
{
System.out.print("odfxsltrunner v");
System.out.println( VERSION );
System.out.println();
System.out.println( "usage: odfxsltrunner