KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > style > XSLElement


1 package com.icl.saxon.style;
2 import com.icl.saxon.tree.AttributeCollection;
3 import com.icl.saxon.*;
4 import com.icl.saxon.om.Name;
5 import com.icl.saxon.om.NamePool;
6 import com.icl.saxon.om.NamespaceException;
7 import com.icl.saxon.expr.*;
8 import com.icl.saxon.output.*;
9 import javax.xml.transform.*;
10
11
12 /**
13 * An xsl:element element in the stylesheet.<BR>
14 */

15
16 public class XSLElement extends StyleElement {
17
18     private Expression elementName;
19     private Expression namespace = null;
20     private String JavaDoc use;
21     private boolean declared = false; // used by compiler
22

23     /**
24     * Determine whether this node is an instruction.
25     * @return true - it is an instruction
26     */

27
28     public boolean isInstruction() {
29         return true;
30     }
31
32     /**
33     * Determine whether this type of element is allowed to contain a template-body
34     * @return true: yes, it may contain a template-body
35     */

36
37     public boolean mayContainTemplateBody() {
38         return true;
39     }
40
41     public void prepareAttributes() throws TransformerConfigurationException {
42
43         StandardNames sn = getStandardNames();
44         AttributeCollection atts = getAttributeList();
45
46         String JavaDoc nameAtt = null;
47         String JavaDoc namespaceAtt = null;
48         
49         for (int a=0; a<atts.getLength(); a++) {
50             int nc = atts.getNameCode(a);
51             int f = nc & 0xfffff;
52             if (f==sn.NAME) {
53                 nameAtt = atts.getValue(a);
54             } else if (f==sn.NAMESPACE) {
55                 namespaceAtt = atts.getValue(a);
56             } else if (f==sn.USE_ATTRIBUTE_SETS) {
57                 use = atts.getValue(a);
58             } else {
59                 checkUnknownAttribute(nc);
60             }
61         }
62
63         if (nameAtt==null) {
64             reportAbsence("name");
65         } else {
66             elementName = makeAttributeValueTemplate(nameAtt);
67             if (elementName instanceof StringValue) {
68                 if (!Name.isQName(((StringValue)elementName).asString())) {
69                     compileError("Element name is not a valid QName");
70                 }
71             }
72         }
73
74         if (namespaceAtt!=null) {
75             namespace = makeAttributeValueTemplate(namespaceAtt);
76         }
77        
78     }
79
80     public void validate() throws TransformerConfigurationException {
81         checkWithinTemplate();
82         if (use!=null) {
83             findAttributeSets(use); // find any referenced attribute sets
84
}
85     }
86
87     public void process(Context context) throws TransformerException
88     {
89         Controller controller = context.getController();
90         NamePool pool = controller.getNamePool();
91         
92         // produce (pending) output
93

94         String JavaDoc expandedName = elementName.evaluateAsString(context);
95
96         if (!Name.isQName(expandedName)) {
97             controller.reportRecoverableError(
98                 "Invalid element name: " + expandedName, this);
99                 // don't write this element;
100
// but signal the outputter to ignore any following attributes
101
context.getOutputter().writeStartTag(-1);
102             processChildren(context);
103             return;
104         }
105
106         String JavaDoc prefix = Name.getPrefix(expandedName);
107         short uriCode;
108         
109         if (namespace==null) {
110             
111             // NB, we can't just call makeNameCode() because that would use the wrong
112
// name pool
113
try {
114                 uriCode = getURICodeForPrefix(prefix); // error if not present
115
} catch (NamespaceException err) {
116                 // TODO: should be a recoverable error?
117
throw styleError(err.getMessage());
118             }
119         
120         } else {
121             
122             String JavaDoc uri = namespace.evaluateAsString(context);
123             if (uri.equals("")) {
124                 // there is a special rule for this case in the specification;
125
// we force the element to go in the null namespace
126
prefix = "";
127             }
128             uriCode = pool.allocateCodeForURI(uri);
129         }
130                         
131         String JavaDoc localName = Name.getLocalName(expandedName);
132         int nameCode = pool.allocate(prefix, uriCode, localName);
133
134         Outputter out = context.getOutputter();
135         out.writeStartTag(nameCode);
136         out.writeNamespaceDeclaration(pool.allocateNamespaceCode(nameCode));
137
138         // apply the content of any attribute sets mentioned in use-attribute-sets
139
processAttributeSets(context);
140
141         // process subordinate elements in stylesheet
142
processChildren(context);
143
144         // output the element end tag
145
out.writeEndTag(nameCode);
146     }
147
148
149 }
150
151 //
152
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
153
// you may not use this file except in compliance with the License. You may obtain a copy of the
154
// License at http://www.mozilla.org/MPL/
155
//
156
// Software distributed under the License is distributed on an "AS IS" basis,
157
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
158
// See the License for the specific language governing rights and limitations under the License.
159
//
160
// The Original Code is: all this file.
161
//
162
// The Initial Developer of the Original Code is
163
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
164
//
165
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
166
//
167
// Contributor(s): none.
168
//
169
Popular Tags