KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > tax > test > Simple


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.tax.test;
20
21 import java.lang.reflect.*;
22 import java.util.*;
23 import java.io.*;
24
25 import org.netbeans.tax.*;
26 import org.netbeans.tax.io.*;
27 import org.netbeans.tax.decl.*;
28
29 /**
30  */

31 public class Simple {
32
33     /** Creates new Tests. */
34     public Simple () {
35     }
36
37     /**
38     * @param args the command line arguments
39     */

40     public static void main (String JavaDoc args[]) throws Exception JavaDoc {
41
42         System.out.println ("Runnig_tests...");
43         testEncodings();
44 // testMerge("tests/merge/merge", "tests/merge/peer", "tests/merge/out"); // NOI18N
45
// testDTDBuilder ("XMLsamples/ParserTest.dtd"); // NOI18N
46
// testDTDBuilder ("xml/simple.dtd"); // NOI18N
47
// testTree();
48
// testAllTree();
49

50         testUtilities();
51     }
52
53     private static boolean testEncodings () {
54         System.out.println ("This test check all 'supported' encoding if they are realy supported!"); // NOI18N
55

56         int invalid = 0;
57         Iterator it = TreeUtilities.getSupportedEncodings().iterator();
58         while (it.hasNext()) {
59             String JavaDoc encoding = (String JavaDoc)it.next();
60             boolean valid = TreeUtilities.isValidDocumentEncoding (encoding);
61             String JavaDoc prefix;
62             String JavaDoc suffix;
63             if ( valid ) {
64                 prefix = " "; // NOI18N
65
suffix = "is valid."; // NOI18N
66
} else {
67                 invalid++;
68                 prefix = "!!! "; // NOI18N
69
suffix = "is *INVALID* !!!"; // NOI18N
70
}
71             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
72             sb.append (prefix).append ("encoding = '").append (encoding).append ("'\t").append (suffix); // NOI18N
73
System.out.println (sb.toString());
74         }
75         
76         if ( invalid != 0 ) {
77             System.out.println ("I found " + invalid + " invalid encodings!"); // NOI18N
78
}
79
80         return ( invalid == 0 );
81     }
82
83
84     private static void testTree () throws Exception JavaDoc {
85         Class JavaDoc clazz = Class.forName ("org.netbeans.tax.TreeAttlistDeclAttributeDef"); // NOI18N
86
clazz = TreeAttlistDeclAttributeDef.class;
87         System.out.println ("AttributeDef.class = " + clazz); // NOI18N
88

89     TreeAttlistDecl attlistDecl = new TreeAttlistDecl ("elemName"); // NOI18N
90
TreeAttlistDeclAttributeDef attrDef;
91         attlistDecl.setAttributeDef (attrDef = new TreeAttlistDeclAttributeDef ("attr1", TreeAttlistDeclAttributeDef.TYPE_CDATA, null, TreeAttlistDeclAttributeDef.DEFAULT_TYPE_FIXED, "value")); // NOI18N
92
println (attlistDecl);
93         System.out.println ("AttributeDef.class = " + attrDef.getClass().getName()); // NOI18N
94

95
96     TreeCharacterReference charRef = new TreeCharacterReference ("#35"); // NOI18N
97
println (charRef);
98     charRef = new TreeCharacterReference ("#x35"); // NOI18N
99
println (charRef);
100
101         TreeDocumentType doctype = createDocumentType();
102         println (doctype);
103         
104         System.out.println("===create=document======="); // NOI18N
105
TreeDocument doc = new TreeDocument("1.0", null, "no"); // NOI18N
106
doc.appendChild (new TreeComment ("==============")); // NOI18N
107
TreeElement elem = new TreeElement ("xxx"); // NOI18N
108
doc.setDocumentElement (elem);
109
110         elem.addAttribute (new TreeAttribute("attr", "value")); // NOI18N
111
elem.addAttribute (new TreeAttribute("attr", "value2")); // NOI18N
112
elem.addAttribute (new TreeAttribute("attr3", "value3")); // NOI18N
113
System.out.println ("CYCLE: elem.appendChild (elem);"); // NOI18N
114

115         elem.appendChild (new TreeComment ("Chtel bych vyzkouset get[Previous|Next]Sibling")); // NOI18N
116
elem.appendChild (new TreeCDATASection ("<!-------------><BLA>")); // NOI18N
117
elem.appendChild (new TreeElement ("BLA")); // NOI18N
118
elem.appendChild (new TreeComment ("... tak jeste zaverecny komentar!")); // NOI18N
119

120         println (doc);
121
122         elem = doc.getDocumentElement();
123         println (elem);
124         TreeChild child = elem.item (1);
125         println (child);
126         child = child.getNextSibling();
127         println (child);
128         
129         //
130
// TreeDTD
131
//
132
TreeDTD dtd = new TreeDTD (null, null);
133 // dtd.appendChild (new TreeElementDecl ("elem", "(xxx,yyy?)")); // NOI18N
134
TreeAttlistDecl attlist = new TreeAttlistDecl ("elem"); // NOI18N
135
attlist.setAttributeDef (new TreeAttlistDeclAttributeDef ("attr", TreeAttlistDeclAttributeDef.TYPE_ID, null, TreeAttlistDeclAttributeDef.DEFAULT_TYPE_REQUIRED, "bla")); // NOI18N
136
dtd.appendChild (attlist);
137
138 // dtd.appendChild (new TreeElementDecl ("elem2", "ANY")); // NOI18N
139
attlist = new TreeAttlistDecl ("elem2"); // NOI18N
140
attlist.setAttributeDef (new TreeAttlistDeclAttributeDef ("attr2", TreeAttlistDeclAttributeDef.TYPE_NOTATION, new String JavaDoc[] { "abc", "def" }, TreeAttlistDeclAttributeDef.DEFAULT_TYPE_NULL, "bla")); // NOI18N
141
dtd.appendChild (attlist);
142
143         println (dtd);
144         child = dtd.item (3);
145         println (child);
146         child = child.getPreviousSibling();
147         println (child);
148         
149     }
150     
151     protected static TreeDocumentType createDocumentType() throws Exception JavaDoc {
152         TreeDocumentType docType = new TreeDocumentType ("element"); // NOI18N
153

154         docType.setSystemId ("xml/simple.dtd"); // NOI18N
155

156         TreeDTD treeDTD = null;
157         System.out.println("TreeDTD: " + XMLStringResult.toString(treeDTD));//d // NOI18N
158
System.out.println("LastChild: " + XMLStringResult.toString(treeDTD.getLastChild()));//d // NOI18N
159

160         TreeChild child = treeDTD.getFirstChild();
161         while (child != null) {
162             //docType.appendChild((TreeChild) child.clone());
163
System.out.println("Sibling: " + XMLStringResult.toString(child));//d // NOI18N
164
child = child.getNextSibling();
165             System.out.println("NextSibling: " + XMLStringResult.toString(child) + " :: " + child);//d // NOI18N
166
}
167         /*
168         TreeObjectList childList = treeDTD.getChildNodes();
169         for (int i = 0; i < childList.size(); i++) {
170             TreeChild child = (TreeChild) ((TreeObject)childList.get(i)).clone();
171             docType.appendChild(child);
172         }
173         */

174         // How I can created DTD from String??? - pretahat nodes z DTDdataObject
175
//docType.setInternalDtdDecl(dtdString);
176
return docType;
177     }
178
179
180     /**
181      * Test DTD builder.
182      * Check missing attributes.
183      */

184     static void testDTDBuilder(String JavaDoc dtd) throws Exception JavaDoc {
185         Iterator it;
186
187         TreeDTD tree = null;
188
189         it = tree.getChildNodes().iterator();
190         while (it.hasNext()) {
191             System.out.println (">" + it.next());
192         }
193
194         it = tree.getElementDeclarations().iterator();
195         while (it.hasNext()) {
196             System.out.println("#" + ((TreeNode)it.next()).toString()); // NOI18N
197
}
198
199         System.out.println("Listing Any attributes"); // NOI18N
200

201 // TreeElementDecl elemDecl = tree.findElementDecl("Any"); // NOI18N
202
// it = elemDecl.getDeclAttrs();
203
// while (it.hasNext()) {
204
// TreeAttributeDecl next = (TreeAttributeDecl) it.next();
205
// System.out.println(next.toString());
206
// }
207

208         System.out.println("-------------------"); // NOI18N
209
println (tree);
210         TreeChild child = tree.getFirstChild();
211         println (child);
212         child = child.getNextSibling();
213         println (child);
214         System.out.println("==================="); // NOI18N
215
}
216
217     /**
218      * Try to merge two trees.
219      * @param target repository resource prefix
220      * @param src reource resource prefix
221      * @param output file prefix
222      */

223     static void testMerge(String JavaDoc target, String JavaDoc src, String JavaDoc logg) throws Exception JavaDoc {
224     }
225
226
227     //
228
// API tests
229
//
230

231     /**
232      */

233     public static void println (TreeNode node) throws TreeException {
234         System.out.println("###################"); // NOI18N
235
System.out.println("node: " + node); // NOI18N
236
System.out.println("node.ownerDocument: " + node.getOwnerDocument()); // NOI18N
237
System.out.println("-=#BEGIN#=-"); // NOI18N
238
String JavaDoc string = XMLStringResult.toString (node);
239     System.out.println (string);
240         System.out.println("-=# END #=-"); // NOI18N
241
}
242
243
244     /**
245      */

246     public static void testAttlistDecl () throws TreeException {
247     TreeAttlistDecl attlistDecl = new TreeAttlistDecl ("elemName"); // NOI18N
248
attlistDecl.setAttributeDef (new TreeAttlistDeclAttributeDef ("attr1", TreeAttlistDeclAttributeDef.TYPE_CDATA, null, TreeAttlistDeclAttributeDef.DEFAULT_TYPE_FIXED, "value")); // NOI18N
249
attlistDecl.setAttributeDef (new TreeAttlistDeclAttributeDef ("attr2", TreeAttlistDeclAttributeDef.TYPE_NOTATION, new String JavaDoc[] { "abc", "def", "ghi" }, TreeAttlistDeclAttributeDef.DEFAULT_TYPE_IMPLIED, null)); // NOI18N
250
println (attlistDecl);
251     }
252
253     /**
254      */

255     public static void testAttribute () throws TreeException {
256     TreeAttribute attribute = new TreeAttribute ("name", "value"); // NOI18N
257
println (attribute);
258     }
259
260     /**
261      */

262     public static void testCDATASection () throws TreeException {
263     TreeCDATASection cdataSect = new TreeCDATASection ("<cdata data>"); // NOI18N
264
println (cdataSect);
265     }
266
267     /**
268      */

269     public static void testCharacterReference () throws TreeException {
270     TreeCharacterReference charRef = new TreeCharacterReference ("#35"); // NOI18N
271
println (charRef);
272     }
273
274     /**
275      */

276     public static void testComment () throws TreeException {
277     TreeComment comment = new TreeComment ("komentar - komentar"); // NOI18N
278
println (comment);
279     }
280
281     /**
282      */

283     public static void testConditionalSection () throws TreeException {
284     TreeConditionalSection condSect = new TreeConditionalSection (true);
285     println (condSect);
286     }
287
288     /**
289      */

290     public static void testDTD () throws TreeException {
291     TreeDTD dtd = new TreeDTD (null, null);
292     println (dtd);
293     }
294
295     /**
296      */

297     public static void testDocument () throws TreeException {
298     TreeDocument document = new TreeDocument (null, null, null);
299     println (document);
300     }
301
302     /**
303      */

304     public static void testDocumentFragment () throws TreeException {
305     TreeDocumentFragment docFrag = new TreeDocumentFragment (null, null);
306     println (docFrag);
307     }
308
309     /**
310      */

311     public static void testDocumentType () throws TreeException {
312     TreeDocumentType docType = new TreeDocumentType ("element"); // NOI18N
313
println (docType);
314     }
315
316     /**
317      */

318     public static void testElement () throws TreeException {
319     TreeElement element = new TreeElement ("ns:element"); // NOI18N
320
println (element);
321     }
322
323     /**
324      */

325     public static void testElementDecl () throws TreeException {
326     TreeElementDecl elemDecl = new TreeElementDecl ("element", new ANYType()); // NOI18N
327
println (elemDecl);
328     }
329
330     /**
331      */

332     public static void testEntityDecl () throws TreeException {
333     TreeEntityDecl entDecl = new TreeEntityDecl ("ent1", "text"); // NOI18N
334
println (entDecl);
335     entDecl = new TreeEntityDecl ("ent2", "pub1", "sys1"); // NOI18N
336
println (entDecl);
337     entDecl = new TreeEntityDecl (TreeEntityDecl.PARAMETER_DECL, "ent3", null, "sys2"); // NOI18N
338
println (entDecl);
339     entDecl = new TreeEntityDecl ("ent4", "pub2", "sys3", "not1"); // NOI18N
340
println (entDecl);
341     entDecl = new TreeEntityDecl ("ent5", "pub3", "sys4", null); // NOI18N
342
println (entDecl);
343     }
344
345     /**
346      */

347     public static void testGeneralEntityReference () throws TreeException {
348     TreeGeneralEntityReference geRef = new TreeGeneralEntityReference ("ge-ref"); // NOI18N
349
println (geRef);
350     }
351
352     /**
353      */

354     public static void testNotationDecl () throws TreeException {
355     TreeNotationDecl notDecl = new TreeNotationDecl ("not1", "pub1", "sys1"); // NOI18N
356
println (notDecl);
357     notDecl = new TreeNotationDecl ("not2", "pub2", null); // NOI18N
358
println (notDecl);
359     notDecl = new TreeNotationDecl ("not3", null, "sys2"); // NOI18N
360
println (notDecl);
361     }
362
363     /**
364      */

365     public static void testParameterEntityReference () throws TreeException {
366     TreeParameterEntityReference peRef = new TreeParameterEntityReference ("pe-ref"); // NOI18N
367
println (peRef);
368     }
369
370     /**
371      */

372     public static void testProcessingInstruction () throws TreeException {
373     TreeProcessingInstruction pi = new TreeProcessingInstruction ("target", "pi-data"); // NOI18N
374
println (pi);
375     }
376
377     /**
378      */

379     public static void testText () throws TreeException {
380     TreeText text = new TreeText ("text"); // NOI18N
381
println (text);
382     }
383
384
385     /**
386      */

387     public static void testAllTree () throws ClassNotFoundException JavaDoc, NoSuchMethodException JavaDoc {
388     String JavaDoc[] methods = new String JavaDoc [] {
389         "testAttlistDecl", // NOI18N
390
"testAttribute", // NOI18N
391
"testCDATASection", // NOI18N
392
"testCharacterReference", // NOI18N
393
"testComment", // NOI18N
394
"testConditionalSection", // NOI18N
395
"testDTD", // NOI18N
396
"testDocument", // NOI18N
397
"testDocumentFragment", // NOI18N
398
"testDocumentType", // NOI18N
399
"testElement", // NOI18N
400
"testElementDecl", // NOI18N
401
"testEntityDecl", // NOI18N
402
"testGeneralEntityReference", // NOI18N
403
"testNotationDecl", // NOI18N
404
"testParameterEntityReference", // NOI18N
405
"testProcessingInstruction", // NOI18N
406
"testText" // NOI18N
407
};
408     Class JavaDoc testsClass = Class.forName ("org.netbeans.tax.Tests"); // NOI18N
409
System.out.println ("clazz = " + testsClass); // NOI18N
410
for (int i = 0; i < methods.length; i++) {
411             Method testMethod = testsClass.getMethod (methods[i], null);
412 // System.out.println (" method = " + testMethod); // NOI18N
413
// System.out.println ("<!-- v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v -->"); // NOI18N
414
try {
415                 testMethod.invoke (null, null);
416             } catch (Exception JavaDoc exc) {
417                 System.out.println ("<!--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!-->\n"); // NOI18N
418
System.out.println ("Exception [ " + exc.getClass().getName() + " ] : " + exc.getMessage()); // NOI18N
419
exc.printStackTrace (System.out);
420             }
421         System.out.println ("<!--#######################################################################################-->"); // NOI18N
422
// System.out.println ("<!-- ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ -->\n"); // NOI18N
423
}
424     }
425
426
427     public static void testUtilities () {
428         System.out.println ("\nTest TreeUtilities ...");
429
430         char[] PUBLIC_ID = "\n\r -'()+,./:=?;!*#@$_%0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
431         char ac = (char) 0;
432         for (int i = 0; i < PUBLIC_ID.length; i++) {
433             char nc = PUBLIC_ID[i];
434             if ( ac > nc ) {
435                 System.out.println (" Precondiction failed: '" + ac + "' > '" + nc + "' !!!");
436             }
437             if ( UnicodeClasses.isXMLPubidLiteral (nc) == false ) {
438                 System.out.println (" Char '" + nc + "' is not correct Pubid Literal !!!");
439             } else {
440                 ac = nc;
441                 continue;
442             }
443             break;
444         }
445     }
446     
447 }
448
Popular Tags