private static InputStream getAsInputStreamFromClassLoader(String filename) { ClassLoader cl = Thread.currentThread().getContextClassLoader(); InputStream is = cl == null ? null : cl.getResourceAsStream(filename); if (is == null) { // check system class loader is = XmlConfigurator.class.getClassLoader().getResourceAsStream(filename); } return is; }
public static void main(String[] args) throws Exception { String input_file = null; XmlConfigurator conf; boolean old_format = false; for (int i = 0; i < args.length; i++) { if (args[i].equals("-old")) { old_format = true; continue; } if (args[i].equals("-file")) { input_file = args[++i]; continue; } help(); return; } if (input_file != null) { InputStream input = null; try { input = new FileInputStream(new File(input_file)); } catch (Throwable t) { } if (input == null) { try { input = new URL(input_file).openStream(); } catch (Throwable t) { } } if (input == null) input = Thread.currentThread().getContextClassLoader().getResourceAsStream(input_file); if (old_format) { String cfg = inputAsString(input); List<ProtocolConfiguration> tmp = Configurator.parseConfigurations(cfg); System.out.println(dump(tmp)); } else { conf = XmlConfigurator.getInstance(input); String tmp = conf.getProtocolStackString(); System.out.println("\n" + tmp); } } else { log.error("no input file given"); } }
public void parse(String xmlInputFileName, String xmlOutputFileName) throws Exception { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse( Thread.currentThread().getContextClassLoader().getResourceAsStream(xmlInputFileName)); removeEvenNodes(document); removeSpaces(document); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, OUTPUT_KEYS_INTEND_YES); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(new File(xmlOutputFileName)); transformer.transform(source, result); } catch (ParserConfigurationException | TransformerException | SAXException | IOException e) { LOGGER.error(e.toString(), e); throw e; } }