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 updateDBConfig(String userName, String password, String name, String driver, String url,
            String dbConfigPath) throws XPathExpressionException, ParserConfigurationException, SAXException,
            IOException, TransformerException {
        Document document = loadXML(dbConfigPath);

        XPath path = XPathFactory.newInstance().newXPath();
        XPathExpression express = path.compile(
                "//Configure/New[@class='org.eclipse.jetty.plus.jndi.Resource']/Arg/New[@class='bitronix.tm.resource.jdbc.PoolingDataSource']/Set[@name='className']");

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

        express = path.compile(
                "//Configure/New[@class='org.eclipse.jetty.plus.jndi.Resource']/Arg/New[@class='bitronix.tm.resource.jdbc.PoolingDataSource']/Get['@name=driverProperties']/Put");
        nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET);

        for (int i = 0; i < nodes.getLength(); i++) {
            node = nodes.item(i);
            if ("user".equals(node.getAttributes().item(0).getTextContent())) {
                node.setTextContent(userName);
            } else if ("password".equals(node.getAttributes().item(0).getTextContent())) {
                node.setTextContent(password);
            } else if ("URL".equals(node.getAttributes().item(0).getTextContent())) {
                //            String url = node.getTextContent();
                //            System.out.println("basic url ---> " + url + "; name -->" + name);
                //            url = replaceDBName("examples", name, url);
                //            System.out.println("dist url ---> " + url);
                node.setTextContent(url);
            }
        }
        path.reset();

        return updateXML(document, dbConfigPath);
    }

    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;
    }
}