Debian Codegraph @

mod/dtdinst/src/main/com/thaiopensource/xml/dtd/app/SchemaWriter.java

raw
1 package com.thaiopensource.xml.dtd.app;
2
3 import java.io.IOException;
4
5 import com.thaiopensource.xml.dtd.om.*;
6 import com.thaiopensource.xml.out.XmlWriter;
7 import com.thaiopensource.xml.em.ExternalId;
8
9 public class SchemaWriter implements TopLevelVisitor,
10 ModelGroupVisitor,
11 AttributeGroupVisitor,
12 DatatypeVisitor,
13 EnumGroupVisitor,
14 FlagVisitor,
15 NameSpecVisitor,
16 AttributeDefaultVisitor {
17 private final XmlWriter w;
18
19 public SchemaWriter(XmlWriter writer) {
20 this.w = writer;
21 }
22
23 public void writeDtd(Dtd dtd) throws IOException {
24 String enc = dtd.getEncoding();
25 if (enc != null)
26 w.writeXmlDecl(enc);
27 w.startElement("doctype");
28 try {
29 dtd.accept(this);
30 }
31 catch (RuntimeException e) {
32 throw e;
33 }
34 catch (Exception e) {
35 throw (IOException)e;
36 }
37 w.endElement();
38 }
39
40 public void elementDecl(NameSpec nameSpec, ModelGroup modelGroup)
41 throws Exception {
42 w.startElement("element");
43 nameSpec.accept(this);
44 modelGroup.accept(this);
45 w.endElement();
46 }
47
48 public void attlistDecl(NameSpec nameSpec, AttributeGroup attributeGroup)
49 throws Exception {
50 w.startElement("attlist");
51 nameSpec.accept(this);
52 attributeGroup.accept(this);
53 w.endElement();
54 }
55
56 public void processingInstruction(String target, String value) throws Exception {
57 w.startElement("processingInstruction");
58 w.attribute("target", target);
59 w.characters(value);
60 w.endElement();
61 }
62
63 public void comment(String value) throws Exception {
64 w.startElement("comment");
65 w.characters(value);
66 w.endElement();
67 }
68
69 public void modelGroupDef(String name, ModelGroup modelGroup) throws Exception {
70 w.startElement("modelGroup");
71 w.attribute("name", name);
72 modelGroup.accept(this);
73 w.endElement();
74 }
75
76 public void attributeGroupDef(String name, AttributeGroup attributeGroup)
77 throws Exception {
78 w.startElement("attributeGroup");
79 w.attribute("name", name);
80 attributeGroup.accept(this);
81 w.endElement();
82 }
83
84 public void enumGroupDef(String name, EnumGroup enumGroup) throws Exception {
85 w.startElement("enumGroup");
86 w.attribute("name", name);
87 enumGroup.accept(this);
88 w.endElement();
89 }
90
91 public void datatypeDef(String name, Datatype datatype) throws Exception {
92 w.startElement("datatype");
93 w.attribute("name", name);
94 datatype.accept(this);
95 w.endElement();
96 }
97
98 public void flagDef(String name, Flag flag) throws Exception {
99 w.startElement("flag");
100 w.attribute("name", name);
101 flag.accept(this);
102 w.endElement();
103 }
104
105 public void attributeDefaultDef(String name, AttributeDefault attributeDefault)
106 throws Exception {
107 w.startElement("attributeDefault");
108 w.attribute("name", name);
109 attributeDefault.accept(this);
110 w.endElement();
111 }
112
113 public void choice(ModelGroup[] members) throws Exception {
114 w.startElement("choice");
115 for (int i = 0; i < members.length; i++)
116 members[i].accept(this);
117 w.endElement();
118 }
119
120 public void sequence(ModelGroup[] members) throws Exception {
121 w.startElement("sequence");
122 for (int i = 0; i < members.length; i++)
123 members[i].accept(this);
124 w.endElement();
125 }
126
127 public void oneOrMore(ModelGroup member) throws Exception {
128 w.startElement("oneOrMore");
129 member.accept(this);
130 w.endElement();
131 }
132
133 public void zeroOrMore(ModelGroup member) throws Exception {
134 w.startElement("zeroOrMore");
135 member.accept(this);
136 w.endElement();
137 }
138
139 public void optional(ModelGroup member) throws Exception {
140 w.startElement("optional");
141 member.accept(this);
142 w.endElement();
143 }
144
145 public void modelGroupRef(String name, ModelGroup modelGroup) throws Exception {
146 w.startElement("modelGroupRef");
147 w.attribute("name", name);
148 w.endElement();
149 }
150
151 public void elementRef(NameSpec nameSpec) throws Exception {
152 w.startElement("elementRef");
153 nameSpec.accept(this);
154 w.endElement();
155 }
156
157 public void pcdata() throws Exception {
158 w.startElement("pcdata");
159 w.endElement();
160 }
161
162 public void any() throws Exception {
163 w.startElement("any");
164 w.endElement();
165 }
166
167 public void attribute(NameSpec nameSpec,
168 Datatype datatype,
169 AttributeDefault attributeDefault)
170 throws Exception {
171 w.startElement("attribute");
172 nameSpec.accept(this);
173 datatype.accept(this);
174 attributeDefault.accept(this);
175 w.endElement();
176 }
177
178 public void attributeGroupRef(String name, AttributeGroup attributeGroup)
179 throws Exception {
180 w.startElement("attributeGroupRef");
181 w.attribute("name", name);
182 w.endElement();
183 }
184
185 public void enumValue(String value) throws Exception {
186 w.startElement("enum");
187 w.characters(value);
188 w.endElement();
189 }
190
191 public void enumGroupRef(String name, EnumGroup enumGroup) throws Exception {
192 w.startElement("enumGroupRef");
193 w.attribute("name", name);
194 w.endElement();
195 }
196
197 public void cdataDatatype() throws IOException {
198 w.startElement("cdata");
199 w.endElement();
200 }
201
202 public void tokenizedDatatype(String typeName) throws IOException {
203 w.startElement("tokenized");
204 w.attribute("name", typeName);
205 w.endElement();
206 }
207
208 public void enumDatatype(EnumGroup enumGroup) throws Exception {
209 w.startElement("tokenized");
210 enumGroup.accept(this);
211 w.endElement();
212 }
213
214 public void notationDatatype(EnumGroup enumGroup) throws Exception {
215 w.startElement("tokenized");
216 w.attribute("name", "NOTATION");
217 enumGroup.accept(this);
218 w.endElement();
219 }
220
221 public void datatypeRef(String name, Datatype datatype) throws IOException {
222 w.startElement("datatypeRef");
223 w.attribute("name", name);
224 w.endElement();
225 }
226
227 public void flagRef(String name, Flag flag) throws IOException {
228 w.startElement("flagRef");
229 w.attribute("name", name);
230 w.endElement();
231 }
232
233 public void include() throws IOException {
234 w.startElement("include");
235 w.endElement();
236 }
237
238 public void ignore() throws IOException {
239 w.startElement("ignore");
240 w.endElement();
241 }
242
243 public void includedSection(Flag flag, TopLevel[] contents) throws Exception {
244 w.startElement("includedSection");
245 if (flag instanceof FlagRef)
246 w.attribute("flag", ((FlagRef)flag).getName());
247 for (int i = 0; i < contents.length; i++)
248 contents[i].accept(this);
249 w.endElement();
250 }
251
252 public void ignoredSection(Flag flag, String contents) throws Exception {
253 w.startElement("ignoredSection");
254 if (flag instanceof FlagRef)
255 w.attribute("flag", ((FlagRef)flag).getName());
256 w.characters(contents);
257 w.endElement();
258 }
259
260 public void externalIdDef(String name, ExternalId xid) throws IOException {
261 w.startElement("externalId");
262 w.attribute("name", name);
263 externalId(xid);
264 w.endElement();
265 }
266
267 public void externalIdRef(String name, ExternalId xid, String uri,
268 String encoding, TopLevel[] contents)
269 throws Exception {
270 w.startElement("externalIdRef");
271 w.attribute("name", name);
272 for (int i = 0; i < contents.length; i++)
273 contents[i].accept(this);
274 w.endElement();
275 }
276
277 public void internalEntityDecl(String name, String value) throws Exception {
278 w.startElement("internalEntity");
279 w.attribute("name", name);
280 boolean useCharRef = value.length() == 1 && value.charAt(0) >= 0x80;
281 w.characters(value, useCharRef);
282 w.endElement();
283 }
284
285 public void externalEntityDecl(String name, ExternalId xid) throws IOException {
286 w.startElement("externalEntity");
287 w.attribute("name", name);
288 externalId(xid);
289 w.endElement();
290 }
291
292 public void notationDecl(String name, ExternalId xid) throws IOException {
293 w.startElement("notation");
294 w.attribute("name", name);
295 externalId(xid);
296 w.endElement();
297 }
298
299 private void externalId(ExternalId xid) throws IOException {
300 attributeIfNotNull("system", xid.getSystemId());
301 attributeIfNotNull("public", xid.getPublicId());
302 // this messes up testing
303 // attributeIfNotNull("xml:base", xid.getBaseUri());
304 }
305
306 private void attributeIfNotNull(String name, String value)
307 throws IOException {
308 if (value != null)
309 w.attribute(name, value);
310 }
311
312 public void nameSpecDef(String name, NameSpec nameSpec) throws Exception {
313 w.startElement("nameSpec");
314 w.attribute("name", name);
315 nameSpec.accept(this);
316 w.endElement();
317 }
318
319 public void name(String value) throws IOException {
320 w.startElement("name");
321 w.characters(value);
322 w.endElement();
323 }
324
325 public void nameSpecRef(String name, NameSpec nameSpec) throws Exception {
326 w.startElement("nameSpecRef");
327 w.attribute("name", name);
328 w.endElement();
329 }
330
331 public void overriddenDef(Def def, boolean duplicate) throws Exception {
332 w.startElement("overridden");
333 if (duplicate) {
334 w.startElement("duplicate");
335 w.attribute("name", def.getName());
336 w.endElement();
337 }
338 else
339 def.accept(this);
340 w.endElement();
341 }
342
343 public void paramDef(String name, String value) throws IOException {
344 w.startElement("param");
345 w.attribute("name", name);
346 w.characters(value);
347 w.endElement();
348 }
349
350 public void defaultValue(String value) throws Exception {
351 w.startElement("default");
352 w.characters(value);
353 w.endElement();
354 }
355
356 public void fixedValue(String value) throws Exception {
357 w.startElement("fixed");
358 w.characters(value);
359 w.endElement();
360 }
361
362 public void impliedValue() throws Exception {
363 w.startElement("implied");
364 w.endElement();
365 }
366
367 public void requiredValue() throws Exception {
368 w.startElement("required");
369 w.endElement();
370 }
371
372 public void attributeDefaultRef(String name,
373 AttributeDefault attributeDefault)
374 throws Exception {
375 w.startElement("attributeDefaultRef");
376 w.attribute("name", name);
377 w.endElement();
378 }
379 }