1 package DTDDoc;
2
3 import com.uwyn.jhighlight.highlighter.ExplicitStateHighlighter;
4 import com.uwyn.jhighlight.renderer.XhtmlRenderer;
5 import com.uwyn.jhighlight.tools.StringUtils;
6
7 import java.io.BufferedReader;
8 import java.io.BufferedWriter;
9 import java.io.IOException;
10 import java.io.Reader;
11 import java.io.StringReader;
12 import java.io.Writer;
13 import java.util.HashMap;
14 import java.util.Map;
15
16 public class DtdXhtmlRenderer extends XhtmlRenderer {
17 public final static Map DEFAULT_CSS = new HashMap() {
18 {
19 put("h1", "font-family: sans-serif; " + "font-size: 16pt; "
20 + "font-weight: bold; " + "color: rgb(0,0,0); "
21 + "background: rgb(210,210,210); "
22 + "border: solid 1px black; " + "padding: 5px; "
23 + "text-align: center;");
24
25 put("code", "color: rgb(0,0,0); " + "font-family: monospace; "
26 + "font-size: 12px; " + "white-space: nowrap;");
27
28 put(".dtd_plain", "color: rgb(0,0,0);");
29
30 put(".dtd_char_data", "color: rgb(0,0,0);");
31
32 put(".dtd_tag_symbols", "color: rgb(0,59,255);");
33
34 put(".dtd_comment", "color: rgb(147,147,147); "
35 + "background-color: rgb(247,247,247);");
36
37 put(".dtd_attribute_value", "color: rgb(193,0,0);");
38
39 put(".dtd_attribute_name", "color: rgb(0,0,0); "
40 + "font-weight: bold;");
41
42 put(".dtd_processing_instruction", "color: rgb(0,0,0); "
43 + "font-weight: bold; " + "font-style: italic;");
44
45 put(".dtd_tag_name", "color: rgb(0,55,255);");
46
47 put(".dtd_dtddoc_tag", "color: rgb(0,55,255);");
48 }
49 };
50
51 protected Map getDefaultCssStyles() {
52 return DEFAULT_CSS;
53 }
54
55 protected String getCssClass(int style) {
56 switch (style) {
57 case DtdHighlighter.PLAIN_STYLE:
58 return "dtd_plain";
59 case DtdHighlighter.CHAR_DATA:
60 return "dtd_char_data";
61 case DtdHighlighter.TAG_SYMBOLS:
62 return "dtd_tag_symbols";
63 case DtdHighlighter.COMMENT:
64 return "dtd_comment";
65 case DtdHighlighter.ATTRIBUTE_VALUE:
66 return "dtd_attribute_value";
67 case DtdHighlighter.ATTRIBUTE_NAME:
68 return "dtd_attribute_name";
69 case DtdHighlighter.PROCESSING_INSTRUCTION:
70 return "dtd_processing_instruction";
71 case DtdHighlighter.TAG_NAME:
72 return "dtd_tag_name";
73 case DtdHighlighter.DTDDOC_TAG_STYLE:
74 return "dtd_dtddoc_tag";
75 case DtdHighlighter.KEYWORD:
76 return "dtd_keyword";
77 }
78
79 return null;
80 }
81
82 protected ExplicitStateHighlighter getHighlighter() {
83 return new DtdHighlighter();
84 }
85
86 public void highlight(Reader in, Writer out) throws IOException {
87 ExplicitStateHighlighter highlighter = getHighlighter();
88
89 BufferedReader r = new BufferedReader(in);
90 BufferedWriter w = new BufferedWriter(out);
91
92 String line;
93 String token;
94 int length;
95 int style;
96 String css_class;
97 int previous_style = 0;
98 boolean newline = false;
99 while ((line = r.readLine()) != null) {
100 line += "\n";
101 line = StringUtils.convertTabsToSpaces(line, 4);
102
103
104 Reader lineReader = new StringReader(line);
105 highlighter.setReader(lineReader);
106 int index = 0;
107 while (index < line.length()) {
108 style = highlighter.getNextToken();
109 length = highlighter.getTokenLength();
110 token = StringUtils.replace(line.substring(index, index
111 + length), "\n", "");
112
113 if ((style != previous_style || newline) && token.length() > 0) {
114 css_class = getCssClass(style);
115
116 if (css_class != null) {
117 if (previous_style != 0 && !newline) {
118 w.write("</span>");
119 }
120 w.write("<span class=\"" + css_class + "\">");
121
122 previous_style = style;
123 }
124 }
125 newline = false;
126 w.write(StringUtils.encodeHtml(token));
127
128 index += length;
129 }
130
131 if (previous_style != 0) {
132 w.write("</span>");
133 }
134 w.write("\n");
135 newline = true;
136 previous_style = 0;
137 }
138 w.flush();
139 }
140
141 }