XMLDocument.java

    1/*******************************************************************************
    2 * Copyright (c) 2009 Mountainminds GmbH & Co. KG and others
    3 * All rights reserved. This program and the accompanying materials
    4 * are made available under the terms of the Eclipse Public License v1.0
    5 * which accompanies this distribution, and is available at
    6 * http://www.eclipse.org/legal/epl-v10.html
    7 *
    8 * Contributors:
    9 *    Marc R. Hoffmann - initial API and implementation
   10 *    
   11 * $Id: $
   12 *******************************************************************************/
   13package org.jacoco.report.xml;
   14
   15import static java.lang.String.format;
   16
   17import java.io.IOException;
   18import java.io.OutputStream;
   19import java.io.OutputStreamWriter;
   20import java.io.Writer;
   21
   22/**
   23 * Root element of an XML document. Each instance represents a separate output
   24 * document.
   25 * 
   26 * @see XMLElement
   27 * @author Marc R. Hoffmann
   28 * @version $Revision: $
   29 */
   30public class XMLDocument extends XMLElement {
   31
   32    /** XML header template */
   33    private static final String HEADER = "<?xml version=\"1.0\" encoding=\"%s\"?>";
   34
   35    /** XML header template for standalone documents */
   36    private static final String HEADER_STANDALONE = "<?xml version=\"1.0\" encoding=\"%s\" standalone=\"yes\"?>";
   37
   38    /** DOCTYPE declaration template */
   39    private static final String DOCTYPE = "<!DOCTYPE %s PUBLIC \"%s\" \"%s\">";
   40
   41    /**
   42     * Writes a new document to the given writer. The document might contain a
   43     * document type declaration.
   44     * 
   45     * @param rootnode
   46     *            name of the root node
   47     * @param pubId
   48     *            optional doctype identifier or <code>null</code>
   49     * @param system
   50     *            system reference, required if doctype is given
   51     * @param encoding
   52     *            encoding that will be specified in the header
   53     * @param standalone
   54     *            <code>true</code> if this is a standalone document
   55     * @param writer
   56     *            writer for content output
   57     * @throws IOException
   58     *             in case of problems with the writer
   59     */
   60    public XMLDocument(final String rootnode, final String pubId,
   61            final String system, final String encoding,
   62            final boolean standalone, final Writer writer) throws IOException {
   63        super(writer, rootnode);
   64        writeHeader(rootnode, pubId, system, encoding, standalone, writer);
   65        beginOpenTag();
   66    }
   67
   68    /**
   69     * Writes a new document to the given binary stream. The document might
   70     * contain a document type declaration.
   71     * 
   72     * @param rootnode
   73     *            name of the root node
   74     * @param pubId
   75     *            optional doctype identifier or <code>null</code>
   76     * @param system
   77     *            system reference, required if doctype is given
   78     * @param encoding
   79     *            encoding of the XML document
   80     * @param standalone
   81     *            <code>true</code> if this is a standalone document
   82     * @param output
   83     *            output for content output
   84     * @throws IOException
   85     *             in case of problems with the writer
   86     */
   87    public XMLDocument(final String rootnode, final String pubId,
   88            final String system, final String encoding,
   89            final boolean standalone, final OutputStream output)
   90            throws IOException {
   91        this(rootnode, pubId, system, encoding, standalone,
   92                new OutputStreamWriter(output, encoding));
   93    }
   94
   95    @Override
   96    public void close() throws IOException {
   97        super.close();
   98        writer.close();
   99    }
  100
  101    private static void writeHeader(final String rootnode, final String pubId,
  102            final String system, final String encoding,
  103            final boolean standalone, final Writer writer) throws IOException {
  104        if (standalone) {
  105            writer.write(format(HEADER_STANDALONE, encoding));
  106        } else {
  107            writer.write(format(HEADER, encoding));
  108        }
  109        if (pubId != null) {
  110            writer.write(format(DOCTYPE, rootnode, pubId, system));
  111        }
  112    }
  113
  114}