1 package net.sf.saxon.pull; 2 3 import net.sf.saxon.event.Receiver; 4 import net.sf.saxon.event.SequenceReceiver; 5 import net.sf.saxon.om.AttributeCollection; 6 import net.sf.saxon.om.NamespaceDeclarations; 7 import net.sf.saxon.om.Orphan; 8 import net.sf.saxon.trans.XPathException; 9 import net.sf.saxon.type.Type; 10 11 20 21 public class PullPushTee extends PullFilter { 22 23 private Receiver branch; 24 boolean previousAtomic = false; 25 26 31 32 public PullPushTee(PullProvider base, Receiver branch) throws XPathException { 33 super(base); 34 this.branch = branch; 35 branch.open(); 36 } 37 38 41 42 public Receiver getReceiver() { 43 return branch; 44 } 45 46 53 54 public int next() throws XPathException { 55 currentEvent = super.next(); 56 copyEvent(currentEvent); 57 return currentEvent; 58 } 59 60 61 64 65 private void copyEvent(int event) throws XPathException { 66 PullProvider in = getUnderlyingProvider(); 67 Receiver out = branch; 68 switch (event) { 69 case START_DOCUMENT: 70 out.startDocument(0); 71 break; 72 73 case START_ELEMENT: 74 out.startElement(in.getNameCode(), in.getTypeAnnotation(), 0, 0); 75 76 NamespaceDeclarations decl = in.getNamespaceDeclarations(); 77 for (int i=0; i<decl.getLength(); i++) { 78 out.namespace(decl.getNamespaceCode(i), 0); 79 } 80 81 AttributeCollection atts = in.getAttributes(); 82 for (int i=0; i<atts.getLength(); i++) { 83 out.attribute(atts.getNameCode(i), atts.getTypeAnnotation(i), 84 atts.getValue(i), 0, atts.getProperties(i)); 85 } 86 87 out.startContent(); 88 break; 89 90 case TEXT: 91 92 out.characters(in.getStringValue(), 0, 0); 93 break; 94 95 case COMMENT: 96 97 out.comment(in.getStringValue(), 0, 0); 98 break; 99 100 case PROCESSING_INSTRUCTION: 101 102 out.processingInstruction( 103 in.getPipelineConfiguration().getConfiguration().getNamePool().getLocalName(in.getNameCode()), 104 in.getStringValue(), 0, 0); 105 break; 106 107 case END_ELEMENT: 108 109 out.endElement(); 110 break; 111 112 case END_DOCUMENT: 113 out.endDocument(); 114 break; 115 116 case END_OF_INPUT: 117 in.close(); 118 out.close(); 119 break; 120 121 case ATOMIC_VALUE: 122 if (out instanceof SequenceReceiver) { 123 ((SequenceReceiver)out).append(super.getAtomicValue(), 0, 0); 124 break; 125 } else { 126 if (previousAtomic) { 127 out.characters(" ", 0, 0); 128 } 129 CharSequence chars = in.getStringValue(); 130 out.characters(chars, 0, 0); 131 break; 132 } 133 134 case ATTRIBUTE: 135 if (out instanceof SequenceReceiver) { 136 Orphan o = new Orphan(in.getPipelineConfiguration().getConfiguration()); 137 o.setNameCode(getNameCode()); 138 o.setNodeKind(Type.ATTRIBUTE); 139 o.setStringValue(getStringValue()); 140 ((SequenceReceiver)out).append(o, 0, 0); 141 break; 142 } else { 143 out.attribute(getNameCode(), getTypeAnnotation(), getStringValue(), 0, 0); 144 break; 145 } 147 148 case NAMESPACE: 149 if (out instanceof SequenceReceiver) { 150 Orphan o = new Orphan(in.getPipelineConfiguration().getConfiguration()); 151 o.setNameCode(getNameCode()); 152 o.setNodeKind(Type.NAMESPACE); 153 o.setStringValue(getStringValue()); 154 ((SequenceReceiver)out).append(o, 0, 0); 155 break; 156 } else { 157 int nsCode = getNamePool().getNamespaceCode(getNameCode()); 158 out.namespace(nsCode, 0); 159 break; 160 } 162 163 default: 164 throw new UnsupportedOperationException ("" + event); 165 166 } 167 previousAtomic = (event == ATOMIC_VALUE); 168 } 169 } 170 171 | Popular Tags |