KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > graph > Comment


1 /* ============================================================
2  * JRobin : Pure java implementation of RRDTool's functionality
3  * ============================================================
4  *
5  * Project Info: http://www.jrobin.org
6  * Project Lead: Sasa Markovic (saxon@jrobin.org)
7  *
8  * Developers: Sasa Markovic (saxon@jrobin.org)
9  * Arne Vandamme (cobralord@jrobin.org)
10  *
11  * (C) Copyright 2003, by Sasa Markovic.
12  *
13  * This library is free software; you can redistribute it and/or modify it under the terms
14  * of the GNU Lesser General Public License as published by the Free Software Foundation;
15  * either version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  * See the GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License along with this
22  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */

25 package org.jrobin.graph;
26
27 import java.util.Vector JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 import org.jrobin.core.RrdException;
31 import org.jrobin.core.XmlWriter;
32
33 /**
34  * <p>Represent a piece of aligned text to be drawn on the graph.</p>
35  *
36  * @author Arne Vandamme (cobralord@jrobin.org)
37  */

38 class Comment
39 {
40     // ================================================================
41
// -- Members
42
// ================================================================
43
protected static final int CMT_DEFAULT = 0;
44     protected static final int CMT_LEGEND = 1;
45     protected static final int CMT_GPRINT = 2;
46     protected static final int CMT_NOLEGEND = 3;
47     
48     protected static final Byte JavaDoc TKN_ALF = new Byte JavaDoc( (byte) 1); // Align left with Linefeed
49
protected static final Byte JavaDoc TKN_ARF = new Byte JavaDoc( (byte) 2); // Align right with linefeed
50
protected static final Byte JavaDoc TKN_ACF = new Byte JavaDoc( (byte) 3); // Align center with linefeed
51
protected static final Byte JavaDoc TKN_AL = new Byte JavaDoc( (byte) 4); // Align right no linefeed
52
protected static final Byte JavaDoc TKN_AR = new Byte JavaDoc( (byte) 5); // Align left no linefeed
53
protected static final Byte JavaDoc TKN_AC = new Byte JavaDoc( (byte) 6); // Align center no linefeed
54
protected static final Byte JavaDoc TKN_NULL = null;
55     
56     protected int lineCount = 0;
57     protected boolean endLf = false;
58     protected boolean addSpacer = true;
59     protected boolean trimString = false;
60     protected int commentType = CMT_DEFAULT;
61     protected Byte JavaDoc lfToken = TKN_ALF;
62     
63     protected String JavaDoc text;
64     protected ArrayList JavaDoc oList = new ArrayList JavaDoc(3);
65
66
67     // ================================================================
68
// -- Constructors
69
// ================================================================
70
Comment( ) {
71     }
72     
73     /**
74      * Constructs a <code>Comment</code> object of a given text string.
75      * The original text string is parsed into new string/token pairs
76      * where byte tokens are used to specify alignment markers.
77      * @param text Text with alignment/new-line tokens as a single string.
78      * @throws RrdException Thrown in case of a JRobin specific error.
79      */

80     Comment( String JavaDoc text ) throws RrdException
81     {
82         this.text = text;
83         
84         if ( text != null )
85             parseComment();
86     }
87
88
89     // ================================================================
90
// -- Protected methods
91
// ================================================================
92
/**
93      * Splits the string up in string/token pairs.
94      * The tokens specify alignment or new-lines.
95      * @throws RrdException Thrown in case of a JRobin specific error.
96      */

97     void parseComment() throws RrdException
98     {
99         // Get off the last token to see for spacer suppressing
100
String JavaDoc text = this.text;
101         
102         int mpos = text.indexOf("@g");
103         if ( mpos >= 0 && mpos == (text.length() - 2) ) {
104             addSpacer = false;
105             trimString = true;
106             text = text.substring( 0, text.length() - 2);
107         }
108         else {
109             mpos = text.indexOf("@G");
110             if ( mpos >= 0 && mpos == (text.length() - 2) ) {
111                 addSpacer = false;
112                 trimString = false;
113                 text = text.substring( 0, text.length() - 2);
114             }
115         }
116         
117         // @l and \n are the same
118
Byte JavaDoc tkn;
119         int lastPos = 0;
120         mpos = text.indexOf("@");
121         int lfpos = text.indexOf("\n");
122         if ( mpos == text.length() ) mpos = -1;
123         if ( lfpos == text.length() ) lfpos = -1;
124     
125         while ( mpos >= 0 || lfpos >= 0 )
126         {
127             if ( mpos >= 0 && lfpos >= 0 )
128             {
129                 if ( mpos < lfpos )
130                 {
131                     tkn = getToken( text.charAt(mpos + 1) );
132                     if ( tkn != TKN_NULL ) {
133                         oList.add( text.substring(lastPos, mpos) );
134                         oList.add( tkn );
135                         lastPos = mpos + 2;
136                         mpos = text.indexOf("@", lastPos);
137                     }
138                     else {
139                         mpos = text.indexOf("@", mpos + 1);
140                     }
141                 }
142                 else
143                 {
144                     oList.add( text.substring(lastPos, lfpos) );
145                     oList.add( lfToken );
146                     endLf = true;
147                     lineCount++;
148                     lastPos = lfpos + 1;
149                     lfpos = text.indexOf("\n", lastPos);
150                 }
151             }
152             else if ( mpos >= 0 )
153             {
154                 tkn = getToken( text.charAt(mpos + 1) );
155                 if ( tkn != TKN_NULL ) {
156                     oList.add( text.substring(lastPos, mpos) );
157                     oList.add( tkn );
158                     lastPos = mpos + 2;
159                     mpos = text.indexOf("@", lastPos);
160                 }
161                 else
162                     mpos = text.indexOf("@", mpos + 1);
163             }
164             else
165             {
166                 oList.add( text.substring(lastPos, lfpos) );
167                 oList.add( lfToken );
168                 endLf = true;
169                 lineCount++;
170                 lastPos = lfpos + 1;
171                 lfpos = text.indexOf("\n", lastPos);
172             }
173         
174             // Check if the 'next token', isn't at end of string
175
if ( mpos == text.length() ) mpos = -1;
176             if ( lfpos == text.length() ) lfpos = -1;
177         }
178     
179         // Add last part of the string if necessary
180
if ( lastPos < text.length() )
181         {
182             oList.add( text.substring(lastPos) );
183             oList.add( TKN_NULL );
184         }
185     }
186     
187     /**
188      * Retrieves the corresponding token-byte for a given token character.
189      * @param tokenChar Character to retrieve corresponding bytevalue of.
190      * @return Token bytevalue for the corresponding token character.
191      */

192     Byte JavaDoc getToken( char tokenChar )
193     {
194         switch ( tokenChar )
195         {
196             case 'l':
197                 lineCount++;
198                 endLf = true;
199                 return TKN_ALF;
200             case 'L':
201                 return TKN_AL;
202             case 'r':
203                 lineCount++;
204                 endLf = true;
205                 return TKN_ARF;
206             case 'R':
207                 return TKN_AR;
208             case 'c':
209                 lineCount++;
210                 endLf = true;
211                 return TKN_ACF;
212             case 'C':
213                 return TKN_AC;
214             default:
215                 return TKN_NULL;
216         }
217     }
218     
219     /**
220      * Used to check it a <code>Comment</code> item ends with a linefeed.
221      * @return True if this Comment ends with a linefeed.
222      */

223     boolean isCompleteLine()
224     {
225         return endLf;
226     }
227     
228     /**
229      * Retrieves a <code>ArrayList</code> containing all string/token pairs in order of <code>String</code> - <code>Byte</code>.
230      * @return ArrayList containing all string/token pairs of this Comment.
231      */

232     ArrayList JavaDoc getTokens()
233     {
234         return oList;
235     }
236     
237     /**
238      * Counts the number of complete lines (linefeed markers) in the <code>Comment</code> object.
239      * @return Number of complete lines in this Comment.
240      */

241     int getLineCount()
242     {
243         return lineCount;
244     }
245     
246     boolean addSpacer() {
247         return addSpacer;
248     }
249     
250     boolean trimString() {
251         return trimString;
252     }
253
254     String JavaDoc getText() {
255         return text;
256     }
257
258     void exportXmlTemplate(XmlWriter xml) {
259         xml.writeTag("comment", getText());
260     }
261 }
262
Popular Tags