File: Tokenize.java

package info (click to toggle)
libdtdparser-java 1.21a-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 400 kB
  • ctags: 393
  • sloc: java: 3,143; xml: 66; makefile: 15
file content (255 lines) | stat: -rw-r--r-- 7,655 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package com.wutka.dtd;

import java.io.*;
import java.util.*;
import java.net.URL;

/** Example program to read a DTD and print out its object model
 *
 * @author Mark Wutka
 * @version $Revision: 1.17 $ $Date: 2002/07/28 13:33:12 $ by $Author: wutka $
 */

class Tokenize
{
	public static void main(String[] args)
	{
		try
		{
            DTDParser parser = null;
// MAW Version 1.17
// If it looks like the filename may be a URL, use the URL class
            if (args[0].indexOf("://") > 0)
            {
                parser = new DTDParser(new URL(args[0]), true);
            }
            else
            {
                parser = new DTDParser(new File(args[0]), true);
            }

// Parse the DTD and ask the parser to guess the root element
            DTD dtd = parser.parse(true);

            if (dtd.rootElement != null)
            {
                System.out.println("Root element is probably: "+
                    dtd.rootElement.name);
            }

            Enumeration e = dtd.elements.elements();

            while (e.hasMoreElements())
            {
                DTDElement elem = (DTDElement) e.nextElement();

                System.out.println("Element: "+elem.name);
                System.out.print("   Content: ");
                dumpDTDItem(elem.content);
                System.out.println();

                if (elem.attributes.size() > 0)
                {
                    System.out.println("   Attributes: ");
                    Enumeration attrs = elem.attributes.elements();
                    while (attrs.hasMoreElements())
                    {
                        System.out.print("        ");
                        DTDAttribute attr = (DTDAttribute) attrs.nextElement();
                        dumpAttribute(attr);
                    }
                    System.out.println();
                }
            }

            e = dtd.entities.elements();

            while (e.hasMoreElements())
            {
                DTDEntity entity = (DTDEntity) e.nextElement();

                if (entity.isParsed) System.out.print("Parsed ");

                System.out.println("Entity: "+entity.name);
                
                if (entity.value != null)
                {
                    System.out.println("    Value: "+entity.value);
                }

                if (entity.externalID != null)
                {
                    if (entity.externalID instanceof DTDSystem)
                    {
                        System.out.println("    System: "+
                            entity.externalID.system);
                    }
                    else
                    {
                        DTDPublic pub = (DTDPublic) entity.externalID;

                        System.out.println("    Public: "+
                            pub.pub+" "+pub.system);
                    }
                }

                if (entity.ndata != null)
                {
                    System.out.println("    NDATA "+entity.ndata);
                }
            }
            e = dtd.notations.elements();

            while (e.hasMoreElements())
            {
                DTDNotation notation = (DTDNotation) e.nextElement();

                System.out.println("Notation: "+notation.name);
                
                if (notation.externalID != null)
                {
                    if (notation.externalID instanceof DTDSystem)
                    {
                        System.out.println("    System: "+
                            notation.externalID.system);
                    }
                    else
                    {
                        DTDPublic pub = (DTDPublic) notation.externalID;

                        System.out.print("    Public: "+
                            pub.pub+" ");
                        if (pub.system != null)
                        {
                            System.out.println(pub.system);
                        }
                        else
                        {
                            System.out.println();
                        }
                    }
                }
            }
		}
		catch (Exception exc)
		{
			exc.printStackTrace(System.out);
		}
	}

    public static void dumpDTDItem(DTDItem item)
    {
        if (item == null) return;

        if (item instanceof DTDAny)
        {
            System.out.print("Any");
        }
        else if (item instanceof DTDEmpty)
        {
            System.out.print("Empty");
        }
        else if (item instanceof DTDName)
        {
            System.out.print(((DTDName) item).value);
        }
        else if (item instanceof DTDChoice)
        {
            System.out.print("(");
            DTDItem[] items = ((DTDChoice) item).getItems();

            for (int i=0; i < items.length; i++)
            {
                if (i > 0) System.out.print("|");
                dumpDTDItem(items[i]);
            }
            System.out.print(")");
        }
        else if (item instanceof DTDSequence)
        {
            System.out.print("(");
            DTDItem[] items = ((DTDSequence) item).getItems();

            for (int i=0; i < items.length; i++)
            {
                if (i > 0) System.out.print(",");
                dumpDTDItem(items[i]);
            }
            System.out.print(")");
        }
        else if (item instanceof DTDMixed)
        {
            System.out.print("(");
            DTDItem[] items = ((DTDMixed) item).getItems();

            for (int i=0; i < items.length; i++)
            {
                if (i > 0) System.out.print(",");
                dumpDTDItem(items[i]);
            }
            System.out.print(")");
        }
        else if (item instanceof DTDPCData)
        {
            System.out.print("#PCDATA");
        }

        if (item.cardinal == DTDCardinal.OPTIONAL)
        {
            System.out.print("?");
        }
        else if (item.cardinal == DTDCardinal.ZEROMANY)
        {
            System.out.print("*");
        }
        else if (item.cardinal == DTDCardinal.ONEMANY)
        {
            System.out.print("+");
        }
    }

    public static void dumpAttribute(DTDAttribute attr)
    {
        System.out.print(attr.name+" ");
        if (attr.type instanceof String)
        {
            System.out.print(attr.type);
        }
        else if (attr.type instanceof DTDEnumeration)
        {
            System.out.print("(");
            String[] items = ((DTDEnumeration) attr.type).getItems();

            for (int i=0; i < items.length; i++)
            {
                if (i > 0) System.out.print(",");
                System.out.print(items[i]);
            }
            System.out.print(")");
        }
        else if (attr.type instanceof DTDNotationList)
        {
            System.out.print("Notation (");
            String[] items = ((DTDNotationList) attr.type).getItems();

            for (int i=0; i < items.length; i++)
            {
                if (i > 0) System.out.print(",");
                System.out.print(items[i]);
            }
            System.out.print(")");
        }

        if (attr.decl != null)
        {
            System.out.print(" "+attr.decl.name);
        }

        if (attr.defaultValue != null)
        {
            System.out.print(" "+attr.defaultValue);
        }

        System.out.println();
    }
}