View Javadoc

1   package com.wutka.dtd;
2   
3   import java.io.*;
4   import java.util.*;
5   import java.net.URL;
6   
7   /*** Example program to read a DTD and print out its object model
8    *
9    * @author Mark Wutka
10   * @version $Revision: 1.17 $ $Date: 2002/07/28 13:33:12 $ by $Author: wutka $
11   */
12  
13  class Tokenize
14  {
15  	public static void main(String[] args)
16  	{
17  		try
18  		{
19              DTDParser parser = null;
20  // MAW Version 1.17
21  // If it looks like the filename may be a URL, use the URL class
22              if (args[0].indexOf("://") > 0)
23              {
24                  parser = new DTDParser(new URL(args[0]), true);
25              }
26              else
27              {
28                  parser = new DTDParser(new File(args[0]), true);
29              }
30  
31  // Parse the DTD and ask the parser to guess the root element
32              DTD dtd = parser.parse(true);
33  
34              if (dtd.rootElement != null)
35              {
36                  System.out.println("Root element is probably: "+
37                      dtd.rootElement.name);
38              }
39  
40              Enumeration e = dtd.elements.elements();
41  
42              while (e.hasMoreElements())
43              {
44                  DTDElement elem = (DTDElement) e.nextElement();
45  
46                  System.out.println("Element: "+elem.name);
47                  System.out.print("   Content: ");
48                  dumpDTDItem(elem.content);
49                  System.out.println();
50  
51                  if (elem.attributes.size() > 0)
52                  {
53                      System.out.println("   Attributes: ");
54                      Enumeration attrs = elem.attributes.elements();
55                      while (attrs.hasMoreElements())
56                      {
57                          System.out.print("        ");
58                          DTDAttribute attr = (DTDAttribute) attrs.nextElement();
59                          dumpAttribute(attr);
60                      }
61                      System.out.println();
62                  }
63              }
64  
65              e = dtd.entities.elements();
66  
67              while (e.hasMoreElements())
68              {
69                  DTDEntity entity = (DTDEntity) e.nextElement();
70  
71                  if (entity.isParsed) System.out.print("Parsed ");
72  
73                  System.out.println("Entity: "+entity.name);
74                  
75                  if (entity.value != null)
76                  {
77                      System.out.println("    Value: "+entity.value);
78                  }
79  
80                  if (entity.externalID != null)
81                  {
82                      if (entity.externalID instanceof DTDSystem)
83                      {
84                          System.out.println("    System: "+
85                              entity.externalID.system);
86                      }
87                      else
88                      {
89                          DTDPublic pub = (DTDPublic) entity.externalID;
90  
91                          System.out.println("    Public: "+
92                              pub.pub+" "+pub.system);
93                      }
94                  }
95  
96                  if (entity.ndata != null)
97                  {
98                      System.out.println("    NDATA "+entity.ndata);
99                  }
100             }
101             e = dtd.notations.elements();
102 
103             while (e.hasMoreElements())
104             {
105                 DTDNotation notation = (DTDNotation) e.nextElement();
106 
107                 System.out.println("Notation: "+notation.name);
108                 
109                 if (notation.externalID != null)
110                 {
111                     if (notation.externalID instanceof DTDSystem)
112                     {
113                         System.out.println("    System: "+
114                             notation.externalID.system);
115                     }
116                     else
117                     {
118                         DTDPublic pub = (DTDPublic) notation.externalID;
119 
120                         System.out.print("    Public: "+
121                             pub.pub+" ");
122                         if (pub.system != null)
123                         {
124                             System.out.println(pub.system);
125                         }
126                         else
127                         {
128                             System.out.println();
129                         }
130                     }
131                 }
132             }
133 		}
134 		catch (Exception exc)
135 		{
136 			exc.printStackTrace(System.out);
137 		}
138 	}
139 
140     public static void dumpDTDItem(DTDItem item)
141     {
142         if (item == null) return;
143 
144         if (item instanceof DTDAny)
145         {
146             System.out.print("Any");
147         }
148         else if (item instanceof DTDEmpty)
149         {
150             System.out.print("Empty");
151         }
152         else if (item instanceof DTDName)
153         {
154             System.out.print(((DTDName) item).value);
155         }
156         else if (item instanceof DTDChoice)
157         {
158             System.out.print("(");
159             DTDItem[] items = ((DTDChoice) item).getItems();
160 
161             for (int i=0; i < items.length; i++)
162             {
163                 if (i > 0) System.out.print("|");
164                 dumpDTDItem(items[i]);
165             }
166             System.out.print(")");
167         }
168         else if (item instanceof DTDSequence)
169         {
170             System.out.print("(");
171             DTDItem[] items = ((DTDSequence) item).getItems();
172 
173             for (int i=0; i < items.length; i++)
174             {
175                 if (i > 0) System.out.print(",");
176                 dumpDTDItem(items[i]);
177             }
178             System.out.print(")");
179         }
180         else if (item instanceof DTDMixed)
181         {
182             System.out.print("(");
183             DTDItem[] items = ((DTDMixed) item).getItems();
184 
185             for (int i=0; i < items.length; i++)
186             {
187                 if (i > 0) System.out.print(",");
188                 dumpDTDItem(items[i]);
189             }
190             System.out.print(")");
191         }
192         else if (item instanceof DTDPCData)
193         {
194             System.out.print("#PCDATA");
195         }
196 
197         if (item.cardinal == DTDCardinal.OPTIONAL)
198         {
199             System.out.print("?");
200         }
201         else if (item.cardinal == DTDCardinal.ZEROMANY)
202         {
203             System.out.print("*");
204         }
205         else if (item.cardinal == DTDCardinal.ONEMANY)
206         {
207             System.out.print("+");
208         }
209     }
210 
211     public static void dumpAttribute(DTDAttribute attr)
212     {
213         System.out.print(attr.name+" ");
214         if (attr.type instanceof String)
215         {
216             System.out.print(attr.type);
217         }
218         else if (attr.type instanceof DTDEnumeration)
219         {
220             System.out.print("(");
221             String[] items = ((DTDEnumeration) attr.type).getItems();
222 
223             for (int i=0; i < items.length; i++)
224             {
225                 if (i > 0) System.out.print(",");
226                 System.out.print(items[i]);
227             }
228             System.out.print(")");
229         }
230         else if (attr.type instanceof DTDNotationList)
231         {
232             System.out.print("Notation (");
233             String[] items = ((DTDNotationList) attr.type).getItems();
234 
235             for (int i=0; i < items.length; i++)
236             {
237                 if (i > 0) System.out.print(",");
238                 System.out.print(items[i]);
239             }
240             System.out.print(")");
241         }
242 
243         if (attr.decl != null)
244         {
245             System.out.print(" "+attr.decl.name);
246         }
247 
248         if (attr.defaultValue != null)
249         {
250             System.out.print(" "+attr.defaultValue);
251         }
252 
253         System.out.println();
254     }
255 }