use of org.apache.xml.dtm.DTMIterator in project robovm by robovm.
the class FuncLast method getCountOfContextNodeList.
/**
* Get the position in the current context node list.
*
* @param xctxt non-null reference to XPath runtime context.
*
* @return The number of nodes in the list.
*
* @throws javax.xml.transform.TransformerException
*/
public int getCountOfContextNodeList(XPathContext xctxt) throws javax.xml.transform.TransformerException {
// assert(null != m_contextNodeList, "m_contextNodeList must be non-null");
// If we're in a predicate, then this will return non-null.
SubContextList iter = m_isTopLevel ? null : xctxt.getSubContextList();
// System.out.println("iter: "+iter);
if (null != iter)
return iter.getLastPos(xctxt);
DTMIterator cnl = xctxt.getContextNodeList();
int count;
if (null != cnl) {
count = cnl.getLength();
// System.out.println("count: "+count);
} else
count = 0;
return count;
}
use of org.apache.xml.dtm.DTMIterator in project intellij-community by JetBrains.
the class XalanStyleFrame method eval.
public Value eval(String expr) throws Debugger.EvaluationException {
assert isValid();
try {
final DTMIterator context = myTransformer.getContextNodeList();
final int ctx;
final DTM dtm = context.getDTM(myCurrentNode);
if (dtm.getDocumentRoot(myCurrentNode) == myCurrentNode) {
ctx = dtm.getFirstChild(myCurrentNode);
} else {
ctx = myCurrentNode;
}
final DTMNodeProxy c = new DTMNodeProxy(dtm, ctx);
final PrefixResolver prefixResolver = new PrefixResolverDefault(c) {
public String getNamespaceForPrefix(String prefix, Node context) {
if (context instanceof DTMNodeProxy) {
final DTMNodeProxy proxy = (DTMNodeProxy) context;
final DTM dtm = proxy.getDTM();
int p = proxy.getDTMNodeNumber();
while (p != DTM.NULL) {
int nsNode = dtm.getFirstNamespaceNode(p, true);
while (nsNode != DTM.NULL) {
final String s = dtm.getLocalName(nsNode);
if (s.equals(prefix)) {
return dtm.getNodeValue(nsNode);
}
nsNode = dtm.getNextNamespaceNode(p, nsNode, true);
}
p = dtm.getParent(p);
}
}
return super.getNamespaceForPrefix(prefix, context);
}
};
final XPath xPath = new XPath(expr, myCurrentElement, prefixResolver, XPath.SELECT, myTransformer.getErrorListener());
return new XObjectValue(xPath.execute(myContext, myCurrentNode, myCurrentElement));
} catch (Exception e) {
debug(e);
final String message = e.getMessage();
throw new Debugger.EvaluationException(message != null ? message : e.getClass().getSimpleName());
}
}
use of org.apache.xml.dtm.DTMIterator in project intellij-community by JetBrains.
the class XalanTraceListener method trace.
@Override
public void trace(TracerEvent ev) {
if (myTracing)
return;
// prevents handling of recursive trace() events
myTracing = true;
try {
// init
if (firstTrace) {
firstTrace = false;
final SerializationHandler handler = myTransformer.getSerializationHandler();
myTransformer.setSerializationHandler(new TracingSerializationHandler(myDebugger, handler));
}
super.trace(ev);
final DTMIterator iterator = myTransformer.getContextNodeList();
final int node = myTransformer.getMatchedNode();
final Debugger.SourceFrame sourceFrame = myDebugger.getSourceFrame();
final boolean withSource;
if (sourceFrame == null || ((MySourceFrame) sourceFrame).getMatchedNode() != node) {
myDebugger.pushSource(new MySourceFrame(sourceFrame, iterator.getDTM(node), node));
withSource = true;
} else {
withSource = false;
}
myDebugger.enter(new XalanStyleFrame(ev, myDebugger.getCurrentFrame(), withSource));
} finally {
myTracing = false;
}
}
use of org.apache.xml.dtm.DTMIterator 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.DTMIterator 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;
}
Aggregations