1 18 19 package org.osgi.util.xml; 20 21 import java.io.*; 22 import java.net.URL ; 23 import java.util.*; 24 25 import javax.xml.parsers.*; 26 27 import org.osgi.framework.*; 28 29 80 public class XMLParserActivator implements BundleActivator, ServiceFactory { 81 82 private BundleContext context; 83 87 public static final String SAXFACTORYNAME = "javax.xml.parsers.SAXParserFactory"; 88 92 public static final String DOMFACTORYNAME = "javax.xml.parsers.DocumentBuilderFactory"; 93 94 private static final String PARSERCLASSFILEPATH = "/META-INF/services/"; 95 96 public static final String SAXCLASSFILE = PARSERCLASSFILEPATH 97 + SAXFACTORYNAME; 98 99 public static final String DOMCLASSFILE = PARSERCLASSFILEPATH 100 + DOMFACTORYNAME; 101 102 private static final String SAXFACTORYDESCRIPTION = "A JAXP Compliant SAX Parser"; 103 104 private static final String DOMFACTORYDESCRIPTION = "A JAXP Compliant DOM Parser"; 105 109 public static final String PARSER_VALIDATING = "parser.validating"; 110 114 public static final String PARSER_NAMESPACEAWARE = "parser.namespaceAware"; 115 120 private static final String FACTORYNAMEKEY = "parser.factoryname"; 121 122 142 public void start(BundleContext context) throws Exception { 143 this.context = context; 144 Bundle parserBundle = context.getBundle(); 145 try { 146 registerSAXParsers(getParserFactoryClassNames(parserBundle 148 .getResource(SAXCLASSFILE))); 149 registerDOMParsers(getParserFactoryClassNames(parserBundle 151 .getResource(DOMCLASSFILE))); 152 } 153 catch (IOException ioe) { 154 ioe.printStackTrace(); 157 throw new FactoryConfigurationError(ioe); 158 } 159 } 160 161 173 public void stop(BundleContext context) throws Exception { 174 } 175 176 187 private Vector getParserFactoryClassNames(URL parserUrl) throws IOException { 188 Vector v = new Vector(1); 189 if (parserUrl != null) { 190 String parserFactoryClassName = null; 191 InputStream is = parserUrl.openStream(); 192 BufferedReader br = new BufferedReader(new InputStreamReader(is)); 193 while (true) { 194 parserFactoryClassName = br.readLine(); 195 if (parserFactoryClassName == null) { 196 break; } 198 String pfcName = parserFactoryClassName.trim(); 199 if (pfcName.length() == 0) { 200 continue; } 202 int commentIdx = pfcName.indexOf("#"); 203 if (commentIdx == 0) { continue; 205 } 206 else 207 if (commentIdx < 0) { v.addElement(pfcName); 209 } 210 else { 211 v.addElement(pfcName.substring(0, commentIdx).trim()); 212 } 213 } 214 return v; 215 } 216 else { 217 return null; 218 } 219 } 220 221 229 private void registerSAXParsers(Vector parserFactoryClassNames) 230 throws FactoryConfigurationError { 231 if (parserFactoryClassNames != null) { 232 Enumeration e = parserFactoryClassNames.elements(); 233 int index = 0; 234 while (e.hasMoreElements()) { 235 String parserFactoryClassName = (String ) e.nextElement(); 236 SAXParserFactory factory = (SAXParserFactory) getFactory(parserFactoryClassName); 241 Hashtable properties = new Hashtable(7); 242 setDefaultSAXProperties(factory, properties, index); 244 properties.put(FACTORYNAMEKEY, parserFactoryClassName); 248 factory = null; 250 context.registerService(SAXFACTORYNAME, this, properties); 252 index++; 253 } 254 } 255 } 256 257 273 private void setDefaultSAXProperties(SAXParserFactory factory, 274 Hashtable props, int index) { 275 props.put(Constants.SERVICE_DESCRIPTION, SAXFACTORYDESCRIPTION); 276 props.put(Constants.SERVICE_PID, SAXFACTORYNAME + "." 277 + context.getBundle().getBundleId() + "." + index); 278 setSAXProperties(factory, props); 279 } 280 281 302 public void setSAXProperties(SAXParserFactory factory, Hashtable properties) { 303 boolean validating = true; 305 factory.setValidating(true); 306 factory.setNamespaceAware(false); 307 try { 308 factory.newSAXParser(); 309 } 310 catch (Exception pce_val) { 311 validating = false; 312 } 313 boolean namespaceaware = true; 315 factory.setValidating(false); 316 factory.setNamespaceAware(true); 317 try { 318 factory.newSAXParser(); 319 } 320 catch (Exception pce_nsa) { 321 namespaceaware = false; 322 } 323 factory.setValidating(validating); 325 factory.setNamespaceAware(namespaceaware); 326 properties.put(PARSER_NAMESPACEAWARE, new Boolean (namespaceaware)); 328 properties.put(PARSER_VALIDATING, new Boolean (validating)); 329 } 330 331 339 private void registerDOMParsers(Vector parserFactoryClassNames) 340 throws FactoryConfigurationError { 341 if (parserFactoryClassNames != null) { 342 Enumeration e = parserFactoryClassNames.elements(); 343 int index = 0; 344 while (e.hasMoreElements()) { 345 String parserFactoryClassName = (String ) e.nextElement(); 346 DocumentBuilderFactory factory = (DocumentBuilderFactory) getFactory(parserFactoryClassName); 351 Hashtable properties = new Hashtable(7); 352 setDefaultDOMProperties(factory, properties, index); 354 properties.put(FACTORYNAMEKEY, parserFactoryClassName); 358 factory = null; 360 context.registerService(DOMFACTORYNAME, this, properties); 362 index++; 363 } 364 } 365 } 366 367 381 private void setDefaultDOMProperties(DocumentBuilderFactory factory, 382 Hashtable props, int index) { 383 props.put(Constants.SERVICE_DESCRIPTION, DOMFACTORYDESCRIPTION); 384 props.put(Constants.SERVICE_PID, DOMFACTORYNAME + "." 385 + context.getBundle().getBundleId() + "." + index); 386 setDOMProperties(factory, props); 387 } 388 389 410 public void setDOMProperties(DocumentBuilderFactory factory, Hashtable props) { 411 boolean validating = true; 413 factory.setValidating(true); 414 factory.setNamespaceAware(false); 415 try { 416 factory.newDocumentBuilder(); 417 } 418 catch (Exception pce_val) { 419 validating = false; 420 } 421 boolean namespaceaware = true; 423 factory.setValidating(false); 424 factory.setNamespaceAware(true); 425 try { 426 factory.newDocumentBuilder(); 427 } 428 catch (Exception pce_nsa) { 429 namespaceaware = false; 430 } 431 factory.setValidating(validating); 433 factory.setNamespaceAware(namespaceaware); 434 props.put(PARSER_VALIDATING, new Boolean (validating)); 436 props.put(PARSER_NAMESPACEAWARE, new Boolean (namespaceaware)); 437 } 438 439 447 private Object getFactory(String parserFactoryClassName) 448 throws FactoryConfigurationError { 449 Exception e = null; 450 try { 451 return Class.forName(parserFactoryClassName).newInstance(); 452 } 453 catch (ClassNotFoundException cnfe) { 454 e = cnfe; 455 } 456 catch (InstantiationException ie) { 457 e = ie; 458 } 459 catch (IllegalAccessException iae) { 460 e = iae; 461 } 462 throw new FactoryConfigurationError(e); 463 } 464 465 486 public Object getService(Bundle bundle, ServiceRegistration registration) { 487 ServiceReference sref = registration.getReference(); 488 String parserFactoryClassName = (String ) sref 489 .getProperty(FACTORYNAMEKEY); 490 try { 491 Object factory = getFactory(parserFactoryClassName); 493 if (factory instanceof SAXParserFactory) { 494 ((SAXParserFactory) factory).setValidating(((Boolean ) sref 495 .getProperty(PARSER_VALIDATING)).booleanValue()); 496 ((SAXParserFactory) factory).setNamespaceAware(((Boolean ) sref 497 .getProperty(PARSER_NAMESPACEAWARE)).booleanValue()); 498 } 499 else 500 if (factory instanceof DocumentBuilderFactory) { 501 ((DocumentBuilderFactory) factory) 502 .setValidating(((Boolean ) sref 503 .getProperty(PARSER_VALIDATING)) 504 .booleanValue()); 505 ((DocumentBuilderFactory) factory) 506 .setNamespaceAware(((Boolean ) sref 507 .getProperty(PARSER_NAMESPACEAWARE)) 508 .booleanValue()); 509 } 510 return factory; 511 } 512 catch (FactoryConfigurationError fce) { 513 fce.printStackTrace(); 514 return null; 515 } 516 } 517 518 527 public void ungetService(Bundle bundle, ServiceRegistration registration, 528 Object service) { 529 } 530 } 531 | Popular Tags |