KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > resource > include > IncludeContext


1 package org.sapia.resource.include;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5 import java.net.URI JavaDoc;
6 import java.net.URISyntaxException JavaDoc;
7
8 import org.sapia.resource.Resource;
9 import org.sapia.resource.ResourceNotFoundException;
10 import org.sapia.resource.Utils;
11
12 /**
13  * An instance of this class encapsulates state pertaining to a single resource-include. It is
14  * created by an application-specific {@link org.sapia.resource.include.IncludeContextFactory}.
15  *
16  * <p>
17  * This class is meant to be inherited. It's {@link #doInclude(InputStream, Object)} method will
18  * typically be overridden in order to implement application-specific resource processing behavior (see the
19  * {@link #include()} method for more information.
20  *
21  * @see org.sapia.resource.include.IncludeContextFactory
22  *
23  * @author yduchesne
24  *
25  */

26 public class IncludeContext {
27  
28   private IncludeContext _parent;
29   private String JavaDoc _uri;
30   private IncludeConfig _conf;
31   private Resource _resource;
32   
33   void setParent(IncludeContext parent){
34     _parent = parent;
35   }
36   
37   void setUri(String JavaDoc uri){
38     _uri = uri;
39   }
40   
41   void setConfig(IncludeConfig conf){
42     _conf = conf;
43   }
44   
45   public IncludeConfig getConfig(){
46     return _conf;
47   }
48   
49   /**
50    * @return the parent <code>IncludeContext</code> of this instance, or <code>null</code>
51    * if this context correspond to a "root" resource-include.
52    */

53   public IncludeContext getParent(){
54     return _parent;
55   }
56   
57   /**
58    * @return the URI of the resource that this instance includes (as specified by the application).
59    */

60   public String JavaDoc getUri(){
61     return _uri;
62   }
63   
64   /**
65    * @return the absolute, canonical URI of the resource that this instance includes.
66    * @throws ResourceNotFoundException
67    * @throws IOException
68    */

69   public String JavaDoc getCanonicalUri() throws ResourceNotFoundException, IOException JavaDoc, Exception JavaDoc{
70     return resolve().getURI();
71   }
72   
73   /**
74    * This method resolves the stream corresponding to this instance's URI and internally
75    * calls the {@link #doInclude(InputStream, Object)} method of this instance, passing it
76    * the stream that was resolved - the called method is expected to return an object corresponding
77    * to the given stream. This method then pops this instance from the include stack
78    * prior to returning the included object.
79    *
80    * @return the <code>Object</code> that was "included" by the {@link #doInclude(InputStream, Object)}
81    * template method that is called by this method.
82    *
83    * @throws ResourceNotFoundException if no resource was found for this instance's URI.
84    * @throws IOException if an IO problem occurred while performing this operation.
85    * @throws Exception if an undetermined problem occurred while performing this operation.
86    */

87   public Object JavaDoc include() throws ResourceNotFoundException, IOException JavaDoc, Exception JavaDoc{
88     InputStream JavaDoc is = null;
89     try{
90       Resource res = resolve();
91       is = res.getInputStream();
92       return doInclude(is, null);
93     }finally{
94       if(is != null)
95         is.close();
96       pop();
97     }
98   }
99   
100   /**
101    * This method takes an arbitrary, application-specific context object. This is up to classes inheriting
102    * from this class to handler that object in a consistent manner.
103    *
104    * @see #include()
105    * @param context an arbitrary context object.
106    */

107   public Object JavaDoc include(Object JavaDoc context) throws ResourceNotFoundException, IOException JavaDoc, Exception JavaDoc{
108     InputStream JavaDoc is = null;
109     try{
110       Resource res = resolve();
111       is = res.getInputStream();
112       return doInclude(is, context);
113     }finally{
114       if(is != null)
115         is.close();
116       pop();
117     }
118   }
119
120   /**
121    * This method this instance from the include stack prior to returning
122    * the included stream.
123    *
124    * @return the <code>InputStream</code> that this instance includes.
125    *
126    * @throws ResourceNotFoundException if no resource was found for this instance's URI.
127    * @throws IOException if an IO problem occurred while performing this operation.
128    * @throws Exception if an undetermined problem occurred while performing this operation.
129    */

130   public InputStream JavaDoc includeStream() throws ResourceNotFoundException, IOException JavaDoc, Exception JavaDoc{
131     try{
132       Resource res = resolve();
133       return res.getInputStream();
134     }finally{
135       pop();
136     }
137   }
138   
139     
140   /**
141    * This method this instance from the include stack prior to returning
142    * the included resource.
143    *
144    * @return the <code>Resource</code> that this instance includes.
145    *
146    * @throws ResourceNotFoundException if no resource was found for this instance's URI.
147    * @throws IOException if an IO problem occurred while performing this operation.
148    * @throws Exception if an undetermined problem occurred while performing this operation.
149    */

150   public Resource includeResource() throws ResourceNotFoundException, IOException JavaDoc, Exception JavaDoc{
151     try{
152       return resolve();
153     }finally{
154       pop();
155     }
156   }
157   
158   /**
159    * @return this instance's <code>Resource</code>.
160    *
161    * @throws ResourceNotFoundException if no resource was found for this instance's URI.
162    * @throws IOException if an IO problem occurred while performing this operation.
163    * @throws Exception if an undetermined problem occurred while performing this operation.
164    */

165   public Resource getResource() throws ResourceNotFoundException, IOException JavaDoc, Exception JavaDoc{
166     return resolve();
167   }
168   
169   /**
170    * This method attempts returning a <code>Resource</code> that is relative to this instance's
171    * <code>Resource</code>. If the given URI is not relative, this instance will attempt resolving
172    * it as an absolute resource.
173    *
174    * @param uri a relative or absolute uri.
175    * @return a <code>Resource</code>
176    *
177    * @throws ResourceNotFoundException if no resource was found for the given URI.
178    * @throws IOException if an IO problem occurred while performing this operation.
179    * @throws Exception if an undetermined problem occurred while performing this operation.
180    */

181   public Resource getRelative(String JavaDoc uri) throws ResourceNotFoundException, IOException JavaDoc, Exception JavaDoc{
182     try{
183       if(_uri == null){
184         return _conf.getResources().resolveResource(uri);
185       }
186       boolean hasScheme = Utils.hasScheme(uri);
187       if(hasScheme && Utils.isAbsolute(new URI JavaDoc(uri))){
188         return _conf.getResources().resolveResource(uri);
189       }
190       else if(!hasScheme &&
191           Utils.isAbsolute(
192               new URI JavaDoc(new StringBuffer JavaDoc("scheme:").append(uri).toString())
193           )){
194         return _conf.getResources().resolveResource(uri);
195       }
196       else{
197         return resolve().getRelative(uri);
198       }
199     }catch(URISyntaxException JavaDoc e){
200       throw new IOException JavaDoc("Invalid URI: " + uri + " - " + e.getMessage());
201     }
202   }
203   
204   /**
205    * This method's default implementation returns the passed in stream. It is meant to be
206    * overridden if this default implementation does not correspond to the desired result.
207    *
208    * @param is the <code>InputStream</code> that was resolved.
209    * @param context an application-specific context object (<code>null</code> if no such object was
210    * passed to the calling <code>include()</code> method).
211    * @throws IOException
212    * @throws Exception
213    *
214    * @see #include()
215    * @see #include(Object)
216    */

217   protected Object JavaDoc doInclude(InputStream JavaDoc is, Object JavaDoc context) throws IOException JavaDoc, Exception JavaDoc{
218     return is;
219   }
220   
221   Resource resolve() throws ResourceNotFoundException, IOException JavaDoc, Exception JavaDoc{
222     if(_resource != null){
223       return _resource;
224     }
225     if(_parent != null && _parent._uri != null){
226       Resource res = _parent.resolve();
227       boolean hasScheme = Utils.hasScheme(uri());
228       if(hasScheme && Utils.isAbsolute(new URI JavaDoc(uri()))){
229         _resource = _conf.getResources().resolveResource(uri());
230       }
231       else if(!hasScheme &&
232           Utils.isAbsolute(
233               new URI JavaDoc(new StringBuffer JavaDoc("scheme:").append(uri()).toString())
234           )){
235         _resource = _conf.getResources().resolveResource(uri());
236       }
237       else{
238         _resource = res.getRelative(uri());
239       }
240     }
241     else{
242       _resource = _conf.getResources().resolveResource(uri());
243     }
244     return _resource;
245   }
246   
247   private void pop(){
248     IncludeState.popContext(_conf.getAppKey());
249   }
250   
251   private String JavaDoc uri(){
252     return _uri;
253   }
254 }
255
Popular Tags