KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > expr > CastAsQName


1 package net.sf.saxon.expr;
2
3 import net.sf.saxon.om.Item;
4 import net.sf.saxon.om.Name;
5 import net.sf.saxon.om.NamePool;
6 import net.sf.saxon.om.QNameException;
7 import net.sf.saxon.style.StandardNames;
8 import net.sf.saxon.trans.StaticError;
9 import net.sf.saxon.trans.XPathException;
10 import net.sf.saxon.type.AtomicType;
11 import net.sf.saxon.type.ItemType;
12 import net.sf.saxon.type.Type;
13 import net.sf.saxon.value.*;
14
15 import java.io.PrintStream JavaDoc;
16 import java.util.Iterator JavaDoc;
17
18 /**
19  * This class supports casting a string to a QName or a notation.
20  */

21
22 public class CastAsQName extends ComputedExpression {
23
24     private Expression operand;
25     private AtomicType targetType;
26
27     public CastAsQName(Expression s, AtomicType target) {
28         operand = s;
29         targetType = target;
30     }
31
32     public Expression analyze(StaticContext env, ItemType contextItemType) throws XPathException {
33         if (operand instanceof StringValue) {
34             try {
35                 CharSequence JavaDoc arg = ((StringValue)operand).getStringValueCS();
36                 String JavaDoc parts[] = Name.getQNameParts(arg);
37                 String JavaDoc uri;
38                 if ("".equals(parts[0])) {
39                     uri = "";
40                 } else {
41                     uri = env.getURIForPrefix(parts[0]);
42                     if (uri==null) {
43                         StaticError e = new StaticError("Prefix '" + parts[0] + "' has not been declared");
44                         throw e;
45                     }
46                 }
47                 if (targetType.getFingerprint() == StandardNames.XS_QNAME) {
48                     return new QNameValue(parts[0], uri, parts[1]);
49                 } else if (Type.isSubType(targetType, Type.QNAME_TYPE)) {
50                     QNameValue q = new QNameValue(parts[0], uri, parts[1]);
51                     AtomicValue av = targetType.makeDerivedValue(q, arg, true);
52                     if (av instanceof ValidationErrorValue) {
53                         throw ((ValidationErrorValue)av).getException();
54                     }
55                     return av;
56                 } else {
57                     NotationValue n = new NotationValue(parts[0], uri, parts[1]);
58                     AtomicValue av = targetType.makeDerivedValue(n, arg, true);
59                     if (av instanceof ValidationErrorValue) {
60                         throw ((ValidationErrorValue)av).getException();
61                     }
62                     return av;
63                 }
64             } catch (QNameException err) {
65                 StaticError e = new StaticError(err);
66                 throw e;
67             }
68         } else {
69             StaticError err = new StaticError("The argument of a QName constructor must be a string literal");
70             throw err;
71         }
72     }
73
74     public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException {
75         if (operand instanceof StringValue) {
76             try {
77                 CharSequence JavaDoc arg = ((StringValue)operand).getStringValueCS();
78                 String JavaDoc parts[] = Name.getQNameParts(arg);
79                 String JavaDoc uri;
80                 if ("".equals(parts[0])) {
81                     uri = "";
82                 } else {
83                     uri = env.getURIForPrefix(parts[0]);
84                     if (uri==null) {
85                         StaticError e = new StaticError("Prefix '" + parts[0] + "' has not been declared");
86                         throw e;
87                     }
88                 }
89                 if (targetType.getFingerprint() == StandardNames.XS_QNAME) {
90                     return new QNameValue(parts[0], uri, parts[1]);
91                 } else if (Type.isSubType(targetType, Type.QNAME_TYPE)) {
92                     QNameValue q = new QNameValue(parts[0], uri, parts[1]);
93                     AtomicValue av = targetType.makeDerivedValue(q, arg, true);
94                     if (av instanceof ValidationErrorValue) {
95                         throw ((ValidationErrorValue)av).getException();
96                     }
97                     return av;
98                 } else {
99                     NotationValue n = new NotationValue(parts[0], uri, parts[1]);
100                     AtomicValue av = targetType.makeDerivedValue(n, arg, true);
101                     if (av instanceof ValidationErrorValue) {
102                         throw ((ValidationErrorValue)av).getException();
103                     }
104                     return av;
105                 }
106             } catch (QNameException err) {
107                 StaticError e = new StaticError(err);
108                 throw e;
109             }
110         } else {
111             StaticError err = new StaticError("The argument of a QName constructor must be a string literal");
112             throw err;
113         }
114     }
115
116     /**
117      * Perform optimisation of an expression and its subexpressions.
118      * <p/>
119      * <p>This method is called after all references to functions and variables have been resolved
120      * to the declaration of the function or variable, and after all type checking has been done.</p>
121      *
122      * @param opt the optimizer in use. This provides access to supporting functions; it also allows
123      * different optimization strategies to be used in different circumstances.
124      * @param env the static context of the expression
125      * @param contextItemType the static type of "." at the point where this expression is invoked.
126      * The parameter is set to null if it is known statically that the context item will be undefined.
127      * If the type of the context item is not known statically, the argument is set to
128      * {@link net.sf.saxon.type.Type#ITEM_TYPE}
129      * @return the original expression, rewritten if appropriate to optimize execution
130      * @throws net.sf.saxon.trans.StaticError if an error is discovered during this phase
131      * (typically a type error)
132      */

133
134     public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException {
135         operand = operand.optimize(opt, env, contextItemType);
136         if (operand instanceof Value) {
137             return Value.asValue(evaluateItem(null));
138         }
139         return this;
140     }
141
142     public Expression promote(PromotionOffer offer) throws XPathException {
143         return super.promote(offer);
144     }
145
146     public int computeCardinality() {
147         return StaticProperty.EXACTLY_ONE;
148     }
149
150     public ItemType getItemType() {
151         return targetType;
152     }
153
154     /**
155      * Determine the special properties of this expression
156      * @return {@link StaticProperty#NON_CREATIVE}.
157      */

158
159     public int computeSpecialProperties() {
160         int p = super.computeSpecialProperties();
161         return p | StaticProperty.NON_CREATIVE;
162     }
163
164     public int getIntrinsicDependencies() {
165         return 0;
166     }
167
168     public Iterator JavaDoc iterateSubExpressions() {
169         return new MonoIterator(operand);
170     }
171
172     public Item evaluateItem(XPathContext context) throws XPathException {
173         throw new UnsupportedOperationException JavaDoc("A QName constructor cannot be evaluated at run-time");
174     }
175
176     public void display(int level, NamePool pool, PrintStream JavaDoc out) {
177         out.println(ExpressionTool.indent(level) + "cast as QName");
178         operand.display(level+1, pool, out);
179     }
180 }
181 //
182
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
183
// you may not use this file except in compliance with the License. You may obtain a copy of the
184
// License at http://www.mozilla.org/MPL/
185
//
186
// Software distributed under the License is distributed on an "AS IS" basis,
187
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
188
// See the License for the specific language governing rights and limitations under the License.
189
//
190
// The Original Code is: all this file.
191
//
192
// The Initial Developer of the Original Code is Michael H. Kay.
193
//
194
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
195
//
196
// Contributor(s): none.
197
//
Popular Tags