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 package org.exolab.castor.dsml;
38
39
40 import java.util.Enumeration;
41 import org.xml.sax.DocumentHandler;
42 import org.xml.sax.SAXException;
43 import org.xml.sax.AttributeList;
44 import org.xml.sax.Locator;
45 import org.castor.core.util.Messages;
46
47
48
49
50
51
52
53
54 public abstract class Consumer implements DocumentHandler {
55
56
57 private boolean _insideRoot;
58
59
60 private DocumentHandler _redirect;
61
62
63 public Consumer() {}
64
65
66 public abstract Enumeration getResults();
67
68
69 protected abstract DocumentHandler getEntryConsumer();
70
71
72 public void startElement(String tagName, AttributeList attr) throws SAXException {
73 if (_redirect != null) {
74
75
76 _redirect.startElement(tagName, attr);
77 } else if (tagName.equals(XML.Namespace.ROOT)) {
78
79 if (_insideRoot)
80 throw new SAXException(Messages.format("dsml.elementNested", XML.Namespace.ROOT));
81 _insideRoot = true;
82 } else {
83 if (!_insideRoot)
84 throw new SAXException(
85 Messages.format("dsml.expectingOpeningTag", XML.Namespace.ROOT, tagName));
86 if (tagName.equals(XML.Schema.ELEMENT) || tagName.equals(XML.Entries.ELEMENT)) {
87 DocumentHandler entry;
88
89 entry = getEntryConsumer();
90 entry.startElement(tagName, attr);
91 _redirect = entry;
92 } else {
93 throw new SAXException(Messages.format("dsml.openingTagNotRecognized", tagName));
94 }
95 }
96 }
97
98
99 public void endElement(String tagName) throws SAXException {
100 if (_redirect == null) {
101
102
103 if (tagName.equals(XML.Namespace.ROOT)) {
104 if (_insideRoot == true)
105 _insideRoot = false;
106 else
107 throw new SAXException(Messages.format("dsml.closingOutsideRoot", tagName));
108 } else {
109 throw new SAXException(
110 Messages.format("dsml.expectingClosingTag", XML.Namespace.ROOT, tagName));
111 }
112 } else {
113 _redirect.endElement(tagName);
114
115 if (tagName.equals(XML.Schema.ELEMENT) || tagName.equals(XML.Entries.ELEMENT)) {
116
117
118 _redirect = null;
119 }
120
121
122 }
123 }
124
125
126 public void characters(char[] ch, int offset, int length) throws SAXException {
127 if (_redirect != null) {
128 _redirect.characters(ch, offset, length);
129 }
130 }
131
132
133 public void ignorableWhitespace(char[] ch, int offset, int length) throws SAXException {
134 if (_redirect != null) {
135 _redirect.ignorableWhitespace(ch, offset, length);
136 }
137 }
138
139
140 public void processingInstruction(String target, String instruction) throws SAXException {
141 if (_redirect != null) {
142 _redirect.processingInstruction(target, instruction);
143 }
144 }
145
146
147 public void startDocument() throws SAXException {}
148
149
150 public void endDocument() throws SAXException {
151 if (_insideRoot)
152 throw new SAXException(Messages.message("dsml.documentRootStillOpen"));
153 }
154
155
156 public void setDocumentLocator(Locator locator) {}
157
158
159 }
160