KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > browser > core > xmlparser > ContextXMLParser


1 /*===========================================================================
2
3 ObjectWeb Naming Context Framework
4 Copyright (C) 2003 USTL - LIFL - GOAL
5 Contact: architecture@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.
23 Contributor(s): ______________________________________.
24
25 ===========================================================================*/

26
27 package org.objectweb.util.browser.core.xmlparser;
28
29 /** The console's imports */
30 import org.objectweb.util.browser.core.api.ContextContainer;
31 import org.objectweb.util.browser.core.api.ContextProperty;
32 import org.objectweb.util.browser.core.api.DecoderFactory;
33 import org.objectweb.util.browser.core.common.ClassResolver;
34 import org.objectweb.util.browser.contextConfig.Context;
35 import org.objectweb.util.browser.contextConfig.ContextUnmarshaller;
36 import org.objectweb.util.browser.contextConfig.Decoders;
37 import org.objectweb.util.browser.contextConfig.Decoder;
38 import org.objectweb.util.browser.core.naming.DefaultContextContainer;
39 import org.objectweb.util.browser.core.naming.DefaultDecoderFactory;
40
41 /** The java API's imports */
42 import java.io.File JavaDoc;
43 import java.io.FileInputStream JavaDoc;
44 import java.io.FileNotFoundException JavaDoc;
45 import java.io.IOException JavaDoc;
46 import java.io.InputStream JavaDoc;
47 import java.util.List JavaDoc;
48 import java.util.Iterator JavaDoc;
49
50 /**
51  *
52  *
53  * @author <a HREF="mailto:Jerome.Moroy@lifl.fr">Jerome Moroy</a>
54  * @version 0.1
55  */

56 public class ContextXMLParser implements XMLParser, ContextProperty {
57
58     // ==================================================================
59
//
60
// Internal states.
61
//
62
// ==================================================================
63

64     /** The Decoder corresponding to the configuration of the context */
65     protected org.objectweb.util.browser.core.api.Decoder decoder_;
66
67     // ==================================================================
68
//
69
// Protected methods.
70
//
71
// ==================================================================
72

73     /**
74      * Builds the chain of decoder corresponding to the given decoder list
75      * @param decoders The decoders node to interpret
76      */

77     protected void buildDecoder(Decoders decoders) {
78         if (decoders != null) {
79             ContextContainer context = new DefaultContextContainer();
80             List JavaDoc decoderList = decoders.getDecoderList();
81             Iterator JavaDoc it = decoderList.iterator();
82             while (it.hasNext()) {
83                 Decoder decoderNode = (Decoder) it.next();
84                 String JavaDoc className = decoderNode.getJavaClass();
85                 try {
86                     Class JavaDoc c = ClassResolver.resolve(className);
87                     context.addEntry(className, c);
88                 } catch (ClassNotFoundException JavaDoc e) {
89                     System.out.println("Error : " + className + " : Class not found !");
90                 }
91             }
92             DecoderFactory decoderFactory = new DefaultDecoderFactory();
93             decoder_ = decoderFactory.create(context);
94         }
95     }
96
97     // ==================================================================
98
//
99
// Public methods of XMLDecoder interface.
100
//
101
// ==================================================================
102

103     /*
104      * @see org.objectweb.kilim.browser.xmlreader.decoder.BrowserConfigXMLDecoder#getDecoder()
105      */

106     public org.objectweb.util.browser.core.api.Decoder getDecoder() {
107         return decoder_;
108     }
109
110     /*
111      * @see org.objectweb.kilim.browser.xmlreader.decoder.XMLDecoder#setPropertyFile(java.io.File)
112      */

113     public void setPropertyFile(String JavaDoc path) {
114         InputStream JavaDoc inputStream = null;
115         // Tries to load the resource from the ClassLoader
116
inputStream = ClassResolver.getResourceAsStream(path);
117         if(inputStream==null){
118             // Tries to load the resource from the file system
119
try {
120                 File JavaDoc file = new File JavaDoc(path);
121                 if (file != null && !file.isDirectory()) {
122                     try {
123                         inputStream = new FileInputStream JavaDoc(file);
124                     } catch (java.io.FileNotFoundException JavaDoc e2) {
125                         System.out.println("Error : " + file + " : File not found !");
126                     }
127                 }
128             } catch(Exception JavaDoc e1) {
129                 System.out.println("[ContextXMLParser.setPropertyFile] " + path + ": Resource not found ! ");
130             }
131         }
132         if(inputStream!=null){
133             try {
134                 Context context = ContextUnmarshaller.unmarshal(inputStream);
135                 if (context != null) {
136                     buildDecoder(context.getDecoders());
137                 }
138             } catch (FileNotFoundException JavaDoc e) {
139                 System.out.println("Error : " + path + " : File not found !");
140             } catch (IOException JavaDoc e) {
141                 System.out.println("Error : " + path + " : I/O Exception !");
142             }
143         }
144     }
145
146     /**
147      * Fixes an array of files (or directories) which contains the properties
148      * @param files The path containing the XML file(s) containing the configuration
149      * we want to abtain under a Context view (Each value of the array can be a single file or a directory)
150      */

151     public void setPropertyFile(String JavaDoc[] files) {
152         for (int i = 0; i < files.length; i++)
153             setPropertyFile(files[i]);
154     }
155
156 }
157
Popular Tags