• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Conditions Of Use
3 *
4 * This software was developed by employees of the National Institute of
5 * Standards and Technology (NIST), an agency of the Federal Government.
6 * Pursuant to title 15 Untied States Code Section 105, works of NIST
7 * employees are not subject to copyright protection in the United States
8 * and are considered to be in the public domain.  As a result, a formal
9 * license is not needed to use the software.
10 *
11 * This software is provided by NIST as a service and is expressly
12 * provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
13 * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
14 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
15 * AND DATA ACCURACY.  NIST does not warrant or make any representations
16 * regarding the use of the software or the results thereof, including but
17 * not limited to the correctness, accuracy, reliability or usefulness of
18 * the software.
19 *
20 * Permission to use this software is contingent upon your acceptance
21 * of the terms of this agreement
22 *
23 * .
24 *
25 */
26 package gov.nist.core;
27 
28 import java.util.*;
29 import java.text.ParseException;
30 
31 /** Base string token splitter.
32 *
33 *@version 1.2
34 *
35 *@author M. Ranganathan   <br/>
36 *
37 *
38 *
39 */
40 
41 public class StringTokenizer {
42 
43     protected String buffer;
44     protected int bufferLen;
45     protected int ptr;
46     protected int savedPtr;
47 
StringTokenizer()48     protected StringTokenizer() {
49     }
50 
StringTokenizer(String buffer)51     public StringTokenizer(String buffer) {
52         this.buffer = buffer;
53         bufferLen = buffer.length();
54         ptr = 0;
55     }
56 
nextToken()57     public String nextToken() {
58         int startIdx = ptr;
59 
60         while (ptr < bufferLen) {
61             char c = buffer.charAt(ptr);
62             ptr++;
63             if (c == '\n') {
64                 break;
65             }
66         }
67 
68         return buffer.substring(startIdx, ptr);
69     }
70 
hasMoreChars()71     public boolean hasMoreChars() {
72         return ptr < bufferLen;
73     }
74 
isHexDigit(char ch)75     public static boolean isHexDigit(char ch) {
76         return (ch >= 'A' && ch <= 'F') ||
77                (ch >= 'a' && ch <= 'f') ||
78                isDigit(ch);
79     }
80 
isAlpha(char ch)81     public static boolean isAlpha(char ch) {
82         if (ch <= 127) {
83             return ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'));
84         }
85         else {
86             return Character.isLowerCase(ch) || Character.isUpperCase(ch);
87         }
88     }
89 
isDigit(char ch)90     public static boolean isDigit(char ch) {
91         if (ch <= 127) {
92             return (ch <= '9' && ch >= '0');
93         }
94         else {
95             return Character.isDigit(ch);
96         }
97     }
98 
isAlphaDigit(char ch)99     public static boolean isAlphaDigit(char ch) {
100         if (ch <= 127) {
101             return (ch >= 'a' && ch <= 'z') ||
102                 (ch >= 'A' && ch <= 'Z') ||
103                 (ch <= '9' && ch >= '0');
104         }
105         else {
106             return Character.isLowerCase(ch) ||
107                 Character.isUpperCase(ch) ||
108                 Character.isDigit(ch);
109         }
110     }
111 
getLine()112     public String getLine() {
113         int startIdx = ptr;
114         while (ptr < bufferLen && buffer.charAt(ptr) != '\n') {
115             ptr++;
116         }
117         if (ptr < bufferLen && buffer.charAt(ptr) == '\n') {
118             ptr++;
119         }
120         return buffer.substring(startIdx, ptr);
121     }
122 
peekLine()123     public String peekLine() {
124         int curPos = ptr;
125         String retval = this.getLine();
126         ptr = curPos;
127         return retval;
128     }
129 
lookAhead()130     public char lookAhead() throws ParseException {
131         return lookAhead(0);
132     }
133 
lookAhead(int k)134     public char lookAhead(int k) throws ParseException {
135         // Debug.out.println("ptr = " + ptr);
136         try {
137             return buffer.charAt(ptr + k);
138         }
139         catch (IndexOutOfBoundsException e) {
140             return '\0';
141         }
142     }
143 
getNextChar()144     public char getNextChar() throws ParseException {
145         if (ptr >= bufferLen)
146             throw new ParseException(
147                 buffer + " getNextChar: End of buffer",
148                 ptr);
149         else
150             return buffer.charAt(ptr++);
151     }
152 
consume()153     public void consume() {
154         ptr = savedPtr;
155     }
156 
consume(int k)157     public void consume(int k) {
158         ptr += k;
159     }
160 
161     /** Get a Vector of the buffer tokenized by lines
162      */
getLines()163     public Vector<String> getLines() {
164         Vector<String> result = new Vector<String>();
165         while (hasMoreChars()) {
166             String line = getLine();
167             result.addElement(line);
168         }
169         return result;
170     }
171 
172     /** Get the next token from the buffer.
173     */
getNextToken(char delim)174     public String getNextToken(char delim) throws ParseException {
175         int startIdx = ptr;
176         while (true) {
177             char la = lookAhead(0);
178             if (la == delim)
179                 break;
180             else if (la == '\0')
181                 throw new ParseException("EOL reached", 0);
182             consume(1);
183         }
184         return buffer.substring(startIdx, ptr);
185     }
186 
187     /** get the SDP field name of the line
188      *  @return String
189      */
getSDPFieldName(String line)190     public static String getSDPFieldName(String line) {
191         if (line == null)
192             return null;
193         String fieldName = null;
194         try {
195             int begin = line.indexOf("=");
196             fieldName = line.substring(0, begin);
197         } catch (IndexOutOfBoundsException e) {
198             return null;
199         }
200         return fieldName;
201     }
202 
203 }
204 
205