//************************************************************************** // Filename : XmlFactory.java, WSSE Toolkit 2005 // // Author : Denis Pilipchuk // // Purpose : Factory for XML-related functionality. Instantiates appropriate // implementation of DocumentBuilder. // // Disclaimer: This software is provided "as-is" and does not imply // any warranty or responsibility. //**************************************************************************** package wsse.Toolkit.Utils.Xml; import java.util.HashMap; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.ParserConfigurationException; public class XmlFactory { private static final String DBFProperty = "javax.xml.parsers.DocumentBuilderFactory"; private static final String DefDBFImpl = "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"; public XmlFactory() { } public XmlDocHelper GetDocHelper(String dbfImpl) throws XmlDocException { String key = null != dbfImpl ? dbfImpl : ""; DocumentBuilder db = null; synchronized (m_docBuilders) { if (m_docBuilders.containsKey(key)) { db = (DocumentBuilder)m_docBuilders.get(key); } else { // Create a new one for the requested implementation try { db = CreateDocBuilder(dbfImpl); m_docBuilders.put(key,db); } catch(ParserConfigurationException pce) { throw new XmlDocException("Error creating document builder, impl: " + dbfImpl, pce); } } } return new XmlDocHelper(db); } private DocumentBuilder CreateDocBuilder(String dbfImpl) throws ParserConfigurationException { System.setProperty(XmlFactory.DBFProperty, null != dbfImpl && dbfImpl.length() > 0 ? dbfImpl : XmlFactory.DefDBFImpl); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); return dbf.newDocumentBuilder(); } private HashMap m_docBuilders = new HashMap(); }