1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.backsource.utils.javadoc;
21 import java.io.InputStreamReader;
22 import java.io.StringWriter;
23
24 import org.w3c.dom.Element;
25 import org.backsource.utils.xml.DocumentUtil;
26 import org.backsource.utils.xml.ElementUtil;
27 import org.backsource.utils.io.IOHelper;
28 /***
29 * <p>Simple utility to help escape XML fragments one wants to put into javadoc comments.
30 *
31 * <p>When run as main may be given either a string argument consisting of the xml to escape, or with <b>-</b> wich will make it read in the data from stdin, to finish press ctr-d. <p>The outputed data will be placed in <pre></pre> tags.
32 *
33 *
34 * @author <a href="mailto:pra@tim.se">Peter Antman</a>
35 * @version $Revision: 1.1.1.1 $
36 */
37
38 public class XMLEscaper{
39 static final String PRE_XML ="<pre></pre>";
40
41 public XMLEscaper (){
42
43 }
44
45 /***
46 * Escape xml string so its possible to use as element content, and in
47 * a javadoc comment.
48 *
49 * The returned XML will be placed inside <pre></pre> element.
50 */
51 public static String escape(String xml) throws Exception{
52 Element el = DocumentUtil.getDocumentElement(PRE_XML);
53 ElementUtil.setContent(el, xml);
54 return DocumentUtil.getXml(el);
55 }
56
57 public static String readInput() throws Exception {
58 InputStreamReader r = new InputStreamReader(System.in);
59 StringWriter wr = new StringWriter();
60 IOHelper.connect(r,wr);
61 return wr.toString();
62 }
63
64 /***
65 *<p> Escape XML fragments one wants to put into javadoc comments.
66 *
67 *<p> may be given either a string argument consisting of the xml to escape, or with <b>-</b> wich will make it read in the data from stdin, to finish press ctr-d. <p>The outputed data will be placed in <pre></pre> tags.
68 */
69 public static void main(String[] args){
70 try {
71 if ( args.length <1) {
72 System.out.println("Usage: XMLEscaper [xmldata | -]");
73 System.exit(0);
74 }
75
76 String data = args[0];
77 if ( "-".equals(data)) {
78 System.out.println("Reading data from ins: type your XML finish by ctr-d");
79 data = readInput();
80 }
81
82
83 System.out.println(escape(data));
84 } catch (Exception e) {
85 System.err.println("Error while escaping XML "+e);
86 e.printStackTrace();
87 }
88
89
90 }
91 }