java/parameter-lister/src/info/glogalcode/parameterLister/modules/XmlModule.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 11 Sep 2016 19:52:26 +0200
changeset 28 bfef9f34e438
parent 23 4ad40aaed4bb
permissions -rw-r--r--
license: GNU GPL v3
franta-hg@28
     1
/**
franta-hg@28
     2
 * parameter-lister
franta-hg@28
     3
 * Copyright © 2015 František Kučera (frantovo.cz)
franta-hg@28
     4
 *
franta-hg@28
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@28
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@28
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@28
     8
 * (at your option) any later version.
franta-hg@28
     9
 *
franta-hg@28
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@28
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@28
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@28
    13
 * GNU General Public License for more details.
franta-hg@28
    14
 *
franta-hg@28
    15
 * You should have received a copy of the GNU General Public License
franta-hg@28
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@28
    17
 */
franta-hg@21
    18
package info.glogalcode.parameterLister.modules;
franta-hg@21
    19
franta-hg@21
    20
import info.glogalcode.parameterLister.OutputModule;
franta-hg@21
    21
import info.glogalcode.parameterLister.OutputModuleException;
franta-hg@21
    22
import java.io.OutputStream;
franta-hg@21
    23
import java.io.PrintWriter;
franta-hg@21
    24
import java.util.List;
franta-hg@21
    25
franta-hg@21
    26
/**
franta-hg@23
    27
 * Just a simple XML output.
franta-hg@23
    28
 * For more complex documents, use {@linkplain javax.xml.stream.XMLStreamWriter}
franta-hg@21
    29
 *
franta-hg@21
    30
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@21
    31
 */
franta-hg@22
    32
public class XmlModule implements OutputModule {
franta-hg@21
    33
franta-hg@21
    34
	@Override
franta-hg@21
    35
	public void process(OutputStream output, List<String> parameters) throws OutputModuleException {
franta-hg@21
    36
		try (PrintWriter out = new PrintWriter(output)) {
franta-hg@23
    37
			out.println("<parameters>");
franta-hg@21
    38
			for (String parameter : parameters) {
franta-hg@23
    39
				out.print("\t<parameter>");
franta-hg@23
    40
				out.print(escapeXmlText(parameter));
franta-hg@23
    41
				out.println("</parameter>");
franta-hg@21
    42
				out.flush();
franta-hg@21
    43
			}
franta-hg@23
    44
			out.println("</parameters>");
franta-hg@21
    45
		}
franta-hg@21
    46
	}
franta-hg@21
    47
franta-hg@23
    48
	/**
franta-hg@23
    49
	 * @param s original text
franta-hg@23
    50
	 * @return escaped text that can be safely concatenated between two XML tags (not safe for
franta-hg@23
    51
	 * attribute
franta-hg@23
    52
	 * values)
franta-hg@23
    53
	 */
franta-hg@23
    54
	private static String escapeXmlText(String s) {
franta-hg@23
    55
		return s.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
franta-hg@23
    56
	}
franta-hg@21
    57
}