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