use of org.apache.xml.dtm.DTM in project robovm by robovm.
the class TransformerImpl method transformNode.
/**
* Process the source node to the output result, if the
* processor supports the "http://xml.org/trax/features/dom/input"
* feature.
* %REVIEW% Do we need a Node version of this?
* @param node The input source node, which can be any valid DTM node.
*
* @throws TransformerException
*/
public void transformNode(int node) throws TransformerException {
//dml
setExtensionsTable(getStylesheet());
// Make sure we're not writing to the same output content handler.
synchronized (m_serializationHandler) {
m_hasBeenReset = false;
XPathContext xctxt = getXPathContext();
DTM dtm = xctxt.getDTM(node);
try {
pushGlobalVars(node);
// ==========
// Give the top-level templates a chance to pass information into
// the context (this is mainly for setting up tables for extensions).
StylesheetRoot stylesheet = this.getStylesheet();
int n = stylesheet.getGlobalImportCount();
for (int i = 0; i < n; i++) {
StylesheetComposed imported = stylesheet.getGlobalImport(i);
int includedCount = imported.getIncludeCountComposed();
for (int j = -1; j < includedCount; j++) {
Stylesheet included = imported.getIncludeComposed(j);
included.runtimeInit(this);
for (ElemTemplateElement child = included.getFirstChildElem(); child != null; child = child.getNextSiblingElem()) {
child.runtimeInit(this);
}
}
}
// ===========
// System.out.println("Calling applyTemplateToNode - "+Thread.currentThread().getName());
DTMIterator dtmIter = new org.apache.xpath.axes.SelfIteratorNoPredicate();
dtmIter.setRoot(node, xctxt);
xctxt.pushContextNodeList(dtmIter);
try {
this.applyTemplateToNode(null, null, node);
} finally {
xctxt.popContextNodeList();
}
// System.out.println("Done with applyTemplateToNode - "+Thread.currentThread().getName());
if (null != m_serializationHandler) {
m_serializationHandler.endDocument();
}
} catch (Exception se) {
// SAXSourceLocator
while (se instanceof org.apache.xml.utils.WrappedRuntimeException) {
Exception e = ((org.apache.xml.utils.WrappedRuntimeException) se).getException();
if (null != e)
se = e;
}
if (null != m_serializationHandler) {
try {
if (se instanceof org.xml.sax.SAXParseException)
m_serializationHandler.fatalError((org.xml.sax.SAXParseException) se);
else if (se instanceof TransformerException) {
TransformerException te = ((TransformerException) se);
SAXSourceLocator sl = new SAXSourceLocator(te.getLocator());
m_serializationHandler.fatalError(new org.xml.sax.SAXParseException(te.getMessage(), sl, te));
} else {
m_serializationHandler.fatalError(new org.xml.sax.SAXParseException(se.getMessage(), new SAXSourceLocator(), se));
}
} catch (Exception e) {
}
}
if (se instanceof TransformerException) {
m_errorHandler.fatalError((TransformerException) se);
} else if (se instanceof org.xml.sax.SAXParseException) {
m_errorHandler.fatalError(new TransformerException(se.getMessage(), new SAXSourceLocator((org.xml.sax.SAXParseException) se), se));
} else {
m_errorHandler.fatalError(new TransformerException(se));
}
} finally {
this.reset();
}
}
}
use of org.apache.xml.dtm.DTM in project robovm by robovm.
the class TransformerImpl method transform.
/**
* Process the source tree to SAX parse events.
* @param source The input for the source tree.
* @param shouldRelease Flag indicating whether to release DTMManager.
*
* @throws TransformerException
*/
public void transform(Source source, boolean shouldRelease) throws TransformerException {
try {
// here or in reset(). -is
if (getXPathContext().getNamespaceContext() == null) {
getXPathContext().setNamespaceContext(getStylesheet());
}
String base = source.getSystemId();
// If no systemID of the source, use the base of the stylesheet.
if (null == base) {
base = m_stylesheetRoot.getBaseIdentifier();
}
// As a last resort, use the current user dir.
if (null == base) {
String currentDir = "";
try {
currentDir = System.getProperty("user.dir");
}// user.dir not accessible from applet
catch (SecurityException se) {
}
if (currentDir.startsWith(java.io.File.separator))
base = "file://" + currentDir;
else
base = "file:///" + currentDir;
base = base + java.io.File.separatorChar + source.getClass().getName();
}
setBaseURLOfSource(base);
DTMManager mgr = m_xcontext.getDTMManager();
/*
* According to JAXP1.2, new SAXSource()/StreamSource()
* should create an empty input tree, with a default root node.
* new DOMSource()creates an empty document using DocumentBuilder.
* newDocument(); Use DocumentBuilder.newDocument() for all 3 situations,
* since there is no clear spec. how to create an empty tree when
* both SAXSource() and StreamSource() are used.
*/
if ((source instanceof StreamSource && source.getSystemId() == null && ((StreamSource) source).getInputStream() == null && ((StreamSource) source).getReader() == null) || (source instanceof SAXSource && ((SAXSource) source).getInputSource() == null && ((SAXSource) source).getXMLReader() == null) || (source instanceof DOMSource && ((DOMSource) source).getNode() == null)) {
try {
DocumentBuilderFactory builderF = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderF.newDocumentBuilder();
String systemID = source.getSystemId();
source = new DOMSource(builder.newDocument());
// Copy system ID from original, empty Source to new Source
if (systemID != null) {
source.setSystemId(systemID);
}
} catch (ParserConfigurationException e) {
fatalError(e);
}
}
DTM dtm = mgr.getDTM(source, false, this, true, true);
dtm.setDocumentBaseURI(base);
// %REVIEW% I have to think about this. -sb
boolean hardDelete = true;
try {
// NOTE: This will work because this is _NOT_ a shared DTM, and thus has
// only a single Document node. If it could ever be an RTF or other
// shared DTM, look at dtm.getDocumentRoot(nodeHandle).
this.transformNode(dtm.getDocument());
} finally {
if (shouldRelease)
mgr.release(dtm, hardDelete);
}
// Kick off the parse. When the ContentHandler gets
// the startDocument event, it will call transformNode( node ).
// reader.parse( xmlSource );
// This has to be done to catch exceptions thrown from
// the transform thread spawned by the STree handler.
Exception e = getExceptionThrown();
if (null != e) {
if (e instanceof javax.xml.transform.TransformerException) {
throw (javax.xml.transform.TransformerException) e;
} else if (e instanceof org.apache.xml.utils.WrappedRuntimeException) {
fatalError(((org.apache.xml.utils.WrappedRuntimeException) e).getException());
} else {
throw new javax.xml.transform.TransformerException(e);
}
} else if (null != m_serializationHandler) {
m_serializationHandler.endDocument();
}
} catch (org.apache.xml.utils.WrappedRuntimeException wre) {
Throwable throwable = wre.getException();
while (throwable instanceof org.apache.xml.utils.WrappedRuntimeException) {
throwable = ((org.apache.xml.utils.WrappedRuntimeException) throwable).getException();
}
fatalError(throwable);
}// Patch attributed to David Eisenberg <david@catcode.com>
catch (org.xml.sax.SAXParseException spe) {
fatalError(spe);
} catch (org.xml.sax.SAXException se) {
m_errorHandler.fatalError(new TransformerException(se));
} finally {
m_hasTransformThreadErrorCatcher = false;
// This looks to be redundent to the one done in TransformNode.
reset();
}
}
use of org.apache.xml.dtm.DTM in project robovm by robovm.
the class DTMManagerDefault method getDTMHandleFromNode.
/**
* Given a W3C DOM node, try and return a DTM handle.
* Note: calling this may be non-optimal, and there is no guarantee that
* the node will be found in any particular DTM.
*
* @param node Non-null reference to a DOM node.
*
* @return a valid DTM handle.
*/
public synchronized int getDTMHandleFromNode(org.w3c.dom.Node node) {
if (null == node)
//"node must be non-null for getDTMHandleFromNode!");
throw new IllegalArgumentException(XMLMessages.createXMLMessage(XMLErrorResources.ER_NODE_NON_NULL, null));
if (node instanceof org.apache.xml.dtm.ref.DTMNodeProxy)
return ((org.apache.xml.dtm.ref.DTMNodeProxy) node).getDTMNodeNumber();
else {
// Find the DOM2DTMs wrapped around this Document (if any)
// and check whether they contain the Node in question.
//
// NOTE that since a DOM2DTM may represent a subtree rather
// than a full document, we have to be prepared to check more
// than one -- and there is no guarantee that we will find
// one that contains ancestors or siblings of the node we're
// seeking.
//
// %REVIEW% We could search for the one which contains this
// node at the deepest level, and thus covers the widest
// subtree, but that's going to entail additional work
// checking more DTMs... and getHandleOfNode is not a
// cheap operation in most implementations.
//
// TODO: %REVIEW% If overflow addressing, we may recheck a DTM
// already examined. Ouch. But with the increased number of DTMs,
// scanning back to check this is painful.
// POSSIBLE SOLUTIONS:
// Generate a list of _unique_ DTM objects?
// Have each DTM cache last DOM node search?
int max = m_dtms.length;
for (int i = 0; i < max; i++) {
DTM thisDTM = m_dtms[i];
if ((null != thisDTM) && thisDTM instanceof DOM2DTM) {
int handle = ((DOM2DTM) thisDTM).getHandleOfNode(node);
if (handle != DTM.NULL)
return handle;
}
}
// Not found; generate a new DTM.
//
// %REVIEW% Is this really desirable, or should we return null
// and make folks explicitly instantiate from a DOMSource? The
// latter is more work but gives the caller the opportunity to
// explicitly add the DTM to a DTMManager... and thus to know when
// it can be discarded again, which is something we need to pay much
// more attention to. (Especially since only DTMs which are assigned
// to a manager can use the overflow addressing scheme.)
//
// %BUG% If the source node was a DOM2DTM$defaultNamespaceDeclarationNode
// and the DTM wasn't registered with this DTMManager, we will create
// a new DTM and _still_ not be able to find the node (since it will
// be resynthesized). Another reason to push hard on making all DTMs
// be managed DTMs.
// Since the real root of our tree may be a DocumentFragment, we need to
// use getParent to find the root, instead of getOwnerDocument. Otherwise
// DOM2DTM#getHandleOfNode will be very unhappy.
Node root = node;
Node p = (root.getNodeType() == Node.ATTRIBUTE_NODE) ? ((org.w3c.dom.Attr) root).getOwnerElement() : root.getParentNode();
for (; p != null; p = p.getParentNode()) {
root = p;
}
DOM2DTM dtm = (DOM2DTM) getDTM(new javax.xml.transform.dom.DOMSource(root), false, null, true, true);
int handle;
if (node instanceof org.apache.xml.dtm.ref.dom2dtm.DOM2DTMdefaultNamespaceDeclarationNode) {
// Can't return the same node since it's unique to a specific DTM,
// but can return the equivalent node -- find the corresponding
// Document Element, then ask it for the xml: namespace decl.
handle = dtm.getHandleOfNode(((org.w3c.dom.Attr) node).getOwnerElement());
handle = dtm.getAttributeNode(handle, node.getNamespaceURI(), node.getLocalName());
} else
handle = ((DOM2DTM) dtm).getHandleOfNode(node);
if (DTM.NULL == handle)
//"Could not resolve the node to a handle!");
throw new RuntimeException(XMLMessages.createXMLMessage(XMLErrorResources.ER_COULD_NOT_RESOLVE_NODE, null));
return handle;
}
}
use of org.apache.xml.dtm.DTM in project robovm by robovm.
the class TransformerImpl method applyTemplateToNode.
/**
* Given an element and mode, find the corresponding
* template and process the contents.
*
* @param xslInstruction The calling element.
* @param template The template to use if xsl:for-each, current template for apply-imports, or null.
* @param child The source context node.
* @throws TransformerException
* @return true if applied a template, false if not.
* @xsl.usage advanced
*/
public // xsl:apply-templates or xsl:for-each
boolean applyTemplateToNode(// xsl:apply-templates or xsl:for-each
ElemTemplateElement xslInstruction, ElemTemplate template, int child) throws TransformerException {
DTM dtm = m_xcontext.getDTM(child);
short nodeType = dtm.getNodeType(child);
boolean isDefaultTextRule = false;
boolean isApplyImports = false;
isApplyImports = ((xslInstruction == null) ? false : xslInstruction.getXSLToken() == Constants.ELEMNAME_APPLY_IMPORTS);
if (null == template || isApplyImports) {
int maxImportLevel, endImportLevel = 0;
if (isApplyImports) {
maxImportLevel = template.getStylesheetComposed().getImportCountComposed() - 1;
endImportLevel = template.getStylesheetComposed().getEndImportCountComposed();
} else {
maxImportLevel = -1;
}
// We want to match -no- templates. See bugzilla bug 1170.
if (isApplyImports && (maxImportLevel == -1)) {
template = null;
} else {
// Find the XSL template that is the best match for the
// element.
XPathContext xctxt = m_xcontext;
try {
xctxt.pushNamespaceContext(xslInstruction);
QName mode = this.getMode();
if (isApplyImports)
template = m_stylesheetRoot.getTemplateComposed(xctxt, child, mode, maxImportLevel, endImportLevel, m_quietConflictWarnings, dtm);
else
template = m_stylesheetRoot.getTemplateComposed(xctxt, child, mode, m_quietConflictWarnings, dtm);
} finally {
xctxt.popNamespaceContext();
}
}
// See http://www.w3.org/TR/xslt#built-in-rule.
if (null == template) {
switch(nodeType) {
case DTM.DOCUMENT_FRAGMENT_NODE:
case DTM.ELEMENT_NODE:
template = m_stylesheetRoot.getDefaultRule();
break;
case DTM.CDATA_SECTION_NODE:
case DTM.TEXT_NODE:
case DTM.ATTRIBUTE_NODE:
template = m_stylesheetRoot.getDefaultTextRule();
isDefaultTextRule = true;
break;
case DTM.DOCUMENT_NODE:
template = m_stylesheetRoot.getDefaultRootRule();
break;
default:
// No default rules for processing instructions and the like.
return false;
}
}
}
// the value directly to the result tree.
try {
pushElemTemplateElement(template);
m_xcontext.pushCurrentNode(child);
pushPairCurrentMatched(template, child);
// Fix copy copy29 test.
if (!isApplyImports) {
DTMIterator cnl = new org.apache.xpath.NodeSetDTM(child, m_xcontext.getDTMManager());
m_xcontext.pushContextNodeList(cnl);
}
if (isDefaultTextRule) {
switch(nodeType) {
case DTM.CDATA_SECTION_NODE:
case DTM.TEXT_NODE:
ClonerToResultTree.cloneToResultTree(child, nodeType, dtm, getResultTreeHandler(), false);
break;
case DTM.ATTRIBUTE_NODE:
dtm.dispatchCharactersEvents(child, getResultTreeHandler(), false);
break;
}
} else {
// And execute the child templates.
// 9/11/00: If template has been compiled, hand off to it
// since much (most? all?) of the processing has been inlined.
// (It would be nice if there was a single entry point that
// worked for both... but the interpretive system works by
// having the Tranformer execute the children, while the
// compiled obviously has to run its own code. It's
// also unclear that "execute" is really the right name for
// that entry point.)
m_xcontext.setSAXLocator(template);
// m_xcontext.getVarStack().link();
m_xcontext.getVarStack().link(template.m_frameSize);
executeChildTemplates(template, true);
}
} catch (org.xml.sax.SAXException se) {
throw new TransformerException(se);
} finally {
if (!isDefaultTextRule)
m_xcontext.getVarStack().unlink();
m_xcontext.popCurrentNode();
if (!isApplyImports) {
m_xcontext.popContextNodeList();
}
popCurrentMatched();
popElemTemplateElement();
}
return true;
}
use of org.apache.xml.dtm.DTM in project robovm by robovm.
the class XalanTransformState method resetState.
/**
* @see org.apache.xml.serializer.TransformStateSetter#resetState(Transformer)
*/
public void resetState(Transformer transformer) {
if ((transformer != null) && (transformer instanceof TransformerImpl)) {
m_transformer = (TransformerImpl) transformer;
m_currentElement = m_transformer.getCurrentElement();
m_currentTemplate = m_transformer.getCurrentTemplate();
m_matchedTemplate = m_transformer.getMatchedTemplate();
int currentNodeHandle = m_transformer.getCurrentNode();
DTM dtm = m_transformer.getXPathContext().getDTM(currentNodeHandle);
m_currentNode = dtm.getNode(currentNodeHandle);
m_matchedNode = m_transformer.getMatchedNode();
m_contextNodeList = m_transformer.getContextNodeList();
}
}
Aggregations