1 64 package com.jcorporate.expresso.kernel.digester; 65 66 import com.jcorporate.expresso.kernel.metadata.ComponentMetadata; 67 import com.jcorporate.expresso.kernel.util.FastStringBuffer; 68 import org.apache.commons.digester.Digester; 69 import org.apache.log4j.Logger; 70 import org.xml.sax.SAXException ; 71 72 import javax.xml.parsers.FactoryConfigurationError ; 73 import javax.xml.parsers.ParserConfigurationException ; 74 import java.io.IOException ; 75 import java.io.InputStream ; 76 import java.net.URL ; 77 78 85 86 public class ComponentMetadataConfig implements java.io.Serializable { 87 92 private static final int MAX_NESTING = 20; 93 94 95 private static final Logger log = Logger.getLogger(ComponentMetadataConfig.class); 96 97 private ComponentMetadata root; 98 99 private SaxParserConfigurer saxParserCoonfig = new SaxParserConfigurer(); 100 101 102 105 public ComponentMetadataConfig() { 106 } 107 108 109 120 public void loadComponentMetadata(String resourceLocation) { 121 if (resourceLocation == null) { 122 throw new IllegalArgumentException ("Parameter resourceLocation must not be null"); 123 } 124 125 InputStream is = null; 126 try { 127 if (log.isDebugEnabled()) { 128 log.debug("Loading metadata from location: " + resourceLocation); 129 } 130 131 saxParserCoonfig.setSAXParser(); 132 javax.xml.parsers.SAXParserFactory spf = javax.xml.parsers.SAXParserFactory.newInstance(); 133 javax.xml.parsers.SAXParser sp = spf.newSAXParser(); 134 135 Digester digester = new Digester(sp); 136 setupResolvers(digester); 137 setDigesterRules(digester); 138 139 is = this.getClass().getResourceAsStream(resourceLocation); 140 digester.parse(is); 141 } catch (FactoryConfigurationError ex) { 142 log.error("Fatal error trying to find a suitable Digester compatible parser.", ex); 143 } catch (SAXException ex) { 144 log.error("Fatal error trying to digest expresso-services.xml file", ex); 145 } catch (ParserConfigurationException ex) { 146 log.error("Fatal error trying to find a suitable Digester compatible parser.", ex); 147 } catch (java.io.IOException ex) { 148 log.error("Fatal IO error parsing input.", ex); 149 } finally { 150 try { 151 if (is != null) { 152 is.close(); 153 } 154 } catch (IOException ex) { 155 log.error("Error closing resource stream: " + resourceLocation, ex); 156 } 157 } 158 159 } 160 161 166 private void setupResolvers(Digester digester) { 167 URL url = this.getClass().getResource("/com/jcorporate/expresso/kernel/expresso-metadata_5_1.dtd"); 168 if (url != null) { 169 digester.register("-//Jcorporate Ltd//DTD Expresso Component Metadata Configuration 5.1//EN", 170 url.toString()); 171 } else { 172 throw new IllegalArgumentException ("Unable to locate " + 173 "expresso-services_5_1.dtd in component package"); 174 } 175 } 176 177 183 public void loadComponentMetadata(URL resourceLocation) { 184 if (resourceLocation == null) { 185 throw new IllegalArgumentException ("Parameter resourceLocation must not be null"); 186 } 187 188 if (log.isDebugEnabled()) { 189 log.debug("Loading metadata from location: " + resourceLocation.toString()); 190 } 191 192 try { 193 saxParserCoonfig.setSAXParser(); 194 javax.xml.parsers.SAXParserFactory spf = javax.xml.parsers.SAXParserFactory.newInstance(); 195 javax.xml.parsers.SAXParser sp = spf.newSAXParser(); 196 197 Digester digester = new Digester(sp); 198 setupResolvers(digester); 199 setDigesterRules(digester); 200 201 digester.parse(resourceLocation.openStream()); 202 } catch (FactoryConfigurationError ex) { 203 log.error("Fatal error trying to find a suitable Digester compatible parser.", ex); 204 } catch (SAXException ex) { 205 log.error("Fatal error trying to digest expresso-services.xml file", ex); 206 } catch (ParserConfigurationException ex) { 207 log.error("Fatal error trying to find a suitable Digester compatible parser.", ex); 208 } catch (java.io.IOException ex) { 209 log.error("Fatal IO error parsing input.", ex); 210 } 211 } 212 213 214 222 protected void setDigesterRules(Digester digester) { 223 root = new ComponentMetadata(); 224 225 digester.push(root); 226 for (int i = 0; i < MAX_NESTING; i++) { 227 String componentDepthString = getComponentDepthString(i); 228 if (i > 0) { 229 digester.addObjectCreate("component-metadata" + componentDepthString, 230 com.jcorporate.expresso.kernel.metadata.ComponentMetadata.class); 231 } 232 233 digester.addSetProperties("component-metadata" + componentDepthString); 234 235 digester.addCallMethod("component-metadata" + componentDepthString + "/description", "setDescription", 1); 236 digester.addCallParam("component-metadata" + componentDepthString + "/description", 0); 237 238 digester.addCallMethod("component-metadata" + componentDepthString + "/version-info", "setVersionNumber", 239 3); 240 digester.addCallParam("component-metadata" + componentDepthString + "/version-info/major-version", 0); 241 digester.addCallParam("component-metadata" + componentDepthString + "/version-info/minor-version", 1); 242 digester.addCallParam("component-metadata" + componentDepthString + "/version-info/micro-version", 2); 243 244 digester.addCallMethod("component-metadata" + componentDepthString + "/message-bundle", "setMessageBundle", 245 1); 246 digester.addCallParam("component-metadata" + componentDepthString + "/message-bundle", 0); 247 248 digester.addObjectCreate("component-metadata" + componentDepthString + "/property-list/property", 249 com.jcorporate.expresso.kernel.metadata.SimpleProperty.class); 250 251 digester.addSetProperties("component-metadata" + componentDepthString + "/property-list/property"); 252 digester.addCallMethod( 253 "component-metadata" + componentDepthString + "/property-list/property/property-valid-value", 254 "addValidValue", 2); 255 digester.addCallParam( 256 "component-metadata" + componentDepthString + "/property-list/property/property-valid-value", 0, 257 "name"); 258 digester.addCallParam( 259 "component-metadata" + componentDepthString + "/property-list/property/property-valid-value", 1, 260 "value"); 261 digester.addSetNext("component-metadata" + componentDepthString + "/property-list/property", "addProperty"); 262 263 264 digester.addObjectCreate("component-metadata" + componentDepthString + "/property-list/mapped-property", 265 com.jcorporate.expresso.kernel.metadata.MappedProperty.class); 266 digester.addSetProperties("component-metadata" + componentDepthString + "/property-list/mapped-property"); 267 digester.addCallMethod( 268 "component-metadata" + componentDepthString + "/property-list/mapped-property/mapped-value", 269 "setMappedValue", 2); 270 digester.addCallParam( 271 "component-metadata" + componentDepthString + "/property-list/mapped-property/mapped-value", 0, 272 "key"); 273 digester.addCallParam( 274 "component-metadata" + componentDepthString + "/property-list/mapped-property/mapped-value", 1, 275 "value"); 276 digester.addSetNext("component-metadata" + componentDepthString + "/property-list/mapped-property", 277 "addProperty"); 278 279 digester.addObjectCreate("component-metadata" + componentDepthString + "/property-list/indexed-property", 280 com.jcorporate.expresso.kernel.metadata.IndexedProperty.class); 281 digester.addSetProperties("component-metadata" + componentDepthString + "/property-list/indexed-property"); 282 digester.addCallMethod( 283 "component-metadata" + componentDepthString + "/property-list/indexed-property/indexed-property-value", 284 "setIndexedValue", 2); 285 digester.addCallParam( 286 "component-metadata" + componentDepthString + "/property-list/indexed-property/indexed-property-value", 287 0, "index"); 288 digester.addCallParam( 289 "component-metadata" + componentDepthString + "/property-list/indexed-property/indexed-property-value", 290 1, "value"); 291 digester.addSetNext("component-metadata" + componentDepthString + "/property-list/indexed-property", 292 "addProperty"); 293 294 295 digester.addObjectCreate("component-metadata" + componentDepthString + "/method-list/method", 296 com.jcorporate.expresso.kernel.metadata.Method.class); 297 digester.addSetProperties("component-metadata" + componentDepthString + "/method-list/method", 298 new String []{"name", "return-type", "description"}, 299 new String []{"name", "returnType", "description"}); 300 digester.addCallMethod("component-metadata" + componentDepthString + "/method-list/method/method-arg", 301 "addArgument", 2); 302 digester.addCallParam("component-metadata" + componentDepthString + "/method-list/method/method-arg", 0, 303 "name"); 304 digester.addCallParam("component-metadata" + componentDepthString + "/method-list/method/method-arg", 1, 305 "type"); 306 digester.addSetNext("component-metadata" + componentDepthString + "/method-list/method", "addMethod"); 307 308 309 digester.addObjectCreate("component-metadata" + componentDepthString + "/schema", 313 com.jcorporate.expresso.kernel.metadata.SchemaData.class); 314 315 digester.addObjectCreate("component-metadata" + componentDepthString + "/schema/dbobject", 316 com.jcorporate.expresso.kernel.metadata.DBObjectData.class); 317 digester.addSetProperties("component-metadata" + componentDepthString + "/schema/dbobject/classname", 318 new String []{"name"}, new String []{"className"}); 319 digester.addSetNext("component-metadata" + componentDepthString + "/schema/dbobject", "addDataObject"); 320 321 digester.addObjectCreate("component-metadata" + componentDepthString + "/schema/controller", 322 com.jcorporate.expresso.kernel.metadata.ControllerData.class); 323 digester.addSetProperties("component-metadata" + componentDepthString + "/schema/controller/classname", 324 new String []{"name"}, new String []{"className"}); 325 digester.addSetNext("component-metadata" + componentDepthString + "/schema/controller", "addController"); 326 327 digester.addObjectCreate("component-metadata" + componentDepthString + "/schema/job", 328 com.jcorporate.expresso.kernel.metadata.JobData.class); 329 digester.addSetProperties("component-metadata" + componentDepthString + "/schema/job/classname", 330 new String []{"name"}, new String []{"className"}); 331 digester.addSetNext("component-metadata" + componentDepthString + "/schema/job", "addJob"); 332 333 digester.addObjectCreate("component-metadata" + componentDepthString + "/schema/report", 334 com.jcorporate.expresso.kernel.metadata.ReportData.class); 335 digester.addSetProperties("component-metadata" + componentDepthString + "/schema/report/classname", 336 new String []{"name"}, new String []{"className"}); 337 digester.addSetNext("component-metadata" + componentDepthString + "/schema/report", "addReportObject"); 338 339 digester.addSetNext("component-metadata" + componentDepthString + "/schema", "setSchemaData"); 340 341 if (i > 0) { 342 digester.addSetNext("component-metadata" + componentDepthString, "addChildComponent"); 343 } 344 } 345 346 } 347 348 354 private String getComponentDepthString(int index) { 355 FastStringBuffer fsb = FastStringBuffer.getInstance(); 356 for (int i = 0; i < index; i++) { 357 if (i == 0) { 358 fsb.append("/embedded-components"); 359 } 360 fsb.append("/component-metadata"); 361 } 362 363 String returnValue = fsb.toString(); 364 fsb.release(); 365 366 return returnValue; 367 } 368 369 370 376 public ComponentMetadata getMetadata() { 377 return root; 378 } 379 380 } | Popular Tags |