/** * Test constructor creates and empty configuration object with given name. * * @param configName configuration object name. */ protected TinyConfigBuilder(String configName) { config = new Config(configName); }
/** * 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); } }
config.setProperty(value(attributes, "name"), value(attributes, "value")); } else { Config config = new Config(qName); Config parent = parentsStack.peek(); parent.addChild(config);
/** * Construct application configuration instance with information from given servlet context. * * @param context Servlet context bound to application, * @param properties context properties defining variables to inject on descriptors. * @throws ConfigException if application descriptor is not well formed or invalid. */ public TinyConfigBuilder(ServletContext context, Properties properties) throws ConfigException { config = new Config(getAppName(context.getContextPath())); File contextDir = new File(context.getRealPath("")); File webinfDir = new File(contextDir, "WEB-INF"); Loader loader = new Loader(config, properties); // first load library descriptor loadXML(Classes.getResourceAsStream("lib-descriptor.xml"), loader); // load application specific configuration document(s) File file = new File(webinfDir, "app.xml"); try { loadXML(new FileInputStream(file), loader); } catch (FileNotFoundException e) { log.debug("Application |%s| has no descriptor. No application specific configuration.", config.getName()); new ConfigException(e); } }
@Override public Config build() throws ConfigException { Config config = new Config("i18n-repository"); config.setProperty("multi-locale", multiLocale); config.setProperty("base-dir", baseDir); config.setProperty("sub-path", subpath); config.setProperty("files-pattern", filesPattern); return config; } }