KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jegg > impl > AppXMLContentHandler


1 /*
2  * Copyright (c) 2004, Bruce Lowery
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * - Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * - Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * - Neither the name of GOSSIP nor the names of its contributors may be used
14  * to endorse or promote products derived from this software without
15  * specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */

29 package jegg.impl;
30
31 import java.io.File JavaDoc;
32 import java.io.FileInputStream JavaDoc;
33 import java.io.FileNotFoundException JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.InputStream JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.Map JavaDoc;
39 import java.util.Properties JavaDoc;
40
41 import javax.xml.parsers.ParserConfigurationException JavaDoc;
42
43
44
45 import org.apache.commons.logging.Log;
46 import org.apache.commons.logging.LogFactory;
47 import org.xml.sax.Attributes JavaDoc;
48 import org.xml.sax.InputSource JavaDoc;
49 import org.xml.sax.SAXException JavaDoc;
50 import org.xml.sax.SAXParseException JavaDoc;
51 import org.xml.sax.helpers.DefaultHandler JavaDoc;
52
53 /**
54  *
55  *
56  * @author Bruce Lowery
57  */

58 public class AppXMLContentHandler extends DefaultHandler JavaDoc
59 {
60     private static final Log LOG = LogFactory.getLog(AppXMLContentHandler.class);
61     
62     // Tags
63
private static final String JavaDoc BASKET = "basket";
64     private static final String JavaDoc EGG = "egg";
65     private static final String JavaDoc USES = "uses";
66     private static final String JavaDoc INCLUDE = "include";
67     private static final String JavaDoc VAR = "var";
68     private static final String JavaDoc PROPERTY = "property";
69     
70     // Attributes
71
private static final String JavaDoc NAME = "name";
72     private static final String JavaDoc HANDLER = "handler";
73     private static final String JavaDoc LOAD = "load";
74     private static final String JavaDoc VALUE = "value";
75     
76     // Attribute values
77
private static final String JavaDoc EARLY = "early";
78     private static final String JavaDoc LATE = "late";
79     
80     private Map JavaDoc _environment;
81     private boolean _resolve;
82     
83     private String JavaDoc _currentDispatcherName;
84     private EggInfo _currentEgg;
85     private String JavaDoc _currentInclude;
86     private Map JavaDoc _currentVars = new HashMap JavaDoc();
87
88     public AppXMLContentHandler(final Map JavaDoc vars, final boolean resolve)
89     {
90         if (null != vars)
91         {
92         _environment = new HashMap JavaDoc();
93         _environment.putAll(vars);
94         }
95         _resolve = resolve;
96     }
97     
98     public void warn(SAXParseException JavaDoc e)
99     {
100         LOG.warn(e);
101     }
102     
103     public void error(SAXParseException JavaDoc e)
104     {
105         LOG.error(e);
106     }
107     
108     public void fatalError(SAXParseException JavaDoc e)
109     {
110         LOG.fatal(e);
111     }
112     
113     public void startDocument() throws SAXException JavaDoc
114     {
115         try
116         {
117             HenHouse.getHenHouse().hatch("root-port-registry", "jegg.impl.PortRegistry", null, true, false);
118         }
119         catch (HatchException e)
120         {
121             throw new SAXException JavaDoc("unable to create root port registry");
122         }
123     }
124     
125     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes)
126     throws SAXException JavaDoc
127     {
128         if (LOG.isDebugEnabled())
129             LOG.debug("startElement (qName=" + qName+")");
130         
131         if (qName.equals(BASKET))
132         {
133             // If this is null, the egg should be assigned to
134
// the default dispatcher.
135
String JavaDoc name = attributes.getValue(NAME);
136             if (null != name)
137                 _currentDispatcherName = expand(name,_environment);
138             else
139                 _currentDispatcherName = null;
140             
141             if (LOG.isDebugEnabled())
142                 LOG.debug("CurrentDispatcher: " + _currentDispatcherName);
143             _currentEgg = null;
144         }
145         else
146         if (qName.equals(EGG))
147         {
148             String JavaDoc name = attributes.getValue(NAME);
149             String JavaDoc expandedName = expand(name,_environment);
150             String JavaDoc handlerName = attributes.getValue(HANDLER);
151             String JavaDoc handler = expand(handlerName,_environment);
152             String JavaDoc load = attributes.getValue(LOAD);
153             load = (null == load) ? EARLY : load;
154             
155             if (!load.equalsIgnoreCase(EARLY) &&
156                 !load.equalsIgnoreCase(LATE))
157             {
158                 throw new SAXException JavaDoc("Invalid 'load' attribute: " +load);
159             }
160             
161             if (LOG.isDebugEnabled())
162                 LOG.debug("Creating egg "+expandedName);
163             
164             _currentEgg = new EggInfo(
165                     expandedName,
166                     handler,
167                     _currentDispatcherName,
168                     (null==load) ? false : load.equalsIgnoreCase(EARLY));
169             
170             HenHouse hh = HenHouse.getHenHouse();
171             hh.add(_currentEgg);
172         }
173         else
174         if (qName.equals(USES))
175         {
176             String JavaDoc name = attributes.getValue(NAME);
177             String JavaDoc expandedName = expand(name,_environment);
178             if (LOG.isDebugEnabled())
179                 LOG.debug(_currentEgg.getEggName()+" uses "+expandedName);
180             _currentEgg.addDependency(expandedName);
181         }
182         else
183         if (qName.equals(PROPERTY))
184         {
185             String JavaDoc propName = attributes.getValue(NAME);
186             String JavaDoc expandedPropName = expand(propName,_environment);
187             String JavaDoc propValue = attributes.getValue(VALUE);
188             String JavaDoc expandedPropValue = expand(propValue,_environment);
189             if (LOG.isDebugEnabled())
190                 LOG.debug("Environment setting: " + expandedPropName+"="+expandedPropValue);
191             _currentEgg.addProperty(expandedPropName, expandedPropValue);
192         }
193         else
194         if (qName.equals(INCLUDE))
195         {
196             String JavaDoc name = attributes.getValue(NAME);
197             _currentInclude = expand(name,_environment);
198             if (LOG.isDebugEnabled())
199                 LOG.debug("Include name: " +_currentInclude);
200             _currentVars.clear();
201             if (null != _environment)
202                 _currentVars.putAll(_environment);
203         }
204         else if (qName.equals(VAR))
205         {
206             String JavaDoc varName = attributes.getValue(NAME);
207             String JavaDoc expandedVarName = expand(varName,_environment);
208             String JavaDoc varValue = attributes.getValue(VALUE);
209             String JavaDoc expandedVarValue = expand(varValue,_environment);
210             if (LOG.isDebugEnabled())
211                 LOG.debug("Environment setting: " + expandedVarName+"="+expandedVarValue);
212             _currentVars.put(expandedVarName,expandedVarValue);
213         }
214     }
215     
216     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc
217     {
218         if (LOG.isDebugEnabled())
219             LOG.debug("endElement (qName=" + qName+")");
220         
221         if (qName.equals(EGG))
222         {
223             if (_currentEgg.shouldBindEarly())
224             {
225                 try
226                 {
227                     // Hatch but don't start yet
228
HenHouse.getHenHouse().hatch(_currentEgg, false, false);
229                 }
230                 catch (HatchException e)
231                 {
232                     throw new SAXException JavaDoc(e);
233                 }
234             }
235         }
236         else
237         if (qName.equals(INCLUDE))
238         {
239             InputSource JavaDoc in = null;
240             
241                 File JavaDoc xml = new File JavaDoc(_currentInclude);
242                 if (xml.exists() && xml.canRead())
243                 {
244                     try
245                     {
246                         FileInputStream JavaDoc fis = new FileInputStream JavaDoc(xml);
247                         in = new InputSource JavaDoc(fis);
248                     }
249                     catch (FileNotFoundException JavaDoc e)
250                     {
251                         throw new SAXException JavaDoc(e);
252                     }
253                 }
254             else
255             {
256                 // Classpath
257
ClassLoader JavaDoc cl = ClassLoader.getSystemClassLoader();
258                 InputStream JavaDoc is = cl.getResourceAsStream(_currentInclude);
259                 if (null != is)
260                     in = new InputSource JavaDoc(is);
261             }
262             if (null != in)
263             {
264                 if (LOG.isDebugEnabled())
265                     LOG.debug("Loading "+_currentInclude);
266                 try { AppRunner.load(in,_currentVars, false); }
267                 catch (IOException JavaDoc e) {LOG.error(e); throw new SAXException JavaDoc(e);}
268                 catch (ParserConfigurationException JavaDoc e) {LOG.error(e); throw new SAXException JavaDoc(e);}
269             }
270         }
271     }
272     
273     public void endDocument() throws SAXException JavaDoc
274     {
275         if (!_resolve) {return;}
276         
277         HenHouse henHouse = HenHouse.getHenHouse();
278         for (Iterator JavaDoc it = henHouse.iterator(); it.hasNext(); )
279         {
280             EggInfo info = (EggInfo) it.next();
281             if (info.shouldBindEarly())
282             {
283                 try
284                 {
285                     henHouse.hatch(info,true, false);
286                 }
287                 catch (HatchException e)
288                 {
289                     throw new SAXException JavaDoc(e);
290                 }
291             }
292         }
293         
294         // Start all the dispatchers
295
for (Iterator JavaDoc it = Dispatcher.iterator(); it.hasNext(); )
296         {
297             Dispatcher d = (Dispatcher) it.next();
298             if (!d.isAlive())
299                 d.start();
300         }
301     }
302     
303     private static final String JavaDoc BEGIN_TOKEN = "${";
304     private static final String JavaDoc END_TOKEN = "}";
305     
306     String JavaDoc expand(String JavaDoc raw, Map JavaDoc env)
307     {
308         if (null == raw) {return null;}
309         
310         StringBuffer JavaDoc buf = null;
311         int pos = 0;
312         while(true)
313         {
314             int newpos = raw.indexOf(BEGIN_TOKEN, pos);
315             if (0 > newpos) {break;}
316             int end = raw.indexOf(END_TOKEN,newpos+BEGIN_TOKEN.length());
317             if (0 > end) {break;}
318             if (null == buf) {buf = new StringBuffer JavaDoc();}
319             buf.append(raw.substring(pos,newpos));
320             newpos += BEGIN_TOKEN.length();
321             String JavaDoc key = raw.substring(newpos,end);
322             if (null != env)
323             {
324                 String JavaDoc val = (String JavaDoc) env.get(key);
325                 if (null != val)
326                     buf.append(val);
327             }
328             pos = end+1;
329             if (pos >= raw.length()) {break;}
330         }
331         
332         if (null != buf)
333         {
334             if (pos < raw.length())
335             {
336                 buf.append(raw.substring(pos));
337             }
338         }
339         return (null == buf) ? raw : buf.toString();
340     }
341 }
342
Popular Tags