KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javacc > jjdoc > HTMLGenerator


1
2 /*
3  * Copyright © 2002 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
4  * California 95054, U.S.A. All rights reserved. Sun Microsystems, Inc. has
5  * intellectual property rights relating to technology embodied in the product
6  * that is described in this document. In particular, and without limitation,
7  * these intellectual property rights may include one or more of the U.S.
8  * patents listed at http://www.sun.com/patents and one or more additional
9  * patents or pending patent applications in the U.S. and in other countries.
10  * U.S. Government Rights - Commercial software. Government users are subject
11  * to the Sun Microsystems, Inc. standard license agreement and applicable
12  * provisions of the FAR and its supplements. Use is subject to license terms.
13  * Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
14  * trademarks of Sun Microsystems, Inc. in the U.S. and other countries. This
15  * product is covered and controlled by U.S. Export Control laws and may be
16  * subject to the export or import laws in other countries. Nuclear, missile,
17  * chemical biological weapons or nuclear maritime end uses or end users,
18  * whether direct or indirect, are strictly prohibited. Export or reexport
19  * to countries subject to U.S. embargo or to entities identified on U.S.
20  * export exclusion lists, including, but not limited to, the denied persons
21  * and specially designated nationals lists is strictly prohibited.
22  */

23
24
25 package org.javacc.jjdoc;
26
27 import java.io.PrintWriter JavaDoc;
28 import java.util.Hashtable JavaDoc;
29
30 import org.javacc.parser.*;
31
32 public class HTMLGenerator extends Generator {
33   private Hashtable JavaDoc id_map = new Hashtable JavaDoc();
34   private int id = 1;
35
36   public HTMLGenerator(PrintWriter JavaDoc o) {
37     super(o);
38   }
39
40   private String JavaDoc get_id(String JavaDoc nt) {
41     String JavaDoc i = (String JavaDoc)id_map.get(nt);
42     if (i == null) {
43       i = "prod" + id++;
44       id_map.put(nt, i);
45     }
46     return i;
47   }
48
49   private void println(String JavaDoc s) {
50     print(s + "\n");
51   }
52
53   public void text(String JavaDoc s) {
54     String JavaDoc ss = "";
55     for (int i = 0; i < s.length(); ++i) {
56       if (s.charAt(i) == '<') {
57     ss += "&lt;";
58       } else if (s.charAt(i) == '>') {
59     ss += "&gt;";
60       } else if (s.charAt(i) == '&') {
61     ss += "&amp;";
62       } else {
63     ss += s.charAt(i);
64       }
65     }
66     print(ss);
67   }
68
69   public void print(String JavaDoc s) {
70     ostr.print(s);
71   }
72
73   public void specialTokens(String JavaDoc s) {
74     if (!JJDocOptions.getOneTable()) {
75       println("<PRE>");
76       print(s);
77       println("</PRE>");
78     }
79   }
80
81   public void documentStart() {
82     println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">");
83     println("<HTML>");
84     println("<HEAD>");
85     if (JJDocGlobals.input_file != null) {
86       println("<TITLE>BNF for " + JJDocGlobals.input_file + "</TITLE>");
87     } else {
88       println("<TITLE>A BNF grammar by JJDoc</TITLE>");
89     }
90     println("</HEAD>");
91     println("<BODY>");
92     println("<H1 ALIGN=CENTER>BNF for " + JJDocGlobals.input_file + "</H1>");
93   }
94   public void documentEnd() {
95     println("</BODY>");
96     println("</HTML>");
97   }
98
99   public void nonterminalsStart() {
100     println("<H2 ALIGN=CENTER>NON-TERMINALS</H2>");
101     if (JJDocOptions.getOneTable()) {
102       println("<TABLE>");
103     }
104   }
105   public void nonterminalsEnd() {
106     if (JJDocOptions.getOneTable()) {
107       println("</TABLE>");
108     }
109   }
110
111   public void tokensStart() {
112     println("<H2 ALIGN=CENTER>TOKENS</H2>");
113     println("<TABLE>");
114   }
115   public void tokensEnd() {
116     println("</TABLE>");
117   }
118
119   public void javacode(JavaCodeProduction jp) {
120     productionStart(jp);
121     println("<I>java code</I></TD></TR>");
122     productionEnd(jp);
123   }
124
125   public void productionStart(NormalProduction np) {
126     if (!JJDocOptions.getOneTable()) {
127       println("");
128       println("<TABLE ALIGN=CENTER>");
129       println("<CAPTION><STRONG>" + np.lhs + "</STRONG></CAPTION>");
130     }
131     println("<TR>");
132     println("<TD ALIGN=RIGHT VALIGN=BASELINE><A NAME=\"" + get_id(np.lhs) + "\">" + np.lhs + "</A></TD>");
133     println("<TD ALIGN=CENTER VALIGN=BASELINE>::=</TD>");
134     print("<TD ALIGN=LEFT VALIGN=BASELINE>");
135   }
136   public void productionEnd(NormalProduction np) {
137     if (!JJDocOptions.getOneTable()) {
138       println("</TABLE>");
139       println("<HR>");
140     }
141   }
142
143   public void expansionStart(Expansion e, boolean first) {
144     if (!first) {
145       println("<TR>");
146       println("<TD ALIGN=RIGHT VALIGN=BASELINE></TD>");
147       println("<TD ALIGN=CENTER VALIGN=BASELINE>|</TD>");
148       print("<TD ALIGN=LEFT VALIGN=BASELINE>");
149     }
150   }
151   public void expansionEnd(Expansion e, boolean first) {
152     println("</TD>");
153     println("</TR>");
154   }
155
156   public void nonTerminalStart(NonTerminal nt) {
157     print("<A HREF=\"#" + get_id(nt.name) + "\">");
158   }
159   public void nonTerminalEnd(NonTerminal nt) {
160     print("</A>");
161   }
162
163   public void reStart(RegularExpression r) {
164   }
165   public void reEnd(RegularExpression r) {
166   }
167 }
168
Popular Tags