Search in sources :

Example 36 with SerializationHandler

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);
}
Also used : SerializationHandler(org.apache.xml.serializer.SerializationHandler) XPathContext(org.apache.xpath.XPathContext) TransformerException(javax.xml.transform.TransformerException) SAXException(org.xml.sax.SAXException)

Example 37 with SerializationHandler

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());
}
Also used : TransformerImpl(org.apache.xalan.transformer.TransformerImpl) StreamResult(javax.xml.transform.stream.StreamResult) SerializationHandler(org.apache.xml.serializer.SerializationHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 38 with SerializationHandler

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;
    }
}
Also used : Debugger(org.intellij.plugins.xsltDebugger.rt.engine.Debugger) LocalDebugger(org.intellij.plugins.xsltDebugger.rt.engine.local.LocalDebugger) SerializationHandler(org.apache.xml.serializer.SerializationHandler) DTMIterator(org.apache.xml.dtm.DTMIterator)

Aggregations

SerializationHandler (org.apache.xml.serializer.SerializationHandler)38 TransformerException (javax.xml.transform.TransformerException)28 SAXException (org.xml.sax.SAXException)20 XPathContext (org.apache.xpath.XPathContext)14 DTM (org.apache.xml.dtm.DTM)6 ToXMLSAXHandler (org.apache.xml.serializer.ToXMLSAXHandler)6 XObject (org.apache.xpath.objects.XObject)6 DTMIterator (org.apache.xml.dtm.DTMIterator)5 IOException (java.io.IOException)4 ContentHandler (org.xml.sax.ContentHandler)4 LexicalHandler (org.xml.sax.ext.LexicalHandler)4 StreamResult (javax.xml.transform.stream.StreamResult)3 StringWriter (java.io.StringWriter)2 Vector (java.util.Vector)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 SourceLocator (javax.xml.transform.SourceLocator)2 DOMResult (javax.xml.transform.dom.DOMResult)2 SAXResult (javax.xml.transform.sax.SAXResult)2 ElemTemplateElement (org.apache.xalan.templates.ElemTemplateElement)2 ElemTextLiteral (org.apache.xalan.templates.ElemTextLiteral)2