Home | History | Annotate | Download | only in templates
      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: ElemChoose.java 468643 2006-10-28 06:56:03Z minchau $
     20  */
     21 package org.apache.xalan.templates;
     22 
     23 import javax.xml.transform.TransformerException;
     24 
     25 import org.apache.xalan.res.XSLTErrorResources;
     26 import org.apache.xalan.transformer.TransformerImpl;
     27 import org.apache.xpath.XPathContext;
     28 import org.apache.xpath.objects.XObject;
     29 
     30 /**
     31  * Implement xsl:choose.
     32  * <pre>
     33  * <!ELEMENT xsl:choose (xsl:when+, xsl:otherwise?)>
     34  * <!ATTLIST xsl:choose %space-att;>
     35  * </pre>
     36  * @see <a href="http://www.w3.org/TR/xslt#section-Conditional-Processing-with-xsl:choose">XXX in XSLT Specification</a>
     37  * @xsl.usage advanced
     38  */
     39 public class ElemChoose extends ElemTemplateElement
     40 {
     41     static final long serialVersionUID = -3070117361903102033L;
     42 
     43   /**
     44    * Get an int constant identifying the type of element.
     45    * @see org.apache.xalan.templates.Constants
     46    *
     47    * @return The token ID for this element
     48    */
     49   public int getXSLToken()
     50   {
     51     return Constants.ELEMNAME_CHOOSE;
     52   }
     53 
     54   /**
     55    * Return the node name.
     56    *
     57    * @return The element's name
     58    */
     59   public String getNodeName()
     60   {
     61     return Constants.ELEMNAME_CHOOSE_STRING;
     62   }
     63 
     64   /**
     65    * Constructor ElemChoose
     66    *
     67    */
     68   public ElemChoose(){}
     69 
     70   /**
     71    * Execute the xsl:choose transformation.
     72    *
     73    *
     74    * @param transformer non-null reference to the the current transform-time state.
     75    *
     76    * @throws TransformerException
     77    */
     78   public void execute(TransformerImpl transformer) throws TransformerException
     79   {
     80 
     81     boolean found = false;
     82 
     83     for (ElemTemplateElement childElem = getFirstChildElem();
     84             childElem != null; childElem = childElem.getNextSiblingElem())
     85     {
     86       int type = childElem.getXSLToken();
     87 
     88       if (Constants.ELEMNAME_WHEN == type)
     89       {
     90         found = true;
     91 
     92         ElemWhen when = (ElemWhen) childElem;
     93 
     94         // must be xsl:when
     95         XPathContext xctxt = transformer.getXPathContext();
     96         int sourceNode = xctxt.getCurrentNode();
     97 
     98         // System.err.println("\""+when.getTest().getPatternString()+"\"");
     99 
    100         // if(when.getTest().getPatternString().equals("COLLECTION/icuser/ictimezone/LITERAL='GMT +13:00 Pacific/Tongatapu'"))
    101         // 	System.err.println("Found COLLECTION/icuser/ictimezone/LITERAL");
    102 
    103           if (when.getTest().bool(xctxt, sourceNode, when)) {
    104               transformer.executeChildTemplates(when, true);
    105 
    106               return;
    107           }
    108       }
    109       else if (Constants.ELEMNAME_OTHERWISE == type)
    110       {
    111         found = true;
    112 
    113         // xsl:otherwise
    114         transformer.executeChildTemplates(childElem, true);
    115 
    116         return;
    117       }
    118     }
    119 
    120     if (!found)
    121       transformer.getMsgMgr().error(
    122         this, XSLTErrorResources.ER_CHOOSE_REQUIRES_WHEN);
    123   }
    124 
    125   /**
    126    * Add a child to the child list.
    127    *
    128    * @param newChild Child to add to this node's child list
    129    *
    130    * @return The child that was just added to the child list
    131    *
    132    * @throws DOMException
    133    */
    134   public ElemTemplateElement appendChild(ElemTemplateElement newChild)
    135   {
    136 
    137     int type = ((ElemTemplateElement) newChild).getXSLToken();
    138 
    139     switch (type)
    140     {
    141     case Constants.ELEMNAME_WHEN :
    142     case Constants.ELEMNAME_OTHERWISE :
    143 
    144       // TODO: Positional checking
    145       break;
    146     default :
    147       error(XSLTErrorResources.ER_CANNOT_ADD,
    148             new Object[]{ newChild.getNodeName(),
    149                           this.getNodeName() });  //"Can not add " +((ElemTemplateElement)newChild).m_elemName +
    150 
    151     //" to " + this.m_elemName);
    152     }
    153 
    154     return super.appendChild(newChild);
    155   }
    156 
    157   /**
    158    * Tell if this element can accept variable declarations.
    159    * @return true if the element can accept and process variable declarations.
    160    */
    161   public boolean canAcceptVariables()
    162   {
    163   	return false;
    164   }
    165 
    166 }
    167