1 package net.sf.saxon.instruct; 2 import net.sf.saxon.Controller; 3 import net.sf.saxon.event.Emitter; 4 import net.sf.saxon.event.TreeReceiver; 5 import net.sf.saxon.expr.*; 6 import net.sf.saxon.om.*; 7 import net.sf.saxon.pattern.NoNodeTest; 8 import net.sf.saxon.style.StandardNames; 9 import net.sf.saxon.trans.DynamicError; 10 import net.sf.saxon.trans.XPathException; 11 import net.sf.saxon.type.ItemType; 12 13 import javax.xml.transform.OutputKeys ; 14 import java.io.OutputStreamWriter ; 15 import java.io.PrintStream ; 16 import java.util.ArrayList ; 17 import java.util.Iterator ; 18 import java.util.Properties ; 19 20 23 24 public class Message extends Instruction { 25 26 28 private Expression terminate; 29 private Expression select; 30 31 public Message(Expression select, Expression terminate) { 32 this.terminate = terminate; 33 this.select = select; 34 adoptChildExpression(terminate); 35 adoptChildExpression(select); 36 } 37 38 45 46 public Expression simplify(StaticContext env) throws XPathException { 47 select = select.simplify(env); 48 if (terminate != null) { 49 terminate = terminate.simplify(env); 50 } 51 return this; 52 } 53 54 public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException { 55 select = select.typeCheck(env, contextItemType); 56 adoptChildExpression(select); 57 if (terminate != null) { 58 terminate = terminate.typeCheck(env, contextItemType); 59 adoptChildExpression(terminate); 60 } 61 return this; 62 } 63 64 public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException { 65 select = select.optimize(opt, env, contextItemType); 66 adoptChildExpression(select); 67 if (terminate != null) { 68 terminate = terminate.optimize(opt, env, contextItemType); 69 adoptChildExpression(terminate); 70 } 71 return this; 72 } 73 74 77 78 public int getInstructionNameCode() { 79 return StandardNames.XSL_MESSAGE; 80 } 81 82 public ItemType getItemType() { 83 return NoNodeTest.getInstance(); 84 } 85 86 public int getCardinality() { 87 return StaticProperty.EMPTY; 88 } 89 90 94 95 public final boolean createsNewNodes() { 96 return true; 97 } 98 103 104 protected void promoteInst(PromotionOffer offer) throws XPathException { 105 if (select != null) { 106 select = doPromotion(select, offer); 107 } 108 if (terminate != null) { 109 terminate = doPromotion(terminate, offer); 110 } 111 } 112 113 118 119 public Iterator iterateSubExpressions() { 120 ArrayList list = new ArrayList (2); 121 if (select != null) { 122 list.add(select); 123 } 124 if (terminate != null) { 125 list.add(terminate); 126 } 127 return list.iterator(); 128 } 129 130 public TailCall processLeavingTail(XPathContext context) throws XPathException { 131 Controller controller = context.getController(); 132 Emitter emitter = controller.getMessageEmitter(); 133 if (emitter==null) { 134 emitter = controller.makeMessageEmitter(); 135 } 136 if (emitter.getWriter()==null) { 137 emitter.setWriter(new OutputStreamWriter (System.err)); 138 } 139 140 TreeReceiver rec = new TreeReceiver(emitter); 141 142 XPathContext c2 = context.newMinorContext(); 143 c2.setOrigin(this); 144 Properties props = new Properties (); 145 props.put(OutputKeys.OMIT_XML_DECLARATION, "yes"); 146 emitter.setOutputProperties(props); 147 c2.changeOutputDestination(props, rec, false, Validation.PRESERVE, null); 148 149 if (select != null) { 150 SequenceIterator iter = select.iterate(c2); 151 while (true) { 152 Item item = iter.next(); 153 if (item == null) { 154 break; 155 } 156 rec.append(item, locationId, NodeInfo.ALL_NAMESPACES); 157 } 158 } 159 rec.close(); 160 161 if (terminate != null) { 162 String term = terminate.evaluateAsString(context); 163 if (term.equals("no")) { 164 } else if (term.equals("yes")) { 166 throw new TerminationException("Processing terminated by xsl:message at line " + getLineNumber()); 167 } else { 168 DynamicError e = new DynamicError("The terminate attribute of xsl:message must be 'yes' or 'no'"); 169 e.setXPathContext(context); 170 e.setErrorCode("XTDE0030"); 171 throw e; 172 } 173 } 174 return null; 175 } 176 177 184 185 public void display(int level, NamePool pool, PrintStream out) { 186 out.println(ExpressionTool.indent(level) + "message"); 187 } 188 } 189 | Popular Tags |