1 16 19 20 package org.apache.xalan.xsltc.dom; 21 22 import java.io.FileNotFoundException ; 23 24 import javax.xml.transform.stream.StreamSource ; 25 26 import org.apache.xalan.xsltc.DOM; 27 import org.apache.xalan.xsltc.DOMCache; 28 import org.apache.xalan.xsltc.DOMEnhancedForDTM; 29 import org.apache.xalan.xsltc.TransletException; 30 import org.apache.xalan.xsltc.runtime.AbstractTranslet; 31 import org.apache.xalan.xsltc.trax.TemplatesImpl; 32 import org.apache.xml.dtm.DTM; 33 import org.apache.xml.dtm.DTMAxisIterator; 34 import org.apache.xml.dtm.DTMManager; 35 import org.apache.xml.dtm.ref.DTMDefaultBase; 36 import org.apache.xml.dtm.ref.EmptyIterator; 37 import org.apache.xml.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 String baseURI = xslURI; 104 if (!SystemIDResolver.isAbsoluteURI(xslURI)) 105 baseURI = SystemIDResolver.getAbsoluteURIFromRelative(xslURI); 106 107 String href = (String )arg; 108 if (href.length() == 0) { 109 href = ""; 110 TemplatesImpl templates = (TemplatesImpl)translet.getTemplates(); 114 DOM sdom = null; 115 if (templates != null) { 116 sdom = templates.getStylesheetDOM(); 117 } 118 119 if (sdom != null) { 123 return document(sdom, translet, dom); 124 } 125 else { 126 return document(href, baseURI, translet, dom, true); 127 } 128 } 129 else { 130 return document(href, baseURI, translet, dom); 131 } 132 } else if (arg instanceof DTMAxisIterator) { 133 return document((DTMAxisIterator)arg, null, translet, dom); 134 } else { 135 final String err = "document("+arg.toString()+")"; 136 throw new IllegalArgumentException (err); 137 } 138 } catch (Exception e) { 139 throw new TransletException(e); 140 } 141 } 142 143 private static DTMAxisIterator document(String uri, String base, 144 AbstractTranslet translet, DOM dom) 145 throws Exception 146 { 147 return document(uri, base, translet, dom, false); 148 } 149 150 private static DTMAxisIterator document(String uri, String base, 151 AbstractTranslet translet, DOM dom, 152 boolean cacheDOM) 153 throws Exception 154 { 155 try { 156 final String originalUri = uri; 157 MultiDOM multiplexer = (MultiDOM)dom; 158 159 if (base != null && !base.equals("")) { 161 uri = SystemIDResolver.getAbsoluteURI(uri, base); 162 } 163 164 if (uri == null || uri.equals("")) { 167 return(EmptyIterator.getInstance()); 168 } 169 170 int mask = multiplexer.getDocumentMask(uri); 172 if (mask != -1) { 173 DOM newDom = ((DOMAdapter)multiplexer.getDOMAdapter(uri)) 174 .getDOMImpl(); 175 if (newDom instanceof DOMEnhancedForDTM) { 176 return new SingletonIterator(((DOMEnhancedForDTM)newDom) 177 .getDocument(), 178 true); 179 } 180 } 181 182 DOMCache cache = translet.getDOMCache(); 184 DOM newdom; 185 186 mask = multiplexer.nextMask(); 188 if (cache != null) { 189 newdom = cache.retrieveDocument(base, originalUri, translet); 190 if (newdom == null) { 191 final Exception e = new FileNotFoundException (originalUri); 192 throw new TransletException(e); 193 } 194 } else { 195 XSLTCDTMManager dtmManager = (XSLTCDTMManager)multiplexer 199 .getDTMManager(); 200 DOMEnhancedForDTM enhancedDOM = 201 (DOMEnhancedForDTM) dtmManager.getDTM(new StreamSource (uri), 202 false, null, true, false, 203 translet.hasIdCall(), cacheDOM); 204 newdom = enhancedDOM; 205 206 if (cacheDOM) { 208 TemplatesImpl templates = (TemplatesImpl)translet.getTemplates(); 209 if (templates != null) { 210 templates.setStylesheetDOM(enhancedDOM); 211 } 212 } 213 214 translet.prepassDocument(enhancedDOM); 215 enhancedDOM.setDocumentURI(uri); 216 } 217 218 final DOMAdapter domAdapter = translet.makeDOMAdapter(newdom); 220 multiplexer.addDOMAdapter(domAdapter); 221 222 translet.buildKeys(domAdapter, null, null, newdom.getDocument()); 224 225 return new SingletonIterator(newdom.getDocument(), true); 227 } catch (Exception e) { 228 throw e; 229 } 230 } 231 232 233 private static DTMAxisIterator document(DTMAxisIterator arg1, 234 String baseURI, 235 AbstractTranslet translet, DOM dom) 236 throws Exception 237 { 238 UnionIterator union = new UnionIterator(dom); 239 int node = DTM.NULL; 240 241 while ((node = arg1.next()) != DTM.NULL) { 242 String uri = dom.getStringValueX(node); 243 if (baseURI == null) { 245 baseURI = dom.getDocumentURI(node); 246 if (!SystemIDResolver.isAbsoluteURI(baseURI)) 247 baseURI = SystemIDResolver.getAbsoluteURIFromRelative(baseURI); 248 } 249 union.addIterator(document(uri, baseURI, translet, dom)); 250 } 251 return(union); 252 } 253 254 263 private static DTMAxisIterator document(DOM newdom, 264 AbstractTranslet translet, 265 DOM dom) 266 throws Exception 267 { 268 DTMManager dtmManager = ((MultiDOM)dom).getDTMManager(); 269 if (dtmManager != null && newdom instanceof DTM) { 271 ((DTM)newdom).migrateTo(dtmManager); 272 } 273 274 translet.prepassDocument(newdom); 275 276 final DOMAdapter domAdapter = translet.makeDOMAdapter(newdom); 278 ((MultiDOM)dom).addDOMAdapter(domAdapter); 279 280 translet.buildKeys(domAdapter, null, null, 282 newdom.getDocument()); 283 284 return new SingletonIterator(newdom.getDocument(), true); 286 } 287 288 } 289 | Popular Tags |