Java XML Document Create createDocument(boolean createRoot)

Here you can find the source of createDocument(boolean createRoot)

Description

Create and return an empty XML Document.

License

Open Source License

Declaration

public static Document createDocument(boolean createRoot) 

Method Source Code

//package com.java2s;
/*/*from  w  ww.  j  av  a 2s.c  om*/
 * Copyright 2010, 2011 Institut Pasteur.
 * 
 * This file is part of ICY.
 * 
 * ICY 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.
 * 
 * ICY 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.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ICY. If not, see <http://www.gnu.org/licenses/>.
 */

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.TransformerFactory;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

public class Main {
    public static final String NODE_ROOT_NAME = "root";
    private static DocumentBuilderFactory docBuilderFactory = null;
    private static DocumentBuilder docBuilder = null;
    private static TransformerFactory transformerFactory = null;
    private static Transformer transformer = null;

    /**
     * Create and return an empty XML Document.
     */
    public static Document createDocument(boolean createRoot) {
        init();

        if (docBuilder != null) {
            final Document result;

            synchronized (docBuilder) {
                // create document
                result = docBuilder.newDocument();
            }

            // add default "root" element if wanted
            if (createRoot)
                createRootElement(result);

            return result;
        }

        return null;
    }

    private static synchronized void init() {
        // initialize static builder
        if (docBuilder == null)
            docBuilder = createDocumentBuilder();
        // initialize static transformer
        if (transformer == null)
            transformer = createTransformer();
    }

    /**
     * Create root element for specified document if it does not already exist and return it
     */
    public static Element createRootElement(Document doc) {
        return createRootElement(doc, NODE_ROOT_NAME);
    }

    /**
     * Create root element for specified document if it does not already exist and return it
     */
    public static Element createRootElement(Document doc, String name) {
        return getRootElement(doc, true, name);
    }

    /**
     * Create and returns a new DocumentBuilder.
     */
    public static DocumentBuilder createDocumentBuilder() {
        initFactories();

        try {
            return docBuilderFactory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            return null;
        }
    }

    /**
     * Create and returns a new Transformer.
     */
    public static Transformer createTransformer() {
        initFactories();

        final Transformer result;

        try {
            result = transformerFactory.newTransformer();
        } catch (TransformerConfigurationException e) {
            return null;
        }

        result.setOutputProperty(OutputKeys.METHOD, "xml");
        result.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
        result.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        result.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        result.setOutputProperty(OutputKeys.INDENT, "yes");

        return result;
    }

    /**
     * Return the root element for specified document<br>
     * Create if it does not already exist with the specified name
     */
    private static Element getRootElement(Document doc, boolean create, String name) {
        if (doc != null) {
            Element result = doc.getDocumentElement();

            if ((result == null) && create) {
                result = doc.createElement(name);
                doc.appendChild(result);
            }

            return result;
        }

        return null;
    }

    /**
     * Return the root element for specified document<br>
     * Create if it does not already exist with the default {@link #NODE_ROOT_NAME}
     */
    public static Element getRootElement(Document doc, boolean create) {
        return getRootElement(doc, create, NODE_ROOT_NAME);
    }

    /**
     * Return the root element for specified document (null if not found)<br>
     */
    public static Element getRootElement(Document doc) {
        return getRootElement(doc, false);
    }

    private static synchronized void initFactories() {
        // initialize static factories
        if (docBuilderFactory == null)
            docBuilderFactory = DocumentBuilderFactory.newInstance();
        if (transformerFactory == null)
            transformerFactory = TransformerFactory.newInstance();
    }
}

Related

  1. createDocument()
  2. createDocument()
  3. createDocument()
  4. createDocument()
  5. createDocument()
  6. createDocument(boolean isSecureProcessing)
  7. createDocument(boolean validating, boolean namespaceAware)
  8. createDocument(byte[] data)
  9. createDocument(File file)