KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > planetamessenger > util > JKeyParser


1 /*
2     =========================================================================
3     Package util - Implements the Utilities classes.
4
5     This module is developed and maintained by PlanetaMessenger.org.
6     Specs, New and updated versions can be found in
7     http://www.planetamessenger.org
8     If you want contact the Team please send a email to Project Manager
9     Leidson Campos Alves Ferreira at leidson@planetamessenger.org
10
11     Copyright (C) since 2001 by PlanetaMessenger.org
12     
13     This library is free software; you can redistribute it and/or
14     modify it under the terms of the GNU Lesser General Public
15     License as published by the Free Software Foundation; either
16     version 2.1 of the License, or (at your option) any later version.
17
18     This library is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21     Lesser General Public License for more details.
22
23     You should have received a copy of the GNU Lesser General Public
24     License along with this library; if not, write to the Free Software
25     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
27     =========================================================================
28 */

29 /**
30  *
31  * $Id: JKeyParser.java,v 1.7 2007/02/11 13:52:37 popolony2k Exp $
32  * $Author: popolony2k $
33  * $Name: $
34  * $Revision: 1.7 $
35  * $State: Exp $
36  *
37  */

38
39 package org.planetamessenger.util;
40
41 import java.util.*;
42 import java.io.*;
43 import java.net.*;
44
45
46
47 public class JKeyParser {
48   
49   /**
50    * Parse the InputReader keys into a hash table.
51    *
52    * @param inReader Reader object previsously initialized;
53    * @param hash The that will receive the key/value pair;
54    * @param isSpaceSeparator The Space char is significant to build
55    * keys/values;
56    * @return true if successful
57    */

58   static public boolean parseKeys( Reader inReader, HashMap<String JavaDoc, String JavaDoc> hash, boolean isSpaceSeparator ) {
59     
60     StreamTokenizer tokens = new StreamTokenizer( inReader );
61     String JavaDoc strKey = "";
62     String JavaDoc strValue = "";
63     boolean bExpectingValue = false;
64     int nChar;
65       
66   
67
68     tokens.eolIsSignificant( true );
69     tokens.whitespaceChars( '=', '=' );
70     
71     tokens.ordinaryChar( '.' );
72     tokens.ordinaryChar( '|' );
73     tokens.ordinaryChar( '!' );
74     tokens.ordinaryChar( '@' );
75     tokens.ordinaryChar( '#' );
76     tokens.ordinaryChar( '$' );
77     tokens.ordinaryChar( '%' );
78     tokens.ordinaryChar( '&' );
79     tokens.ordinaryChar( '*' );
80     tokens.ordinaryChar( '(' );
81     tokens.ordinaryChar( ')' );
82     tokens.ordinaryChar( '+' );
83     tokens.ordinaryChar( '\\' );
84     tokens.ordinaryChar( '"' );
85     tokens.ordinaryChar( '\'' );
86     tokens.ordinaryChars( '0', '9' );
87     
88     tokens.wordChars( '.', '.' );
89     tokens.wordChars( '|', '|' );
90     tokens.wordChars( '!', '!' );
91     tokens.wordChars( '@', '@' );
92     tokens.wordChars( '#', '#' );
93     tokens.wordChars( '$', '$' );
94     tokens.wordChars( '%', '%' );
95     tokens.wordChars( '&', '&' );
96     tokens.wordChars( '*', '*' );
97     tokens.wordChars( '(', '(' );
98     tokens.wordChars( ')', ')' );
99     tokens.wordChars( '+', '+' );
100     tokens.wordChars( '\\', '\\' );
101     tokens.wordChars( '"', '"' );
102     tokens.wordChars( '\'', '\'' );
103     tokens.wordChars( '0', '9' );
104     
105     if( !isSpaceSeparator ) {
106       tokens.ordinaryChar( ' ' );
107       tokens.wordChars( ' ', ' ' );
108     }
109         
110     try {
111     
112       out:
113         
114       while( true ) {
115        
116         nChar = tokens.nextToken();
117         
118         switch( nChar ) {
119          
120           case StreamTokenizer.TT_EOF : {
121                                               if( strKey.compareTo( "" ) != 0 ) {
122                                                 hash.put( strKey, strValue );
123                                                 bExpectingValue = false;
124                                                 strValue = "";
125                                                 strKey = "";
126                                               }
127                                               
128                                               break out;
129                                             }
130           
131           case StreamTokenizer.TT_EOL : {
132                                               if( strKey.compareTo( "" ) != 0 ) {
133                                                 hash.put( strKey, strValue );
134                                                 bExpectingValue = false;
135                                                 strValue = "";
136                                                 strKey = "";
137                                               }
138                                             } break;
139
140           /*
141           case StreamTokenizer.TT_NUMBER : {
142                                               if( bExpectingValue ) {
143                                                 strValue+=Integer.toString( ( int ) tokens.nval );
144                                               }
145                                               else {
146                                                 //bErrFlag = true;
147                                                 break out;
148                                               }
149                                             } break;
150           */

151                                             
152           case StreamTokenizer.TT_WORD : {
153                                               if( bExpectingValue ) {
154                                                 
155                                                 if( isSpaceSeparator && ( strValue.length() > 0 ) )
156                                                   strValue += " ";
157                                                   
158                                                 strValue+=tokens.sval;
159                                                 
160                                                 if( !isSpaceSeparator )
161                                                   strValue+="\r\n";
162                                               }
163                                               else {
164                                                 bExpectingValue = true;
165                                                 strKey = tokens.sval;
166                                               }
167                                             } break;
168                                             
169           default : {
170                                               strValue+=( char ) nChar;
171                                             } break;
172         }
173       }
174       
175     } catch( IOException e ) {
176       System.err.println( e );
177       return false;
178     }
179     
180     return true;
181   }
182
183   /**
184    * Parse a input string into String Array;
185    * @param strInput The String that will be parsed;
186    * @param aOutput The array that will receive the result;
187    * @param chSeparator The character Key separator;
188    * @param chEndOfList The character end of list;
189    * @return true If the String was correclty parsed; false if Error in parse;
190    */

191   static public boolean parseKeys( java.lang.String JavaDoc strInput, java.util.ArrayList JavaDoc<String JavaDoc> aOutput, char chSeparator, char chEndOfList ) {
192     
193     int nBeginIndex = 0;
194     int nEndIndex = strInput.indexOf( chSeparator, nBeginIndex );
195   
196     
197     if( nEndIndex == -1 )
198       if( ( nEndIndex = strInput.indexOf( chEndOfList, nBeginIndex ) ) == -1 )
199         return false;
200
201     do {
202       aOutput.add( strInput.substring( nBeginIndex, nEndIndex ) );
203       nBeginIndex = nEndIndex + 1;
204       nEndIndex = strInput.indexOf( chSeparator, nBeginIndex );
205       
206       if( nEndIndex == -1 )
207         nEndIndex = strInput.indexOf( chEndOfList, nBeginIndex );
208       
209     } while( nEndIndex > 0 );
210     
211     return true;
212   }
213   
214   /**
215    * Remove the CR/LF pair from String.
216    * @param strIn The String that CRLF will be removed;
217    * @return the input string with CRLF removed.
218    */

219   static public java.lang.String JavaDoc removeCRLF( java.lang.String JavaDoc strIn ) {
220     StringBuilder JavaDoc strOut = new StringBuilder JavaDoc();;
221     int nBeginIndex = 0;
222     int nEndIndex;
223   
224     
225     while( ( nEndIndex = strIn.indexOf( "\r\n", nBeginIndex ) ) >= 0 ) {
226       strOut.append( strIn.substring( nBeginIndex, nEndIndex ) );
227       nBeginIndex = nEndIndex + 2;
228     }
229     
230     return strOut.toString();
231   }
232   
233   /**
234    * Replaces a string sequence from input string.
235    *
236    * @param strIn The input string that will be replaced;
237    * @param strOldSequence - The old sequence to be replaced;
238    * @param strNewSequence - The new sequence that will be inserted into string;
239    * If strOldSequence and strNewSequence differs in size, the function
240    * not will replace and will return the strIn data string.
241    * @return strIn with all occurances of strOldSequence replaced with
242    * strNewSequence UNLESS new&old are different length, then it returns
243    * strIn
244    */

245   static public java.lang.String JavaDoc replace( java.lang.String JavaDoc strIn, java.lang.String JavaDoc strOldSequence, java.lang.String JavaDoc strNewSequence ) {
246
247     String JavaDoc strOut = strIn;
248     
249     if( strOldSequence.length() == strNewSequence.length() ) {
250       int nSize = strOldSequence.length();
251       
252       for( int nCount = 0; nCount < nSize; nCount++ )
253         strOut = strOut.replace( strOldSequence.charAt( nCount ), strNewSequence.charAt( nCount ) );
254     }
255     
256     return strOut;
257   }
258 }
259
260 // JKeyParser class
Popular Tags