KickJava   Java API By Example, From Geeks To Geeks.

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

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

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