KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > explorer > parser > lib > ExplorerParserLoader


1 /*====================================================================
2
3 Objectweb Explorer Framework
4 Copyright (C) 2000-2005 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 $Id: ExplorerParserLoader.java,v 1.2 2005/07/06 15:36:00 moroy Exp $
27 ====================================================================*/

28
29 package org.objectweb.util.explorer.parser.lib;
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.io.InputStreamReader JavaDoc;
37 import java.net.MalformedURLException JavaDoc;
38 import java.net.URL JavaDoc;
39
40 import org.objectweb.fractal.api.NoSuchInterfaceException;
41 import org.objectweb.fractal.api.control.BindingController;
42 import org.objectweb.fractal.api.control.IllegalBindingException;
43 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
44 import org.objectweb.util.explorer.core.common.lib.ClassResolver;
45 import org.objectweb.util.explorer.core.common.lib.XMLFileFilter;
46 import org.objectweb.util.explorer.explorerConfig.beans.ExplorerBean;
47 import org.objectweb.util.explorer.explorerConfig.beans.ExplorerBeanImpl;
48 import org.objectweb.util.explorer.parser.api.ExplorerParser;
49
50 /**
51  *
52  *
53  * @author <a HREF="mailto:Jerome.Moroy@lifl.fr">Jérôme Moroy</a>,
54  * <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>.
55  *
56  * @version 0.1
57  */

58 public class ExplorerParserLoader
59      extends AbstractParser
60   implements BindingController
61 {
62
63     //==================================================================
64
//
65
// No Internal State.
66
//
67
// ==================================================================
68

69     /** The reference to the dispatcher. */
70     protected ExplorerParser dispatcher_;
71     
72     // ==================================================================
73
//
74
// No Constructors.
75
//
76
// ==================================================================
77

78     // ==================================================================
79
//
80
// No internal method.
81
//
82
// ==================================================================
83

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

94     protected InputStream JavaDoc getInputStream(String JavaDoc resource){
95         // Tries to load the resource from the ClassLoader
96
InputStream JavaDoc inputStream = ClassResolver.getResourceAsStream(resource);
97         if(inputStream==null) {
98             try {
99                 // Tries to load the resource as an URL
100
inputStream = new URL JavaDoc(resource).openStream();
101             } catch (MalformedURLException JavaDoc e) {
102                 try {
103                     // Tries to load the resource on the file system
104
inputStream = new FileInputStream JavaDoc(resource);
105                 } catch (FileNotFoundException JavaDoc e1) {
106                     getTrace().info(resource + ": File not found on the file system!");
107                     return null;
108                 }
109             } catch (IOException JavaDoc e) {
110                 getTrace().info(resource + ": Failed to open stream on resource!");
111                 return null;
112             }
113         }
114         return inputStream;
115     }
116    
117     /**
118      * Parses and interprets the given stream.
119      */

120     protected void parse(InputStream JavaDoc inputStream){
121         if(inputStream!=null){
122             try {
123                 // We can't validate with DTD at this time (no way to fix the DTD file)
124
ExplorerBean explorer = ExplorerBeanImpl.unmarshalBean(new InputStreamReader JavaDoc(inputStream),false);
125                 if (explorer != null){
126                     dispatcher_.parseExplorer(explorer);
127                 }
128             } catch (java.io.IOException JavaDoc e) {
129                 getTrace().info("Error during parsing!\n" + e.getMessage());
130             }
131         }
132     }
133     
134     /**
135      * Parses and interprets the given resource.
136      * If the given resource is a local directory, all its contained files are interpreted.
137      */

138     protected void parse(String JavaDoc resource){
139         // Checks if the given resource is a directory
140
File JavaDoc file = new File JavaDoc(resource);
141         if (file.exists() && file.isDirectory()) {
142             File JavaDoc[] files = file.listFiles(new XMLFileFilter());
143             for (int i = 0; i < files.length; i++) {
144                 parse(files[i].toString());
145             }
146         } else {
147             parse(getInputStream(resource));
148         }
149     }
150     
151     // ==================================================================
152
//
153
// Public methods for BindingController interface.
154
//
155
// ==================================================================
156

157     /* (non-Javadoc)
158      * @see org.objectweb.fractal.api.control.BindingController#bindFc(java.lang.String, java.lang.Object)
159      */

160     public void bindFc(String JavaDoc itfName, Object JavaDoc itfValue)
161             throws NoSuchInterfaceException, IllegalBindingException,
162             IllegalLifeCycleException {
163         if(itfName.equals(ExplorerParser.EXPLORER_PARSER))
164             dispatcher_ = (ExplorerParser)itfValue;
165     }
166     /* (non-Javadoc)
167      * @see org.objectweb.fractal.api.control.BindingController#lookupFc(java.lang.String)
168      */

169     public Object JavaDoc lookupFc(String JavaDoc itfName) throws NoSuchInterfaceException {
170         if(itfName.equals(ExplorerParser.EXPLORER_PARSER))
171             return dispatcher_;
172         return null;
173     }
174     /* (non-Javadoc)
175      * @see org.objectweb.fractal.api.control.BindingController#unbindFc(java.lang.String)
176      */

177     public void unbindFc(String JavaDoc itfName) throws NoSuchInterfaceException,
178             IllegalBindingException, IllegalLifeCycleException {
179         if(itfName.equals(ExplorerParser.EXPLORER_PARSER))
180             dispatcher_ = null;
181     }
182     
183     /* (non-Javadoc)
184      * @see org.objectweb.fractal.api.control.BindingController#listFc()
185      */

186     public String JavaDoc[] listFc(){
187         return new String JavaDoc[]{ExplorerParser.EXPLORER_PARSER};
188     }
189     
190     // ==================================================================
191
//
192
// Public method inherits from AbstractParser class.
193
//
194
// ==================================================================
195
// Functional concerns.
196

197     /**
198      * Parses and interprets the defined Explorer XML files.
199      */

200     public void parse() {
201         // The use of an iterator here is impossible because the list
202
// may grow and such an iterator will throw
203
// the java.util.ConcurrentModificationException
204
int i = 0;
205         while(i<urlFiles_.size()){
206             parse((String JavaDoc)urlFiles_.get(i));
207             i++;
208         }
209         urlFiles_.clear();
210     }
211
212 }
213
Popular Tags