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