Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * This file is part of lolin1dp-data-provider.
 * <p/>
 * lolin1dp-data-provider is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 * <p/>
 * lolin1dp-data-provider is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * <p/>
 * You should have received a copy of the GNU General Public License along with
 * lolin1dp-data-provider. If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;

import java.util.ArrayList;
import java.util.Iterator;
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.Attr;
import org.w3c.dom.Document;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import org.xml.sax.SAXException;

public class Main {
    /**
     * Adds a new node to a file.
     *
     * @param nodeType {@link String} The type of the element to add.
     * @param idField {@link String} The name of the field used to identify this
     * node.
     * @param nodeID {@link String} The identifier for this node, so its data
     * can be later retrieved and modified.
     * @param destFile {@link File} The file where the node must be added.
     * @param attributes {@link ArrayList} of array of String. The arrays must
     * be bidimensional (first index must contain attribute name, second one
     * attribute value). Otherwise, an error will be thrown. However, if
     * <value>null</value>, it is ignored.
     */
    public static void addNode(String nodeType, String idField, String nodeID, File destFile,
            ArrayList<String[]> attributes) {
        if (attributes != null) {
            for (Iterator<String[]> it = attributes.iterator(); it.hasNext();) {
                if (it.next().length != 2) {
                    throw new IllegalArgumentException("Invalid attribute combination");
                }
            }
        }
        /*
         * XML DATA CREATION - BEGINNING
         */
        DocumentBuilder docBuilder;
        Document doc;
        try {
            docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            doc = docBuilder.parse(destFile);
        } catch (SAXException | IOException | ParserConfigurationException ex) {
            return;
        }

        Node index = doc.getFirstChild(), newElement = doc.createElement(nodeType);
        NamedNodeMap elementAttributes = newElement.getAttributes();

        Attr attrID = doc.createAttribute(idField);
        attrID.setValue(nodeID);
        elementAttributes.setNamedItem(attrID);

        if (attributes != null) {
            for (Iterator<String[]> it = attributes.iterator(); it.hasNext();) {
                String[] x = it.next();
                Attr currAttr = doc.createAttribute(x[0]);
                currAttr.setValue(x[1]);
                elementAttributes.setNamedItem(currAttr);
            }
        }

        index.appendChild(newElement);
        /*
         * XML DATA CREATION - END
         */

        /*
         * XML DATA DUMP - BEGINNING
         */
        Transformer transformer;
        try {
            transformer = TransformerFactory.newInstance().newTransformer();
        } catch (TransformerConfigurationException ex) {
            return;
        }
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(doc);
        try {
            transformer.transform(source, result);
        } catch (TransformerException ex) {
            return;
        }

        String xmlString = result.getWriter().toString();
        try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(destFile))) {
            bufferedWriter.write(xmlString);
        } catch (IOException ex) {
        }
        /*
         * XML DATA DUMP - END
         */
    }
}