private Document readFileToDocument(String filePath) { Document doc = null; try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); File file = new File(filePath); if (file.exists() && file.isFile()) { doc = docBuilder.parse(file); } else { doc = docBuilder.newDocument(); Element root = doc.createElement("settings"); doc.appendChild(root); } doc.getDocumentElement().normalize(); } catch (Exception ex) { FabricPlugin.getLogger().error(ex); } return doc; }
private String prepareNewSettingsFile() { // check the users maven settings String userSettings = MavenPlugin.getMavenConfiguration().getUserSettingsFile(); if (userSettings == null || userSettings.length() == 0) { userSettings = MavenCli.DEFAULT_USER_SETTINGS_FILE.getAbsolutePath(); } if (userSettings != null && userSettings.trim().length() > 0) { File mvnConfig = new File(userSettings); // now we have to parse the xml config file and check for // a servers/server entry named like our fabric Document document = readFileToDocument(userSettings); String fabServerName = this.node.getFabricNameWithoutSpaces(); boolean entryExists = checkForFabricServer(document, fabServerName); // if yes - no more to do...user knows what he is doing if (entryExists) { // the entry is already there...use it } else { // if no - add the server entry for the fabric with auth // information and save config to temp folder - then reference // to this config file for the next mvn deploy command String fabServerUser = this.node.getFabric().getUserName(); String fabServerPass = this.node.getFabric().getPassword(); // create a server entry NodeList serversList = document.getElementsByTagName(SERVERS_TAG); Element serversElement = null; if (serversList.getLength() < 1) { // no servers element - create one serversElement = document.createElement(SERVERS_TAG); document.getDocumentElement().appendChild(serversElement); } else { serversElement = (Element) serversList.item(0); } Element server = document.createElement(SERVER_TAG); serversElement.appendChild(server); Element sId = document.createElement(ID_TAG); server.appendChild(sId); Text textId = document.createTextNode(fabServerName); sId.appendChild(textId); Element sUser = document.createElement(USERNAME_TAG); server.appendChild(sUser); Text textUser = document.createTextNode(fabServerUser); sUser.appendChild(textUser); Element sPass = document.createElement(PASSWORD_TAG); server.appendChild(sPass); Text textPass = document.createTextNode(fabServerPass); sPass.appendChild(textPass); // now save the config to a new file File newSettings = null; try { // create the temp file newSettings = File.createTempFile("mvnSettings_", "_ide.xml"); // prepare dom document for writing Source source = new DOMSource(document); // prepare to write Result result = new StreamResult(newSettings); // Write the DOM document to the file Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.transform(source, result); // switch to new maven config userSettings = newSettings.getPath(); } catch (Exception ex) { FabricPlugin.getLogger().error(ex); } finally { // mark file for deletion newSettings.deleteOnExit(); } } } return userSettings; }