1 package net.sf.saxon.instruct; 2 import net.sf.saxon.expr.*; 3 import net.sf.saxon.om.*; 4 import net.sf.saxon.trans.DynamicError; 5 import net.sf.saxon.trans.StaticError; 6 import net.sf.saxon.trans.XPathException; 7 import net.sf.saxon.type.ItemType; 8 import net.sf.saxon.type.Type; 9 10 import java.io.PrintStream ; 11 import java.util.Iterator ; 12 13 18 19 public abstract class SimpleNodeConstructor extends Instruction { 20 21 protected Expression select = null; 22 23 public SimpleNodeConstructor() { 24 } 25 26 public void setSelect(Expression select) throws StaticError { 27 this.select = select; 28 adoptChildExpression(select); 29 } 30 31 35 36 public final boolean createsNewNodes() { 37 return true; 38 } 39 40 public Expression simplify(StaticContext env) throws XPathException { 41 if (select != null) { 42 select = select.simplify(env); 43 } 44 return this; 45 } 46 47 54 55 public int computeSpecialProperties() { 56 return super.computeSpecialProperties() | 57 StaticProperty.SINGLE_DOCUMENT_NODESET; 58 } 59 60 public abstract void localTypeCheck(StaticContext env, ItemType contextItemType) throws XPathException; 61 62 71 72 public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException { 73 localTypeCheck(env, contextItemType); 74 75 if (select != null) { 76 select = select.typeCheck(env, contextItemType); 77 if (!Type.isSubType(select.getItemType(), Type.ANY_ATOMIC_TYPE)) { 78 select = new Atomizer(select, env.getConfiguration()); 79 } 80 if (!Type.isSubType(select.getItemType(), Type.STRING_TYPE)) { 81 select = new AtomicSequenceConverter(select, Type.STRING_TYPE); 82 } 83 adoptChildExpression(select); 84 } 85 return this; 86 } 87 88 public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException { 89 if (select != null) { 90 select = select.optimize(opt, env, contextItemType); 91 adoptChildExpression(select); 92 } 93 return this; 94 } 95 96 public Iterator iterateSubExpressions() { 97 return new MonoIterator(select); 98 } 99 100 105 106 public CharSequence expandChildren(XPathContext context) throws XPathException { 107 Item item = select.evaluateItem(context); 108 if (item==null) { 109 return ""; 110 } else { 111 return item.getStringValueCS(); 112 } 113 } 114 115 120 121 public Item evaluateItem(XPathContext context) throws XPathException { 122 String content = (select==null ? 123 "" : 124 select.evaluateAsString(context)); 125 content = checkContent(content, context); 126 try { 127 Orphan o = new Orphan(context.getController().getConfiguration()); 128 o.setNodeKind((short)getItemType().getPrimitiveType()); 129 o.setStringValue(content); 130 o.setNameCode(evaluateNameCode(context)); 131 return o; 132 } catch (SkipInstructionException skip) { 133 return null; 135 } 136 } 137 138 145 146 protected String checkContent(String data, XPathContext context) throws DynamicError { 147 return data; 148 } 149 150 protected int evaluateNameCode(XPathContext context) 151 throws XPathException, XPathException { 152 return -1; 153 } 154 155 public SequenceIterator iterate(XPathContext context) throws XPathException { 156 return SingletonIterator.makeIterator(evaluateItem(context)); 157 } 158 159 162 163 public void display(int level, NamePool pool, PrintStream out) { 164 if (select != null) { 165 select.display(level, pool, out); 166 } 167 } 168 169 181 182 protected void promoteInst(PromotionOffer offer) throws XPathException { 183 if (select != null) { 184 select = doPromotion(select, offer); 185 } 186 super.promoteInst(offer); 187 } 188 189 190 } 191 192 | Popular Tags |