/** * Return a XML with config * * @param path Path for saving de config * @param config GUIConfig */ public static void saveXMLFromGUIConfig(String path, GUIConfig config) { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder; Document doc = null; try { docBuilder = docFactory.newDocumentBuilder(); // root elements doc = docBuilder.newDocument(); Element rootElement = doc.createElement("gui-configuration"); doc.appendChild(rootElement); Element language = doc.createElement("language"); language.setTextContent(config.getLanguage()); rootElement.appendChild(language); Element recentConfigFiles = doc.createElement("recentConfigFiles"); rootElement.appendChild(recentConfigFiles); for (String configFilePath : config.getRecentConfigFiles()) { Element configFile = doc.createElement("configFile"); configFile.setTextContent(configFilePath); recentConfigFiles.appendChild(configFile); } // write the content into xml file TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); // At the moment we are not to validate Scheme // validateSchema(doc, Constants.configSchemaFilePath); // normalize text representation doc.getDocumentElement().normalize(); StreamResult result = new StreamResult(new File(path)); transformer.transform(source, result); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public static void getGUIConfigFromXml(InputStream is, GUIConfig config) { try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.parse(is); validateSchema(doc, Constants.CONFIG_SCHEMA_FILE_PATH); // normalize text representation doc.getDocumentElement().normalize(); NodeList language = doc.getElementsByTagName("language").item(0).getChildNodes(); for (int s = 0; s < language.getLength(); s++) { Node nodeAux = language.item(s); config.setLanguage(nodeAux.getTextContent().trim()); } NodeList recentConfigFiles = doc.getElementsByTagName("recentConfigFiles").item(0).getChildNodes(); for (int s = 0; s < recentConfigFiles.getLength(); s++) { Node nodeAux = recentConfigFiles.item(s); if (nodeAux.getNodeType() == Node.ELEMENT_NODE) { if (nodeAux.getNodeName().equals("configFile")) { config.addRecentConfigFile(nodeAux.getTextContent().trim()); } } } } catch (SAXParseException err) { logger.error( "** Parsing error" + ", line " + err.getLineNumber() + ", uri " + err.getSystemId()); logger.error(" " + err.getMessage()); throw new ConfigException(err); } catch (SAXException e) { throw new ConfigException(e); } catch (Throwable t) { throw new ConfigException(t); } } // end of main