/** * Create configuration object with tag name and initialize its attributes. If tag name is <code>property</code> add new * property to last configuration object from stack. In any case reset text builder. */ @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { textBuilder.setLength(0); if (qName.equals("property")) { Config config = stack.peek(); config.setProperty(value(attributes, "name"), value(attributes, "value")); } else { Config config = new Config(qName); if (!stack.isEmpty()) { Config parent = stack.peek(); parent.addChild(config); } stack.push(config); for (int i = 0; i < attributes.getLength(); ++i) { config.setAttribute(attributes.getQName(i), attributes.getValue(i)); } } }
/** * Build configuration object. Configuration object is not reusable so this factory creates a new instance for every call. * * @return newly created configuration object. * @throws ConfigException if XML stream read operation fails or is not well formed. */ public Config build() throws ConfigException { if (properties != null) { Config config = new Config("properties"); config.setProperties(properties); return config; } try { Loader loader = new Loader(); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); XMLReader reader = parser.getXMLReader(); reader.setContentHandler(loader); reader.parse(new InputSource(xmlStream)); return loader.getConfig(); } catch (Exception e) { throw new ConfigException(e); } }