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.JavaSource;
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: * <xsl:value-of select="j2h:setSource(.)"/>
023: * <xsl:value-of select="j2h:writeFile('temp.xml')"/>
024: * <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 JavaSourceConversionOptions options = JavaSourceConversionOptions
034: .getDefault();
035: private static AbstractJavaSourceToXmlConverter converter = new JavaSource2XhtmlConverter();
036: private static JavaSource source = null;
037: private static boolean xhtml = false;
038:
039: /**
040: * set options from xsl.
041: * @param lineNumbers if line numbers should be in the output code.
042: * @param pre if output code should be formatted using non-breaking spaces and <br>.
043: * @param xhtml if output should be viewable stand-alone.
044: */
045: public static void setOptions(boolean lineNumbers, boolean pre,
046: boolean xhtml) {
047: if (xhtml) {
048: converter = new JavaSource2XhtmlConverter();
049: } else {
050: converter = new JavaSource2XmlConverter();
051: }
052: converter.setOptions(lineNumbers, pre);
053: XsltTask.xhtml = xhtml;
054: }
055:
056: /** hand over java source read from main xml file. */
057: public static void setSource(String javaSource) {
058: source = new JavaSourceParser().parse(javaSource);
059: }
060:
061: /** read java source from file. */
062: public static void readFile(String javaFile) throws IOException {
063: source = new JavaSourceParser().parse(new File(javaFile));
064: }
065:
066: /** return java source in text form, html codes will be escaped. */
067: public static String getSource() throws IOException {
068: StringWriter writer = new StringWriter();
069: writer.write(converter.getDocumentHeader(options, "")); //$NON-NLS-1$
070: converter.convert(source, options, writer);
071: writer.write(converter.getDocumentFooter(options));
072: return writer.getBuffer().toString();
073: }
074:
075: /** output file to a separate xml file, less problems.
076: * @deprecated As of Mar 11, 2004 (Markus Gebhard), replaced by {@link #writeFile(File)}*/
077: public static void writeFile(String filename) throws IOException {
078: writeFile(new File(filename));
079: }
080:
081: /** output file to a separate xml file */
082: public static void writeFile(File file) throws IOException {
083: 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: } finally {
089: IoUtilities.close(fileWriter);
090: }
091: }
092:
093: /** use this class like a common comandline tool.
094: changing of options is not supported, yet */
095: public static void main(String args[]) {
096: String ext = ".xhtml"; //$NON-NLS-1$
097: if (!xhtml)
098: ext = ".xml"; //$NON-NLS-1$
099: for (int i = 0; args.length > i; i++) {
100: try {
101: readFile(args[i]);
102: writeFile(args[i] + ext);
103: } catch (IOException e) {
104: e.printStackTrace();
105: }
106: }
107: }
108: }
|