1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49:
50:
70: final public class TeeConsumer
71: implements EventConsumer,
72: ContentHandler, DTDHandler,
73: LexicalHandler,DeclHandler
74: {
75: private EventConsumer first, rest;
76:
77:
78: private ContentHandler docFirst, docRest;
79: private DeclHandler declFirst, declRest;
80: private LexicalHandler lexFirst, lexRest;
81:
82:
83:
92: public TeeConsumer (EventConsumer car, EventConsumer cdr)
93: {
94: if (car == null || cdr == null)
95: throw new NullPointerException ();
96: first = car;
97: rest = cdr;
98:
99:
100:
101:
102: docFirst = first.getContentHandler ();
103: docRest = rest.getContentHandler ();
104:
105:
106: try {
107: declFirst = null;
108: declFirst = (DeclHandler) first.getProperty (
109: EventFilter.DECL_HANDLER);
110: } catch (SAXException e) {}
111: try {
112: declRest = null;
113: declRest = (DeclHandler) rest.getProperty (
114: EventFilter.DECL_HANDLER);
115: } catch (SAXException e) {}
116:
117: try {
118: lexFirst = null;
119: lexFirst = (LexicalHandler) first.getProperty (
120: EventFilter.LEXICAL_HANDLER);
121: } catch (SAXException e) {}
122: try {
123: lexRest = null;
124: lexRest = (LexicalHandler) rest.getProperty (
125: EventFilter.LEXICAL_HANDLER);
126: } catch (SAXException e) {}
127: }
128:
129:
145:
146:
147: public EventConsumer getFirst ()
148: { return first; }
149:
150:
151: public EventConsumer getRest ()
152: { return rest; }
153:
154:
155: final public ContentHandler getContentHandler ()
156: {
157: if (docRest == null)
158: return docFirst;
159: if (docFirst == null)
160: return docRest;
161: return this;
162: }
163:
164:
165: final public DTDHandler getDTDHandler ()
166: {
167:
168: if (rest.getDTDHandler () == null)
169: return first.getDTDHandler ();
170: if (first.getDTDHandler () == null)
171: return rest.getDTDHandler ();
172: return this;
173: }
174:
175:
176: final public Object getProperty (String id)
177: throws SAXNotRecognizedException
178: {
179:
180:
181:
182: Object firstProp = null, restProp = null;
183:
184: try { firstProp = first.getProperty (id); }
185: catch (SAXNotRecognizedException e) { }
186: try { restProp = rest.getProperty (id); }
187: catch (SAXNotRecognizedException e) { }
188:
189: if (restProp == null)
190: return firstProp;
191: if (firstProp == null)
192: return restProp;
193:
194:
195:
196:
197: if (EventFilter.DECL_HANDLER.equals (id))
198: return this;
199: if (EventFilter.LEXICAL_HANDLER.equals (id))
200: return this;
201:
202:
203:
204:
205:
206: throw new SAXNotRecognizedException ("can't tee: " + id);
207: }
208:
209:
213: public void setErrorHandler (ErrorHandler handler)
214: {
215: first.setErrorHandler (handler);
216: rest.setErrorHandler (handler);
217: }
218:
219:
220:
221:
222:
223: public void setDocumentLocator (Locator locator)
224: {
225:
226: docFirst.setDocumentLocator (locator);
227: docRest.setDocumentLocator (locator);
228: }
229:
230: public void startDocument ()
231: throws SAXException
232: {
233: docFirst.startDocument ();
234: docRest.startDocument ();
235: }
236:
237: public void endDocument ()
238: throws SAXException
239: {
240: try {
241: docFirst.endDocument ();
242: } finally {
243: docRest.endDocument ();
244: }
245: }
246:
247: public void startPrefixMapping (String prefix, String uri)
248: throws SAXException
249: {
250: docFirst.startPrefixMapping (prefix, uri);
251: docRest.startPrefixMapping (prefix, uri);
252: }
253:
254: public void endPrefixMapping (String prefix)
255: throws SAXException
256: {
257: docFirst.endPrefixMapping (prefix);
258: docRest.endPrefixMapping (prefix);
259: }
260:
261: public void skippedEntity (String name)
262: throws SAXException
263: {
264: docFirst.skippedEntity (name);
265: docRest.skippedEntity (name);
266: }
267:
268: public void startElement (String uri, String localName,
269: String qName, Attributes atts)
270: throws SAXException
271: {
272: docFirst.startElement (uri, localName, qName, atts);
273: docRest.startElement (uri, localName, qName, atts);
274: }
275:
276: public void endElement (String uri, String localName, String qName)
277: throws SAXException
278: {
279: docFirst.endElement (uri, localName, qName);
280: docRest.endElement (uri, localName, qName);
281: }
282:
283: public void processingInstruction (String target, String data)
284: throws SAXException
285: {
286: docFirst.processingInstruction (target, data);
287: docRest.processingInstruction (target, data);
288: }
289:
290: public void characters (char ch [], int start, int length)
291: throws SAXException
292: {
293: docFirst.characters (ch, start, length);
294: docRest.characters (ch, start, length);
295: }
296:
297: public void ignorableWhitespace (char ch [], int start, int length)
298: throws SAXException
299: {
300: docFirst.ignorableWhitespace (ch, start, length);
301: docRest.ignorableWhitespace (ch, start, length);
302: }
303:
304:
305:
306:
307:
308: public void notationDecl (String name, String publicId, String systemId)
309: throws SAXException
310: {
311: DTDHandler l1 = first.getDTDHandler ();
312: DTDHandler l2 = rest.getDTDHandler ();
313:
314: l1.notationDecl (name, publicId, systemId);
315: l2.notationDecl (name, publicId, systemId);
316: }
317:
318: public void unparsedEntityDecl (String name,
319: String publicId, String systemId,
320: String notationName
321: ) throws SAXException
322: {
323: DTDHandler l1 = first.getDTDHandler ();
324: DTDHandler l2 = rest.getDTDHandler ();
325:
326: l1.unparsedEntityDecl (name, publicId, systemId, notationName);
327: l2.unparsedEntityDecl (name, publicId, systemId, notationName);
328: }
329:
330:
331:
332:
333:
334: public void attributeDecl (String eName, String aName,
335: String type,
336: String mode, String value)
337: throws SAXException
338: {
339: declFirst.attributeDecl (eName, aName, type, mode, value);
340: declRest.attributeDecl (eName, aName, type, mode, value);
341: }
342:
343: public void elementDecl (String name, String model)
344: throws SAXException
345: {
346: declFirst.elementDecl (name, model);
347: declRest.elementDecl (name, model);
348: }
349:
350: public void externalEntityDecl (String name,
351: String publicId, String systemId)
352: throws SAXException
353: {
354: declFirst.externalEntityDecl (name, publicId, systemId);
355: declRest.externalEntityDecl (name, publicId, systemId);
356: }
357:
358: public void internalEntityDecl (String name, String value)
359: throws SAXException
360: {
361: declFirst.internalEntityDecl (name, value);
362: declRest.internalEntityDecl (name, value);
363: }
364:
365:
366:
367:
368:
369: public void comment (char ch [], int start, int length)
370: throws SAXException
371: {
372: lexFirst.comment (ch, start, length);
373: lexRest.comment (ch, start, length);
374: }
375:
376: public void startCDATA ()
377: throws SAXException
378: {
379: lexFirst.startCDATA ();
380: lexRest.startCDATA ();
381: }
382:
383: public void endCDATA ()
384: throws SAXException
385: {
386: lexFirst.endCDATA ();
387: lexRest.endCDATA ();
388: }
389:
390: public void startEntity (String name)
391: throws SAXException
392: {
393: lexFirst.startEntity (name);
394: lexRest.startEntity (name);
395: }
396:
397: public void endEntity (String name)
398: throws SAXException
399: {
400: lexFirst.endEntity (name);
401: lexRest.endEntity (name);
402: }
403:
404: public void startDTD (String name, String publicId, String systemId)
405: throws SAXException
406: {
407: lexFirst.startDTD (name, publicId, systemId);
408: lexRest.startDTD (name, publicId, systemId);
409: }
410:
411: public void endDTD ()
412: throws SAXException
413: {
414: lexFirst.endDTD ();
415: lexRest.endDTD ();
416: }
417: }