View Javadoc

1   /*
2    * $Id$
3    *
4    * The contents of this file are subject to the Mozilla Public License 
5    * Version 1.1 (the "License"); you may not use this file except in 
6    * compliance with the License. You may obtain a copy of the License at 
7    * http://www.mozilla.org/MPL/ 
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis, 
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
11   * for the specific language governing rights and limitations under the License.
12   *
13   * The Original Code is XML Hammer code. (org.xmlhammer.*)
14   *
15   * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
16   * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
17   * All Rights Reserved.
18   *
19   * Contributor(s): Edwin Dankert <edankert@gmail.com>
20   */
21  
22  package org.xmlhammer.gui.output;
23  
24  import java.text.DateFormat;
25  import java.util.ArrayList;
26  import java.util.Date;
27  import java.util.List;
28  
29  import org.xmlhammer.Identity;
30  
31  public class SourceBuilder {
32      private StringBuilder builder = null;
33      
34      public static String COMMENT_START = "<FONT COLOR=\"#3F7F5F\"><I>";
35      public static String COMMENT_END = "</I></FONT>";
36      
37      public static String RESERVED_START = "<FONT COLOR=\"#7F0055\">";
38      public static String RESERVED_END = "</FONT>";
39  
40      public static String CONSTANT_START = "<FONT COLOR=\"#0000C0\">";
41      public static String CONSTANT_END = "</FONT>";
42      
43      
44      public SourceBuilder() {
45          reset();
46      }
47      
48      public static void appendImport(StringBuilder builder, String statement) {
49          appendReserved(builder, "import ");
50          builder.append(statement);
51          builder.append(";\n");
52      }
53  
54      public static void appendCatch(StringBuilder builder, String indent, String exception) {
55          builder.append(indent);
56          builder.append("} ");
57          appendReserved(builder, "catch ");
58          builder.append("(");
59          builder.append(exception);
60          builder.append(" e) {\n");
61          builder.append(indent);
62          builder.append("    e.printStackTrace();\n");
63      }
64  
65      public static void appendComment(StringBuilder builder, String comment) {
66          builder.append(COMMENT_START);
67          builder.append(comment);
68          builder.append(COMMENT_END);
69      }
70  
71      public void appendComment(String comment) {
72          builder.append(COMMENT_START);
73          builder.append(comment);
74          builder.append(COMMENT_END);
75      }
76      
77      public static void appendReserved(StringBuilder builder, String reserved) {
78          builder.append(RESERVED_START);
79          builder.append(reserved);
80          builder.append(RESERVED_END);
81      }
82  
83      public void appendReserved(String reserved) {
84          builder.append(RESERVED_START);
85          builder.append(reserved);
86          builder.append(RESERVED_END);
87      }
88  
89      public static void appendConstant(StringBuilder builder, String constant) {
90          builder.append(CONSTANT_START);
91          builder.append(constant);
92          builder.append(CONSTANT_END);
93      }
94  
95      public void appendConstant(String constant) {
96          builder.append(CONSTANT_START);
97          builder.append(constant);
98          builder.append(CONSTANT_END);
99      }
100 
101     public static void appendConstant(StringBuilder builder, boolean constant) {
102         builder.append(CONSTANT_START);
103         builder.append(constant);
104         builder.append(CONSTANT_END);
105     }
106 
107     public void appendConstant(boolean constant) {
108         builder.append(CONSTANT_START);
109         builder.append(constant);
110         builder.append(CONSTANT_END);
111     }
112 
113     public void addCode(String code) {
114         builder.append(code);
115     }
116 
117     private void setHeader() {
118         builder.append(COMMENT_START);
119         builder.append("/*\n");
120         builder.append(" * Generated by <a href=\"http://www.xmlhammer.org\">XML Hammer ");
121         builder.append(Identity.getInstance().getVersion());
122         builder.append("</a> at ");
123         builder.append(DateFormat.getDateTimeInstance().format(new Date()));
124         builder.append("\n");
125         builder.append(" */\n");
126         builder.append(COMMENT_END);
127     }
128 
129     public void appendDisclaimer() {
130         builder.append(COMMENT_START);
131         builder.append("\n\n/*\n");
132         builder.append(" * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR\n");
133         builder.append(" * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n");
134         builder.append(" * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN\n");
135         builder.append(" * NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,\n");
136         builder.append(" * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n");
137         builder.append(" * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,\n");
138         builder.append(" * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n");
139         builder.append(" * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\n");
140         builder.append(" * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,\n");
141         builder.append(" * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n");
142         builder.append(" */\n");
143         builder.append(COMMENT_END);
144     }
145 
146     public String toString() {
147         String value = builder.toString();
148         
149         List<String> imports = getImports(value);
150         
151         for (String importClass : imports) {
152             value = replaceReferences(value, importClass);
153         }
154         
155         return value;
156     }
157     
158     private List<String> getImports(String value) {
159         List<String> imports = new ArrayList<String>();
160         
161         int index = value.indexOf("import </FONT>");
162         while (index != -1) {
163             int start = index + "import </FONT>".length();
164             int end = value.indexOf(';', start);
165             
166             imports.add(value.substring(start, end));
167             
168             index = value.indexOf("import </FONT>", end);
169         }
170         
171         return imports;
172     }
173 
174     private String replaceReferences(String value, String importClass) {
175         String simple = importClass.substring(importClass.lastIndexOf('.')+1);
176         String reference = importClass.replace('.', '/')+".html";
177         
178         value = value.replaceAll(importClass+";", "<a href=\"http://java.sun.com/j2se/1.5.0/docs/api/"+reference+"\">"+importClass+"</a>;");
179 
180         value = value.replaceAll(" "+simple+" ", " <a href=\"http://java.sun.com/j2se/1.5.0/docs/api/"+reference+"\">"+simple+"</a> ");
181         value = value.replaceAll(" "+simple+"//.", " <a href=\"http://java.sun.com/j2se/1.5.0/docs/api/"+reference+"\">"+simple+"</a>.");
182         value = value.replaceAll("//("+simple+" ", "(<a href=\"http://java.sun.com/j2se/1.5.0/docs/api/"+reference+"\">"+simple+"</a> ");
183         value = value.replaceAll(" "+simple+"<", " <a href=\"http://java.sun.com/j2se/1.5.0/docs/api/"+reference+"\">"+simple+"</a><");
184         value = value.replaceAll(" "+simple+"//(", " <a href=\"http://java.sun.com/j2se/1.5.0/docs/api/"+reference+"\">"+simple+"</a>(");
185         value = value.replaceAll("//("+simple+"//)", "(<a href=\"http://java.sun.com/j2se/1.5.0/docs/api/"+reference+"\">"+simple+"</a>)");
186         value = value.replaceAll(" "+simple+"&", " <a href=\"http://java.sun.com/j2se/1.5.0/docs/api/"+reference+"\">"+simple+"</a>&");
187         
188         return value;
189     }
190     
191     public void reset() {
192         builder = new StringBuilder("<HTML><BODY LINK=\"#339933\" ALINK=\"#333399\"><CODE><PRE>");
193         setHeader();
194     }
195 }