KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > instruct > Namespace


1 package net.sf.saxon.instruct;
2 import net.sf.saxon.Controller;
3 import net.sf.saxon.event.ReceiverOptions;
4 import net.sf.saxon.event.SequenceReceiver;
5 import net.sf.saxon.expr.*;
6 import net.sf.saxon.om.NamePool;
7 import net.sf.saxon.om.XMLChar;
8 import net.sf.saxon.om.NamespaceConstant;
9 import net.sf.saxon.pattern.NodeKindTest;
10 import net.sf.saxon.style.StandardNames;
11 import net.sf.saxon.trans.DynamicError;
12 import net.sf.saxon.trans.XPathException;
13 import net.sf.saxon.type.ItemType;
14
15 import java.io.PrintStream JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Iterator JavaDoc;
18
19 /**
20 * An xsl:namespace element in the stylesheet. (XSLT 2.0)
21 */

22
23 public class Namespace extends SimpleNodeConstructor {
24
25     private Expression name;
26
27     public Namespace (Expression name) {
28         this.name = name;
29         adoptChildExpression(name);
30     }
31
32     /**
33     * Set the name of this instruction for diagnostic and tracing purposes
34     */

35
36     public int getInstructionNameCode() {
37         return StandardNames.XSL_NAMESPACE;
38     }
39
40     public Expression simplify(StaticContext env) throws XPathException {
41         name = name.simplify(env);
42         return super.simplify(env);
43     }
44
45     public ItemType getItemType() {
46         return NodeKindTest.NAMESPACE;
47     }
48
49     public int getCardinality() {
50         return StaticProperty.EXACTLY_ONE;
51     }
52
53     protected void promoteInst(PromotionOffer offer) {
54         // do nothing
55
//throw new UnsupportedOperationException("Namespace instruction cannot be used as an expression");
56
}
57
58     public void localTypeCheck(StaticContext env, ItemType contextItemType) {}
59
60     public Iterator JavaDoc iterateSubExpressions() {
61         ArrayList JavaDoc list = new ArrayList JavaDoc(6);
62         if (select != null) {
63             list.add(select);
64         }
65         list.add(name);
66         return list.iterator();
67     }
68
69
70     public TailCall processLeavingTail(XPathContext context) throws XPathException {
71         Controller controller = context.getController();
72         String JavaDoc prefix = name.evaluateAsString(context);
73
74         if (!(prefix.equals("") || XMLChar.isValidNCName(prefix))) {
75             DynamicError err = new DynamicError("Namespace prefix is invalid: " + prefix, this);
76             err.setErrorCode("XTDE0920");
77             err.setXPathContext(context);
78             throw dynamicError(this, err, context);
79         }
80
81         if (prefix.equals("xmlns")) {
82             DynamicError err = new DynamicError("Namespace prefix 'xmlns' is not allowed", this);
83             err.setErrorCode("XTDE0920");
84             err.setXPathContext(context);
85             throw dynamicError(this, err, context);
86         }
87
88         String JavaDoc uri = expandChildren(context).toString();
89
90         if (prefix.equals("xml") != uri.equals(NamespaceConstant.XML)) {
91             DynamicError err = new DynamicError("Namespace prefix 'xml' and namespace uri " + NamespaceConstant.XML +
92                     " must only be used together", this);
93             err.setErrorCode("XTDE0925");
94             err.setXPathContext(context);
95             //context.getController().recoverableError(err);
96
throw dynamicError(this, err, context);
97         }
98
99         if (uri.equals("")) {
100             DynamicError err = new DynamicError("Namespace URI is an empty string", this);
101             err.setErrorCode("XTDE0930");
102             err.setXPathContext(context);
103             //context.getController().recoverableError(err);
104
throw dynamicError(this, err, context);
105         }
106
107         int nscode = controller.getNamePool().allocateNamespaceCode(prefix, uri);
108         SequenceReceiver out = context.getReceiver();
109         out.namespace(nscode, ReceiverOptions.REJECT_DUPLICATES);
110         return null;
111     }
112
113     /**
114      * Display this instruction as an expression, for diagnostics
115      */

116
117     public void display(int level, NamePool pool, PrintStream JavaDoc out) {
118         out.println(ExpressionTool.indent(level) + "namespace");
119         name.display(level+1, pool, out);
120         super.display(level+1, pool, out);
121     }
122
123 }
124
125 //
126
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
127
// you may not use this file except in compliance with the License. You may obtain a copy of the
128
// License at http://www.mozilla.org/MPL/
129
//
130
// Software distributed under the License is distributed on an "AS IS" basis,
131
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
132
// See the License for the specific language governing rights and limitations under the License.
133
//
134
// The Original Code is: all this file.
135
//
136
// The Initial Developer of the Original Code is Michael H. Kay.
137
//
138
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
139
//
140
// Contributor(s): none.
141
//
142
Popular Tags