1 package net.sf.saxon.instruct; 2 3 import net.sf.saxon.Controller; 4 import net.sf.saxon.event.Receiver; 5 import net.sf.saxon.event.SequenceReceiver; 6 import net.sf.saxon.expr.Expression; 7 import net.sf.saxon.expr.ExpressionTool; 8 import net.sf.saxon.expr.StaticContext; 9 import net.sf.saxon.expr.XPathContext; 10 import net.sf.saxon.om.*; 11 import net.sf.saxon.pattern.NodeKindTest; 12 import net.sf.saxon.pull.UnconstructedDocument; 13 import net.sf.saxon.style.StandardNames; 14 import net.sf.saxon.tinytree.TinyBuilder; 15 import net.sf.saxon.trans.DynamicError; 16 import net.sf.saxon.trans.XPathException; 17 import net.sf.saxon.type.ItemType; 18 import net.sf.saxon.value.TextFragmentValue; 19 20 import java.io.PrintStream ; 21 22 23 33 34 public class DocumentInstr extends ParentNodeConstructor { 35 36 private static final int[] treeSizeParameters = {50, 10, 5, 200}; 37 39 private boolean textOnly; 40 private String constantText; 41 private String baseURI; 42 43 44 public DocumentInstr(boolean textOnly, 45 String constantText, 46 String baseURI) { 47 this.textOnly = textOnly; 48 this.constantText = constantText; 49 this.baseURI = baseURI; 50 } 51 52 56 57 public int getImplementationMethod() { 58 return Expression.EVALUATE_METHOD; 59 } 60 61 62 65 66 public void setValidationAction(int action) { 67 validation = action; 68 } 69 70 78 79 public Expression simplify(StaticContext env) throws XPathException { 80 setLazyConstruction(env.getConfiguration().isLazyConstructionMode()); 81 return super.simplify(env); 82 } 83 84 public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException { 85 Expression res = super.typeCheck(env, contextItemType); 86 return res; 88 } 89 90 public ItemType getItemType() { 91 return NodeKindTest.DOCUMENT; 92 } 93 94 public TailCall processLeavingTail(XPathContext context) throws XPathException { 95 Item item = evaluateItem(context); 96 if (item != null) { 97 SequenceReceiver out = context.getReceiver(); 98 out.append(item, locationId, NodeInfo.ALL_NAMESPACES); 99 } 100 return null; 101 } 102 103 106 107 public Item evaluateItem(XPathContext context) throws XPathException { 108 if (isLazyConstruction()) { 109 return new UnconstructedDocument(this, context); 110 } else { 111 Controller controller = context.getController(); 112 DocumentInfo root; 113 if (textOnly) { 114 CharSequence textValue; 115 if (constantText != null) { 116 textValue = constantText; 117 } else { 118 FastStringBuffer sb = new FastStringBuffer(100); 119 SequenceIterator iter = content.iterate(context); 120 if (iter instanceof AtomizableIterator) { 121 ((AtomizableIterator)iter).setIsAtomizing(true); 122 } 123 while (true) { 124 Item item = iter.next(); 125 if (item==null) break; 126 sb.append(item.getStringValueCS()); 127 } 128 textValue = sb.condense(); 129 } 130 root = new TextFragmentValue(textValue, baseURI); 131 ((TextFragmentValue)root).setConfiguration(controller.getConfiguration()); 132 } else { 133 try { 134 XPathContext c2 = context.newMinorContext(); 135 c2.setOrigin(this); 136 137 TinyBuilder builder = new TinyBuilder(); 138 builder.setSizeParameters(treeSizeParameters); 139 builder.setLineNumbering(controller.getConfiguration().isLineNumbering()); 140 141 Receiver receiver = builder; 142 receiver.setSystemId(baseURI); 143 receiver.setPipelineConfiguration(controller.makePipelineConfiguration()); 144 145 c2.changeOutputDestination(null, 146 receiver, 147 false, 148 validation, 149 getSchemaType()); 150 Receiver out = c2.getReceiver(); 151 out.open(); 152 out.startDocument(0); 153 154 content.process(c2); 155 156 out.endDocument(); 157 out.close(); 158 159 root = (DocumentInfo)builder.getCurrentRoot(); 160 } catch (XPathException e) { 161 if (e.getLocator() == null) { 162 e.setLocator(this); 163 } 164 if (e instanceof DynamicError && ((DynamicError)e).getXPathContext() == null) { 165 ((DynamicError)e).setXPathContext(context); 166 } 167 throw e; 168 } 169 } 170 return root; 171 } 172 } 173 174 175 179 180 public int getInstructionNameCode() { 181 return StandardNames.XSL_DOCUMENT; 182 } 183 184 187 188 public void display(int level, NamePool pool, PrintStream out) { 189 out.println(ExpressionTool.indent(level) + "document-constructor"); 190 content.display(level+1, pool, out); 191 } 192 } 193 194 | Popular Tags |