1 29 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 58 static public boolean parseKeys( Reader inReader, HashMap<String , String > hash, boolean isSpaceSeparator ) { 59 60 StreamTokenizer tokens = new StreamTokenizer( inReader ); 61 String strKey = ""; 62 String 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 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 191 static public boolean parseKeys( java.lang.String strInput, java.util.ArrayList <String > 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 219 static public java.lang.String removeCRLF( java.lang.String strIn ) { 220 StringBuilder strOut = new StringBuilder ();; 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 245 static public java.lang.String replace( java.lang.String strIn, java.lang.String strOldSequence, java.lang.String strNewSequence ) { 246 247 String 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 | Popular Tags |