package gnu.xml.dom;
import java.io.IOException;
import org.w3c.dom.Document;
import org.w3c.dom.DOMImplementation;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
public final class JAXPFactory
extends DocumentBuilderFactory
{
private static final String PROPERTY = "http://xml.org/sax/properties/";
private static final String FEATURE = "http://xml.org/sax/features/";
private SAXParserFactory pf;
public JAXPFactory()
{
}
public DocumentBuilder newDocumentBuilder()
throws ParserConfigurationException
{
if (pf == null)
{
pf = new gnu.xml.aelfred2.JAXPFactory();
}
pf.setValidating(isValidating());
pf.setNamespaceAware(isNamespaceAware());
try
{
pf.setFeature(FEATURE + "namespace-prefixes", true);
return new JAXPBuilder(pf.newSAXParser().getXMLReader(), this);
}
catch (SAXException e)
{
String msg = "can't create JAXP DocumentBuilder: " + e.getMessage();
throw new ParserConfigurationException(msg);
}
}
public void setAttribute(String name, Object value)
throws IllegalArgumentException
{
if ("http://java.sun.com/xml/jaxp/properties/schemaLanguage".equals(name))
{
}
else
{
throw new IllegalArgumentException(name);
}
}
public Object getAttribute(String name)
throws IllegalArgumentException
{
throw new IllegalArgumentException(name);
}
static final class JAXPBuilder
extends DocumentBuilder
implements ErrorHandler
{
private Consumer consumer;
private XMLReader producer;
private DomImpl impl;
JAXPBuilder(XMLReader parser, JAXPFactory factory)
throws ParserConfigurationException
{
impl = new DomImpl();
try
{
consumer = new Consumer();
}
catch (SAXException e)
{
throw new ParserConfigurationException(e.getMessage());
}
consumer.setHidingReferences(factory.isExpandEntityReferences());
consumer.setHidingComments(factory.isIgnoringComments());
consumer.setHidingWhitespace(factory.isIgnoringElementContentWhitespace());
consumer.setHidingCDATA(factory.isCoalescing());
producer = parser;
producer.setContentHandler(consumer.getContentHandler());
producer.setDTDHandler(consumer.getDTDHandler());
try
{
String id;
if (factory.isValidating ())
{
producer.setFeature(FEATURE + "validation", true);
producer.setErrorHandler(this);
}
producer.setFeature(FEATURE + "namespace-prefixes", true);
producer.setFeature(FEATURE + "namespaces",
factory.isNamespaceAware());
id = PROPERTY + "lexical-handler";
producer.setProperty(id, consumer.getProperty(id));
id = PROPERTY + "declaration-handler";
producer.setProperty(id, consumer.getProperty(id));
}
catch (SAXException e)
{
throw new ParserConfigurationException(e.getMessage());
}
}
public Document parse(InputSource source)
throws SAXException, IOException
{
producer.parse(source);
Document doc = consumer.getDocument();
doc.setDocumentURI(source.getSystemId());
return doc;
}
public boolean isNamespaceAware()
{
try
{
return producer.getFeature(FEATURE + "namespaces");
}
catch (SAXException e)
{
throw new RuntimeException(e.getMessage());
}
}
public boolean isValidating()
{
try
{
return producer.getFeature(FEATURE + "validation");
}
catch (SAXException e)
{
throw new RuntimeException(e.getMessage());
}
}
public void setEntityResolver(EntityResolver resolver)
{
producer.setEntityResolver(resolver);
}
public void setErrorHandler(ErrorHandler handler)
{
producer.setErrorHandler(handler);
consumer.setErrorHandler(handler);
}
public DOMImplementation getDOMImplementation()
{
return impl;
}
public Document newDocument()
{
return new DomDocument();
}
public void fatalError(SAXParseException e)
throws SAXException
{
throw e;
}
public void error(SAXParseException e)
throws SAXException
{
throw e;
}
public void warning(SAXParseException e)
throws SAXException
{
}
}
}