KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > config > Include


1 package org.sapia.soto.config;
2
3 import org.sapia.soto.Debug;
4 import org.sapia.soto.SotoApplicationFactory;
5 import org.sapia.soto.SotoContainer;
6 import org.sapia.soto.util.FileResource;
7 import org.sapia.soto.util.Param;
8 import org.sapia.soto.util.Resource;
9 import org.sapia.soto.util.Utils;
10
11 import org.sapia.util.text.MapContext;
12 import org.sapia.util.text.TemplateContextIF;
13 import org.sapia.util.xml.ProcessingException;
14 import org.sapia.util.xml.confix.ConfigurationException;
15 import org.sapia.util.xml.confix.Dom4jProcessor;
16 import org.sapia.util.xml.confix.ObjectCreationCallback;
17
18 import java.io.File JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Stack JavaDoc;
27
28
29 /**
30  * @author Yanick Duchesne
31  * <dl>
32  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
33  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
34  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
35  * </dl>
36  */

37 public class Include implements ObjectCreationCallback {
38   private static Stack JavaDoc _includes = new Stack JavaDoc();
39   private String JavaDoc _uri;
40   private Resource _resource;
41   private SotoContainer _container;
42   private SotoApplicationFactory _fac;
43   private List JavaDoc _params = new ArrayList JavaDoc();
44
45   /**
46    * Constructor for Include.
47    */

48   public Include(SotoContainer container, SotoApplicationFactory fac) {
49     _container = container;
50     _fac = fac;
51   }
52
53   public void setUri(String JavaDoc uri) {
54     _uri = uri;
55   }
56
57   public String JavaDoc getUri() {
58     return _uri;
59   }
60
61   public Param createParam() {
62     Param p = new Param();
63     _params.add(p);
64
65     return p;
66   }
67
68   /**
69    * @see org.sapia.util.xml.confix.ObjectCreationCallback#onCreate()
70    */

71   public Object JavaDoc onCreate() throws ConfigurationException {
72     if (_uri == null) {
73       throw new ConfigurationException(
74         "'uri' attribute not set on 'include' element");
75     }
76
77     try {
78       if (_resource == null) {
79         Debug.debug("Including: " + _uri);
80
81         if (Utils.isRelativePath(_uri)) {
82           Debug.debug("path is relative: " + _uri);
83
84           if (_includes.size() == 0) {
85             File JavaDoc f = new File JavaDoc(Utils.chopScheme(_uri));
86             _resource = new FileResource(f);
87             Debug.debug("resource: " + f.getAbsolutePath());
88             _uri = f.getAbsolutePath();
89           } else {
90             Include last = (Include) _includes.peek();
91             String JavaDoc uri = last.getUri();
92             _uri = Utils.getRelativePath(uri, _uri, true);
93             Debug.debug("resource: " + _uri);
94             _resource = _container.getResourceHandlerFor(Utils.getScheme(_uri))
95                                   .getResourceObject(_uri);
96           }
97         } else {
98           if (Utils.hasScheme(_uri)) {
99             _resource = _container.getResourceHandlerFor(Utils.getScheme(_uri))
100                                   .getResourceObject(_uri);
101           } else {
102             File JavaDoc f = new File JavaDoc(Utils.chopScheme(_uri));
103             _resource = new FileResource(f);
104             _uri = f.getAbsolutePath();
105           }
106         }
107       }
108
109       _includes.push(this);
110
111             Dom4jProcessor proc = new Dom4jProcessor(_fac);
112
113       TemplateContextIF ctx = getNestedContext();
114       ThreadState.push(new ThreadState(ctx));
115
116       InputStream JavaDoc input = null;
117
118       try {
119         input = _resource.getInputStream();
120
121         Object JavaDoc toReturn = proc.process(Utils.replaceVars(ctx, input, _uri));
122         Object JavaDoc lastObject = ThreadState.currentState().getLastObject();
123
124         return toReturn;
125       } finally {
126         if (input != null) {
127           input.close();
128           ThreadState.pop();
129         }
130
131         Include inc = (Include) _includes.pop();
132       }
133     } catch (ProcessingException e) {
134       throw new ConfigurationException("Could not process resource for: " +
135         _uri, e);
136     } catch (IOException JavaDoc e) {
137       throw new ConfigurationException("Could not get resource for: " + _uri, e);
138     }
139   }
140
141   public Resource getResource() {
142     if (_resource == null) {
143       throw new IllegalStateException JavaDoc("Included resource not set");
144     }
145
146     return _resource;
147   }
148
149   private TemplateContextIF getNestedContext() {
150     if (_params.size() == 0) {
151       return _fac.getTemplateContext();
152     }
153
154     Map JavaDoc params = new HashMap JavaDoc();
155     Param p;
156
157     for (int i = 0; i < _params.size(); i++) {
158       p = (Param) _params.get(i);
159
160       if (p.getName() != null) {
161         params.put(p.getName(), p.getValue());
162       }
163     }
164
165     MapContext nested = new MapContext(params, _fac.getTemplateContext(), false);
166
167     return nested;
168   }
169 }
170
Popular Tags