1 21 22 package nu.xom.samples; 23 24 import java.io.IOException ; 25 26 import nu.xom.Attribute; 27 import nu.xom.Builder; 28 import nu.xom.Element; 29 import nu.xom.NodeFactory; 30 import nu.xom.Nodes; 31 import nu.xom.ParsingException; 32 import java.util.List ; 33 import java.util.ArrayList ; 34 35 46 public class DTDGenerator { 47 48 public static void main(String [] args) { 49 50 if (args.length == 0) { 51 System.err.println( 52 "Usage: java nu.xom.samples.DTDGenerator URL" 53 ); 54 return; 55 } 56 57 try { 58 Builder builder = new Builder(new NamingNodeFactory()); 59 builder.build(args[0]); 60 } 61 catch (IOException ex) { 62 System.err.println("Could not read " + args[0] 63 + " due to " + ex.getMessage()); 64 } 65 catch (ParsingException ex) { 66 System.err.println(args[0] + " is not well-formed"); 67 } 68 69 } 70 71 private static class NamingNodeFactory extends NodeFactory { 72 73 private List names = new ArrayList (); 74 private String currentElement; 75 76 public Element startMakingElement( 77 String name, String namespace) { 78 if (!names.contains(name)) { 79 System.out.println("<!ELEMENT " + name + " ANY>"); 80 names.add(name); 81 } 82 currentElement = name; 83 return super.startMakingElement(name, namespace); 84 } 85 86 public Nodes makeAttribute(String name, String URI, 87 String value, Attribute.Type type) { 88 89 if (type.equals(Attribute.Type.ENUMERATION) 90 || type.equals(Attribute.Type.UNDECLARED)) { 91 type = Attribute.Type.CDATA; 92 } 93 String comboName = currentElement + '#' + name; 94 if (!names.contains(comboName)) { 95 names.add(comboName); 96 System.out.println("<!ATTLIST " + currentElement + " " 97 + name + " " + type.getName() + " #IMPLIED>"); 98 } 99 return super.makeAttribute(name, URI, value, type); 100 } 101 } 102 103 } | Popular Tags |