📄 xmlparseractivator.java
字号:
context.registerService(SAXFACTORYNAME, this, properties); index++; } } } /** * <p>Set the SAX Parser Service Properties. By default, the following properties * are set: * <ul> * <li><tt>SERVICE_DESCRIPTION</tt> * <li><tt>SERVICE_PID</tt> * <li><tt>PARSER_VALIDATING</tt> - instantiates a parser and queries it to find out whether it is * validating or not * <li><tt>PARSER_NAMESPACEAWARE</tt> - instantiates a parser and queries it to find out whether it is * namespace aware or not * <ul> * * @param factory The <tt>SAXParserFactory</tt> object * @param props <tt>Hashtable</tt> of service properties. */ private void setDefaultSAXProperties(SAXParserFactory factory, Hashtable props, int index) { props.put(Constants.SERVICE_DESCRIPTION, SAXFACTORYDESCRIPTION); props.put(Constants.SERVICE_PID, SAXFACTORYNAME+"."+context.getBundle().getBundleId()+"."+index); setSAXProperties(factory, props); } /** * <p>Set the customizable SAX Parser Service Properties. * * <p>This method attempts to instantiate a validating parser and a namespaceaware * parser to determine if the parser can support those features. The appropriate properties are then set * in the specified properties object. * * <p>This method can be overridden to add additional SAX2 features and properties. If you want to be able * to filter searches of the OSGi service registry, this method must put a key, value pair into the properties object * for each feature or property. For example, * * properties.put("http://www.acme.com/features/foo", Boolean.TRUE); * * @param factory - the SAXParserFactory object * @param properties - the properties object for the service */ public void setSAXProperties(SAXParserFactory factory, Hashtable properties) { SAXParser parser = null; // check if this parser can be configured to validate boolean validating = true; factory.setValidating(true); factory.setNamespaceAware(false); try { parser = factory.newSAXParser(); } catch (Exception pce_val) { validating = false; } // check if this parser can be configured to be namespaceaware boolean namespaceaware = true; factory.setValidating(false); factory.setNamespaceAware(true); try { parser = factory.newSAXParser(); } catch (Exception pce_nsa) { namespaceaware = false; } // set the factory values factory.setValidating(validating); factory.setNamespaceAware(namespaceaware); // set the OSGi service properties properties.put(PARSER_NAMESPACEAWARE, new Boolean(namespaceaware)); properties.put(PARSER_VALIDATING, new Boolean(validating)); } /** * Register DOM Parser Factory Services with the framework. * * @param parserFactoryClassNames - a <tt>Vector</tt> of <tt>String</tt> objects containing the names of the parser Factory Classes * @throws FactoryConfigurationError if thrown from <tt>getFactory</tt> */ private void registerDOMParsers(Vector parserFactoryClassNames) throws FactoryConfigurationError { if (parserFactoryClassNames!=null) { Enumeration e = parserFactoryClassNames.elements(); int index = 0; while (e.hasMoreElements()) { String parserFactoryClassName = (String)e.nextElement(); // create a dom parser factory just to get it's default properties. It will never be used since // this class will operate as a service factory and give each service requestor it's own DocumentBuilderFactory DocumentBuilderFactory factory = (DocumentBuilderFactory)getFactory(parserFactoryClassName); Hashtable properties = new Hashtable(7); // figure out the default properties of the parser setDefaultDOMProperties(factory, properties, index); // store the parser factory class name in the properties so that it can be retrieved when getService is called // to return a parser factory properties.put(FACTORYNAMEKEY, parserFactoryClassName); // release the factory factory = null; // register the factory as a service context.registerService(DOMFACTORYNAME, this, properties); index++; } } } /** * Set the DOM parser service properties. * * By default, the following properties are set: * <ul> * <li><tt>SERVICE_DESCRIPTION</tt> * <li><tt>SERVICE_PID</tt> * <li><tt>PARSER_VALIDATING</tt> * <li><tt>PARSER_NAMESPACEAWARE</tt> * <ul> * * @param factory The <tt>DocumentBuilderFactory</tt> object * @param props <tt>Hashtable</tt> of service properties. */ private void setDefaultDOMProperties(DocumentBuilderFactory factory, Hashtable props, int index) { props.put(Constants.SERVICE_DESCRIPTION, DOMFACTORYDESCRIPTION); props.put(Constants.SERVICE_PID, DOMFACTORYNAME+"."+context.getBundle().getBundleId()+"."+index); setDOMProperties(factory,props); } /** * <p>Set the customizable DOM Parser Service Properties. * * <p>This method attempts to instantiate a validating parser and a namespaceaware * parser to determine if the parser can support those features. The appropriate properties are then set * in the specified props object. * * <p>This method can be overridden to add additional DOM2 features and properties. If you want to be able * to filter searches of the OSGi service registry, this method must put a key, value pair into the properties object * for each feature or property. For example, * * properties.put("http://www.acme.com/features/foo", Boolean.TRUE); * * @param factory - the DocumentBuilderFactory object * @param props - Hashtable of service properties. */ public void setDOMProperties(DocumentBuilderFactory factory, Hashtable props) { DocumentBuilder parser = null; // check if this parser can be configured to validate boolean validating = true; factory.setValidating(true); factory.setNamespaceAware(false); try { parser = factory.newDocumentBuilder(); } catch (Exception pce_val) { validating = false; } // check if this parser can be configured to be namespaceaware boolean namespaceaware = true; factory.setValidating(false); factory.setNamespaceAware(true); try { parser = factory.newDocumentBuilder(); } catch (Exception pce_nsa) { namespaceaware = false; } // set the factory values factory.setValidating(validating); factory.setNamespaceAware(namespaceaware); // set the OSGi service properties props.put(PARSER_VALIDATING, new Boolean(validating)); props.put(PARSER_NAMESPACEAWARE, new Boolean(namespaceaware)); } /** * Given a parser factory class name, instantiate that class. * * @param parserFactoryClassName A <tt>String</tt> object containing the name of the parser factory class * @return a parserFactoryClass Object * @pre parserFactoryClassName!=null */ private Object getFactory(String parserFactoryClassName) throws FactoryConfigurationError { Exception e = null; try { return Class.forName(parserFactoryClassName).newInstance(); } catch (ClassNotFoundException cnfe) { e = cnfe; } catch (InstantiationException ie) { e = ie; } catch (IllegalAccessException iae) { e = iae; } throw new FactoryConfigurationError(e); } /** * Creates a new XML Parser Factory object. * * <p>A unique XML Parser Factory object is returned for each call to this method. * * <p>The returned XML Parser Factory object will be configured for validating * and namespace aware support as specified in the service properties of the * specified ServiceRegistration object. * * This method can be overridden to configure additional features in the returned * XML Parser Factory object. * * @param bundle The bundle using the service. * @param registration The <tt>ServiceRegistration</tt> object for the service. * @return A new, configured XML Parser Factory object or null if a configuration * error was encountered */ public Object getService(Bundle bundle, ServiceRegistration registration) { ServiceReference sref = registration.getReference(); String parserFactoryClassName = (String) sref.getProperty(FACTORYNAMEKEY); try { // need to set factory properties Object factory = getFactory(parserFactoryClassName); if (factory instanceof SAXParserFactory) { ((SAXParserFactory)factory).setValidating(((Boolean)sref.getProperty(PARSER_VALIDATING)).booleanValue()); ((SAXParserFactory)factory).setNamespaceAware(((Boolean)sref.getProperty(PARSER_NAMESPACEAWARE)).booleanValue()); } else if (factory instanceof DocumentBuilderFactory) { ((DocumentBuilderFactory)factory).setValidating(((Boolean)sref.getProperty(PARSER_VALIDATING)).booleanValue()); ((DocumentBuilderFactory)factory).setNamespaceAware(((Boolean)sref.getProperty(PARSER_NAMESPACEAWARE)).booleanValue()); } return factory; } catch (FactoryConfigurationError fce) { fce.printStackTrace(); return null; } } /** * Releases a XML Parser Factory object. * * @param bundle The bundle releasing the service. * @param registration The <tt>ServiceRegistration</tt> object for the service. * @param service The XML Parser Factory object returned by a previous call to the <tt>getService</tt> method. */ public void ungetService(Bundle bundle, ServiceRegistration registration, Object service) { }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -