use of org.apache.xml.serializer.SerializationHandler in project robovm by robovm.
the class ElemElement method constructNode.
/**
* Construct a node in the result tree. This method is overloaded by
* xsl:attribute. At this class level, this method creates an element.
* If the node is null, we instantiate only the content of the node in accordance
* with section 7.1.2 of the XSLT 1.0 Recommendation.
*
* @param nodeName The name of the node, which may be <code>null</code>. If <code>null</code>,
* only the non-attribute children of this node will be processed.
* @param prefix The prefix for the namespace, which may be <code>null</code>.
* If not <code>null</code>, this prefix will be mapped and unmapped.
* @param nodeNamespace The namespace of the node, which may be not be <code>null</code>.
* @param transformer non-null reference to the the current transform-time state.
*
* @throws TransformerException
*/
void constructNode(String nodeName, String prefix, String nodeNamespace, TransformerImpl transformer) throws TransformerException {
boolean shouldAddAttrs;
try {
SerializationHandler rhandler = transformer.getResultTreeHandler();
if (null == nodeName) {
shouldAddAttrs = false;
} else {
if (null != prefix) {
rhandler.startPrefixMapping(prefix, nodeNamespace, true);
}
rhandler.startElement(nodeNamespace, QName.getLocalPart(nodeName), nodeName);
super.execute(transformer);
shouldAddAttrs = true;
}
transformer.executeChildTemplates(this, shouldAddAttrs);
// Now end the element if name was valid
if (null != nodeName) {
rhandler.endElement(nodeNamespace, QName.getLocalPart(nodeName), nodeName);
if (null != prefix) {
rhandler.endPrefixMapping(prefix);
}
}
} catch (SAXException se) {
throw new TransformerException(se);
}
}
use of org.apache.xml.serializer.SerializationHandler in project robovm by robovm.
the class ElemElement method execute.
/**
* Create an element in the result tree.
* The xsl:element element allows an element to be created with a
* computed name. The expanded-name of the element to be created
* is specified by a required name attribute and an optional namespace
* attribute. The content of the xsl:element element is a template
* for the attributes and children of the created element.
*
* @param transformer non-null reference to the the current transform-time state.
*
* @throws TransformerException
*/
public void execute(TransformerImpl transformer) throws TransformerException {
SerializationHandler rhandler = transformer.getSerializationHandler();
XPathContext xctxt = transformer.getXPathContext();
int sourceNode = xctxt.getCurrentNode();
String nodeName = m_name_avt == null ? null : m_name_avt.evaluate(xctxt, sourceNode, this);
String prefix = null;
String nodeNamespace = "";
// Only validate if an AVT was used.
if ((nodeName != null) && (!m_name_avt.isSimple()) && (!XML11Char.isXML11ValidQName(nodeName))) {
transformer.getMsgMgr().warn(this, XSLTErrorResources.WG_ILLEGAL_ATTRIBUTE_VALUE, new Object[] { Constants.ATTRNAME_NAME, nodeName });
nodeName = null;
} else if (nodeName != null) {
prefix = QName.getPrefixPart(nodeName);
if (null != m_namespace_avt) {
nodeNamespace = m_namespace_avt.evaluate(xctxt, sourceNode, this);
if (null == nodeNamespace || (prefix != null && prefix.length() > 0 && nodeNamespace.length() == 0))
transformer.getMsgMgr().error(this, XSLTErrorResources.ER_NULL_URI_NAMESPACE);
else {
// Determine the actual prefix that we will use for this nodeNamespace
prefix = resolvePrefix(rhandler, prefix, nodeNamespace);
if (null == prefix)
prefix = "";
if (prefix.length() > 0)
nodeName = (prefix + ":" + QName.getLocalPart(nodeName));
else
nodeName = QName.getLocalPart(nodeName);
}
} else // No namespace attribute was supplied. Use the namespace declarations
// currently in effect for the xsl:element element.
{
try {
// Maybe temporary, until I get this worked out. test: axes59
nodeNamespace = getNamespaceForPrefix(prefix);
if ((null == nodeNamespace) && (prefix.length() == 0))
nodeNamespace = "";
else if (null == nodeNamespace) {
transformer.getMsgMgr().warn(this, XSLTErrorResources.WG_COULD_NOT_RESOLVE_PREFIX, new Object[] { prefix });
nodeName = null;
}
} catch (Exception ex) {
transformer.getMsgMgr().warn(this, XSLTErrorResources.WG_COULD_NOT_RESOLVE_PREFIX, new Object[] { prefix });
nodeName = null;
}
}
}
constructNode(nodeName, prefix, nodeNamespace, transformer);
}
use of org.apache.xml.serializer.SerializationHandler in project nokogiri by sparklemotion.
the class XsltStylesheet method serialize.
@JRubyMethod
public IRubyObject serialize(ThreadContext context, IRubyObject doc) throws IOException, TransformerException {
XmlDocument xmlDoc = (XmlDocument) doc;
TransformerImpl transformer = (TransformerImpl) this.sheet.newTransformer();
ByteArrayOutputStream writer = new ByteArrayOutputStream();
StreamResult streamResult = new StreamResult(writer);
SerializationHandler serializationHandler = transformer.createSerializationHandler(streamResult);
serializationHandler.serialize(xmlDoc.getNode());
return context.getRuntime().newString(writer.toString());
}
use of org.apache.xml.serializer.SerializationHandler 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.serializer.SerializationHandler in project robovm by robovm.
the class TransformerImpl method transformToString.
/**
* Take the contents of a template element, process it, and
* convert it to a string.
*
* @param elem The parent element whose children will be output
* as a string.
*
* @return The stringized result of executing the elements children.
*
* @throws TransformerException
* @xsl.usage advanced
*/
public String transformToString(ElemTemplateElement elem) throws TransformerException {
ElemTemplateElement firstChild = elem.getFirstChildElem();
if (null == firstChild)
return "";
if (elem.hasTextLitOnly() && m_optimizer) {
return ((ElemTextLiteral) firstChild).getNodeValue();
}
// Save the current result tree handler.
SerializationHandler savedRTreeHandler = this.m_serializationHandler;
// Create a Serializer object that will handle the SAX events
// and build the ResultTreeFrag nodes.
StringWriter sw = (StringWriter) m_stringWriterObjectPool.getInstance();
m_serializationHandler = (ToTextStream) m_textResultHandlerObjectPool.getInstance();
if (null == m_serializationHandler) {
// if we didn't get one from the pool, go make a new one
Serializer serializer = org.apache.xml.serializer.SerializerFactory.getSerializer(m_textformat.getProperties());
m_serializationHandler = (SerializationHandler) serializer;
}
m_serializationHandler.setTransformer(this);
m_serializationHandler.setWriter(sw);
String result;
try {
/* Don't call startDocument, the SerializationHandler will
* generate its own internal startDocument call anyways
*/
// this.m_serializationHandler.startDocument();
// Do the transformation of the child elements.
executeChildTemplates(elem, true);
this.m_serializationHandler.endDocument();
result = sw.toString();
} catch (org.xml.sax.SAXException se) {
throw new TransformerException(se);
} finally {
sw.getBuffer().setLength(0);
try {
sw.close();
} catch (Exception ioe) {
}
m_stringWriterObjectPool.freeInstance(sw);
m_serializationHandler.reset();
m_textResultHandlerObjectPool.freeInstance(m_serializationHandler);
// Restore the previous result tree handler.
m_serializationHandler = savedRTreeHandler;
}
return result;
}
Aggregations