package gnu.xml.dom;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Node;
import org.w3c.dom.Text;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.ext.Attributes2;
import gnu.xml.pipeline.DomConsumer;
import gnu.xml.pipeline.EventConsumer;
public class Consumer extends DomConsumer
{
public Consumer ()
throws SAXException
{
super (DomDocument.class);
setHandler (new Backdoor (this));
}
public Consumer (EventConsumer next)
throws SAXException
{
super (DomDocument.class, next);
setHandler (new Backdoor (this));
}
public static class Backdoor extends DomConsumer.Handler
{
protected Backdoor (DomConsumer consumer)
throws SAXException
{ super (consumer); }
private DomDoctype getDoctype ()
throws SAXException
{
DomDocument doc = (DomDocument) getDocument ();
DocumentType dt = doc.getDoctype ();
if (dt == null)
throw new SAXException ("doctype missing!");
return (DomDoctype) dt;
}
public void startDTD (String name, String publicId, String systemId)
throws SAXException
{
DomDocument doc = (DomDocument) getDocument ();
super.startDTD (name, publicId, systemId);
DomDoctype dt = new DomDoctype (doc, name, publicId, systemId);
doc.appendChild (dt);
}
public void endDTD ()
throws SAXException
{
super.endDTD ();
getDoctype ().makeReadonly ();
}
public void notationDecl (
String name,
String publicId, String systemId
) throws SAXException
{
getDoctype ().declareNotation (name, publicId, systemId);
}
public void unparsedEntityDecl (
String name,
String publicId, String systemId,
String notationName
) throws SAXException
{
getDoctype ().declareEntity (name, publicId, systemId,
notationName);
}
public void internalEntityDecl (String name, String value)
throws SAXException
{
getDoctype ().declareEntity (name, null, null, null);
}
public void externalEntityDecl (
String name,
String publicId,
String systemId
) throws SAXException
{
getDoctype ().declareEntity (name, publicId, systemId, null);
}
public void startElement (
String uri,
String localName,
String qName,
Attributes atts
) throws SAXException
{
Node top;
super.startElement (uri, localName, qName, atts);
top = getTop ();
if (!top.hasAttributes () || !(atts instanceof Attributes2))
return;
DomNamedNodeMap map = (DomNamedNodeMap) top.getAttributes ();
Attributes2 attrs = (Attributes2) atts;
int length = atts.getLength ();
for (int i = 0; i < length; i++) {
if (attrs.isSpecified (i))
continue;
String temp = attrs.getQName (i);
DomAttr attr;
if ("".equals (temp))
attr = (DomAttr) map.getNamedItemNS (attrs.getURI (i),
atts.getLocalName (i));
else
attr = (DomAttr) map.getNamedItem (temp);
attr.setSpecified (false);
}
}
public void endElement (
String uri,
String localName,
String qName
) throws SAXException
{
DomNode top = (DomNode) getTop ();
top.compact ();
super.endElement (uri, localName, qName);
}
protected Text createText (
boolean isCDATA,
char buf [],
int off,
int len
) {
DomDocument doc = (DomDocument) getDocument ();
if (isCDATA)
return doc.createCDATASection (buf, off, len);
else
return doc.createTextNode (buf, off, len);
}
public void elementDecl(String name, String model)
throws SAXException
{
getDoctype().elementDecl(name, model);
}
public void attributeDecl (
String ename,
String aname,
String type,
String mode,
String value
) throws SAXException
{
getDoctype().attributeDecl(ename, aname, type, mode, value);
}
public void startDocument () throws SAXException
{
super.startDocument ();
DomDocument doc = (DomDocument) getDocument ();
doc.setStrictErrorChecking(false);
doc.setBuilding(true);
}
public void xmlDecl(String version,
String encoding,
boolean standalone,
String inputEncoding)
throws SAXException
{
super.xmlDecl(version, encoding, standalone, inputEncoding);
DomDocument doc = (DomDocument) getDocument();
doc.setXmlEncoding(encoding);
doc.setInputEncoding(inputEncoding);
}
public void endDocument ()
throws SAXException
{
DomDocument doc = (DomDocument) getDocument ();
doc.setStrictErrorChecking(true);
doc.setBuilding(false);
doc.compact ();
DomDoctype doctype = (DomDoctype) doc.getDoctype();
if (doctype != null)
{
doctype.makeReadonly();
}
super.endDocument ();
}
public boolean canPopulateEntityRefs ()
{ return true; }
public void startEntity (String name)
throws SAXException
{
if (name.charAt (0) == '%' || "[dtd]".equals (name))
return;
super.startEntity (name);
DomNode top = (DomNode) getTop ();
if (top.getNodeType () == Node.ENTITY_REFERENCE_NODE)
top.readonly = false;
}
public void endEntity (String name)
throws SAXException
{
if (name.charAt (0) == '%' || "[dtd]".equals (name))
return;
DomNode top = (DomNode) getTop ();
if (top.getNodeType () == Node.ENTITY_REFERENCE_NODE) {
top.compact ();
top.makeReadonly ();
}
super.endEntity (name);
}
}
}