java: simple XML output module
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 10 Sep 2016 23:22:29 +0200
changeset 234ad40aaed4bb
parent 22 91e52083f255
child 24 a1f501ac1fad
java: simple XML output module
java/parameter-lister/src/info/glogalcode/parameterLister/modules/XmlModule.java
     1.1 --- a/java/parameter-lister/src/info/glogalcode/parameterLister/modules/XmlModule.java	Sat Sep 10 23:08:58 2016 +0200
     1.2 +++ b/java/parameter-lister/src/info/glogalcode/parameterLister/modules/XmlModule.java	Sat Sep 10 23:22:29 2016 +0200
     1.3 @@ -7,6 +7,8 @@
     1.4  import java.util.List;
     1.5  
     1.6  /**
     1.7 + * Just a simple XML output.
     1.8 + * For more complex documents, use {@linkplain javax.xml.stream.XMLStreamWriter}
     1.9   *
    1.10   * @author Ing. František Kučera (frantovo.cz)
    1.11   */
    1.12 @@ -15,12 +17,24 @@
    1.13  	@Override
    1.14  	public void process(OutputStream output, List<String> parameters) throws OutputModuleException {
    1.15  		try (PrintWriter out = new PrintWriter(output)) {
    1.16 +			out.println("<parameters>");
    1.17  			for (String parameter : parameters) {
    1.18 -				out.print("xml> ");
    1.19 -				out.println(parameter);
    1.20 +				out.print("\t<parameter>");
    1.21 +				out.print(escapeXmlText(parameter));
    1.22 +				out.println("</parameter>");
    1.23  				out.flush();
    1.24  			}
    1.25 +			out.println("</parameters>");
    1.26  		}
    1.27  	}
    1.28  
    1.29 +	/**
    1.30 +	 * @param s original text
    1.31 +	 * @return escaped text that can be safely concatenated between two XML tags (not safe for
    1.32 +	 * attribute
    1.33 +	 * values)
    1.34 +	 */
    1.35 +	private static String escapeXmlText(String s) {
    1.36 +		return s.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
    1.37 +	}
    1.38  }