KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import org.objectweb.kilim.KilimConfiguration;
25 import org.objectweb.kilim.description.Instance;
26 import org.objectweb.kilim.InternalException;
27 import org.objectweb.kilim.KilimException;
28 import org.objectweb.kilim.description.TemplateDescription;
29
30 import org.objectweb.kilim.KilimLoggerFactory;
31 import org.objectweb.util.monolog.api.BasicLevel;
32 import org.objectweb.util.monolog.api.Logger;
33
34 /**
35  * Implementation of Repository using a ResourceLoader and a kilim xml format Parser.
36  * {@see org.objectweb.kilim.repository.Repository}
37  * @author dutoo, horn
38  */

39 public class ResourceRepository implements Repository {
40     private static boolean DEBUG_ON = false;
41     private static Logger logger = KilimLoggerFactory.getSingleton().getLogger("org.objectweb.kilim.repository");
42     
43     private TemplateDescriptionParser tdParser;
44     private ResourceLoader resourceLoader;
45     private HashMap JavaDoc resourceCache = new HashMap JavaDoc();
46
47     /**
48      * Creates a new ResourceRepository with the given kilim xml format Parser and its ClassLoader as resource loader.
49      */

50     public ResourceRepository() {
51         tdParser = KilimConfiguration.getTemplateDescriptionParser();
52         resourceLoader = KilimConfiguration.getLoader();
53     }
54     
55     /**
56      * Creates a new ResourceRepository with the given kilim xml format Parser and its ClassLoader as resource loader.
57      * @param aParser the kilim xml format parser to be used
58      */

59     public ResourceRepository(TemplateDescriptionParser aParser) {
60         tdParser = aParser;
61         resourceLoader = new ClassLoaderResourceLoader(getClass().getClassLoader());
62     }
63     
64     /**
65      * @see org.objectweb.kilim.repository.Repository#getTemplateDescription(String)
66      */

67     public TemplateDescription getTemplateDescription(String JavaDoc resourceName) throws ResourceNotFoundException, KilimException {
68
69         //toBeReturned is the top template to be parsed.
70
TemplateDescription toBeReturned = null;
71         ResourceMapping parsingResult = new ResourceMapping(this);
72         toBeReturned = getSingleDescription(resourceName, parsingResult);
73
74         if (DEBUG_ON && logger.isLoggable(BasicLevel.DEBUG)) {
75             logger.log(BasicLevel.DEBUG, "Starting description of \"" + toBeReturned.getName() + "\" ==============================");
76         }
77         //The repository launches the parser on every ".kilim" files, as long as a referenced template is still unparsed.
78
String JavaDoc nextTemplateName;
79         while ((nextTemplateName = parsingResult.getNextUnparsedTemplate()) != null) {
80             getSingleDescription(nextTemplateName, parsingResult);
81         }
82         
83         //The repository resolves the "super" template references in <instance> tags.
84
Iterator JavaDoc iter = parsingResult.getUnknownSuperTemplateOfInstances();
85
86         while (iter.hasNext()) {
87             Instance instance = (Instance) iter.next();
88             TemplateDescription contain = instance.getContainingTemplate();
89             if (contain == null) {
90                 throw new InternalException("BUG dans getTemplateDescription de ResourceRepository (1) " + instance.getLocalName());
91             }
92             TemplateDescription scontain = contain.getSuperTemplate();
93             if (scontain == null) {
94                 throw new InternalException("BUG dans getTemplateDescription de ResourceRepository (2) " + instance.getLocalName() + " defined in template " + instance.getContainingTemplate());
95             }
96             Instance instance1 = scontain.getInstance(instance.getLocalName(), false);
97             if (instance1 == null) {
98                 throw new InternalException("BUG dans getTemplateDescription de ResourceRepository (3) " + instance.getLocalName() + " defined in template " + instance.getContainingTemplate());
99             }
100             
101             TemplateDescription temp1 = instance1.getTemplate();
102             TemplateDescription temp = instance.getTemplate();
103             if (temp == null) {
104                 instance.setTemplate(temp1);
105             } else {
106                 temp.setSuperTemplate(temp1);
107             }
108         }
109         
110         if (DEBUG_ON && logger.isLoggable(BasicLevel.DEBUG)) {
111             logger.log(BasicLevel.DEBUG, "State of " + toBeReturned + " ================================= ");
112         }
113
114         if (DEBUG_ON && logger.isLoggable(BasicLevel.DEBUG)) {
115                 logger.log(BasicLevel.DEBUG, "End of description for \"" + toBeReturned.getName() + "\" ==============================");
116         }
117         
118         return toBeReturned;
119     }
120     
121     /**
122      * Method getSingleDescription. This method parses a single ".kilim" file. It updates the ResourceMapping object
123      * received as a parameter by adding new unknown templates, new "super" references, etc ...
124      * @param resourceName : the name of the template to be parsed
125      * @param parsingResult : the ResourceMapping object used to store the parsing results to be used during next iterations.
126      * @return TemplateDescription : the internal representation of the template described in the resource.
127      * @throws ResourceNotFoundException
128      */

129     protected TemplateDescription getSingleDescription(String JavaDoc resourceName, ResourceMapping parsingResult) throws ResourceNotFoundException {
130         
131         if (DEBUG_ON && logger.isLoggable(BasicLevel.DEBUG)) {
132                 logger.log(BasicLevel.DEBUG, "Getting Single Template Description : " + resourceName);
133         }
134         InputStream JavaDoc is = resourceLoader.getResource(resourceName + KILIM_FILES_EXTENSION);
135             
136         if (is == null) {
137             return null;
138         }
139         
140         if (DEBUG_ON && logger.isLoggable(BasicLevel.DEBUG)) {
141             logger.log(BasicLevel.DEBUG, "Parsing " + resourceName);
142         }
143         try {
144             TemplateDescription toBeReturned = tdParser.importTemplateDescription(is, parsingResult, resourceName);
145             toBeReturned.setResourceLoader(resourceLoader);
146             return toBeReturned;
147         } finally {
148             if (is != null) {
149                 try {
150                     is.close();
151                 } catch (IOException JavaDoc ex) {
152                     System.err.println("Problem when closing input stream of " + resourceName + KILIM_FILES_EXTENSION);
153                 }
154             }
155         }
156     }
157     
158     /**
159      * Sets the repository resourceLoader.
160      * @param aResourceLoader : The resourceLoader to set
161      */

162     public void setResourceLoader(ResourceLoader aResourceLoader) {
163         resourceLoader = aResourceLoader;
164     }
165     
166     /**
167      * Returns the current repository resourceLoader.
168      * @return ResourceLoader
169      */

170     public ResourceLoader getResourceLoader() {
171         return resourceLoader;
172     }
173 }
Popular Tags