package textbender.a.u.encoding; // Copyright 2007, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Textbender Software"), to deal in the Textbender Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Textbender Software, and to permit persons to whom the Textbender Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Textbender Software. THE TEXTBENDER SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE TEXTBENDER SOFTWARE OR THE USE OR OTHER DEALINGS IN THE TEXTBENDER SOFTWARE. import java.io.*; import javax.xml.transform.*; import javax.xml.transform.stream.*; import org.w3c.dom.*; import org.w3c.dom.ls.*; import textbender.d.gene.*; import textbender.d.gene.xhtml.*; import textbender.g.lang.*; import textbender.g.xml.dom.*; /** A run of the genetic encoder. * The single instance of Run is available via Run.i(). */ public @ThreadSafe final class Run { private Run() {} /** The single instance of Run. */ public static Run i() { return instance; } /** Starts the run from the command line. * * @param argumentArray of length zero (there ought to be no arguments) * * @throws IllegalStateException if called more than once */ public static void main( String[] argumentArray ) { if( argumentArray.length > 0 ) throw new IllegalArgumentException( "expecting no arguments, found: " + argumentArray[0] ); // LoggerX.test( LoggerX.i( Run.class )); // PRIVATE final TransformerFactory transformerFactory = TransformerFactory.newInstance(); final PrintWriter errorPrintWriter = new PrintWriter( System.err ); final DOMErrorHandlerPW errorHandler = new DOMErrorHandlerPW( errorPrintWriter ); try { Document document = null; // till read try{ document = Encoder.encoded( System.in, transformerFactory, errorHandler ); } catch( LSException x ) // reported via errorPrintWriter, no need to throw { errorPrintWriter.println(); errorPrintWriter.flush(); // could have made the PrintWriter autoflush, but (unlike PrintStream) it does not autoflush on all newlines, making its behaviour unpredictable assert errorHandler.count() > 0; System.exit( 1 ); } RecombinantXHTML.write( document, transformerFactory, new StreamResult(System.out) ); } catch( RuntimeException xR ) { throw xR; } catch( Exception x ) { printShortMessageAndExit( x ); } } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private static void printShortMessageAndExit( Throwable x ) { System.err.print( '\n' ); System.err.println( x ); // short message System.exit( 1 ); } ////////////////////////////////////////////////////////////////////////////////////////// // Last, so static fields above initialize, and are available during instantiation below private static final Run instance = new Run(); }