Search in sources :

Example 1 with DTMTreeWalker

use of org.apache.xml.dtm.ref.DTMTreeWalker in project robovm by robovm.

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 2 with DTMTreeWalker

use of org.apache.xml.dtm.ref.DTMTreeWalker 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)

Aggregations

TransformerException (javax.xml.transform.TransformerException)2 TreeWalker2Result (org.apache.xalan.transformer.TreeWalker2Result)2 DTM (org.apache.xml.dtm.DTM)2 DTMIterator (org.apache.xml.dtm.DTMIterator)2 DTMTreeWalker (org.apache.xml.dtm.ref.DTMTreeWalker)2 SerializationHandler (org.apache.xml.serializer.SerializationHandler)2 XPathContext (org.apache.xpath.XPathContext)2 XObject (org.apache.xpath.objects.XObject)2