use of org.apache.xml.dtm.DTM in project j2objc by google.
the class ElemApplyTemplates method transformSelectedNodes.
/**
* Perform a query if needed, and call transformNode for each child.
*
* @param transformer non-null reference to the the current transform-time state.
*
* @throws TransformerException Thrown in a variety of circumstances.
* @xsl.usage advanced
*/
public void transformSelectedNodes(TransformerImpl transformer) throws TransformerException {
final XPathContext xctxt = transformer.getXPathContext();
final int sourceNode = xctxt.getCurrentNode();
DTMIterator sourceNodes = m_selectExpression.asIterator(xctxt, sourceNode);
VariableStack vars = xctxt.getVarStack();
int nParams = getParamElemCount();
int thisframe = vars.getStackFrame();
boolean pushContextNodeListFlag = false;
try {
xctxt.pushCurrentNode(DTM.NULL);
xctxt.pushCurrentExpressionNode(DTM.NULL);
xctxt.pushSAXLocatorNull();
transformer.pushElemTemplateElement(null);
final Vector keys = (m_sortElems == null) ? null : transformer.processSortKeys(this, sourceNode);
// Sort if we need to.
if (null != keys)
sourceNodes = sortNodes(xctxt, keys, sourceNodes);
final SerializationHandler rth = transformer.getSerializationHandler();
// ContentHandler chandler = rth.getContentHandler();
final StylesheetRoot sroot = transformer.getStylesheet();
final TemplateList tl = sroot.getTemplateListComposed();
final boolean quiet = transformer.getQuietConflictWarnings();
// Should be able to get this from the iterator but there must be a bug.
DTM dtm = xctxt.getDTM(sourceNode);
int argsFrame = -1;
if (nParams > 0) {
// This code will create a section on the stack that is all the
// evaluated arguments. These will be copied into the real params
// section of each called template.
argsFrame = vars.link(nParams);
vars.setStackFrame(thisframe);
for (int i = 0; i < nParams; i++) {
ElemWithParam ewp = m_paramElems[i];
XObject obj = ewp.getValue(transformer, sourceNode);
vars.setLocalVariable(i, obj, argsFrame);
}
vars.setStackFrame(argsFrame);
}
xctxt.pushContextNodeList(sourceNodes);
pushContextNodeListFlag = true;
IntStack currentNodes = xctxt.getCurrentNodeStack();
IntStack currentExpressionNodes = xctxt.getCurrentExpressionNodeStack();
// pushParams(transformer, xctxt);
int child;
while (DTM.NULL != (child = sourceNodes.nextNode())) {
currentNodes.setTop(child);
currentExpressionNodes.setTop(child);
if (xctxt.getDTM(child) != dtm) {
dtm = xctxt.getDTM(child);
}
final int exNodeType = dtm.getExpandedTypeID(child);
final int nodeType = dtm.getNodeType(child);
final QName mode = transformer.getMode();
ElemTemplate template = tl.getTemplateFast(xctxt, child, exNodeType, mode, -1, quiet, dtm);
// 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 = sroot.getDefaultRule();
// %OPT% direct faster?
break;
case DTM.ATTRIBUTE_NODE:
case DTM.CDATA_SECTION_NODE:
case DTM.TEXT_NODE:
// if(rth.m_elemIsPending || rth.m_docPending)
// rth.flushPending(true);
transformer.pushPairCurrentMatched(sroot.getDefaultTextRule(), child);
transformer.setCurrentElement(sroot.getDefaultTextRule());
// dtm.dispatchCharactersEvents(child, chandler, false);
dtm.dispatchCharactersEvents(child, rth, false);
transformer.popCurrentMatched();
continue;
case DTM.DOCUMENT_NODE:
template = sroot.getDefaultRootRule();
break;
default:
// No default rules for processing instructions and the like.
continue;
}
} else {
transformer.setCurrentElement(template);
}
transformer.pushPairCurrentMatched(template, child);
// See comment with unlink, below
int currentFrameBottom;
if (template.m_frameSize > 0) {
xctxt.pushRTFContext();
// See comment with unlink, below
currentFrameBottom = vars.getStackFrame();
vars.link(template.m_frameSize);
// xsl:params might not be nulled.
if (/* nParams > 0 && */
template.m_inArgsSize > 0) {
int paramIndex = 0;
for (ElemTemplateElement elem = template.getFirstChildElem(); null != elem; elem = elem.getNextSiblingElem()) {
if (Constants.ELEMNAME_PARAMVARIABLE == elem.getXSLToken()) {
ElemParam ep = (ElemParam) elem;
int i;
for (i = 0; i < nParams; i++) {
ElemWithParam ewp = m_paramElems[i];
if (ewp.m_qnameID == ep.m_qnameID) {
XObject obj = vars.getLocalVariable(i, argsFrame);
vars.setLocalVariable(paramIndex, obj);
break;
}
}
if (i == nParams)
vars.setLocalVariable(paramIndex, null);
} else
break;
paramIndex++;
}
}
} else
currentFrameBottom = 0;
// each of them.
for (ElemTemplateElement t = template.m_firstChild; t != null; t = t.m_nextSibling) {
xctxt.setSAXLocator(t);
try {
transformer.pushElemTemplateElement(t);
t.execute(transformer);
} finally {
transformer.popElemTemplateElement();
}
}
if (template.m_frameSize > 0) {
// See Frank Weiss bug around 03/19/2002 (no Bugzilla report yet).
// While unlink will restore to the proper place, the real position
// may have been changed for xsl:with-param, so that variables
// can be accessed.
// of right now.
// More:
// When we entered this function, the current
// frame buffer (cfb) index in the variable stack may
// have been manually set. If we just call
// unlink(), however, it will restore the cfb to the
// previous link index from the link stack, rather than
// the manually set cfb. So,
// the only safe solution is to restore it back
// to the same position it was on entry, since we're
// really not working in a stack context here. (Bug4218)
vars.unlink(currentFrameBottom);
xctxt.popRTFContext();
}
transformer.popCurrentMatched();
}
// end while (DTM.NULL != (child = sourceNodes.nextNode()))
} catch (SAXException se) {
transformer.getErrorListener().fatalError(new TransformerException(se));
} finally {
// Unlink to the original stack frame
if (nParams > 0)
vars.unlink(thisframe);
xctxt.popSAXLocator();
if (pushContextNodeListFlag)
xctxt.popContextNodeList();
transformer.popElemTemplateElement();
xctxt.popCurrentExpressionNode();
xctxt.popCurrentNode();
sourceNodes.detach();
}
}
use of org.apache.xml.dtm.DTM in project j2objc by google.
the class ElemCopyOf method execute.
/**
* The xsl:copy-of element can be used to insert a result tree
* fragment into the result tree, without first converting it to
* a string as xsl:value-of does (see [7.6.1 Generating Text with
* xsl:value-of]).
*
* @param transformer non-null reference to the the current transform-time state.
*
* @throws TransformerException
*/
public void execute(TransformerImpl transformer) throws TransformerException {
try {
XPathContext xctxt = transformer.getXPathContext();
int sourceNode = xctxt.getCurrentNode();
XObject value = m_selectExpression.execute(xctxt, sourceNode, this);
SerializationHandler handler = transformer.getSerializationHandler();
if (null != value) {
int type = value.getType();
String s;
switch(type) {
case XObject.CLASS_BOOLEAN:
case XObject.CLASS_NUMBER:
case XObject.CLASS_STRING:
s = value.str();
handler.characters(s.toCharArray(), 0, s.length());
break;
case XObject.CLASS_NODESET:
// System.out.println(value);
DTMIterator nl = value.iter();
// Copy the tree.
DTMTreeWalker tw = new TreeWalker2Result(transformer, handler);
int pos;
while (DTM.NULL != (pos = nl.nextNode())) {
DTM dtm = xctxt.getDTMManager().getDTM(pos);
short t = dtm.getNodeType(pos);
// generated, so we need to only walk the child nodes.
if (t == DTM.DOCUMENT_NODE) {
for (int child = dtm.getFirstChild(pos); child != DTM.NULL; child = dtm.getNextSibling(child)) {
tw.traverse(child);
}
} else if (t == DTM.ATTRIBUTE_NODE) {
SerializerUtils.addAttribute(handler, pos);
} else {
tw.traverse(pos);
}
}
// nl.detach();
break;
case XObject.CLASS_RTREEFRAG:
SerializerUtils.outputResultTreeFragment(handler, value, transformer.getXPathContext());
break;
default:
s = value.str();
handler.characters(s.toCharArray(), 0, s.length());
break;
}
}
// I don't think we want this. -sb
// if (transformer.getDebug())
// transformer.getTraceManager().fireSelectedEvent(sourceNode, this,
// "endSelect", m_selectExpression, value);
} catch (org.xml.sax.SAXException se) {
throw new TransformerException(se);
}
}
use of org.apache.xml.dtm.DTM in project j2objc by google.
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 j2objc by google.
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 j2objc by google.
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();
}
}
}
Aggregations