1 16 19 20 package com.sun.org.apache.xalan.internal.xsltc.dom; 21 22 import java.io.FileNotFoundException ; 23 24 import javax.xml.transform.stream.StreamSource ; 25 26 import com.sun.org.apache.xalan.internal.xsltc.DOM; 27 import com.sun.org.apache.xalan.internal.xsltc.DOMCache; 28 import com.sun.org.apache.xalan.internal.xsltc.DOMEnhancedForDTM; 29 import com.sun.org.apache.xalan.internal.xsltc.TransletException; 30 import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet; 31 import com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl; 32 import com.sun.org.apache.xml.internal.dtm.DTM; 33 import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator; 34 import com.sun.org.apache.xml.internal.dtm.DTMManager; 35 import com.sun.org.apache.xml.internal.dtm.ref.DTMDefaultBase; 36 import com.sun.org.apache.xml.internal.dtm.ref.EmptyIterator; 37 import com.sun.org.apache.xml.internal.utils.SystemIDResolver; 38 39 import org.xml.sax.InputSource ; 40 import org.xml.sax.XMLReader ; 41 42 45 public final class LoadDocument { 46 47 private static final String NAMESPACE_FEATURE = 48 "http://xml.org/sax/features/namespaces"; 49 50 57 public static DTMAxisIterator documentF(Object arg1, DTMAxisIterator arg2, 58 String xslURI, AbstractTranslet translet, DOM dom) 59 throws TransletException { 60 String baseURI = null; 61 final int arg2FirstNode = arg2.next(); 62 if (arg2FirstNode == DTMAxisIterator.END) { 63 return EmptyIterator.getInstance(); 65 } else { 66 baseURI = dom.getDocumentURI(arg2FirstNode); 70 if (!SystemIDResolver.isAbsoluteURI(baseURI)) 71 baseURI = SystemIDResolver.getAbsoluteURIFromRelative(baseURI); 72 } 73 74 try { 75 if (arg1 instanceof String ) { 76 if (((String )arg1).length() == 0) { 77 return document(xslURI, "", translet, dom); 78 } else { 79 return document((String )arg1, baseURI, translet, dom); 80 } 81 } else if (arg1 instanceof DTMAxisIterator) { 82 return document((DTMAxisIterator)arg1, baseURI, translet, dom); 83 } else { 84 final String err = "document("+arg1.toString()+")"; 85 throw new IllegalArgumentException (err); 86 } 87 } catch (Exception e) { 88 throw new TransletException(e); 89 } 90 } 91 98 public static DTMAxisIterator documentF(Object arg, String xslURI, 99 AbstractTranslet translet, DOM dom) 100 throws TransletException { 101 try { 102 if (arg instanceof String ) { 103 if (xslURI == null ) 104 xslURI=""; 105 String baseURI = xslURI; 106 if (!SystemIDResolver.isAbsoluteURI(xslURI)) 107 baseURI = SystemIDResolver.getAbsoluteURIFromRelative(xslURI); 108 109 String href = (String )arg; 110 if (href.length() == 0) { 111 href = ""; 112 TemplatesImpl templates = (TemplatesImpl)translet.getTemplates(); 116 DOM sdom = null; 117 if (templates != null) { 118 sdom = templates.getStylesheetDOM(); 119 } 120 121 if (sdom != null) { 125 return document(sdom, translet, dom); 126 } 127 else { 128 return document(href, baseURI, translet, dom, true); 129 } 130 } 131 else { 132 return document(href, baseURI, translet, dom); 133 } 134 } else if (arg instanceof DTMAxisIterator) { 135 return document((DTMAxisIterator)arg, null, translet, dom); 136 } else { 137 final String err = "document("+arg.toString()+")"; 138 throw new IllegalArgumentException (err); 139 } 140 } catch (Exception e) { 141 throw new TransletException(e); 142 } 143 } 144 145 private static DTMAxisIterator document(String uri, String base, 146 AbstractTranslet translet, DOM dom) 147 throws Exception 148 { 149 return document(uri, base, translet, dom, false); 150 } 151 152 private static DTMAxisIterator document(String uri, String base, 153 AbstractTranslet translet, DOM dom, 154 boolean cacheDOM) 155 throws Exception 156 { 157 try { 158 final String originalUri = uri; 159 MultiDOM multiplexer = (MultiDOM)dom; 160 161 if (base != null && !base.equals("")) { 163 uri = SystemIDResolver.getAbsoluteURI(uri, base); 164 } 165 166 if (uri == null || uri.equals("")) { 169 return(EmptyIterator.getInstance()); 170 } 171 172 int mask = multiplexer.getDocumentMask(uri); 174 if (mask != -1) { 175 DOM newDom = ((DOMAdapter)multiplexer.getDOMAdapter(uri)) 176 .getDOMImpl(); 177 if (newDom instanceof DOMEnhancedForDTM) { 178 return new SingletonIterator(((DOMEnhancedForDTM)newDom) 179 .getDocument(), 180 true); 181 } 182 } 183 184 DOMCache cache = translet.getDOMCache(); 186 DOM newdom; 187 188 mask = multiplexer.nextMask(); 190 if (cache != null) { 191 newdom = cache.retrieveDocument(base, originalUri, translet); 192 if (newdom == null) { 193 final Exception e = new FileNotFoundException (originalUri); 194 throw new TransletException(e); 195 } 196 } else { 197 XSLTCDTMManager dtmManager = (XSLTCDTMManager)multiplexer 201 .getDTMManager(); 202 DOMEnhancedForDTM enhancedDOM = 203 (DOMEnhancedForDTM) dtmManager.getDTM(new StreamSource (uri), 204 false, null, true, false, 205 translet.hasIdCall(), cacheDOM); 206 newdom = enhancedDOM; 207 208 if (cacheDOM) { 210 TemplatesImpl templates = (TemplatesImpl)translet.getTemplates(); 211 if (templates != null) { 212 templates.setStylesheetDOM(enhancedDOM); 213 } 214 } 215 216 translet.prepassDocument(enhancedDOM); 217 enhancedDOM.setDocumentURI(uri); 218 } 219 220 final DOMAdapter domAdapter = translet.makeDOMAdapter(newdom); 222 multiplexer.addDOMAdapter(domAdapter); 223 224 translet.buildKeys(domAdapter, null, null, newdom.getDocument()); 226 227 return new SingletonIterator(newdom.getDocument(), true); 229 } catch (Exception e) { 230 throw e; 231 } 232 } 233 234 235 private static DTMAxisIterator document(DTMAxisIterator arg1, 236 String baseURI, 237 AbstractTranslet translet, DOM dom) 238 throws Exception 239 { 240 UnionIterator union = new UnionIterator(dom); 241 int node = DTM.NULL; 242 243 while ((node = arg1.next()) != DTM.NULL) { 244 String uri = dom.getStringValueX(node); 245 if (baseURI == null) { 247 baseURI = dom.getDocumentURI(node); 248 if (!SystemIDResolver.isAbsoluteURI(baseURI)) 249 baseURI = SystemIDResolver.getAbsoluteURIFromRelative(baseURI); 250 } 251 union.addIterator(document(uri, baseURI, translet, dom)); 252 } 253 return(union); 254 } 255 256 265 private static DTMAxisIterator document(DOM newdom, 266 AbstractTranslet translet, 267 DOM dom) 268 throws Exception 269 { 270 DTMManager dtmManager = ((MultiDOM)dom).getDTMManager(); 271 if (dtmManager != null && newdom instanceof DTM) { 273 ((DTM)newdom).migrateTo(dtmManager); 274 } 275 276 translet.prepassDocument(newdom); 277 278 final DOMAdapter domAdapter = translet.makeDOMAdapter(newdom); 280 ((MultiDOM)dom).addDOMAdapter(domAdapter); 281 282 translet.buildKeys(domAdapter, null, null, 284 newdom.getDocument()); 285 286 return new SingletonIterator(newdom.getDocument(), true); 288 } 289 290 } 291 | Popular Tags |