Search in sources :

Example 21 with SerializationHandler

use of org.apache.xml.serializer.SerializationHandler 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);
    }
}
Also used : SerializationHandler(org.apache.xml.serializer.SerializationHandler) TreeWalker2Result(org.apache.xalan.transformer.TreeWalker2Result) DTMTreeWalker(org.apache.xml.dtm.ref.DTMTreeWalker) DTMIterator(org.apache.xml.dtm.DTMIterator) XPathContext(org.apache.xpath.XPathContext) DTM(org.apache.xml.dtm.DTM) XObject(org.apache.xpath.objects.XObject) TransformerException(javax.xml.transform.TransformerException)

Example 22 with SerializationHandler

use of org.apache.xml.serializer.SerializationHandler in project j2objc by google.

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 23 with SerializationHandler

use of org.apache.xml.serializer.SerializationHandler in project j2objc by google.

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 24 with SerializationHandler

use of org.apache.xml.serializer.SerializationHandler in project j2objc by google.

the class ElemTemplateElement method unexecuteNSDecls.

/**
   * Send endPrefixMapping events to the result tree handler
   * for all declared prefix mappings in the stylesheet.
   *
   * @param transformer non-null reference to the the current transform-time state.
   * @param ignorePrefix string prefix to not endPrefixMapping
   * 
   * @throws TransformerException
   */
void unexecuteNSDecls(TransformerImpl transformer, String ignorePrefix) throws TransformerException {
    try {
        if (null != m_prefixTable) {
            SerializationHandler rhandler = transformer.getResultTreeHandler();
            int n = m_prefixTable.size();
            for (int i = 0; i < n; i++) {
                XMLNSDecl decl = (XMLNSDecl) m_prefixTable.get(i);
                if (!decl.getIsExcluded() && !(null != ignorePrefix && decl.getPrefix().equals(ignorePrefix))) {
                    rhandler.endPrefixMapping(decl.getPrefix());
                }
            }
        }
    } catch (org.xml.sax.SAXException se) {
        throw new TransformerException(se);
    }
}
Also used : SerializationHandler(org.apache.xml.serializer.SerializationHandler) TransformerException(javax.xml.transform.TransformerException)

Example 25 with SerializationHandler

use of org.apache.xml.serializer.SerializationHandler in project j2objc by google.

the class ElemTemplateElement method executeNSDecls.

/**
   * Send startPrefixMapping events to the result tree handler
   * for all declared prefix mappings in the stylesheet.
   *
   * @param transformer non-null reference to the the current transform-time state.
   * @param ignorePrefix string prefix to not startPrefixMapping
   *
   * @throws TransformerException
   */
void executeNSDecls(TransformerImpl transformer, String ignorePrefix) throws TransformerException {
    try {
        if (null != m_prefixTable) {
            SerializationHandler rhandler = transformer.getResultTreeHandler();
            int n = m_prefixTable.size();
            for (int i = n - 1; i >= 0; i--) {
                XMLNSDecl decl = (XMLNSDecl) m_prefixTable.get(i);
                if (!decl.getIsExcluded() && !(null != ignorePrefix && decl.getPrefix().equals(ignorePrefix))) {
                    rhandler.startPrefixMapping(decl.getPrefix(), decl.getURI(), true);
                }
            }
        }
    } catch (org.xml.sax.SAXException se) {
        throw new TransformerException(se);
    }
}
Also used : SerializationHandler(org.apache.xml.serializer.SerializationHandler) TransformerException(javax.xml.transform.TransformerException)

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