KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > explorer > context > lib > Parser


1 /*====================================================================
2
3 Objectweb Explorer Framework
4 Copyright (C) 2000-2004 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Jerome Moroy, Philippe Merle.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26 package org.objectweb.util.explorer.context.lib;
27
28 import java.io.FileInputStream JavaDoc;
29 import java.io.FileNotFoundException JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.io.InputStreamReader JavaDoc;
33 import java.net.MalformedURLException JavaDoc;
34 import java.net.URL JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37
38 import org.objectweb.fractal.api.NoSuchInterfaceException;
39 import org.objectweb.util.explorer.context.api.ContextParser;
40 import org.objectweb.util.explorer.context.api.ContextPropertiesFeeder;
41 import org.objectweb.util.explorer.contextConfig.Context;
42 import org.objectweb.util.explorer.contextConfig.ContextImpl;
43 import org.objectweb.util.explorer.contextConfig.Decoder;
44 import org.objectweb.util.explorer.contextConfig.Decoders;
45 import org.objectweb.util.explorer.core.common.lib.BindingFeature;
46 import org.objectweb.util.explorer.core.common.lib.ClassResolver;
47
48 /**
49  *
50  *
51  * @author <a HREF="mailto:Jerome.Moroy@lifl.fr">Jérôme Moroy</a>,
52  * <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>.
53  *
54  * @version 0.1
55  */

56 public class Parser
57      extends BindingFeature
58   implements ContextParser
59 {
60
61     //==================================================================
62
//
63
// Internal States.
64
//
65
// ==================================================================
66

67     // ==================================================================
68
//
69
// Constructors.
70
//
71
// ==================================================================
72

73     // ==================================================================
74
//
75
// Internal methods.
76
//
77
// ==================================================================
78

79     /**
80      * Tries to open an InputStream on the given resource.
81      * <ul>
82      * <li>1. First, it looks for the resource in the current class loader.</li>
83      * <li>2. Then, it tries to load the resource as an URL.</li>
84      * <li>3. Finally, it tries to load the resource from within the file system.</li>
85      * </ul>
86      * @param resource The resource for which an InputStream needs to be opened.
87      * @return An InputStream opened on the given resource (or null).
88      */

89     protected InputStream JavaDoc getInputStream(String JavaDoc resource){
90         // Tries to load the resource from the ClassLoader
91
InputStream JavaDoc inputStream = ClassResolver.getResourceAsStream(resource);
92         if(inputStream==null) {
93             try {
94                 // Tries to load the resource as an URL
95
inputStream = new URL JavaDoc(resource).openStream();
96             } catch (MalformedURLException JavaDoc e) {
97                 try {
98                     // Tries to load the resource on the file system
99
inputStream = new FileInputStream JavaDoc(resource);
100                 } catch (FileNotFoundException JavaDoc e1) {
101                     getTrace().info(resource + ": File not found on the file system!");
102                     return null;
103                 }
104             } catch (IOException JavaDoc e) {
105                 getTrace().info(resource + ": Failed to open stream on resource!");
106                 return null;
107             }
108         }
109         return inputStream;
110     }
111     
112     /**
113      * Builds the chain of decoder corresponding to the given decoder list
114      * @param decoders The decoders node to interpret
115      */

116     protected void buildDecoder(Decoders decoders) {
117         if (decoders != null) {
118             List JavaDoc decoderList = decoders.getDecoderList();
119             Iterator JavaDoc it = decoderList.iterator();
120             while (it.hasNext()) {
121                 Decoder decoderNode = (Decoder) it.next();
122                 String JavaDoc className = decoderNode.getJavaClass();
123                 try {
124                     Class JavaDoc c = ClassResolver.resolve(className);
125                     if (c!=null && org.objectweb.util.explorer.context.api.Decoder.class.isAssignableFrom(c)){
126                         getPropertiesFeeder().addDecoder((org.objectweb.util.explorer.context.api.Decoder)c.newInstance());
127                     }
128                 } catch (ClassNotFoundException JavaDoc e) {
129                     getTrace().warn(className + " : Class not found !");
130                 } catch (Exception JavaDoc e) {
131                     getTrace().warn(className + " : Class not found !");
132                 }
133             }
134         }
135     }
136     
137     /**
138      * Parses and interprets the given stream.
139      */

140     protected void parse(InputStream JavaDoc inputStream){
141         if(inputStream!=null){
142             try {
143                 // We can't validate with DTD at this time (no way to fix the DTD file)
144
Context context = ContextImpl.unmarshal(new InputStreamReader JavaDoc(inputStream),false);
145                 if (context != null){
146                     buildDecoder(context.getDecoders());
147                 }
148             } catch (java.io.IOException JavaDoc e) {
149                 getTrace().info("Error during parsing!\n" + e.getMessage());
150             }
151         }
152     }
153     
154     protected ContextPropertiesFeeder getPropertiesFeeder(){
155         try {
156             return (ContextPropertiesFeeder)lookupFc(ContextPropertiesFeeder.CONTEXT_PROPERTIES_FEEDER);
157         } catch (NoSuchInterfaceException e) {
158             getTrace().warn(ContextPropertiesFeeder.CONTEXT_PROPERTIES_FEEDER + ": interface not found!");
159             return null;
160         }
161     }
162     
163     // ==================================================================
164
//
165
// Public methods for BindingFeature abstract class.
166
//
167
// ==================================================================
168

169     /* (non-Javadoc)
170      * @see org.objectweb.util.explorer.core.common.lib.BindingFeature#clientFc()
171      */

172     public String JavaDoc[] clientFc() {
173         return new String JavaDoc[]{ContextPropertiesFeeder.CONTEXT_PROPERTIES_FEEDER};
174     }
175
176     // ==================================================================
177
//
178
// Public methods for ContextParser interface.
179
//
180
// ==================================================================
181

182     /* (non-Javadoc)
183      * @see org.objectweb.util.explorer.context.api.ContextParser#parse(java.lang.String)
184      */

185     public void parse(String JavaDoc fileUrl) {
186         if(fileUrl!=null && !fileUrl.equals(""))
187             parse(getInputStream(fileUrl));
188     }
189
190 }
191
192
193
Popular Tags