KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > style > XSLElement


1 package net.sf.saxon.style;
2 import net.sf.saxon.Configuration;
3 import net.sf.saxon.Err;
4 import net.sf.saxon.expr.Expression;
5 import net.sf.saxon.expr.ExpressionTool;
6 import net.sf.saxon.instruct.*;
7 import net.sf.saxon.om.*;
8 import net.sf.saxon.trans.XPathException;
9 import net.sf.saxon.type.SchemaType;
10 import net.sf.saxon.value.EmptySequence;
11 import net.sf.saxon.value.StringValue;
12
13
14 /**
15 * An xsl:element element in the stylesheet. <br>
16 */

17
18 public class XSLElement extends StyleElement {
19
20     private Expression elementName;
21     private Expression namespace = null;
22     private String JavaDoc use;
23     private AttributeSet[] attributeSets = null;
24     private int validation;
25     private SchemaType schemaType = null;
26     private boolean inheritNamespaces = true;
27
28     /**
29     * Determine whether this node is an instruction.
30     * @return true - it is an instruction
31     */

32
33     public boolean isInstruction() {
34         return true;
35     }
36
37     /**
38     * Determine whether this type of element is allowed to contain a template-body
39     * @return true: yes, it may contain a template-body
40     */

41
42     public boolean mayContainSequenceConstructor() {
43         return true;
44     }
45
46     public void prepareAttributes() throws XPathException {
47
48         AttributeCollection atts = getAttributeList();
49
50         String JavaDoc nameAtt = null;
51         String JavaDoc namespaceAtt = null;
52         String JavaDoc validationAtt = null;
53         String JavaDoc typeAtt = null;
54         String JavaDoc inheritAtt = null;
55
56         for (int a=0; a<atts.getLength(); a++) {
57             int nc = atts.getNameCode(a);
58             String JavaDoc f = getNamePool().getClarkName(nc);
59             if (f==StandardNames.NAME) {
60                 nameAtt = atts.getValue(a).trim();
61             } else if (f==StandardNames.NAMESPACE) {
62                 namespaceAtt = atts.getValue(a);
63             } else if (f==StandardNames.VALIDATION) {
64                 validationAtt = atts.getValue(a).trim();
65             } else if (f==StandardNames.TYPE) {
66                 typeAtt = atts.getValue(a).trim();
67             } else if (f==StandardNames.INHERIT_NAMESPACES) {
68                 inheritAtt = atts.getValue(a).trim();
69             } else if (f==StandardNames.USE_ATTRIBUTE_SETS) {
70                 use = atts.getValue(a);
71             } else {
72                 checkUnknownAttribute(nc);
73             }
74         }
75
76         if (nameAtt==null) {
77             reportAbsence("name");
78         } else {
79             elementName = makeAttributeValueTemplate(nameAtt);
80             if (elementName instanceof StringValue) {
81                 if (!Name.isQName(((StringValue)elementName).getStringValue())) {
82                     compileError("Element name " +
83                             Err.wrap(((StringValue)elementName).getStringValue(), Err.ELEMENT) +
84                             " is not a valid QName", "XTDE0820");
85                     // to prevent duplicate error messages:
86
elementName = new StringValue("saxon-error-element");
87                 }
88             }
89         }
90
91         if (namespaceAtt!=null) {
92             namespace = makeAttributeValueTemplate(namespaceAtt);
93         }
94
95         if (validationAtt!=null) {
96             validation = Validation.getCode(validationAtt);
97             if (validation != Validation.STRIP && !getConfiguration().isSchemaAware(Configuration.XSLT)) {
98                 compileError("To perform validation, a schema-aware XSLT processor is needed", "XTSE1660");
99             }
100             if (validation == Validation.INVALID) {
101                 compileError("Invalid value for @validation attribute. " +
102                              "Permitted values are (strict, lax, preserve, strip)", "XTSE0020");
103             }
104         } else {
105             validation = getContainingStylesheet().getDefaultValidation();
106         }
107
108         if (typeAtt!=null) {
109             if (!getConfiguration().isSchemaAware(Configuration.XSLT)) {
110                 compileError("The @type attribute is available only with a schema-aware XSLT processor", "XTSE1660");
111             }
112             schemaType = getSchemaType(typeAtt);
113         }
114
115         if (typeAtt != null && validationAtt != null) {
116             compileError("The @validation and @type attributes are mutually exclusive", "XTSE1505");
117         }
118
119         if (inheritAtt != null) {
120             if (inheritAtt.equals("yes")) {
121                 inheritNamespaces = true;
122             } else if (inheritAtt.equals("no")) {
123                 inheritNamespaces = false;
124             } else {
125                 compileError("The @inherit-namespaces attribute has permitted values (yes, no)", "XTSE0020");
126             }
127         }
128     }
129
130     public void validate() throws XPathException {
131         checkWithinTemplate();
132         if (use!=null) {
133             attributeSets = getAttributeSets(use, null); // find any referenced attribute sets
134
}
135         elementName = typeCheck("name", elementName);
136         namespace = typeCheck("namespace", namespace);
137     }
138
139     public Expression compile(Executable exec) throws XPathException {
140
141         NamespaceResolver nsContext = null;
142
143         // deal specially with the case where the element name is known statically
144

145         if (elementName instanceof StringValue) {
146             CharSequence JavaDoc qName = ((StringValue)elementName).getStringValueCS();
147
148             String JavaDoc[] parts;
149             try {
150                 parts = Name.getQNameParts(qName);
151             } catch (QNameException e) {
152                 compileError("Invalid element name: " + qName, "XTDE0820");
153                 return null;
154             }
155
156             String JavaDoc nsuri = null;
157             if (namespace instanceof StringValue) {
158                 nsuri = ((StringValue)namespace).getStringValue();
159                 if (nsuri.equals("")) {
160                     parts[0] = "";
161                 }
162             } else if (namespace==null) {
163                 nsuri = getURIForPrefix(parts[0], true);
164                 if (nsuri == null) {
165                     undeclaredNamespaceError(parts[0], "XTDE0280");
166                 }
167             }
168             if (nsuri != null) {
169                 // Local name and namespace are both known statically: generate a FixedElement instruction
170
int nameCode = getTargetNamePool().allocate(parts[0], nsuri, parts[1]);
171                 FixedElement inst = new FixedElement(nameCode,
172                                                      null,
173                                                      inheritNamespaces,
174                                                      schemaType,
175                                                      validation);
176                 Expression content = compileSequenceConstructor(exec, iterateAxis(Axis.CHILD), true);
177
178                 if (attributeSets != null) {
179                     UseAttributeSets use = new UseAttributeSets(attributeSets);
180                     if (content == null) {
181                         content = use;
182                     } else {
183                         content = Block.makeBlock(use, content);
184                     }
185                 }
186                 if (content == null) {
187                     content = EmptySequence.getInstance();
188                 }
189                 inst.setContentExpression(content);
190                 ExpressionTool.makeParentReferences(inst);
191                 return inst;
192             }
193         } else {
194             // if the namespace URI must be deduced at run-time from the element name
195
// prefix, we need to save the namespace context of the instruction
196

197             if (namespace==null) {
198                 nsContext = makeNamespaceContext();
199             }
200         }
201
202         ComputedElement inst = new ComputedElement( elementName,
203                                                     namespace,
204                                                     nsContext,
205                                                     schemaType,
206                                                     validation,
207                                                     inheritNamespaces,
208                                                     false);
209         Expression content = compileSequenceConstructor(exec, iterateAxis(Axis.CHILD), true);
210         if (attributeSets != null) {
211             UseAttributeSets use = new UseAttributeSets(attributeSets);
212             if (content == null) {
213                 content = use;
214             } else {
215                 content = Block.makeBlock(use, content);
216             }
217         }
218         if (content == null) {
219             content = EmptySequence.getInstance();
220         }
221         inst.setContentExpression(content);
222         ExpressionTool.makeParentReferences(inst);
223         return inst;
224     }
225
226
227 }
228
229 //
230
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
231
// you may not use this file except in compliance with the License. You may obtain a copy of the
232
// License at http://www.mozilla.org/MPL/
233
//
234
// Software distributed under the License is distributed on an "AS IS" basis,
235
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
236
// See the License for the specific language governing rights and limitations under the License.
237
//
238
// The Original Code is: all this file.
239
//
240
// The Initial Developer of the Original Code is Michael H. Kay.
241
//
242
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
243
//
244
// Contributor(s): none.
245
//
246
Popular Tags