Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.IOException;
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.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Main {
    public static boolean updatePomDBConfig(String userName, String password, String name, String host, String port,
            String version, String pomPath) throws ParserConfigurationException, SAXException, IOException,
            XPathExpressionException, TransformerException {
        Document document = loadXML(pomPath);
        XPath path = XPathFactory.newInstance().newXPath();
        XPathExpression express = path.compile("//project/properties/mysql.server.version");

        NodeList nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET);
        Node node = nodes.item(0);
        node.setTextContent(version);
        path.reset();

        express = path.compile("//project/properties/mysql.server.host");
        nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET);
        node = nodes.item(0);
        node.setTextContent(host);
        path.reset();

        express = path.compile("//project/properties/mysql.server.port");
        nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET);
        node = nodes.item(0);
        node.setTextContent(port);
        path.reset();

        express = path.compile("//project/properties/mysql.server.database");
        nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET);
        node = nodes.item(0);
        node.setTextContent(name);
        path.reset();

        express = path.compile("//project/properties/mysql.server.user");
        nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET);
        node = nodes.item(0);
        node.setTextContent(userName);
        path.reset();

        express = path.compile("//project/properties/mysql.server.password");
        nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET);
        node = nodes.item(0);
        node.setTextContent(password);
        path.reset();

        return updateXML(document, pomPath);
    }

    private static Document loadXML(String path) throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new File(path));
        document.normalize();

        return document;
    }

    private static boolean updateXML(Document document, String path) throws TransformerException {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();

        // out put encoding.
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        DocumentType type = document.getDoctype();
        if (type != null) {
            System.out.println("doctype -->" + type.getPublicId());
            transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, type.getPublicId());
            transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, type.getSystemId());
        }

        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(new File(path));
        transformer.transform(source, result);

        transformer.reset();
        return true;
    }
}