View Javadoc

1   /*
2    * Copyright (c) 2002 Peter Antman, Teknik i Media  <peter.antman@tim.se>
3    *
4    * $Id: XMLEscaper.java,v 1.1.1.1 2004/05/19 12:07:30 pra Exp $
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version
10   * 
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 &lt;pre&gt;&lt;/pre&gt; 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 &lt;pre&gt;&lt;/pre&gt; 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 &lt;pre&gt;&lt;/pre&gt; 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           } // end of if ()
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           } // end of if ()
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        } // end of try-catch
88        
89  
90     }
91  } // XMLEscaper