1 16 19 package org.apache.xalan.extensions; 20 21 import javax.xml.transform.TransformerException ; 22 23 import org.apache.xalan.templates.Stylesheet; 24 import org.apache.xalan.transformer.ClonerToResultTree; 25 import org.apache.xalan.transformer.TransformerImpl; 26 import org.apache.xml.dtm.DTM; 27 import org.apache.xml.dtm.DTMAxisIterator; 28 import org.apache.xml.dtm.DTMIterator; 29 import org.apache.xalan.serialize.SerializerUtils; 30 import org.apache.xml.serializer.SerializationHandler; 31 import org.apache.xml.utils.QName; 32 import org.apache.xpath.XPathContext; 33 import org.apache.xpath.axes.DescendantIterator; 34 import org.apache.xpath.axes.OneStepIterator; 35 import org.apache.xpath.objects.XBoolean; 36 import org.apache.xpath.objects.XNodeSet; 37 import org.apache.xpath.objects.XNumber; 38 import org.apache.xpath.objects.XObject; 39 import org.apache.xpath.objects.XRTreeFrag; 40 import org.apache.xpath.objects.XString; 41 import org.w3c.dom.DocumentFragment ; 42 import org.w3c.dom.traversal.NodeIterator; 43 44 46 52 public class XSLProcessorContext 53 { 54 55 65 public XSLProcessorContext(TransformerImpl transformer, 66 Stylesheet stylesheetTree) 67 { 68 69 this.transformer = transformer; 70 this.stylesheetTree = stylesheetTree; 71 org.apache.xpath.XPathContext xctxt = transformer.getXPathContext(); 73 this.mode = transformer.getMode(); 74 this.sourceNode = xctxt.getCurrentNode(); 75 this.sourceTree = xctxt.getDTM(this.sourceNode); 76 } 77 78 79 private TransformerImpl transformer; 80 81 86 public TransformerImpl getTransformer() 87 { 88 return transformer; 89 } 90 91 92 private Stylesheet stylesheetTree; 93 94 99 public Stylesheet getStylesheet() 100 { 101 return stylesheetTree; 102 } 103 104 105 private org.apache.xml.dtm.DTM sourceTree; 106 107 112 public org.w3c.dom.Node getSourceTree() 113 { 114 return sourceTree.getNode(sourceTree.getDocumentRoot(sourceNode)); 115 } 116 117 118 private int sourceNode; 119 120 125 public org.w3c.dom.Node getContextNode() 126 { 127 return sourceTree.getNode(sourceNode); 128 } 129 130 131 private QName mode; 132 133 138 public QName getMode() 139 { 140 return mode; 141 } 142 143 157 public void outputToResultTree(Stylesheet stylesheetTree, Object obj) 158 throws TransformerException , java.net.MalformedURLException , 159 java.io.FileNotFoundException , java.io.IOException 160 { 161 162 try 163 { 164 SerializationHandler rtreeHandler = transformer.getResultTreeHandler(); 165 XPathContext xctxt = transformer.getXPathContext(); 166 XObject value; 167 168 if (obj instanceof XObject) 173 { 174 value = (XObject) obj; 175 } 176 else if (obj instanceof String ) 177 { 178 value = new XString((String ) obj); 179 } 180 else if (obj instanceof Boolean ) 181 { 182 value = new XBoolean(((Boolean ) obj).booleanValue()); 183 } 184 else if (obj instanceof Double ) 185 { 186 value = new XNumber(((Double ) obj).doubleValue()); 187 } 188 else if (obj instanceof DocumentFragment ) 189 { 190 int handle = xctxt.getDTMHandleFromNode((DocumentFragment )obj); 191 192 value = new XRTreeFrag(handle, xctxt); 193 } 194 else if (obj instanceof DTM) 195 { 196 DTM dtm = (DTM)obj; 197 DTMIterator iterator = new DescendantIterator(); 198 iterator.setRoot(dtm.getDocument(), xctxt); 204 value = new XNodeSet(iterator); 205 } 206 else if (obj instanceof DTMAxisIterator) 207 { 208 DTMAxisIterator iter = (DTMAxisIterator)obj; 209 DTMIterator iterator = new OneStepIterator(iter, -1); 210 value = new XNodeSet(iterator); 211 } 212 else if (obj instanceof DTMIterator) 213 { 214 value = new XNodeSet((DTMIterator) obj); 215 } 216 else if (obj instanceof NodeIterator) 217 { 218 value = new XNodeSet(new org.apache.xpath.NodeSetDTM(((NodeIterator)obj), xctxt)); 219 } 220 else if (obj instanceof org.w3c.dom.Node ) 221 { 222 value = 223 new XNodeSet(xctxt.getDTMHandleFromNode((org.w3c.dom.Node ) obj), 224 xctxt.getDTMManager()); 225 } 226 else 227 { 228 value = new XString(obj.toString()); 229 } 230 231 int type = value.getType(); 232 String s; 233 234 switch (type) 235 { 236 case XObject.CLASS_BOOLEAN : 237 case XObject.CLASS_NUMBER : 238 case XObject.CLASS_STRING : 239 s = value.str(); 240 241 rtreeHandler.characters(s.toCharArray(), 0, s.length()); 242 break; 243 244 case XObject.CLASS_NODESET : DTMIterator nl = value.iter(); 246 247 int pos; 248 249 while (DTM.NULL != (pos = nl.nextNode())) 250 { 251 DTM dtm = nl.getDTM(pos); 252 int top = pos; 253 254 while (DTM.NULL != pos) 255 { 256 rtreeHandler.flushPending(); 257 ClonerToResultTree.cloneToResultTree(pos, dtm.getNodeType(pos), 258 dtm, rtreeHandler, true); 259 260 int nextNode = dtm.getFirstChild(pos); 261 262 while (DTM.NULL == nextNode) 263 { 264 if (DTM.ELEMENT_NODE == dtm.getNodeType(pos)) 265 { 266 rtreeHandler.endElement("", "", dtm.getNodeName(pos)); 267 } 268 269 if (top == pos) 270 break; 271 272 nextNode = dtm.getNextSibling(pos); 273 274 if (DTM.NULL == nextNode) 275 { 276 pos = dtm.getParent(pos); 277 278 if (top == pos) 279 { 280 if (DTM.ELEMENT_NODE == dtm.getNodeType(pos)) 281 { 282 rtreeHandler.endElement("", "", dtm.getNodeName(pos)); 283 } 284 285 nextNode = DTM.NULL; 286 287 break; 288 } 289 } 290 } 291 292 pos = nextNode; 293 } 294 } 295 break; 296 case XObject.CLASS_RTREEFRAG : 297 SerializerUtils.outputResultTreeFragment( 298 rtreeHandler, value, transformer.getXPathContext()); 299 break; 302 } 303 } 304 catch(org.xml.sax.SAXException se) 305 { 306 throw new TransformerException (se); 307 } 308 } 309 310 318 } 319 | Popular Tags |