View Javadoc
1   /**
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Intalio, Inc.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Intalio, Inc. Exolab is a registered
23   *    trademark of Intalio, Inc.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 1999-2003 (C) Intalio, Inc. All Rights Reserved.
42   */
43  package org.exolab.javasource;
44  
45  import java.util.Enumeration;
46  import java.util.Vector;
47  
48  /**
49   * A class that "SOMEWHAT" represents a JavaDoc Comment.
50   *
51   * @author <a href="mailto:keith AT kvisco DOT com">Keith Visco</a>
52   * @version $Revision$ $Date: 2005-02-26 17:30:28 -0700 (Sat, 26 Feb 2005) $
53   */
54  public final class JDocComment {
55      //--------------------------------------------------------------------------
56  
57      /** An ordered list of descriptors. */
58      private Vector<JDocDescriptor> _descriptors = null;
59      
60      /** The internal buffer for this JDocComment. */
61      private StringBuffer _comment     = null;
62  
63      //--------------------------------------------------------------------------
64  
65      /**
66       * Creates a new JavaDoc Comment.
67       */
68      public JDocComment() {
69          super();
70          
71          _descriptors = new Vector<JDocDescriptor>();
72          _comment     = new StringBuffer();
73      }
74  
75      /**
76       * Creates a new JavaDoc Comment and initializie it with given JDocDescriptor.
77       * 
78       * @param jdesc The JDocDescriptor to add.
79       */
80      public JDocComment(final JDocDescriptor jdesc) {
81          super();
82          
83          _descriptors = new Vector<JDocDescriptor>();
84          _comment = new StringBuffer();
85          
86          addDescriptor(jdesc);
87      }
88  
89      //--------------------------------------------------------------------------
90  
91      /**
92       * Adds the given JDocDescriptor to this JDocComment.
93       *
94       * @param jdesc The JDocDescriptor to add.
95       */
96      public void addDescriptor(final JDocDescriptor jdesc) {
97          if (jdesc == null) { return; }
98          //-- on the fly sorting of descriptors
99          if (_descriptors.size() == 0) {
100             _descriptors.addElement(jdesc);
101             return;
102         }
103 
104         for (int i = 0; i < _descriptors.size(); i++) {
105             JDocDescriptor jdd = _descriptors.elementAt(i);
106 
107             short compare = jdesc.compareTo(jdd);
108 
109             switch (compare) {
110                 case  0: // equal
111                     _descriptors.insertElementAt(jdesc, i + 1);
112                     return;
113                 case -1: //-- less than
114                     _descriptors.insertElementAt(jdesc, i);
115                     return;
116                 default:
117                     //-- keep looking
118                     break;
119             }
120         }
121 
122         //-- if we make it here we need to add
123         _descriptors.addElement(jdesc);
124     }
125 
126     /**
127      * Appends the provided comment String to this JDocComment.
128      *
129      * @param comment The comment to append.
130      */
131     public void appendComment(final String comment) {
132         _comment.append(comment);
133     }
134 
135     /**
136      * Returns the String value of this JDocComment.
137      *
138      * @return The String value of this JDocComment.
139      */
140     public String getComment() {
141         return _comment.toString();
142     }
143 
144     /**
145      * Sets the comment String of this JDocComment.
146      *
147      * @param comment The comment String of this JDocComment.
148      */
149     public void setComment(final String comment) {
150         _comment.setLength(0);
151         _comment.append(comment);
152     }
153 
154     /**
155      * Returns an Enumeration of the parameters of this JDocComment.
156      *
157      * @return An Enumeration of the parameters of this JDocComment.
158      */
159     public Enumeration<JDocDescriptor> getDescriptors() {
160         return _descriptors.elements();
161     }
162 
163     /**
164      * Returns the length of the JavaDoc comment in characters.
165      *
166      * @return The length of the JavaDoc comment in characters.
167      */
168     public int getLength() {
169         return _comment.length();
170     }
171 
172     /**
173      * Returns the Parameter Descriptor associated with the given name.
174      *
175      * @param name The name whose ParamDescriptor is being searched for.
176      * @return the Parameter Descriptor associated with the given name.
177      */
178     public JDocDescriptor getParamDescriptor(final String name) {
179         if (name == null) { return null; }
180 
181         for (int i = 0; i < _descriptors.size(); i++) {
182             JDocDescriptor jdd
183                 = _descriptors.elementAt(i);
184             if (jdd.getType() == JDocDescriptor.PARAM) {
185                 if (name.equals(jdd.getName())) { return jdd; }
186             }
187         }
188         return null;
189     }
190 
191     //--------------------------------------------------------------------------
192 
193     /**
194      * Prints this JavaDoc comment using the given JSourceWriter.
195      *
196      * @param jsw The JSourceWriter to print to.
197      */
198     public void print(final JSourceWriter jsw) {
199         //-- I reuse JComment for printing
200         JComment jComment = new JComment(JComment.JAVADOC_STYLE);
201 
202         jComment.setComment(_comment.toString());
203 
204         //-- force a separating "*" for readability
205         if (_descriptors.size() > 0) {
206             jComment.appendComment("\n");
207         }
208 
209         for (int i = 0; i < _descriptors.size(); i++) {
210             jComment.appendComment("\n");
211             jComment.appendComment(_descriptors.elementAt(i).toString());
212         }
213         jComment.print(jsw);
214     }
215 
216     /**
217      * {@inheritDoc}
218      */
219     public String toString() {
220         StringBuilder sb = new StringBuilder();
221         sb.append("/**\n");
222         sb.append(" * ");
223         sb.append(" */\n");
224         return sb.toString();
225     }
226 
227     //--------------------------------------------------------------------------
228 }