1 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 31 public class Simple { 32 33 34 public Simple () { 35 } 36 37 40 public static void main (String args[]) throws Exception { 41 42 System.out.println ("Runnig_tests..."); 43 testEncodings(); 44 50 testUtilities(); 51 } 52 53 private static boolean testEncodings () { 54 System.out.println ("This test check all 'supported' encoding if they are realy supported!"); 56 int invalid = 0; 57 Iterator it = TreeUtilities.getSupportedEncodings().iterator(); 58 while (it.hasNext()) { 59 String encoding = (String )it.next(); 60 boolean valid = TreeUtilities.isValidDocumentEncoding (encoding); 61 String prefix; 62 String suffix; 63 if ( valid ) { 64 prefix = " "; suffix = "is valid."; } else { 67 invalid++; 68 prefix = "!!! "; suffix = "is *INVALID* !!!"; } 71 StringBuffer sb = new StringBuffer (); 72 sb.append (prefix).append ("encoding = '").append (encoding).append ("'\t").append (suffix); System.out.println (sb.toString()); 74 } 75 76 if ( invalid != 0 ) { 77 System.out.println ("I found " + invalid + " invalid encodings!"); } 79 80 return ( invalid == 0 ); 81 } 82 83 84 private static void testTree () throws Exception { 85 Class clazz = Class.forName ("org.netbeans.tax.TreeAttlistDeclAttributeDef"); clazz = TreeAttlistDeclAttributeDef.class; 87 System.out.println ("AttributeDef.class = " + clazz); 89 TreeAttlistDecl attlistDecl = new TreeAttlistDecl ("elemName"); TreeAttlistDeclAttributeDef attrDef; 91 attlistDecl.setAttributeDef (attrDef = new TreeAttlistDeclAttributeDef ("attr1", TreeAttlistDeclAttributeDef.TYPE_CDATA, null, TreeAttlistDeclAttributeDef.DEFAULT_TYPE_FIXED, "value")); println (attlistDecl); 93 System.out.println ("AttributeDef.class = " + attrDef.getClass().getName()); 95 96 TreeCharacterReference charRef = new TreeCharacterReference ("#35"); println (charRef); 98 charRef = new TreeCharacterReference ("#x35"); println (charRef); 100 101 TreeDocumentType doctype = createDocumentType(); 102 println (doctype); 103 104 System.out.println("===create=document======="); TreeDocument doc = new TreeDocument("1.0", null, "no"); doc.appendChild (new TreeComment ("==============")); TreeElement elem = new TreeElement ("xxx"); doc.setDocumentElement (elem); 109 110 elem.addAttribute (new TreeAttribute("attr", "value")); elem.addAttribute (new TreeAttribute("attr", "value2")); elem.addAttribute (new TreeAttribute("attr3", "value3")); System.out.println ("CYCLE: elem.appendChild (elem);"); 115 elem.appendChild (new TreeComment ("Chtel bych vyzkouset get[Previous|Next]Sibling")); elem.appendChild (new TreeCDATASection ("<!-------------><BLA>")); elem.appendChild (new TreeElement ("BLA")); elem.appendChild (new TreeComment ("... tak jeste zaverecny komentar!")); 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 TreeDTD dtd = new TreeDTD (null, null); 133 TreeAttlistDecl attlist = new TreeAttlistDecl ("elem"); attlist.setAttributeDef (new TreeAttlistDeclAttributeDef ("attr", TreeAttlistDeclAttributeDef.TYPE_ID, null, TreeAttlistDeclAttributeDef.DEFAULT_TYPE_REQUIRED, "bla")); dtd.appendChild (attlist); 137 138 attlist = new TreeAttlistDecl ("elem2"); attlist.setAttributeDef (new TreeAttlistDeclAttributeDef ("attr2", TreeAttlistDeclAttributeDef.TYPE_NOTATION, new String [] { "abc", "def" }, TreeAttlistDeclAttributeDef.DEFAULT_TYPE_NULL, "bla")); 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 { 152 TreeDocumentType docType = new TreeDocumentType ("element"); 154 docType.setSystemId ("xml/simple.dtd"); 156 TreeDTD treeDTD = null; 157 System.out.println("TreeDTD: " + XMLStringResult.toString(treeDTD)); System.out.println("LastChild: " + XMLStringResult.toString(treeDTD.getLastChild())); 160 TreeChild child = treeDTD.getFirstChild(); 161 while (child != null) { 162 System.out.println("Sibling: " + XMLStringResult.toString(child)); child = child.getNextSibling(); 165 System.out.println("NextSibling: " + XMLStringResult.toString(child) + " :: " + child); } 167 174 return docType; 177 } 178 179 180 184 static void testDTDBuilder(String dtd) throws Exception { 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()); } 198 199 System.out.println("Listing Any attributes"); 201 208 System.out.println("-------------------"); println (tree); 210 TreeChild child = tree.getFirstChild(); 211 println (child); 212 child = child.getNextSibling(); 213 println (child); 214 System.out.println("==================="); } 216 217 223 static void testMerge(String target, String src, String logg) throws Exception { 224 } 225 226 227 231 233 public static void println (TreeNode node) throws TreeException { 234 System.out.println("###################"); System.out.println("node: " + node); System.out.println("node.ownerDocument: " + node.getOwnerDocument()); System.out.println("-=#BEGIN#=-"); String string = XMLStringResult.toString (node); 239 System.out.println (string); 240 System.out.println("-=# END #=-"); } 242 243 244 246 public static void testAttlistDecl () throws TreeException { 247 TreeAttlistDecl attlistDecl = new TreeAttlistDecl ("elemName"); attlistDecl.setAttributeDef (new TreeAttlistDeclAttributeDef ("attr1", TreeAttlistDeclAttributeDef.TYPE_CDATA, null, TreeAttlistDeclAttributeDef.DEFAULT_TYPE_FIXED, "value")); attlistDecl.setAttributeDef (new TreeAttlistDeclAttributeDef ("attr2", TreeAttlistDeclAttributeDef.TYPE_NOTATION, new String [] { "abc", "def", "ghi" }, TreeAttlistDeclAttributeDef.DEFAULT_TYPE_IMPLIED, null)); println (attlistDecl); 251 } 252 253 255 public static void testAttribute () throws TreeException { 256 TreeAttribute attribute = new TreeAttribute ("name", "value"); println (attribute); 258 } 259 260 262 public static void testCDATASection () throws TreeException { 263 TreeCDATASection cdataSect = new TreeCDATASection ("<cdata data>"); println (cdataSect); 265 } 266 267 269 public static void testCharacterReference () throws TreeException { 270 TreeCharacterReference charRef = new TreeCharacterReference ("#35"); println (charRef); 272 } 273 274 276 public static void testComment () throws TreeException { 277 TreeComment comment = new TreeComment ("komentar - komentar"); println (comment); 279 } 280 281 283 public static void testConditionalSection () throws TreeException { 284 TreeConditionalSection condSect = new TreeConditionalSection (true); 285 println (condSect); 286 } 287 288 290 public static void testDTD () throws TreeException { 291 TreeDTD dtd = new TreeDTD (null, null); 292 println (dtd); 293 } 294 295 297 public static void testDocument () throws TreeException { 298 TreeDocument document = new TreeDocument (null, null, null); 299 println (document); 300 } 301 302 304 public static void testDocumentFragment () throws TreeException { 305 TreeDocumentFragment docFrag = new TreeDocumentFragment (null, null); 306 println (docFrag); 307 } 308 309 311 public static void testDocumentType () throws TreeException { 312 TreeDocumentType docType = new TreeDocumentType ("element"); println (docType); 314 } 315 316 318 public static void testElement () throws TreeException { 319 TreeElement element = new TreeElement ("ns:element"); println (element); 321 } 322 323 325 public static void testElementDecl () throws TreeException { 326 TreeElementDecl elemDecl = new TreeElementDecl ("element", new ANYType()); println (elemDecl); 328 } 329 330 332 public static void testEntityDecl () throws TreeException { 333 TreeEntityDecl entDecl = new TreeEntityDecl ("ent1", "text"); println (entDecl); 335 entDecl = new TreeEntityDecl ("ent2", "pub1", "sys1"); println (entDecl); 337 entDecl = new TreeEntityDecl (TreeEntityDecl.PARAMETER_DECL, "ent3", null, "sys2"); println (entDecl); 339 entDecl = new TreeEntityDecl ("ent4", "pub2", "sys3", "not1"); println (entDecl); 341 entDecl = new TreeEntityDecl ("ent5", "pub3", "sys4", null); println (entDecl); 343 } 344 345 347 public static void testGeneralEntityReference () throws TreeException { 348 TreeGeneralEntityReference geRef = new TreeGeneralEntityReference ("ge-ref"); println (geRef); 350 } 351 352 354 public static void testNotationDecl () throws TreeException { 355 TreeNotationDecl notDecl = new TreeNotationDecl ("not1", "pub1", "sys1"); println (notDecl); 357 notDecl = new TreeNotationDecl ("not2", "pub2", null); println (notDecl); 359 notDecl = new TreeNotationDecl ("not3", null, "sys2"); println (notDecl); 361 } 362 363 365 public static void testParameterEntityReference () throws TreeException { 366 TreeParameterEntityReference peRef = new TreeParameterEntityReference ("pe-ref"); println (peRef); 368 } 369 370 372 public static void testProcessingInstruction () throws TreeException { 373 TreeProcessingInstruction pi = new TreeProcessingInstruction ("target", "pi-data"); println (pi); 375 } 376 377 379 public static void testText () throws TreeException { 380 TreeText text = new TreeText ("text"); println (text); 382 } 383 384 385 387 public static void testAllTree () throws ClassNotFoundException , NoSuchMethodException { 388 String [] methods = new String [] { 389 "testAttlistDecl", "testAttribute", "testCDATASection", "testCharacterReference", "testComment", "testConditionalSection", "testDTD", "testDocument", "testDocumentFragment", "testDocumentType", "testElement", "testElementDecl", "testEntityDecl", "testGeneralEntityReference", "testNotationDecl", "testParameterEntityReference", "testProcessingInstruction", "testText" }; 408 Class testsClass = Class.forName ("org.netbeans.tax.Tests"); System.out.println ("clazz = " + testsClass); for (int i = 0; i < methods.length; i++) { 411 Method testMethod = testsClass.getMethod (methods[i], null); 412 try { 415 testMethod.invoke (null, null); 416 } catch (Exception exc) { 417 System.out.println ("<!--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!-->\n"); System.out.println ("Exception [ " + exc.getClass().getName() + " ] : " + exc.getMessage()); exc.printStackTrace (System.out); 420 } 421 System.out.println ("<!--#######################################################################################-->"); } 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 |