Home | History | Annotate | Download | only in transformer
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one
      3  * or more contributor license agreements. See the NOTICE file
      4  * distributed with this work for additional information
      5  * regarding copyright ownership. The ASF licenses this file
      6  * to you under the Apache License, Version 2.0 (the  "License");
      7  * you may not use this file except in compliance with the License.
      8  * You may obtain a copy of the License at
      9  *
     10  *     http://www.apache.org/licenses/LICENSE-2.0
     11  *
     12  * Unless required by applicable law or agreed to in writing, software
     13  * distributed under the License is distributed on an "AS IS" BASIS,
     14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15  * See the License for the specific language governing permissions and
     16  * limitations under the License.
     17  */
     18 /*
     19  * $Id: TreeWalker2Result.java 468645 2006-10-28 06:57:24Z minchau $
     20  */
     21 package org.apache.xalan.transformer;
     22 
     23 import org.apache.xalan.serialize.SerializerUtils;
     24 import org.apache.xml.dtm.DTM;
     25 import org.apache.xml.dtm.ref.DTMTreeWalker;
     26 import org.apache.xml.serializer.SerializationHandler;
     27 import org.apache.xpath.XPathContext;
     28 
     29 /**
     30  * Handle a walk of a tree, but screen out attributes for
     31  * the result tree.
     32  * @xsl.usage internal
     33  */
     34 public class TreeWalker2Result extends DTMTreeWalker
     35 {
     36 
     37   /** The transformer instance          */
     38   TransformerImpl m_transformer;
     39 
     40   /** The result tree handler          */
     41   SerializationHandler m_handler;
     42 
     43   /** Node where to start the tree walk           */
     44   int m_startNode;
     45 
     46   /**
     47    * Constructor.
     48    *
     49    * @param transformer Non-null transformer instance
     50    * @param handler The Result tree handler to use
     51    */
     52   public TreeWalker2Result(TransformerImpl transformer,
     53                            SerializationHandler handler)
     54   {
     55 
     56     super(handler, null);
     57 
     58     m_transformer = transformer;
     59     m_handler = handler;
     60   }
     61 
     62   /**
     63    * Perform a pre-order traversal non-recursive style.
     64    *
     65    * @param pos Start node for traversal
     66    *
     67    * @throws TransformerException
     68    */
     69   public void traverse(int pos) throws org.xml.sax.SAXException
     70   {
     71     m_dtm = m_transformer.getXPathContext().getDTM(pos);
     72     m_startNode = pos;
     73 
     74     super.traverse(pos);
     75   }
     76 
     77         /**
     78    * End processing of given node
     79    *
     80    *
     81    * @param node Node we just finished processing
     82    *
     83    * @throws org.xml.sax.SAXException
     84    */
     85   protected void endNode(int node) throws org.xml.sax.SAXException
     86   {
     87     super.endNode(node);
     88     if(DTM.ELEMENT_NODE == m_dtm.getNodeType(node))
     89     {
     90       m_transformer.getXPathContext().popCurrentNode();
     91     }
     92   }
     93 
     94   /**
     95    * Start traversal of the tree at the given node
     96    *
     97    *
     98    * @param node Starting node for traversal
     99    *
    100    * @throws TransformerException
    101    */
    102   protected void startNode(int node) throws org.xml.sax.SAXException
    103   {
    104 
    105     XPathContext xcntxt = m_transformer.getXPathContext();
    106     try
    107     {
    108 
    109       if (DTM.ELEMENT_NODE == m_dtm.getNodeType(node))
    110       {
    111         xcntxt.pushCurrentNode(node);
    112 
    113         if(m_startNode != node)
    114         {
    115           super.startNode(node);
    116         }
    117         else
    118         {
    119           String elemName = m_dtm.getNodeName(node);
    120           String localName = m_dtm.getLocalName(node);
    121           String namespace = m_dtm.getNamespaceURI(node);
    122 
    123           //xcntxt.pushCurrentNode(node);
    124           // SAX-like call to allow adding attributes afterwards
    125           m_handler.startElement(namespace, localName, elemName);
    126           boolean hasNSDecls = false;
    127           DTM dtm = m_dtm;
    128           for (int ns = dtm.getFirstNamespaceNode(node, true);
    129                DTM.NULL != ns; ns = dtm.getNextNamespaceNode(node, ns, true))
    130           {
    131             SerializerUtils.ensureNamespaceDeclDeclared(m_handler,dtm, ns);
    132           }
    133 
    134 
    135           for (int attr = dtm.getFirstAttribute(node);
    136                DTM.NULL != attr; attr = dtm.getNextAttribute(attr))
    137           {
    138             SerializerUtils.addAttribute(m_handler, attr);
    139           }
    140         }
    141 
    142       }
    143       else
    144       {
    145         xcntxt.pushCurrentNode(node);
    146         super.startNode(node);
    147         xcntxt.popCurrentNode();
    148       }
    149     }
    150     catch(javax.xml.transform.TransformerException te)
    151     {
    152       throw new org.xml.sax.SAXException(te);
    153     }
    154   }
    155 }
    156