private DOMImpl getDOM(Source source, int mask)
throws TransformerException
{
try {
DOMImpl dom = null;
DTDMonitor dtd = null;
// Get systemId from source
if (source != null) {
_sourceSystemId = source.getSystemId();
}
if (source instanceof SAXSource) {
// Get all info from the input SAXSource object
final SAXSource sax = (SAXSource)source;
XMLReader reader = sax.getXMLReader();
final InputSource input = sax.getInputSource();
// Create a reader if not set by user
if (reader == null) {
reader = _tfactory.getXMLReader();
}
// Create a DTD monitor to trap all DTD/declarative events
dtd = new DTDMonitor();
dtd.handleDTD(reader);
// Create a new internal DOM and set up its builder
dom = new DOMImpl();
final DOMBuilder builder = dom.getBuilder();
try {
reader.setProperty(LEXICAL_HANDLER_PROPERTY, builder);
}
catch (SAXException e) {
// quitely ignored
}
reader.setContentHandler(builder);
// Parse the input and build the internal DOM
reader.parse(input);
dom.setDocumentURI(_sourceSystemId);
}
else if (source instanceof DOMSource) {
final DOMSource domsrc = (DOMSource) source;
final org.w3c.dom.Node node = domsrc.getNode();
final DOM2SAX dom2sax = new DOM2SAX(node);
// Create a DTD monitor to trap all DTD/declarative events
dtd = new DTDMonitor();
dtd.handleDTD(dom2sax);
// Create a new internal DOM and set up its builder to trap
// all content/lexical events
dom = new DOMImpl();
final DOMBuilder builder = dom.getBuilder();
dom2sax.setContentHandler(builder);
// Parse the input and build the internal DOM
dom2sax.parse();
dom.setDocumentURI(_sourceSystemId);
}
else if (source instanceof StreamSource) {
// Get all info from the input StreamSource object
final StreamSource stream = (StreamSource)source;
final InputStream streamInput = stream.getInputStream();
final Reader streamReader = stream.getReader();
final XMLReader reader = _tfactory.getXMLReader();
// Create a DTD monitor to trap all DTD/declarative events
dtd = new DTDMonitor();
dtd.handleDTD(reader);
// Create a new internal DOM and set up its builder to trap
// all content/lexical events
dom = new DOMImpl();
final DOMBuilder builder = dom.getBuilder();
try {
reader.setProperty(LEXICAL_HANDLER_PROPERTY, builder);
}
catch (SAXException e) {
// quitely ignored
}
reader.setContentHandler(builder);
InputSource input;
if (streamInput != null) {
input = new InputSource(streamInput);
input.setSystemId(_sourceSystemId);
}
else if (streamReader != null) {
input = new InputSource(streamReader);
input.setSystemId(_sourceSystemId);
}
else if (_sourceSystemId != null) {
input = new InputSource(_sourceSystemId);
}
else {
ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_NO_SOURCE_ERR);
throw new TransformerException(err.toString());
}
// Parse the input and build the internal DOM
reader.parse(input);
dom.setDocumentURI(_sourceSystemId);
}
else if (source instanceof XSLTCSource) {
final XSLTCSource xsltcsrc = (XSLTCSource)source;
dtd = xsltcsrc.getDTD();
dom = xsltcsrc.getDOM();
}
// DOM already set via a call to setDOM()
else if (_dom != null) {
dtd = _dtdMonitor; // must be set via setDTDMonitor()
dom = _dom; _dom = null; // use only once, so reset to 'null'
}
else {
return null;
}
// Set size of key/id indices
if (!_isIdentity) {
_translet.setIndexSize(dom.getSize());
// If there are any elements with ID attributes, build an index
dtd.buildIdIndex(dom, mask, _translet);
// Pass unparsed entity URIs to the translet
_translet.setDTDMonitor(dtd);
}
return dom;