1 18 package org.objectweb.kilim.repository; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 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 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 resourceCache = new HashMap (); 46 47 50 public ResourceRepository() { 51 tdParser = KilimConfiguration.getTemplateDescriptionParser(); 52 resourceLoader = KilimConfiguration.getLoader(); 53 } 54 55 59 public ResourceRepository(TemplateDescriptionParser aParser) { 60 tdParser = aParser; 61 resourceLoader = new ClassLoaderResourceLoader(getClass().getClassLoader()); 62 } 63 64 67 public TemplateDescription getTemplateDescription(String resourceName) throws ResourceNotFoundException, KilimException { 68 69 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 String nextTemplateName; 79 while ((nextTemplateName = parsingResult.getNextUnparsedTemplate()) != null) { 80 getSingleDescription(nextTemplateName, parsingResult); 81 } 82 83 Iterator 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 129 protected TemplateDescription getSingleDescription(String 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 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 ex) { 152 System.err.println("Problem when closing input stream of " + resourceName + KILIM_FILES_EXTENSION); 153 } 154 } 155 } 156 } 157 158 162 public void setResourceLoader(ResourceLoader aResourceLoader) { 163 resourceLoader = aResourceLoader; 164 } 165 166 170 public ResourceLoader getResourceLoader() { 171 return resourceLoader; 172 } 173 } | Popular Tags |