KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > dom > LoadDocument


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: LoadDocument.java,v 1.25 2004/02/16 22:54:59 minchau Exp $
18  */

19
20 package org.apache.xalan.xsltc.dom;
21
22 import java.io.FileNotFoundException JavaDoc;
23
24 import javax.xml.transform.stream.StreamSource JavaDoc;
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 JavaDoc;
40 import org.xml.sax.XMLReader JavaDoc;
41
42 /**
43  * @author Morten Jorgensen
44  */

45 public final class LoadDocument {
46
47     private static final String JavaDoc NAMESPACE_FEATURE =
48        "http://xml.org/sax/features/namespaces";
49
50     /**
51      * Interprets the arguments passed from the document() function (see
52      * org/apache/xalan/xsltc/compiler/DocumentCall.java) and returns an
53      * iterator containing the requested nodes. Builds a union-iterator if
54      * several documents are requested.
55      * 2 arguments arg1 and arg2. document(Obj, node-set) call
56      */

57     public static DTMAxisIterator documentF(Object JavaDoc arg1, DTMAxisIterator arg2,
58                             String JavaDoc xslURI, AbstractTranslet translet, DOM dom)
59     throws TransletException {
60         String JavaDoc baseURI = null;
61         final int arg2FirstNode = arg2.next();
62         if (arg2FirstNode == DTMAxisIterator.END) {
63             // the second argument node-set is empty
64
return EmptyIterator.getInstance();
65         } else {
66             //System.err.println("arg2FirstNode name: "
67
// + dom.getNodeName(arg2FirstNode )+"["
68
// +Integer.toHexString(arg2FirstNode )+"]");
69
baseURI = dom.getDocumentURI(arg2FirstNode);
70             if (!SystemIDResolver.isAbsoluteURI(baseURI))
71                baseURI = SystemIDResolver.getAbsoluteURIFromRelative(baseURI);
72         }
73       
74         try {
75             if (arg1 instanceof String JavaDoc) {
76                 if (((String JavaDoc)arg1).length() == 0) {
77                     return document(xslURI, "", translet, dom);
78                 } else {
79                     return document((String JavaDoc)arg1, baseURI, translet, dom);
80                 }
81             } else if (arg1 instanceof DTMAxisIterator) {
82                 return document((DTMAxisIterator)arg1, baseURI, translet, dom);
83             } else {
84                 final String JavaDoc err = "document("+arg1.toString()+")";
85                 throw new IllegalArgumentException JavaDoc(err);
86             }
87         } catch (Exception JavaDoc e) {
88             throw new TransletException(e);
89         }
90     }
91     /**
92      * Interprets the arguments passed from the document() function (see
93      * org/apache/xalan/xsltc/compiler/DocumentCall.java) and returns an
94      * iterator containing the requested nodes. Builds a union-iterator if
95      * several documents are requested.
96      * 1 arguments arg. document(Obj) call
97      */

98     public static DTMAxisIterator documentF(Object JavaDoc arg, String JavaDoc xslURI,
99                     AbstractTranslet translet, DOM dom)
100     throws TransletException {
101         try {
102             if (arg instanceof String JavaDoc) {
103                 String JavaDoc baseURI = xslURI;
104                 if (!SystemIDResolver.isAbsoluteURI(xslURI))
105                    baseURI = SystemIDResolver.getAbsoluteURIFromRelative(xslURI);
106                 
107                 String JavaDoc href = (String JavaDoc)arg;
108                 if (href.length() == 0) {
109                     href = "";
110                     // %OPT% Optimization to cache the stylesheet DOM.
111
// The stylesheet DOM is built once and cached
112
// in the Templates object.
113
TemplatesImpl templates = (TemplatesImpl)translet.getTemplates();
114                     DOM sdom = null;
115                     if (templates != null) {
116                         sdom = templates.getStylesheetDOM();
117                     }
118                     
119                     // If the cached dom exists, we need to migrate it
120
// to the new DTMManager and create a DTMAxisIterator
121
// for the document.
122
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 JavaDoc err = "document("+arg.toString()+")";
136                 throw new IllegalArgumentException JavaDoc(err);
137             }
138         } catch (Exception JavaDoc e) {
139             throw new TransletException(e);
140         }
141     }
142  
143     private static DTMAxisIterator document(String JavaDoc uri, String JavaDoc base,
144                     AbstractTranslet translet, DOM dom)
145         throws Exception JavaDoc
146     {
147         return document(uri, base, translet, dom, false);
148     }
149  
150     private static DTMAxisIterator document(String JavaDoc uri, String JavaDoc base,
151                     AbstractTranslet translet, DOM dom,
152                     boolean cacheDOM)
153     throws Exception JavaDoc
154     {
155         try {
156         final String JavaDoc originalUri = uri;
157         MultiDOM multiplexer = (MultiDOM)dom;
158
159         // Prepend URI base to URI (from context)
160
if (base != null && !base.equals("")) {
161             uri = SystemIDResolver.getAbsoluteURI(uri, base);
162         }
163
164         // Return an empty iterator if the URI is clearly invalid
165
// (to prevent some unncessary MalformedURL exceptions).
166
if (uri == null || uri.equals("")) {
167             return(EmptyIterator.getInstance());
168         }
169         
170         // Check if this DOM has already been added to the multiplexer
171
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         // Check if we can get the DOM from a DOMCache
183
DOMCache cache = translet.getDOMCache();
184         DOM newdom;
185
186         mask = multiplexer.nextMask(); // peek
187

188         if (cache != null) {
189             newdom = cache.retrieveDocument(base, originalUri, translet);
190             if (newdom == null) {
191                 final Exception JavaDoc e = new FileNotFoundException JavaDoc(originalUri);
192                 throw new TransletException(e);
193             }
194         } else {
195             // Parse the input document and construct DOM object
196
// Trust the DTMManager to pick the right parser and
197
// set up the DOM correctly.
198
XSLTCDTMManager dtmManager = (XSLTCDTMManager)multiplexer
199                                                               .getDTMManager();
200             DOMEnhancedForDTM enhancedDOM =
201                     (DOMEnhancedForDTM) dtmManager.getDTM(new StreamSource JavaDoc(uri),
202                                             false, null, true, false,
203                                             translet.hasIdCall(), cacheDOM);
204             newdom = enhancedDOM;
205
206             // Cache the stylesheet DOM in the Templates object
207
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         // Wrap the DOM object in a DOM adapter and add to multiplexer
219
final DOMAdapter domAdapter = translet.makeDOMAdapter(newdom);
220         multiplexer.addDOMAdapter(domAdapter);
221
222         // Create index for any key elements
223
translet.buildKeys(domAdapter, null, null, newdom.getDocument());
224
225         // Return a singleton iterator containing the root node
226
return new SingletonIterator(newdom.getDocument(), true);
227         } catch (Exception JavaDoc e) {
228             throw e;
229         }
230     }
231
232
233     private static DTMAxisIterator document(DTMAxisIterator arg1,
234                                             String JavaDoc baseURI,
235                                             AbstractTranslet translet, DOM dom)
236     throws Exception JavaDoc
237     {
238         UnionIterator union = new UnionIterator(dom);
239         int node = DTM.NULL;
240
241         while ((node = arg1.next()) != DTM.NULL) {
242             String JavaDoc uri = dom.getStringValueX(node);
243             //document(node-set) if true; document(node-set,node-set) if false
244
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     /**
255      * Create a DTMAxisIterator for the newdom. This is currently only
256      * used to create an iterator for the cached stylesheet DOM.
257      *
258      * @param newdom the cached stylesheet DOM
259      * @param translet the translet
260      * @param the main dom (should be a MultiDOM)
261      * @return a DTMAxisIterator from the document root
262      */

263     private static DTMAxisIterator document(DOM newdom,
264                                             AbstractTranslet translet,
265                                             DOM dom)
266         throws Exception JavaDoc
267     {
268         DTMManager dtmManager = ((MultiDOM)dom).getDTMManager();
269         // Need to migrate the cached DTM to the new DTMManager
270
if (dtmManager != null && newdom instanceof DTM) {
271             ((DTM)newdom).migrateTo(dtmManager);
272         }
273         
274         translet.prepassDocument(newdom);
275         
276         // Wrap the DOM object in a DOM adapter and add to multiplexer
277
final DOMAdapter domAdapter = translet.makeDOMAdapter(newdom);
278         ((MultiDOM)dom).addDOMAdapter(domAdapter);
279
280         // Create index for any key elements
281
translet.buildKeys(domAdapter, null, null,
282                            newdom.getDocument());
283
284         // Return a singleton iterator containing the root node
285
return new SingletonIterator(newdom.getDocument(), true);
286     }
287  
288 }
289
Popular Tags