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