KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > samples > DTDGenerator


1 /* Copyright 2003 Elliotte Rusty Harold
2    
3    This library is free software; you can redistribute it and/or modify
4    it under the terms of version 2.1 of the GNU Lesser General Public
5    License as published by the Free Software Foundation.
6    
7    This library is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10    GNU Lesser General Public License for more details.
11    
12    You should have received a copy of the GNU Lesser General Public
13    License along with this library; if not, write to the
14    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15    Boston, MA 02111-1307 USA
16    
17    You can contact Elliotte Rusty Harold by sending e-mail to
18    elharo@metalab.unc.edu. Please include the word "XOM" in the
19    subject line. The XOM home page is located at http://www.xom.nu/
20 */

21
22 package nu.xom.samples;
23
24 import java.io.IOException JavaDoc;
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 JavaDoc;
33 import java.util.ArrayList JavaDoc;
34
35 /**
36  *
37  * <p>
38  * Demonstrates walking the tree while collecting
39  * element and attribute names and types.
40  * </p>
41  *
42  * @author Elliotte Rusty Harold
43  * @version 1.0
44  *
45  */

46 public class DTDGenerator {
47  
48     public static void main(String JavaDoc[] 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 JavaDoc 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 JavaDoc names = new ArrayList JavaDoc();
74         private String JavaDoc currentElement;
75         
76         public Element startMakingElement(
77           String JavaDoc name, String JavaDoc 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 JavaDoc name, String JavaDoc URI,
87           String JavaDoc 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 JavaDoc 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