Search in sources :

Example 31 with SerializationHandler

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

Example 32 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 33 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 34 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)

Example 35 with SerializationHandler

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;
}
Also used : ElemTextLiteral(org.apache.xalan.templates.ElemTextLiteral) StringWriter(java.io.StringWriter) SAXException(org.xml.sax.SAXException) SerializationHandler(org.apache.xml.serializer.SerializationHandler) ElemTemplateElement(org.apache.xalan.templates.ElemTemplateElement) TransformerException(javax.xml.transform.TransformerException) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Serializer(org.apache.xml.serializer.Serializer)

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