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 ; 16 import java.util.ArrayList ; 17 import java.util.Iterator ; 18 19 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 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 } 57 58 public void localTypeCheck(StaticContext env, ItemType contextItemType) {} 59 60 public Iterator iterateSubExpressions() { 61 ArrayList list = new ArrayList (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 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 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 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 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 116 117 public void display(int level, NamePool pool, PrintStream 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 | Popular Tags |