All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.netbeans.modules.maven.htmlui.MavenUtilities Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.netbeans.modules.maven.htmlui;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.nio.file.NoSuchFileException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.netbeans.modules.maven.api.FileUtilities;

final class MavenUtilities {

    private static final Logger LOG = Logger.getLogger(MavenUtilities.class.getName());
    private static final String DEFINITION = "android.sdk.path";
    private static final String NBDEFINITION = "netbeans.installation";
    private static final String MOEDEFINITION = "moe.launcher.simulators";
    private static final String ROBOVMDEFINITION = "robovm.iosDeviceName";

    static final String HEADER_XML = "\n";
    static final String HEADER_SETTINGS = "\n";
    static final String FOOTER_SETTINGS = "\n";

    private final File settings;

    MavenUtilities(File settings) {
        this.settings = settings;
    }

    String readAndroidSdkPath() {
        return readProperty(DEFINITION);
    }

    private String readProperty(final String tag) {
        try {
            if (!this.settings.isFile()) {
                return null;
            }
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder;
            dBuilder = dbFactory.newDocumentBuilder();
            Document settingsDoc = dBuilder.parse(this.settings);
            NodeList elementsByTagName = settingsDoc.getElementsByTagName(tag);
            if (elementsByTagName.getLength() >0) return elementsByTagName.item(0).getTextContent();
            return null;
        } catch (NoSuchFileException ex) {
            LOG.log(Level.FINE, "Cannot find " + settings, ex);
            return null;
        } catch (IOException ex) {
            LOG.log(Level.INFO, "Cannot read " + settings, ex);
            return null;
        } catch (ParserConfigurationException ex) {
            LOG.log(Level.INFO, "Cannot read " + settings, ex);
            return null;
        } catch (SAXException ex) {
            LOG.log(Level.INFO, "Cannot read " + settings, ex);
            return null;
        }
    }

    void writeAndroidSdkPath(String path) {
        writeProperty("android.sdk.path", path);
    }

    private void writeProperty(String name, String value) {
        try {
            String dump;
            if (settings.exists()) {
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder;
                dBuilder = dbFactory.newDocumentBuilder();
                Document settingsDoc = dBuilder.parse(this.settings);
                settingsDoc.getDocumentElement().normalize();
                NodeList profilesNode = settingsDoc.getElementsByTagName("profiles");
                Node parent = null;
                if (profilesNode.getLength() > 0) {
                    parent = profilesNode.item(0);
                } else {
                    parent = settingsDoc.createElement("profiles");
                    settingsDoc.getDocumentElement().appendChild(parent);
                }

                Document doc = parent.getOwnerDocument();

                NodeList profiles = parent.getChildNodes();
                for (int i = 0; i < profiles.getLength();) {
                    Node profileNode = profiles.item(i);
                    if (name.equals(profileId(profileNode))) {
                        parent.removeChild(profileNode);
                    } else {
                        i++;
                    }
                }

                Node fragmentNode = DocumentBuilderFactory
                        .newInstance()
                        .newDocumentBuilder().parse(
                                new InputSource(new StringReader(singleProfile(name, value))))
                        .getDocumentElement();
                fragmentNode = doc.importNode(fragmentNode, true);
                parent.appendChild(fragmentNode);
                doc.getDocumentElement().normalize();
                TransformerFactory transformerFactory = TransformerFactory.newInstance();
                Transformer transformer = transformerFactory.newTransformer();
                DOMSource source = new DOMSource(settingsDoc);
                StreamResult result = new StreamResult(settings);
                transformer.setOutputProperty(OutputKeys.INDENT, "yes");
                transformer.transform(source, result);
            } else {
                settings.getParentFile().mkdirs();
                dump = HEADER_XML + HEADER_SETTINGS
                        + "  \n"
                        + singleProfile(name, value)
                        + "  \n"
                        + FOOTER_SETTINGS;

                FileWriter w = new FileWriter(settings);
                w.write(dump);
                w.close();
            }
        } catch (IOException ex) {
            LOG.log(Level.INFO, "Cannot modify " + settings, ex);
        } catch (ParserConfigurationException ex) {
             LOG.log(Level.INFO, "Cannot modify " + settings, ex);
        } catch (SAXException ex) {
             LOG.log(Level.INFO, "Cannot modify " + settings, ex);
        } catch (TransformerConfigurationException ex) {
            LOG.log(Level.INFO, "Cannot modify " + settings, ex);
        } catch (TransformerException ex) {
             LOG.log(Level.INFO, "Cannot modify " + settings, ex);
        }
    }

    static MavenUtilities getDefault() {
        return new MavenUtilities(FileUtilities.getUserSettingsFile(true));
    }

    private static String singleProfile(String name, String path) {
        return "    \n"
                + "      " + name + "\n"
                + "      \n"
                + "        \n"
                + "          !" + name + "\n"
                + "        \n"
                + "      \n"
                + "      \n"
                + "        <" + name + ">" + path + "\n"
                + "      \n"
                + "    \n";
    }

    private static String profileId(Node profile) {
        NodeList children = profile.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node ch = children.item(i);
            if (ch.getNodeName().equals("id")) {
                return ch.getTextContent();
            }
        }
        return null;
    }

    void writeNetBeansInstallation(String path) {
        writeProperty(NBDEFINITION, path);
    }

    String readNetBeansInstallation() {
        return readProperty(NBDEFINITION);
    }

    void writeMoeDevice(String id) {
        writeProperty(MOEDEFINITION, id);
    }

    String readMoeDevice() {
        return readProperty(MOEDEFINITION);
    }

    void writeRobovmDeviceName(String name) {
        writeProperty(ROBOVMDEFINITION, name);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy