package gnu.xml.pipeline;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.DTDHandler;
import org.xml.sax.ErrorHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.ext.DeclHandler;
import org.xml.sax.ext.LexicalHandler;
final public class TeeConsumer
implements EventConsumer,
ContentHandler, DTDHandler,
LexicalHandler,DeclHandler
{
private EventConsumer first, rest;
private ContentHandler docFirst, docRest;
private DeclHandler declFirst, declRest;
private LexicalHandler lexFirst, lexRest;
public TeeConsumer (EventConsumer car, EventConsumer cdr)
{
if (car == null || cdr == null)
throw new NullPointerException ();
first = car;
rest = cdr;
docFirst = first.getContentHandler ();
docRest = rest.getContentHandler ();
try {
declFirst = null;
declFirst = (DeclHandler) first.getProperty (
EventFilter.DECL_HANDLER);
} catch (SAXException e) {}
try {
declRest = null;
declRest = (DeclHandler) rest.getProperty (
EventFilter.DECL_HANDLER);
} catch (SAXException e) {}
try {
lexFirst = null;
lexFirst = (LexicalHandler) first.getProperty (
EventFilter.LEXICAL_HANDLER);
} catch (SAXException e) {}
try {
lexRest = null;
lexRest = (LexicalHandler) rest.getProperty (
EventFilter.LEXICAL_HANDLER);
} catch (SAXException e) {}
}
public EventConsumer getFirst ()
{ return first; }
public EventConsumer getRest ()
{ return rest; }
final public ContentHandler getContentHandler ()
{
if (docRest == null)
return docFirst;
if (docFirst == null)
return docRest;
return this;
}
final public DTDHandler getDTDHandler ()
{
if (rest.getDTDHandler () == null)
return first.getDTDHandler ();
if (first.getDTDHandler () == null)
return rest.getDTDHandler ();
return this;
}
final public Object getProperty (String id)
throws SAXNotRecognizedException
{
Object firstProp = null, restProp = null;
try { firstProp = first.getProperty (id); }
catch (SAXNotRecognizedException e) { }
try { restProp = rest.getProperty (id); }
catch (SAXNotRecognizedException e) { }
if (restProp == null)
return firstProp;
if (firstProp == null)
return restProp;
if (EventFilter.DECL_HANDLER.equals (id))
return this;
if (EventFilter.LEXICAL_HANDLER.equals (id))
return this;
throw new SAXNotRecognizedException ("can't tee: " + id);
}
public void setErrorHandler (ErrorHandler handler)
{
first.setErrorHandler (handler);
rest.setErrorHandler (handler);
}
public void setDocumentLocator (Locator locator)
{
docFirst.setDocumentLocator (locator);
docRest.setDocumentLocator (locator);
}
public void startDocument ()
throws SAXException
{
docFirst.startDocument ();
docRest.startDocument ();
}
public void endDocument ()
throws SAXException
{
try {
docFirst.endDocument ();
} finally {
docRest.endDocument ();
}
}
public void startPrefixMapping (String prefix, String uri)
throws SAXException
{
docFirst.startPrefixMapping (prefix, uri);
docRest.startPrefixMapping (prefix, uri);
}
public void endPrefixMapping (String prefix)
throws SAXException
{
docFirst.endPrefixMapping (prefix);
docRest.endPrefixMapping (prefix);
}
public void skippedEntity (String name)
throws SAXException
{
docFirst.skippedEntity (name);
docRest.skippedEntity (name);
}
public void startElement (String uri, String localName,
String qName, Attributes atts)
throws SAXException
{
docFirst.startElement (uri, localName, qName, atts);
docRest.startElement (uri, localName, qName, atts);
}
public void endElement (String uri, String localName, String qName)
throws SAXException
{
docFirst.endElement (uri, localName, qName);
docRest.endElement (uri, localName, qName);
}
public void processingInstruction (String target, String data)
throws SAXException
{
docFirst.processingInstruction (target, data);
docRest.processingInstruction (target, data);
}
public void characters (char ch [], int start, int length)
throws SAXException
{
docFirst.characters (ch, start, length);
docRest.characters (ch, start, length);
}
public void ignorableWhitespace (char ch [], int start, int length)
throws SAXException
{
docFirst.ignorableWhitespace (ch, start, length);
docRest.ignorableWhitespace (ch, start, length);
}
public void notationDecl (String name, String publicId, String systemId)
throws SAXException
{
DTDHandler l1 = first.getDTDHandler ();
DTDHandler l2 = rest.getDTDHandler ();
l1.notationDecl (name, publicId, systemId);
l2.notationDecl (name, publicId, systemId);
}
public void unparsedEntityDecl (String name,
String publicId, String systemId,
String notationName
) throws SAXException
{
DTDHandler l1 = first.getDTDHandler ();
DTDHandler l2 = rest.getDTDHandler ();
l1.unparsedEntityDecl (name, publicId, systemId, notationName);
l2.unparsedEntityDecl (name, publicId, systemId, notationName);
}
public void attributeDecl (String eName, String aName,
String type,
String mode, String value)
throws SAXException
{
declFirst.attributeDecl (eName, aName, type, mode, value);
declRest.attributeDecl (eName, aName, type, mode, value);
}
public void elementDecl (String name, String model)
throws SAXException
{
declFirst.elementDecl (name, model);
declRest.elementDecl (name, model);
}
public void externalEntityDecl (String name,
String publicId, String systemId)
throws SAXException
{
declFirst.externalEntityDecl (name, publicId, systemId);
declRest.externalEntityDecl (name, publicId, systemId);
}
public void internalEntityDecl (String name, String value)
throws SAXException
{
declFirst.internalEntityDecl (name, value);
declRest.internalEntityDecl (name, value);
}
public void comment (char ch [], int start, int length)
throws SAXException
{
lexFirst.comment (ch, start, length);
lexRest.comment (ch, start, length);
}
public void startCDATA ()
throws SAXException
{
lexFirst.startCDATA ();
lexRest.startCDATA ();
}
public void endCDATA ()
throws SAXException
{
lexFirst.endCDATA ();
lexRest.endCDATA ();
}
public void startEntity (String name)
throws SAXException
{
lexFirst.startEntity (name);
lexRest.startEntity (name);
}
public void endEntity (String name)
throws SAXException
{
lexFirst.endEntity (name);
lexRest.endEntity (name);
}
public void startDTD (String name, String publicId, String systemId)
throws SAXException
{
lexFirst.startDTD (name, publicId, systemId);
lexRest.startDTD (name, publicId, systemId);
}
public void endDTD ()
throws SAXException
{
lexFirst.endDTD ();
lexRest.endDTD ();
}
}