001 package de.java2html.converter;
002 
003 import java.io.BufferedWriter;
004 import java.io.IOException;
005 
006 import de.java2html.javasource.SourceRun;
007 import de.java2html.javasource.SourceType;
008 import de.java2html.javasource.TypedSource;
009 import de.java2html.javasource.TypedSourceIterator;
010 import de.java2html.options.JavaSourceConversionOptions;
011 import de.java2html.options.JavaSourceStyleTable;
012 import de.java2html.util.HtmlUtilities;
013 
014 /**
015  @author Markus Gebhard
016  */
017 public abstract class AbstractJavaSourceToXmlConverter extends AbstractJavaSourceConverter {
018 
019   //original style sheet by Jan Tisje
020   // "td.java, td.java-ln {vertical-align:top;}\n" +
021   // "tt.java, tt.java-ln, pre.java, pre.java-ln {line-height:1em; margin-bottom:0em;}\n" +
022   // "td.java-ln { text-align:right; }\n"+
023   // "tt.java-ln, pre.java-ln { color:#888888 }\n"+
024   // "/* UNDEFINED       */ span.java0  { color:black; }\n" +
025   // "/* CODE            */ span.java1  { color:black; }\n" +
026   // "/* CODE_KEYWORD    */ span.java2  { color:black; font-weight:bold; }\n" +
027   // "/* CODE_TYPE       */ span.java3  { color:darkblue ;}\n" +
028   // "/* QUOTE           */ span.java4  { color:darkgreen; }\n" +
029   // "/* COMMENT_LINE    */ span.java5  { color:#888888; font-style:italic; }\n" +
030   // "/* COMMENT_BLOCK   */ span.java6  { color:#888888; font-style:italic; }\n" +
031   // "/* COMMENT_JAVADOC */ span.java7  { color:green; font-style:italic; }\n" +
032   // "/* COMMENT_KEYWORD */ span.java8  { color:green; font-style:italic; font-weight:bold; }\n" +
033   // "/* EMPTY           */ span.java9  {}\n" +
034   // "/* NUM_CONSTANT    */ span.java10 { color:orange; }\n" +
035   // "/* CHAR_CONSTANT   */ span.java11 { color:red; } ";
036 
037   public final static String XML_HEADER = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
038 
039   private boolean pre = true;
040   private String lineEnd = ""//$NON-NLS-1$
041   private String space = " "//$NON-NLS-1$
042   private String headEnd = ""//$NON-NLS-1$
043   private String foot = ""//$NON-NLS-1$
044 
045   private final static String TAG_START = "<span class=\"java"//$NON-NLS-1$
046   private final static String TAG_END = "\">"//$NON-NLS-1$
047   private final static String TAG_CLOSE = "</span>"//$NON-NLS-1$
048 
049   public AbstractJavaSourceToXmlConverter(ConverterMetaData metaData) {
050     super(metaData);
051     setWritePre(false);
052   }
053 
054   /** @deprecated As of Sep 13, 2004 (Markus Gebhard) Only options in the 
055    * Java2HtmlConversionOptions are available */
056   @Deprecated
057   public final void setWritePre(boolean pre) {
058     this.pre = pre;
059     this.foot = getFooter();
060     this.headEnd = getHeaderEnd();
061     if (pre) {
062       lineEnd = "";
063       space = " "// normal space
064     }
065     else {
066       lineEnd = "<br />";
067       space = "&#xA0;";
068     }
069   }
070 
071   protected final boolean isPre() {
072     return pre;
073   }
074 
075   protected final String getLineEnd() {
076     return lineEnd;
077   }
078 
079   @Override
080   public String getDocumentHeader(JavaSourceConversionOptions options, String title) {
081     return createHeader(options.getStyleTable(), title, options.getCharset()) + headEnd;
082   }
083 
084   @Override
085   public String getDocumentFooter(JavaSourceConversionOptions options) {
086     return foot;
087   }
088 
089   protected abstract String createHeader(JavaSourceStyleTable styleTable, String title, String charset);
090 
091   protected abstract String getHeaderEnd();
092 
093   protected abstract String getFooter();
094 
095   protected static String createStyleSheet(JavaSourceStyleTable styleTable) {
096     final StringBuffer buffer = new StringBuffer();
097     buffer.append("td.java, td.java-ln {vertical-align:top;}\n"
098         "tt.java, tt.java-ln, pre.java, pre.java-ln {line-height:1em; margin-bottom:0em;}\n"
099         "td.java-ln { text-align:right; }\n"
100         "tt.java-ln, pre.java-ln { color:#888888 }\n");
101 
102     final SourceType[] types = SourceType.getAll();
103     for (int i = 0; i < types.length; i++) {
104       appendStyle(buffer, types[i], styleTable);
105     }
106     return buffer.toString();
107   }
108 
109   private static void appendStyle(StringBuffer buffer, SourceType type, JavaSourceStyleTable styleTable) {
110     buffer.append("/* " + type.getName() "       */ ");
111     buffer.append("span.java" + type.getID() "  { ");
112     buffer.append("font-size: 10pt; ");
113     buffer.append("color:" + styleTable.get(type).getHtmlColor() "; ");
114     if (styleTable.get(type).isBold()) {
115       buffer.append("font-weight:bold; ");
116     }
117     if (styleTable.get(type).isItalic()) {
118       buffer.append("font-style:italic; ");
119     }
120     buffer.append("}\n");
121   }
122 
123   @Override
124   public String getBlockSeparator(JavaSourceConversionOptions options) {
125     return "";
126   }
127 
128   protected final void writeCodeToXml(
129       BufferedWriter writer,
130       TypedSource source,
131       JavaSourceConversionOptions optionsthrows IOException {
132     final TypedSourceIterator iterator = source.getIterator();
133     int lineNumber = 1;
134     while (iterator.hasNext()) {
135       final SourceRun run = iterator.getNext();
136       if (run.isAtStartOfLine()) {
137         if (options.isAddLineAnchors()) {
138           writeLineAnchor(options, writer, lineNumber);
139         }
140         lineNumber++;
141       }
142       if (run.getEndIndex() != run.getStartIndex()) {
143         final String t = HtmlUtilities.encode(
144             run.getCode(),
145             0,
146             run.getEndIndex() - run.getStartIndex() 1,
147             "\r\n ");
148 
149         writer.write(TAG_START + run.getType().getID() + TAG_END);
150         for (int i = 0; i < t.length(); ++i) {
151           final char ch = t.charAt(i);
152           if (ch == ' ') {
153             if ((i < t.length() 1&& (t.charAt(i + 1== ' ')) {
154               writer.write(space);
155             }
156             else {
157               writer.write(" ");
158             }
159           }
160           else if (ch != '\r') {
161             writer.write(ch);
162           }
163         }
164         writer.write(TAG_CLOSE);
165       }
166       if (run.isAtEndOfLine() && iterator.hasNext()) {
167         writer.write(lineEnd);
168         writer.write("\n");
169       }
170     }
171   }
172 
173   protected final void writeLineAnchor(JavaSourceConversionOptions options, BufferedWriter writer, int lineNumber)
174       throws IOException {
175     writeLineAnchorStart(options, writer, lineNumber);
176     writeLineAnchorEnd(writer);
177   }
178 
179   private void writeLineAnchorEnd(BufferedWriter writerthrows IOException {
180     writer.write("</a>");
181   }
182 
183   private void writeLineAnchorStart(JavaSourceConversionOptions options, BufferedWriter writer, int lineNumber)
184       throws IOException {
185     writer.write("<a name=\"");
186     writer.write(options.getLineAnchorPrefix() + lineNumber);
187     writer.write("\">");
188   }
189 }