001 package de.tisje.java2html;
002 
003 import java.io.File;
004 import java.io.FileWriter;
005 import java.io.IOException;
006 import java.io.StringWriter;
007 
008 import de.java2html.converter.AbstractJavaSourceToXmlConverter;
009 import de.java2html.converter.JavaSource2XhtmlConverter;
010 import de.java2html.converter.JavaSource2XmlConverter;
011 import de.java2html.javasource.TypedSource;
012 import de.java2html.javasource.JavaSourceParser;
013 import de.java2html.options.JavaSourceConversionOptions;
014 import de.java2html.util.IoUtilities;
015 
016 /**
017  * This class is an interface between XSL and Java2Html.
018  * Before invoking, a namespace def must be added to the <code>xsl:stylesheet</code> tag:<br>
019  <code>xmlns:j2h="de.tisje.java2html.XsltTask"</code><br>
020  * After that, it may be used this way:
021  <pre>
022  *    &lt;xsl:value-of select="j2h:setSource(.)"/>
023  *    &lt;xsl:value-of select="j2h:writeFile('temp.xml')"/>
024  *    &lt;xsl:copy-of select="document('temp.xml')"/>
025  </pre>
026  *
027  @author <a href="mailto:Jan.Tisje@gmx.de">Jan Tisje</a>
028  @version 1.0
029  */
030 //TODO Mar 11, 2004 (Markus Gebhard): This class urgently needs refactoring. Static instance variables - arg...
031 public class XsltTask {
032 
033   private static final JavaSourceConversionOptions options = JavaSourceConversionOptions.getDefault();
034   private static AbstractJavaSourceToXmlConverter converter = new JavaSource2XhtmlConverter();
035   private static TypedSource source = null;
036   private static boolean xhtml = false;
037 
038   /**
039    * set options from xsl.
040    @param lineNumbers if line numbers should be in the output code.
041    @param pre if output code should be formatted using non-breaking spaces and &lt;br>.
042    @param xhtml if output should be viewable stand-alone.
043    */
044   public static void setOptions(boolean lineNumbers, boolean pre, boolean xhtml) {
045     if (xhtml) {
046       converter = new JavaSource2XhtmlConverter();
047     }else {
048       converter = new JavaSource2XmlConverter();
049     }
050     options.setShowLineNumbers(lineNumbers);
051     converter.setWritePre(pre);
052     XsltTask.xhtml = xhtml;
053   }
054 
055   /** hand over java source read from main xml file. */
056   public static void setSource(String javaSource) {
057     source = new JavaSourceParser().parse(javaSource);
058   }
059 
060   /** read java source from file. */
061   public static void readFile(String javaFilethrows IOException {
062     source = new JavaSourceParser().parse(new File(javaFile));
063   }
064 
065   /** return java source in text form, html codes will be escaped. */
066   public static String getSource() throws IOException {
067     final StringWriter writer = new StringWriter();
068     writer.write(converter.getDocumentHeader(options, ""))//$NON-NLS-1$
069     converter.convert(source, options, writer);
070     writer.write(converter.getDocumentFooter(options));
071     return writer.getBuffer().toString();
072   }
073 
074   /** output file to a separate xml file, less problems. 
075    @deprecated As of Mar 11, 2004 (Markus Gebhard), replaced by {@link #writeFile(File)}*/
076   @Deprecated
077   public static void writeFile(String filenamethrows IOException {
078     writeFile(new File(filename));
079   }
080 
081   /** output file to a separate xml file */ 
082   public static void writeFile(File filethrows IOException {
083     final FileWriter fileWriter = new FileWriter(file);
084     try {
085       fileWriter.write(converter.getDocumentHeader(options, ""))//$NON-NLS-1$
086       converter.convert(source, options, fileWriter);
087       fileWriter.write(converter.getDocumentFooter(options));
088     }
089     finally {
090       IoUtilities.close(fileWriter);
091     }
092   }
093 
094   /** use this class like a common comandline tool.
095    changing of options is not supported, yet */
096   public static void main(String args[]) {
097     String ext = ".xhtml"//$NON-NLS-1$
098     if (!xhtml) {
099       ext = ".xml"//$NON-NLS-1$
100     }
101     for (int i = 0; args.length > i; i++) {
102       try {
103         readFile(args[i]);
104         writeFile(args[i+ ext);
105       }
106       catch (final IOException e) {
107         e.printStackTrace();
108       }
109     }
110   }
111 }