Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.ByteArrayInputStream;
import java.io.CharArrayWriter;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
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 org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import org.xml.sax.SAXException;

public class Main {
    private final static Charset CHARSET_UTF8 = Charset.forName("UTF-8");

    /**
     *
     * @param xmlContent
     * @param attributeName
     * @param matches
     * @return
     * @throws SAXException
     * @throws IOException
     * @throws ParserConfigurationException
     * @throws TransformerException
     */
    public static String addAttribute(String xmlContent, String attributeName, String... matches)
            throws SAXException, IOException, ParserConfigurationException, TransformerException {

        return addAttribute(xmlContent, CHARSET_UTF8, attributeName, matches);
    }

    /**
     *
     * @param xmlContent
     * @param charset
     * @param attributeName
     * @param matches
     * @return
     * @throws SAXException
     * @throws IOException
     * @throws ParserConfigurationException
     * @throws TransformerException
     */
    public static String addAttribute(String xmlContent, Charset charset, String attributeName, String... matches)
            throws SAXException, IOException, ParserConfigurationException, TransformerException {

        Document document = parse(xmlContent, charset);
        Element element = document.getDocumentElement();
        addAttributeLoopElement(element, attributeName, 1, matches);

        return write(document);
    }

    /**
     *
     * @param xmlContent
     * @param charset
     * @return
     * @throws SAXException
     * @throws IOException
     * @throws ParserConfigurationException
     */
    private static Document parse(String xmlContent, Charset charset)
            throws SAXException, IOException, ParserConfigurationException {

        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(false);
        documentBuilderFactory.setValidating(false);
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse(new ByteArrayInputStream(xmlContent.getBytes(charset)));

        return document;
    }

    /**
     *
     * @param element
     * @param attributeName
     * @param sequence
     * @param matches
     * @return
     */
    private static int addAttributeLoopElement(Element element, String attributeName, int sequence,
            String... matches) {

        int seqNo = sequence;
        if (matches(element.getTagName(), matches)) {
            String seq = "00000000000000000000" + seqNo++;
            seq = seq.substring(seq.length() - 20);
            element.setAttribute(attributeName, attributeName + "_" + seq);
        }
        NodeList nodeList = element.getChildNodes();
        int length = nodeList.getLength();
        for (int seq = 0; seq < length; seq++) {
            Node node = nodeList.item(seq);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                seqNo = addAttributeLoopElement((Element) node, attributeName, seqNo, matches);
            }
        }

        return seqNo;
    }

    /**
     *
     * @param document
     * @return
     * @throws TransformerException
     * @throws UnsupportedEncodingException
     */
    private static String write(Document document) throws TransformerException, UnsupportedEncodingException {

        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        Source source = new DOMSource(document);
        CharArrayWriter os = new CharArrayWriter();
        Result result = new StreamResult(os);
        transformer.transform(source, result);

        return os.toString();
    }

    /**
     *
     * @param tagName
     * @param matches
     * @return
     */
    private static boolean matches(String tagName, String... matches) {

        if (matches == null || matches.length == 0) {
            return true;
        }

        for (String matche : matches) {
            if (tagName.matches(matche)) {
                return true;
            }
        }

        return false;
    }
}