1 /*
2  * Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 /*
26  * COMPONENT_NAME: idl.parser
27  *
28  * ORIGINS: 27
29  *
30  * Licensed Materials - Property of IBM
31  * 5639-D57 (C) COPYRIGHT International Business Machines Corp. 1997, 1999
32  * RMI-IIOP v1.0
33  *
34  */
35 
36 package com.sun.tools.corba.se.idl;
37 
38 // NOTES:
39 
40 import java.io.PrintWriter;
41 import java.io.IOException;
42 import java.util.StringTokenizer;
43 
44 public class Comment
45 {
46   // Styles
47   static final int UNKNOWN  = -1;
48   static final int JAVA_DOC =  0;
49   static final int C_BLOCK  =  1;
50   static final int CPP_LINE =  2;
51 
52   // System-dependent line separator
53   private static String _eol = System.getProperty ("line.separator");
54 
55   private String _text  = new String ("");
56   private int    _style = UNKNOWN;
57 
Comment()58   Comment () {_text = new String (""); _style = UNKNOWN;} // ctor
59 
Comment(String text)60   Comment (String text) {_text = text; _style = style (_text);} // ctor
61 
62   /** Sets comment text */
text(String string)63   public void text (String string) {_text = string; _style = style (_text);}
64 
65   /** Returns comment text */
text()66   public String text () {return _text;}
67 
68   /** Returns the comment style of a string. */
style(String text)69   private int style (String text)
70   {
71     if (text == null)
72       return UNKNOWN;
73     else if (text.startsWith ("/**") && text.endsWith ("*/"))
74       return JAVA_DOC;
75     else if (text.startsWith ("/*") && text.endsWith ("*/"))
76       return C_BLOCK;
77     else if (text.startsWith ("//"))
78       return CPP_LINE;
79     else
80       return UNKNOWN;
81   } // style
82 
83   /** Writes comment text to standard output (debug). */
write()84   public void write () {System.out.println (_text);}
85 
86   /** Writes comment text to the specified print stream in the appropriate format. */
generate(String indent, PrintWriter printStream)87   public void generate (String indent, PrintWriter printStream)
88   {
89     if (_text == null || printStream == null)
90       return;
91     if (indent == null)
92       indent = new String ("");
93     switch (_style)
94     {
95       case JAVA_DOC:
96         //printJavaDoc (indent, printStream);
97         print (indent, printStream);
98         break;
99       case C_BLOCK:
100         //printCBlock (indent, printStream);
101         print (indent, printStream);
102         break;
103       case CPP_LINE:
104         //printCppLine (indent, printStream);
105         print (indent, printStream);
106         break;
107       default:
108         break;
109     }
110   } // generate
111 
112   /** Writes comment to the specified print stream without altering its format.
113       This routine does not alter vertical or horizontal spacing of comment text,
114       thus, it only works well for comments with a non-indented first line. */
print(String indent, PrintWriter stream)115   private void print (String indent, PrintWriter stream)
116   {
117     String text = _text.trim () + _eol;
118     String line = null;
119 
120     int iLineStart = 0;
121     int iLineEnd   = text.indexOf (_eol);
122     int iTextEnd   = text.length () - 1;
123 
124     stream.println ();
125     while (iLineStart < iTextEnd)
126     {
127       line = text.substring (iLineStart, iLineEnd);
128       stream.println (indent + line);
129       iLineStart = iLineEnd + _eol.length ();
130       iLineEnd = iLineStart + text.substring (iLineStart).indexOf (_eol);
131     }
132   } // print
133 
134   /*
135    *  The following routines print formatted comments of differing styles.
136    *  Each routine will alter the horizontal spacing of the comment text,
137    *  but not the vertical spacing.
138    */
139 
140   /** Writes comment in JavaDoc-style to the specified print stream. */
printJavaDoc(String indent, PrintWriter stream)141   private void printJavaDoc (String indent, PrintWriter stream)
142   {
143     // Strip surrounding "/**", "*/", and whitespace; append sentinel
144     String text = _text.substring (3, (_text.length () - 2)).trim () + _eol;
145     String line = null;
146 
147     int iLineStart = 0;
148     int iLineEnd   = text.indexOf (_eol);
149     int iTextEnd   = text.length () - 1;   // index of last text character
150 
151     stream.println (_eol + indent + "/**");
152     while (iLineStart < iTextEnd)
153     {
154       line = text.substring (iLineStart, iLineEnd).trim ();
155       if (line.startsWith ("*"))
156         // Strip existing "*<ws>" prefix
157         stream.println (indent + " * " + line.substring (1, line.length ()).trim ());
158       else
159         stream.println (indent + " * " + line);
160       iLineStart = iLineEnd + _eol.length ();
161       iLineEnd = iLineStart + text.substring (iLineStart).indexOf (_eol);
162     }
163     stream.println (indent + " */");
164   } // printJavaDoc
165 
166   /** Writes comment in c-block-style to the specified print stream. */
printCBlock(String indent, PrintWriter stream)167   private void printCBlock (String indent, PrintWriter stream)
168   {
169     // Strip surrounding "/*", "*/", and whitespace; append sentinel
170     String text = _text.substring (2, (_text.length () - 2)).trim () + _eol;
171     String line = null;
172 
173     int iLineStart = 0;
174     int iLineEnd   = text.indexOf (_eol);
175     int iTextEnd   = text.length () - 1;   // index of last text character
176 
177     stream.println (indent + "/*");
178     while (iLineStart < iTextEnd)
179     {
180       line = text.substring (iLineStart, iLineEnd).trim ();
181       if (line.startsWith ("*"))
182         // Strip existing "*[ws]" prefix
183         stream.println (indent + " * " + line.substring (1, line.length ()).trim ());
184       else
185         stream.println (indent + " * " + line);
186       iLineStart = iLineEnd + _eol.length ();
187       iLineEnd   = iLineStart + text.substring (iLineStart).indexOf (_eol);
188     }
189     stream.println (indent + " */");
190   } // printCBlock
191 
192   /** Writes a line comment to the specified print stream. */
printCppLine(String indent, PrintWriter stream)193   private void printCppLine (String indent, PrintWriter stream)
194   {
195     stream.println (indent + "//");
196     // Strip "//[ws]" prefix
197     stream.println (indent + "// " + _text.substring (2).trim ());
198     stream.println (indent + "//");
199   } // printCppLine
200 } // class Comment
201 
202 
203 /*==================================================================================
204   DATE<AUTHOR>   ACTION
205   ----------------------------------------------------------------------------------
206   11aug1997<daz> Initial version completed.
207   18aug1997<daz> Modified generate to write comment unformatted.
208   ==================================================================================*/
209