1 package net.sf.saxon.instruct; 2 import net.sf.saxon.Controller; 3 import net.sf.saxon.event.*; 4 import net.sf.saxon.expr.ExpressionTool; 5 import net.sf.saxon.expr.XPathContext; 6 import net.sf.saxon.om.Item; 7 import net.sf.saxon.om.NamePool; 8 import net.sf.saxon.om.NodeInfo; 9 import net.sf.saxon.style.StandardNames; 10 import net.sf.saxon.trans.DynamicError; 11 import net.sf.saxon.trans.XPathException; 12 import net.sf.saxon.type.AnyItemType; 13 import net.sf.saxon.type.ItemType; 14 import net.sf.saxon.type.SchemaType; 15 import net.sf.saxon.type.Type; 16 17 import java.io.PrintStream ; 18 19 22 23 public class Copy extends ElementCreator { 24 25 private boolean copyNamespaces; 26 private ItemType contextItemType; 27 28 public Copy(boolean copyNamespaces, 29 boolean inheritNamespaces, 30 SchemaType schemaType, 31 int validation) { 32 this.copyNamespaces = copyNamespaces; 33 this.inheritNamespaces = inheritNamespaces; 34 setSchemaType(schemaType); 35 this.validation = validation; 36 if (copyNamespaces) { 37 setLazyConstruction(false); 38 } 41 } 42 43 46 47 public int getInstructionNameCode() { 48 return StandardNames.XSL_COPY; 49 } 50 51 55 56 public ItemType getItemType() { 57 if (contextItemType == null) { 58 return AnyItemType.getInstance(); 59 } else { 60 return contextItemType; 61 } 62 } 63 64 70 71 public int getNameCode(XPathContext context) 72 throws XPathException { 73 return ((NodeInfo)context.getContextItem()).getNameCode(); 74 } 75 76 82 83 protected void outputNamespaceNodes(XPathContext context, Receiver receiver) 84 throws XPathException { 85 if (copyNamespaces) { 86 NodeInfo element = (NodeInfo)context.getContextItem(); 87 element.sendNamespaceDeclarations(receiver, true); 88 } 89 } 90 91 96 97 public int[] getActiveNamespaces() throws XPathException { 98 if (copyNamespaces) { 99 throw new UnsupportedOperationException (); 101 } else { 102 return null; 103 } 104 } 105 106 public TailCall processLeavingTail(XPathContext context) throws XPathException { 107 Controller controller = context.getController(); 108 XPathContext c2 = context.newMinorContext(); 109 c2.setOrigin(this); 110 SequenceReceiver out = c2.getReceiver(); 111 Item item = context.getContextItem(); 112 if (!(item instanceof NodeInfo)) { 113 out.append(item, locationId, NodeInfo.ALL_NAMESPACES); 114 return null; 115 } 116 NodeInfo source = (NodeInfo)item; 117 118 120 switch(source.getNodeKind()) { 121 122 case Type.ELEMENT: 123 return super.processLeavingTail(c2); 125 126 case Type.ATTRIBUTE: 127 try { 128 CopyOf.copyAttribute(source, getSchemaType(), validation, this, c2, false); 129 } catch (NoOpenStartTagException err) { 130 err.setXPathContext(context); 131 throw dynamicError(this, err, c2); 132 } 133 break; 134 135 case Type.TEXT: 136 out.characters(source.getStringValueCS(), locationId, 0); 137 break; 138 139 case Type.PROCESSING_INSTRUCTION: 140 out.processingInstruction(source.getDisplayName(), source.getStringValueCS(), locationId, 0); 141 break; 142 143 case Type.COMMENT: 144 out.comment(source.getStringValueCS(), locationId, 0); 145 break; 146 147 case Type.NAMESPACE: 148 try { 149 source.copy(out, NodeInfo.NO_NAMESPACES, false, locationId); 150 } catch (NoOpenStartTagException err) { 151 DynamicError e = new DynamicError(err.getMessage()); 152 e.setXPathContext(context); 153 e.setErrorCode(err.getErrorCodeLocalPart()); 154 throw dynamicError(this, e, context); 155 } 156 break; 157 158 case Type.DOCUMENT: 159 Receiver val = controller.getConfiguration(). 160 getDocumentValidator(out, 161 source.getBaseURI(), 162 controller.getNamePool(), 163 validation, getSchemaType()); 164 if (val != out) { 165 SequenceReceiver sr = new TreeReceiver(val); 166 sr.setPipelineConfiguration(out.getPipelineConfiguration()); 167 c2.setReceiver(sr); 168 val = sr; 169 } 170 val.startDocument(0); 171 content.process(c2); 172 val.endDocument(); 173 break; 174 175 default: 176 throw new IllegalArgumentException ("Unknown node kind " + source.getNodeKind()); 177 178 } 179 return null; 180 } 181 182 187 188 public Item evaluateItem(XPathContext context) throws XPathException { 189 Controller controller = context.getController(); 190 XPathContext c2 = context.newMinorContext(); 191 c2.setOrigin(this); 192 SequenceOutputter seq = new SequenceOutputter(1); 193 seq.setPipelineConfiguration(controller.makePipelineConfiguration()); 194 c2.setTemporaryReceiver(seq); 195 process(c2); 196 seq.close(); 197 return seq.getFirstItem(); 198 } 199 200 207 208 public void display(int level, NamePool pool, PrintStream out) { 209 out.println(ExpressionTool.indent(level) + "copy"); 210 } 211 212 213 } 214 215 | Popular Tags |