001: package net.sf.saxon.instruct;
002:
003: import net.sf.saxon.Controller;
004: import net.sf.saxon.event.ReceiverOptions;
005: import net.sf.saxon.event.SequenceReceiver;
006: import net.sf.saxon.expr.*;
007: import net.sf.saxon.om.NamePool;
008: import net.sf.saxon.om.NamespaceConstant;
009: import net.sf.saxon.pattern.NodeKindTest;
010: import net.sf.saxon.style.StandardNames;
011: import net.sf.saxon.trans.DynamicError;
012: import net.sf.saxon.trans.XPathException;
013: import net.sf.saxon.type.ItemType;
014: import net.sf.saxon.type.TypeHierarchy;
015:
016: import java.io.PrintStream;
017: import java.util.ArrayList;
018: import java.util.Iterator;
019:
020: /**
021: * An xsl:namespace element in the stylesheet. (XSLT 2.0)
022: */
023:
024: public class Namespace extends SimpleNodeConstructor {
025:
026: private Expression name;
027:
028: public Namespace(Expression name) {
029: this .name = name;
030: adoptChildExpression(name);
031: }
032:
033: /**
034: * Set the name of this instruction for diagnostic and tracing purposes
035: */
036:
037: public int getInstructionNameCode() {
038: return StandardNames.XSL_NAMESPACE;
039: }
040:
041: public Expression simplify(StaticContext env) throws XPathException {
042: name = name.simplify(env);
043: return super .simplify(env);
044: }
045:
046: public ItemType getItemType(TypeHierarchy th) {
047: return NodeKindTest.NAMESPACE;
048: }
049:
050: public int getCardinality() {
051: return StaticProperty.EXACTLY_ONE;
052: }
053:
054: protected void promoteInst(PromotionOffer offer) {
055: // do nothing
056: //throw new UnsupportedOperationException("Namespace instruction cannot be used as an expression");
057: }
058:
059: public void localTypeCheck(StaticContext env,
060: ItemType contextItemType) {
061: }
062:
063: public Iterator iterateSubExpressions() {
064: ArrayList list = new ArrayList(6);
065: if (select != null) {
066: list.add(select);
067: }
068: list.add(name);
069: return list.iterator();
070: }
071:
072: public TailCall processLeavingTail(XPathContext context)
073: throws XPathException {
074: Controller controller = context.getController();
075: String prefix = name.evaluateAsString(context);
076:
077: if (!(prefix.equals("") || context.getConfiguration()
078: .getNameChecker().isValidNCName(prefix))) {
079: DynamicError err = new DynamicError(
080: "Namespace prefix is invalid: " + prefix, this );
081: err.setErrorCode("XTDE0920");
082: err.setXPathContext(context);
083: throw dynamicError(this , err, context);
084: }
085:
086: if (prefix.equals("xmlns")) {
087: DynamicError err = new DynamicError(
088: "Namespace prefix 'xmlns' is not allowed", this );
089: err.setErrorCode("XTDE0920");
090: err.setXPathContext(context);
091: throw dynamicError(this , err, context);
092: }
093:
094: String uri = expandChildren(context).toString();
095:
096: if (prefix.equals("xml") != uri.equals(NamespaceConstant.XML)) {
097: DynamicError err = new DynamicError(
098: "Namespace prefix 'xml' and namespace uri "
099: + NamespaceConstant.XML
100: + " must only be used together", this );
101: err.setErrorCode("XTDE0925");
102: err.setXPathContext(context);
103: //context.getController().recoverableError(err);
104: throw dynamicError(this , err, context);
105: }
106:
107: if (uri.equals("")) {
108: DynamicError err = new DynamicError(
109: "Namespace URI is an empty string", this );
110: err.setErrorCode("XTDE0930");
111: err.setXPathContext(context);
112: //context.getController().recoverableError(err);
113: throw dynamicError(this , err, context);
114: }
115:
116: int nscode = controller.getNamePool().allocateNamespaceCode(
117: prefix, uri);
118: SequenceReceiver out = context.getReceiver();
119: out.namespace(nscode, ReceiverOptions.REJECT_DUPLICATES);
120: return null;
121: }
122:
123: /**
124: * Display this instruction as an expression, for diagnostics
125: */
126:
127: public void display(int level, NamePool pool, PrintStream out) {
128: out.println(ExpressionTool.indent(level) + "namespace");
129: name.display(level + 1, pool, out);
130: super .display(level + 1, pool, out);
131: }
132:
133: }
134:
135: //
136: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
137: // you may not use this file except in compliance with the License. You may obtain a copy of the
138: // License at http://www.mozilla.org/MPL/
139: //
140: // Software distributed under the License is distributed on an "AS IS" basis,
141: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
142: // See the License for the specific language governing rights and limitations under the License.
143: //
144: // The Original Code is: all this file.
145: //
146: // The Initial Developer of the Original Code is Michael H. Kay.
147: //
148: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
149: //
150: // Contributor(s): none.
151: //
|