KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > kilim > repository > ResourceRepository1


1 /**
2  * Copyright (C) 2002 Kelua SA
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.kilim.repository;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import org.objectweb.kilim.description.BasicNamedElement;
25 import org.objectweb.kilim.description.KILIM;
26 import org.objectweb.kilim.description.Property;
27 import org.objectweb.kilim.InternalException;
28 import org.objectweb.kilim.KilimException;
29 import org.objectweb.kilim.description.TemplateDescription;
30
31 /**
32  * Implementation of Repository using a ResourceLoader and a kilim xml format Parser.
33  * {@see org.objectweb.kilim.repository.Repository}
34  * @author dutoo, horn
35  */

36 public class ResourceRepository1 extends ResourceRepository {
37     
38     /**
39      * Creates a new ResourceRepository with the given kilim xml format Parser and its ClassLoader as resource loader.
40      */

41     public ResourceRepository1() {
42         super();
43     }
44     
45     /**
46      * Creates a new ResourceRepository with the given kilim xml format Parser and its ClassLoader as resource loader.
47      * @param aParser the kilim xml format parser to be used
48      */

49     public ResourceRepository1(TemplateDescriptionParser aParser) {
50         super(aParser);
51     }
52         
53     /**
54      * Method getSingleDescription. This method parses a single ".kilim" file. It updates the ResourceMapping object
55      * received as a parameter by adding new unknown templates, new "super" references, etc ...
56      * @param resourceName : the name of the template to be parsed
57      * @param parsingResult : the ResourceMapping object used to store the parsing results to be used during next iterations.
58      * @return TemplateDescription : the internal representation of the template described in the resource.
59      * @throws ResourceNotFoundException
60      */

61     protected TemplateDescription getSingleDescription(String JavaDoc resourceName, ResourceMapping parsingResult) throws ResourceNotFoundException {
62         TemplateDescription toBeReturned = super.getSingleDescription(resourceName, parsingResult);
63         
64         String JavaDoc prp_fl_nm = resourceName + ".prop";
65         
66         InputStream JavaDoc is = null;
67         try {
68             is = getResourceLoader().getResource(prp_fl_nm);
69         } catch (ResourceNotFoundException rsnf) {
70             System.err.println("warning : No Properties found for " + prp_fl_nm + ".");
71         }
72         
73         if (is == null) { System.err.println("warning : No .prop file found for " + prp_fl_nm + "."); }
74         
75         Properties JavaDoc addtnl_prps = new Properties JavaDoc();
76         try {
77             if (is != null) { addtnl_prps.load(is); }
78         } catch (IOException JavaDoc io) {
79
80             System.err.println("warning : Properties " + prp_fl_nm + "could not be loaded properly.");
81         }
82         
83         for (Enumeration JavaDoc it = addtnl_prps.keys() ; it.hasMoreElements() ;) {
84             String JavaDoc crrt_prp = (String JavaDoc) it.nextElement();
85             BasicNamedElement prp = null;
86             try {
87                 prp = toBeReturned.getAnyProvider(crrt_prp , false);
88             } catch (KilimException exc) {
89                 System.err.println("warning : attempt to set an unknown property " + crrt_prp);
90             }
91             
92             if (prp == null) {
93                 System.err.println("warning : trying to set an unknown property " + crrt_prp);
94             } else if (!(prp instanceof Property)) {
95                 System.err.println("warning : attempt to set a provider which is not a property " + crrt_prp + " (" + prp.getClass() + ")");
96             }
97
98             if (prp != null) {
99                 setValue((Property) prp , (String JavaDoc) addtnl_prps.get(crrt_prp));
100             }
101         }
102                 
103         return toBeReturned;
104     }
105     
106     private static void setValue(Property property, String JavaDoc value) {
107         
108         int tp_knd = property.getTypeKind();
109         
110         switch (tp_knd) {
111         
112             case KILIM.BOOLEAN:
113                 try {
114                     Boolean JavaDoc bln = new Boolean JavaDoc(value);
115                     property.setValue(bln);
116                 } catch (Exception JavaDoc e) {
117                     error(property , value , e);
118                     e.printStackTrace();
119                 }
120             break;
121             
122             case KILIM.CHAR:
123                 try {
124                     Character JavaDoc chr = new Character JavaDoc(value.charAt(0));
125                     property.setValue(chr);
126                 } catch (Exception JavaDoc e) {
127                     error(property, value, e);
128                     e.printStackTrace();
129                 }
130             break;
131             
132             case KILIM.BYTE:
133                 try {
134                     Byte JavaDoc bt = new Byte JavaDoc(value);
135                     property.setValue(bt);
136                 } catch (Exception JavaDoc e) {
137                     error(property , value , e);
138                     e.printStackTrace();
139                 }
140             break;
141             
142             case KILIM.SHORT:
143                 try {
144                     Short JavaDoc shrt = new Short JavaDoc(value);
145                     property.setValue(shrt);
146                 } catch (Exception JavaDoc e) {
147                     error(property , value , e);
148                     e.printStackTrace();
149                 }
150             break;
151             
152             case KILIM.INT:
153                 try {
154                     Integer JavaDoc intgr = new Integer JavaDoc(value);
155                     property.setValue(intgr);
156                 } catch (Exception JavaDoc e) {
157                     error(property , value , e);
158                     e.printStackTrace();
159                 }
160             break;
161             
162             case KILIM.LONG:
163                 try {
164                     Long JavaDoc lng = new Long JavaDoc(value);
165                     property.setValue(lng);
166                 } catch (Exception JavaDoc e) {
167                     error(property, value, e);
168                     e.printStackTrace();
169                 }
170             break;
171             
172             case KILIM.FLOAT:
173                 try {
174                     Float JavaDoc flt = new Float JavaDoc(value);
175                     property.setValue(flt);
176                 } catch (Exception JavaDoc e) {
177                     error(property , value , e);
178                     e.printStackTrace();
179                 }
180             break;
181             
182             case KILIM.DOUBLE:
183                 try {
184                     Double JavaDoc dbl = new Double JavaDoc(value);
185                     property.setValue(dbl);
186                 } catch (Exception JavaDoc e) {
187                     error(property, value, e);
188                     e.printStackTrace();
189                 }
190             break;
191
192             case KILIM.OBJECT:
193             case KILIM.STRING:
194                 try {
195                     property.setValue(value);
196                 } catch (Exception JavaDoc e) {
197                     error(property , value , e);
198                     e.printStackTrace();
199                 }
200             break;
201             
202             default:
203                 throw new InternalException();
204         
205         }
206     }
207     
208     private static void error(Property property, String JavaDoc value, Exception JavaDoc exc) {
209         System.err.println("warning : " + property.getType() + " Property " + property.getLocalName()
210                             + " could not be overloaded with provided value " + value + " because of the following exception :");
211         exc.printStackTrace();
212     }
213 }
Popular Tags