KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > processor > ProcessorExsltFunction


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: ProcessorExsltFunction.java,v 1.9 2004/02/11 18:15:51 minchau Exp $
18  */

19 package org.apache.xalan.processor;
20
21 import javax.xml.transform.SourceLocator JavaDoc;
22
23 import org.apache.xalan.templates.ElemApplyImport;
24 import org.apache.xalan.templates.ElemApplyTemplates;
25 import org.apache.xalan.templates.ElemAttribute;
26 import org.apache.xalan.templates.ElemCallTemplate;
27 import org.apache.xalan.templates.ElemComment;
28 import org.apache.xalan.templates.ElemCopy;
29 import org.apache.xalan.templates.ElemCopyOf;
30 import org.apache.xalan.templates.ElemElement;
31 import org.apache.xalan.templates.ElemExsltFuncResult;
32 import org.apache.xalan.templates.ElemExsltFunction;
33 import org.apache.xalan.templates.ElemFallback;
34 import org.apache.xalan.templates.ElemLiteralResult;
35 import org.apache.xalan.templates.ElemNumber;
36 import org.apache.xalan.templates.ElemPI;
37 import org.apache.xalan.templates.ElemParam;
38 import org.apache.xalan.templates.ElemTemplate;
39 import org.apache.xalan.templates.ElemTemplateElement;
40 import org.apache.xalan.templates.ElemText;
41 import org.apache.xalan.templates.ElemTextLiteral;
42 import org.apache.xalan.templates.ElemValueOf;
43 import org.apache.xalan.templates.ElemVariable;
44 import org.apache.xalan.templates.ElemMessage;
45 import org.apache.xalan.templates.Stylesheet;
46
47 import org.xml.sax.Attributes JavaDoc;
48 import org.xml.sax.SAXException JavaDoc;
49
50
51 /**
52  * This class processes parse events for an exslt func:function element.
53  * @xsl.usage internal
54  */

55 public class ProcessorExsltFunction extends ProcessorTemplateElem
56 {
57   /**
58    * Start an ElemExsltFunction. Verify that it is top level and that it has a name attribute with a
59    * namespace.
60    */

61   public void startElement(
62           StylesheetHandler handler, String JavaDoc uri, String JavaDoc localName, String JavaDoc rawName, Attributes JavaDoc attributes)
63             throws SAXException JavaDoc
64   {
65     //System.out.println("ProcessorFunction.startElement()");
66
String JavaDoc msg = "";
67     if (!(handler.getElemTemplateElement() instanceof Stylesheet))
68     {
69       msg = "func:function element must be top level.";
70       handler.error(msg, new SAXException JavaDoc(msg));
71     }
72     super.startElement(handler, uri, localName, rawName, attributes);
73        
74     String JavaDoc val = attributes.getValue("name");
75     int indexOfColon = val.indexOf(":");
76     if (indexOfColon > 0)
77     {
78       String JavaDoc prefix = val.substring(0, indexOfColon);
79       String JavaDoc localVal = val.substring(indexOfColon + 1);
80       String JavaDoc ns = handler.getNamespaceSupport().getURI(prefix);
81       //if (ns.length() > 0)
82
// System.out.println("fullfuncname " + ns + localVal);
83
}
84     else
85     {
86       msg = "func:function name must have namespace";
87       handler.error(msg, new SAXException JavaDoc(msg));
88     }
89   }
90   
91   /**
92    * Must include; super doesn't suffice!
93    */

94   protected void appendAndPush(
95           StylesheetHandler handler, ElemTemplateElement elem)
96             throws SAXException JavaDoc
97   {
98     //System.out.println("ProcessorFunction appendAndPush()" + elem);
99
super.appendAndPush(handler, elem);
100     //System.out.println("originating node " + handler.getOriginatingNode());
101
elem.setDOMBackPointer(handler.getOriginatingNode());
102     handler.getStylesheet().setTemplate((ElemTemplate) elem);
103   }
104     
105   /**
106    * End an ElemExsltFunction, and verify its validity.
107    */

108   public void endElement(
109           StylesheetHandler handler, String JavaDoc uri, String JavaDoc localName, String JavaDoc rawName)
110             throws SAXException JavaDoc
111   {
112    ElemTemplateElement function = handler.getElemTemplateElement();
113    SourceLocator JavaDoc locator = handler.getLocator();
114    validate(function, handler); // may throw exception
115
super.endElement(handler, uri, localName, rawName);
116   }
117   
118   /**
119    * Non-recursive traversal of FunctionElement tree based on TreeWalker to verify that
120    * there are no literal result elements except within a func:result element and that
121    * the func:result element does not contain any following siblings except xsl:fallback.
122    */

123   public void validate(ElemTemplateElement elem, StylesheetHandler handler)
124     throws SAXException JavaDoc
125   {
126     String JavaDoc msg = "";
127     while (elem != null)
128     {
129       //System.out.println("elem " + elem);
130
if (elem instanceof ElemExsltFuncResult
131           && elem.getNextSiblingElem() != null
132           && !(elem.getNextSiblingElem() instanceof ElemFallback))
133       {
134         msg = "func:result has an illegal following sibling (only xsl:fallback allowed)";
135         handler.error(msg, new SAXException JavaDoc(msg));
136       }
137       
138       if((elem instanceof ElemApplyImport
139      || elem instanceof ElemApplyTemplates
140      || elem instanceof ElemAttribute
141      || elem instanceof ElemCallTemplate
142      || elem instanceof ElemComment
143      || elem instanceof ElemCopy
144      || elem instanceof ElemCopyOf
145      || elem instanceof ElemElement
146      || elem instanceof ElemLiteralResult
147      || elem instanceof ElemNumber
148      || elem instanceof ElemPI
149      || elem instanceof ElemText
150      || elem instanceof ElemTextLiteral
151      || elem instanceof ElemValueOf)
152     && !(ancestorIsOk(elem)))
153       {
154         msg ="misplaced literal result in a func:function container.";
155         handler.error(msg, new SAXException JavaDoc(msg));
156       }
157       ElemTemplateElement nextElem = elem.getFirstChildElem();
158       while (nextElem == null)
159       {
160         nextElem = elem.getNextSiblingElem();
161         if (nextElem == null)
162           elem = elem.getParentElem();
163         if (elem == null || elem instanceof ElemExsltFunction)
164           return; // ok
165
}
166       elem = nextElem;
167     }
168   }
169   
170   /**
171    * Verify that a literal result belongs to a result element, a variable,
172    * or a parameter.
173    */

174   
175   boolean ancestorIsOk(ElemTemplateElement child)
176   {
177     while (child.getParentElem() != null && !(child.getParentElem() instanceof ElemExsltFunction))
178     {
179       ElemTemplateElement parent = child.getParentElem();
180       if (parent instanceof ElemExsltFuncResult
181           || parent instanceof ElemVariable
182           || parent instanceof ElemParam
183           || parent instanceof ElemMessage)
184         return true;
185       child = parent;
186     }
187     return false;
188   }
189   
190 }
191
Popular Tags