View Javadoc

1   /*
2    * UML2MMBase module.
3    *
4    * The contents of this file are subject to the Mozilla Public License
5    * Version 1.0 (the "License"); you may not use this file except in
6    * compliance with the License. You may obtain a copy of the License at
7    * http://www.mozilla.org/MPL/
8    *
9    * Software distributed under the License is distributed on an "AS IS"
10   * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11   * License for the specific language governing rights and limitations
12   * under the License.
13   */
14  
15  package com.finalist.mmbase.uml;
16  
17  import org.xml.sax.SAXException;
18  
19  import javax.xml.transform.TransformerConfigurationException;
20  import javax.xml.transform.sax.SAXTransformerFactory;
21  import javax.xml.transform.sax.TransformerHandler;
22  import javax.xml.transform.stream.StreamResult;
23  import java.io.StringWriter;
24  
25  /***
26   *
27   * Helper classes for formatting the UML documentation.
28   *
29   * @author Rudie Ekkelenkamp - Finalist IT Group
30   * @version $Revision: 1.5 $, $Date: 2005/02/02 15:21:08 $
31   *
32   */
33  public class MMBaseDocumentationHelper {
34  
35     private MMBaseHelper model = null;
36  
37     /***  Default constructor. */
38     public MMBaseDocumentationHelper(MMBaseHelper model) {
39        this.model = model;
40     }
41  
42     /***
43      * Format the passed String as XML by putting it between a CDATA section.
44      *
45      * @param text Text to format as XML
46      * @return String in XML format
47      */
48     public String getXMLCdata(String text) {
49        String cdataStart = "<![CDATA[";
50        String cdataEnd = "]]>";
51        return cdataStart + text + cdataEnd;
52     }
53  
54     /***
55      * Convert the javadoc text to an XML format usable by MMBase
56      *
57      * @param doc Input UML documentation
58      * @return String formatted UML documentation as as XML
59      */
60     public String getXMLDocumentation(String doc) {
61        return getXML(model.javaDocToText(doc));
62     }
63  
64     /***
65      * Format the passed String as XML using a SAX Transformer.
66      * Under construction. The XML header is still returned.
67      * @param text Text to format as XML
68      * @return String in XML format
69      */
70     public String getXML(String text) {
71        if (text == null || text.equals("")) {
72           return "";
73        }
74        StringWriter writer = new StringWriter();
75        StreamResult streamResult = new StreamResult(writer);
76        SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
77  
78        // SAX2.0 ContentHandler. JDK1.4 is needed or another parser.
79        TransformerHandler hd = null;
80        try {
81           hd = tf.newTransformerHandler();
82        }
83        catch (TransformerConfigurationException e) {
84           e.printStackTrace();
85        }
86        hd.setResult(streamResult);
87        try {
88           hd.startDocument();
89           hd.characters(text.toCharArray(), 0, text.length());
90           hd.endDocument();
91        }
92        catch (SAXException e1) {
93           e1.printStackTrace();
94        }
95        writer.flush();
96        String result = writer.toString();
97        int index = -1;
98        // If a xml header is added. So check on it.
99        String xmlHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
100       index = result.indexOf(xmlHeader);
101       if (index != -1) {
102          result = result.substring(index + xmlHeader.length());
103       }
104 
105       return result;
106    }
107 }