/** * Copyright (c) Microsoft Corporation * <p/> * All rights reserved. * <p/> * MIT License * <p/> * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated * documentation files (the "Software"), to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and * to permit persons to whom the Software is furnished to do so, subject to the following conditions: * <p/> * The above copyright notice and this permission notice shall be included in all copies or substantial portions of * the Software. * <p/> * THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package com.microsoft.intellij.ui.libraries; import com.microsoft.intellij.AzurePlugin; import org.w3c.dom.Document; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import static com.microsoft.intellij.ui.messages.AzureBundle.message; /** * Utility class used to parse and save xml files. */ final class ParserXMLUtility { private ParserXMLUtility() { } /** * Parses XML file and returns XML document. * * @param fileName . * @return XML document or <B>null</B> if error occured * @throws WindowsAzureInvalidProjectOperationException */ protected static Document parseXMLFile(final String fileName, String errorMessage) throws Exception { try { DocumentBuilder docBuilder; Document doc = null; DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setIgnoringElementContentWhitespace(true); docBuilder = docBuilderFactory.newDocumentBuilder(); File xmlFile = new File(fileName); doc = docBuilder.parse(xmlFile); return doc; } catch (Exception e) { AzurePlugin.log(String.format("%s%s", errorMessage, e.getMessage()), e); throw new Exception(String.format("%s%s", errorMessage, e.getMessage())); } } /** * save XML file and saves XML document. * * @param fileName * @param doc * @return XML document or <B>null</B> if error occured * @throws IOException * @throws WindowsAzureInvalidProjectOperationException */ protected static boolean saveXMLFile(String fileName, Document doc) throws Exception { File xmlFile = null; FileOutputStream fos = null; Transformer transformer; try { xmlFile = new File(fileName); fos = new FileOutputStream(xmlFile); TransformerFactory transFactory = TransformerFactory.newInstance(); transformer = transFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult destination = new StreamResult(fos); // transform source into result will do save transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform(source, destination); } catch (Exception excp) { AzurePlugin.log(String.format("%s%s", message("saveErrMsg"), excp.getMessage()), excp); } finally { if (fos != null) { fos.close(); } } return true; } }