KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > editor > indentation > programmatic > IndentCoreGenerator


1 /*
2  * IndentCoreGenerator.java
3  *
4  * Created on March 16, 2002, 5:50 PM
5  */

6
7 package org.netbeans.test.editor.indentation.programmatic;
8
9 import java.util.Vector JavaDoc;
10 import java.util.Map JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
13 import org.w3c.dom.Document JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import javax.xml.parsers.DocumentBuilder JavaDoc;
16 import org.xml.sax.EntityResolver JavaDoc;
17 import java.io.InputStream JavaDoc;
18 import org.xml.sax.InputSource JavaDoc;
19 import java.io.File JavaDoc;
20 import java.io.Writer JavaDoc;
21 import java.io.FileWriter JavaDoc;
22
23 /**
24  *
25  * @author lahvac
26  */

27 public class IndentCoreGenerator {
28     
29     /** Creates a new instance of IndentCoreGenerator */
30     public IndentCoreGenerator() {
31     }
32     
33     /**
34      * @param args the command line arguments
35      */

36     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
37         String JavaDoc generatorLocation = "/home/lahvac/31/tests/editor/test/qa-functional/src/org/netbeans/test/editor/indentation/programmatic";
38         cases = new Vector JavaDoc();
39         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
40         
41         factory.setValidating(true);
42         
43         DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
44         
45         builder.setEntityResolver(new EntityResolver JavaDoc() {
46             public InputSource JavaDoc resolveEntity(String JavaDoc a, String JavaDoc b) {
47                 return new InputSource JavaDoc(IndentCoreGenerator.class.getResourceAsStream("definition.dtd"));
48             }
49         });
50
51         Document JavaDoc doc = builder.parse(IndentCoreGenerator.class.getResourceAsStream("definition.xml"), generatorLocation);
52         DefinitionScanner scanner = new DefinitionScanner(doc);
53
54         scanner.visitDocument();
55
56         Writer JavaDoc output = new FileWriter JavaDoc(new File JavaDoc(generatorLocation, "IndentCorePerformer.java"));
57         
58         output.write("package " + IndentCoreGenerator.class.getPackage().getName() + ";\n");
59         output.write("import java.io.*;\n");
60         output.write("import java.util.*;\n");
61         output.write("import org.netbeans.junit.NbTestCase;\n");
62         output.write("import org.netbeans.junit.NbTestSuite;\n");
63     output.write("import java.io.File;\n");
64         output.write("public class IndentCorePerformer extends NbTestCase {\n");
65         output.write(" public IndentCorePerformer(String testCase) {\n");
66         output.write(" super(testCase);\n");
67         output.write(" }\n");
68     output.write(" public void tearDown() throws Exception {\n");
69     output.write(" assertFile(\"Output does not match golden file.\", getGoldenFile(), new File(getWorkDir(), this.getName() + \".ref\"), null, new org.netbeans.test.editor.LineDiff(false));\n");
70     output.write(" }\n");
71         
72         Iterator JavaDoc iterator = cases.iterator();
73
74         while (iterator.hasNext()) {
75             TestCase testCase = (TestCase) iterator.next();
76
77             output.write(testCase.generate());
78         }
79         output.write("}\n");
80         output.close();
81     }
82     
83     private static Vector JavaDoc cases;
84     
85     public static void addTestCase(TestCase testCase) {
86         cases.add(testCase);
87     }
88     
89     public static class TestCase {
90         private String JavaDoc name;
91         private String JavaDoc textLocation;
92         private String JavaDoc textMIMEType;
93         private Map JavaDoc indentProperties;
94         private boolean willFail;
95         private String JavaDoc failReason;
96         
97         public TestCase() {
98             failReason = "";
99             name = "";
100             textLocation = "";
101             textMIMEType = "";
102             indentProperties = new HashMap JavaDoc();
103             willFail = false;
104         }
105         
106         public Map JavaDoc getIndentProperties() {
107             return indentProperties;
108         }
109         
110         public void addIndentProperty(String JavaDoc name, String JavaDoc value) {
111             indentProperties.put(name, value);
112         }
113         
114         public String JavaDoc generate() {
115             StringBuffer JavaDoc result = new StringBuffer JavaDoc();
116             
117             result.append(" public void test");
118             result.append(getName());
119             result.append("() throws Exception {\n");
120             result.append(" PrintWriter log = null;\n");
121             result.append(" PrintWriter ref = null;\n");
122             result.append(" try {\n");
123             result.append(" log = new PrintWriter(getLog());\n");
124             result.append(" ref = new PrintWriter(getRef());\n");
125             result.append(" Map indentationProperties = new HashMap();\n");
126             for (Iterator JavaDoc iterator = getIndentProperties().keySet().iterator();
127                  iterator.hasNext();
128                  ) {
129                 String JavaDoc key = (String JavaDoc) iterator.next();
130                 
131                 result.append(" indentationProperties.put(\"" + key + "\", \"" + getIndentProperties().get(key).toString() + "\");\n");
132             }
133             result.append(" new IndentCore().run(log, ref, \"" + getTextLocation() + "\", \"" + getTextMIMEType() + "\", indentationProperties);\n");
134             result.append(" } finally {\n");
135             result.append(" log.flush();\n");
136             result.append(" ref.flush();\n");
137             result.append(" }\n");
138             result.append(" }\n");
139             
140             return result.toString();
141         }
142         
143         /** Getter for property name.
144          * @return Value of property name.
145          */

146         public java.lang.String JavaDoc getName() {
147             return name;
148         }
149         
150         /** Setter for property name.
151          * @param name New value of property name.
152          */

153         public void setName(java.lang.String JavaDoc name) {
154             this.name = name;
155         }
156         
157         /** Getter for property failReason.
158          * @return Value of property failReason.
159          */

160         public java.lang.String JavaDoc getFailReason() {
161             return failReason;
162         }
163         
164         /** Setter for property failReason.
165          * @param failReason New value of property failReason.
166          */

167         public void setFailReason(java.lang.String JavaDoc failReason) {
168             this.failReason = failReason;
169         }
170         
171         /** Getter for property textLocation.
172          * @return Value of property textLocation.
173          */

174         public java.lang.String JavaDoc getTextLocation() {
175             return textLocation;
176         }
177         
178         /** Setter for property textLocation.
179          * @param textLocation New value of property textLocation.
180          */

181         public void setTextLocation(java.lang.String JavaDoc textLocation) {
182             this.textLocation = textLocation;
183         }
184         
185         /** Getter for property textMIMEType.
186          * @return Value of property textMIMEType.
187          */

188         public java.lang.String JavaDoc getTextMIMEType() {
189             return textMIMEType;
190         }
191         
192         /** Setter for property textMIMEType.
193          * @param textMIMEType New value of property textMIMEType.
194          */

195         public void setTextMIMEType(java.lang.String JavaDoc textMIMEType) {
196             this.textMIMEType = textMIMEType;
197         }
198         
199         /** Getter for property willFail.
200          * @return Value of property willFail.
201          */

202         public boolean isWillFail() {
203             return willFail;
204         }
205         
206         /** Setter for property willFail.
207          * @param willFail New value of property willFail.
208          */

209         public void setWillFail(boolean willFail) {
210             this.willFail = willFail;
211         }
212         
213     }
214     
215 }
216
Popular Tags