// tokenizes a string for PBM and PGM headers and plain PBM and PGM data. If oneChar is true, // then // rather than parsing a whole string, a single character is read (not including whitespace or // comments) static String tokenizeString(InputStream stream, boolean oneChar) throws IOException { final int EOF = -1; StringBuilder b = new StringBuilder(); int c; boolean inComment = false; while (true) { c = stream.read(); if (c == EOF) throw new IOException( "Stream ended prematurely, before table reading was completed."); // no more tokens else if (inComment) { if (c == '\r' || c == '\n') // escape the comment inComment = false; else { } // do nothing } else if (Character.isWhitespace((char) c)) { } // do nothing else if (c == '#') // start of a comment { inComment = true; } else // start of a string { b.append((char) c); break; } } if (oneChar) return b.toString(); // at this point we have a valid string. We read until whitespace or a # while (true) { c = stream.read(); if (c == EOF) break; else if (c == '#') // start of comment, read until a '\n' { while (true) { c = stream.read(); // could hit EOF, which is fine if (c == EOF) break; else if (c == '\r' || c == '\n') break; } // break; // comments are not delimiters } else if (Character.isWhitespace((char) c)) break; else b.append((char) c); } return b.toString(); }
private boolean checkHandlerWorkaround( String elementName, String handlerName, boolean writeHandler) throws ClickException, IOException { // If talking to an old ControlSocket, try the "handlers" handler // instead. String s = readString(elementName, "handlers"); int pos = 0; // Find handler with same name. while (true) { pos = s.indexOf(handlerName, pos); if (pos < 0) // no such handler return false; if ((pos == 0 || s.charAt(pos - 1) == '\n') && Character.isWhitespace(s.charAt(pos + handlerName.length()))) break; pos++; } // we have a matching handler: will it be read/write suitable? char wantType = (writeHandler ? 'w' : 'r'); for (pos += handlerName.length(); pos < s.length() && Character.isWhitespace(s.charAt(pos)); pos++) /* nada */ ; for (; pos < s.length(); pos++) { char c = s.charAt(pos); if (Character.toLowerCase(c) == wantType) return true; else if (Character.isWhitespace(c)) break; } return false; }
/** * Gets the information about an element's handlers in the current router configuration. * * @param el The element name. * @return Vector of HandlerInfo structures. * @exception NoSuchElementException If there is no such element in the current configuration. * @exception HandlerErrorException If the handler returned an error. * @exception PermissionDeniedException If the router would not let us access the handler. * @exception IOException If there was some other error accessing the handler (e.g., there was a * stream or socket error, the ControlSocket returned an unknwon unknown error code, or the * response could otherwise not be understood). * @see #HandlerInfo * @see #getConfigElementNames * @see #getRouterConfig * @see #getRouterFlatConfig */ public Vector getElementHandlers(String elementName) throws ClickException, IOException { Vector v = new Vector(); Vector vh; try { char[] buf = read(elementName, "handlers"); vh = StringUtils.split(buf, 0, '\n'); } catch (ClickException.NoSuchHandlerException e) { return v; } for (int i = 0; i < vh.size(); i++) { String s = (String) vh.elementAt(i); int j; for (j = 0; j < s.length() && !Character.isWhitespace(s.charAt(j)); j++) ; // find record split if (j == s.length()) throw new ClickException.HandlerFormatException(elementName + ".handlers"); HandlerInfo hi = new HandlerInfo(elementName, s.substring(0, j).trim()); while (j < s.length() && Character.isWhitespace(s.charAt(j))) j++; for (; j < s.length(); j++) { char c = s.charAt(j); if (Character.toLowerCase(c) == 'r') hi.canRead = true; else if (Character.toLowerCase(c) == 'w') hi.canWrite = true; else if (Character.isWhitespace(c)) break; } v.addElement(hi); } return v; }
/** * Skips any leading whitespace and return true if any was found. Note that this does not refill * the buffer, so callers should do something like this: while(ring.skipLeadingWhiteSpace()) { * refill ring; } * * @return true if any leading whitespace was found, false otherwise */ public boolean skipLeadingWhiteSpace() { if (Character.isWhitespace(get(0))) { int idx = 0; do { idx++; } while (idx < size() && Character.isWhitespace(get(idx))); skip(idx); return true; } return false; }
private void write(CharSequence text, boolean gsp) { if (!gsp) { out.print(text); return; } for (int ix = 0, ixz = text.length(); ix < ixz; ix++) { char c = text.charAt(ix); String rep = null; if (Character.isWhitespace(c)) { for (ix++; ix < ixz; ix++) { if (Character.isWhitespace(text.charAt(ix))) { continue; } ix--; rep = " "; break; } } else if (c == '&') { if (match(";", text, ix)) { rep = ";"; ix += 5; } else if (match("&", text, ix)) { rep = "&"; ix += 4; } else if (match("<", text, ix)) { rep = "<"; ix += 3; } else if (match(">", text, ix)) { rep = ">"; ix += 3; } } else if (c == '<') { if (match("<br>", text, ix) || match("<hr>", text, ix)) { rep = "\n"; // incrementLineNumber(); ix += 3; } else { int end = match(PARA_BREAK, text, ix); if (end <= 0) end = match(ROW_BREAK, text, ix); if (end > 0) { rep = "\n"; // incrementLineNumber(); ix = end; } } } if (rep != null) { out.print(rep); } else { out.print(c); } } }
public static String[] splitwords(String text) { ArrayList<String> words = new ArrayList<String>(); StringBuilder buf = new StringBuilder(); String st = "ws"; int i = 0; while (i < text.length()) { char c = text.charAt(i); if (st == "ws") { if (!Character.isWhitespace(c)) st = "word"; else i++; } else if (st == "word") { if (c == '"') { st = "quote"; i++; } else if (c == '\\') { st = "squote"; i++; } else if (Character.isWhitespace(c)) { words.add(buf.toString()); buf = new StringBuilder(); st = "ws"; } else { buf.append(c); i++; } } else if (st == "quote") { if (c == '"') { st = "word"; i++; } else if (c == '\\') { st = "sqquote"; i++; } else { buf.append(c); i++; } } else if (st == "squote") { buf.append(c); i++; st = "word"; } else if (st == "sqquote") { buf.append(c); i++; st = "quote"; } } if (st == "word") words.add(buf.toString()); if ((st != "ws") && (st != "word")) return (null); return (words.toArray(new String[0])); }
/** * Called to parse a double or single quote string. * * @return The string parsed. * @throws IOException If an I/O exception occurs. */ protected String parseString() throws IOException { StringBuilder result = new StringBuilder(); eatWhitespace(); if ("\"\'".indexOf(this.source.peek()) != -1) { int delim = this.source.read(); while ((this.source.peek() != delim) && (this.source.peek() != -1)) { if (result.length() > 1000) { break; } int ch = parseSpecialCharacter(); if ((ch == 13) || (ch == 10)) { continue; } result.append((char) ch); } if ("\"\'".indexOf(this.source.peek()) != -1) { this.source.read(); } } else { while (!Character.isWhitespace(this.source.peek()) && (this.source.peek() != -1) && (this.source.peek() != '>')) { result.append(parseSpecialCharacter()); } } return result.toString(); }
/** * @ensures: if input file is read, a String is filled with words from file and searched in * dictionary list, and if found increase words found counter, otherwise increase words not * found. Otherwise error reading file */ public void readFileOliver() { try { FileInputStream inf = new FileInputStream(new File("oliver.txt")); char let; String str = ""; String key = ""; int n = 0; while ((n = inf.read()) != -1) { let = (char) n; if (Character.isLetter(let)) { str += Character.toLowerCase(let); } if ((Character.isWhitespace(let) || let == '-') && !str.isEmpty()) { key = str; str = ""; boolean a = dictionary[(int) key.charAt(0) - 97].contains(key); if (a == true) { counter = dictionary[(int) key.charAt(0) - 97].indexOf(key); counterWFound++; counterWFCompared += counter; } else { counter = dictionary[(int) key.charAt(0) - 97].indexOf( dictionary[(int) key.charAt(0) - 97].getLast()); counterWNotFound++; counterWNFCompared += counter; } } } inf.close(); } catch (IOException e) { e.printStackTrace(); } }
// get a list of whitespace separated classnames from a property. private String[] parseClassNames(String propertyName) { String hands = getProperty(propertyName); if (hands == null) { return new String[0]; } hands = hands.trim(); int ix = 0; Vector result = new Vector(); while (ix < hands.length()) { int end = ix; while (end < hands.length()) { if (Character.isWhitespace(hands.charAt(end))) { break; } if (hands.charAt(end) == ',') { break; } end++; } String word = hands.substring(ix, end); ix = end+1; word = word.trim(); if (word.length() == 0) { continue; } result.add(word); } return (String[]) result.toArray(new String[result.size()]); }
String cleanupSource(String source) { if (source.isEmpty()) { return source.concat("\n"); } int lastChIdx = source.length() - 1; if (source.charAt(lastChIdx) != '\n') { // Append a newline at the end source = source.concat("\n"); } else { // Remove all newlines at the end but one while (lastChIdx >= 1) { Character ch1 = source.charAt(lastChIdx); if (ch1 == '\n' || Character.isWhitespace(ch1)) { source = source.substring(0, lastChIdx--); } else { break; } } source = source.concat("\n"); } return source; }
/* pre : empty args * post: compare input text file to dictionary */ public void spellCheck() { try { FileInputStream inf = new FileInputStream(new File("oliver.txt")); char let; String word = ""; // word to be processed int n = 0; while ((n = inf.read()) != -1) { let = (char) n; if (Character.isLetter(let)) { word += Character.toLowerCase(let); } if ((Character.isWhitespace(let) || let == '-') && !word.isEmpty()) { // if(dictionary[(int)wordstr.charAt(0)-97].search(wordstr)) if (dictionary[(int) word.charAt(0) - 97].search(word, stringComparisons)) { foundWords++; wordsFoundComparison += stringComparisons[0]; } else { notFoundWords++; wordsNotFoundComparison += stringComparisons[0]; } word = ""; } } inf.close(); } catch (IOException e) { e.printStackTrace(); } } // end of spellCheck
/** Skips any white space, returning the character after the white space. */ private int readWS() throws IOException { int nextChar; while ((nextChar = readChar()) != -1 && Character.isWhitespace((char) nextChar)) { readWS = true; } return nextChar; }
/** * Skips over any whitespace characters, except for end-of-lines. After this method is called, the * next input character is either an end-of-line, an end-of-file, or a non-whitespace character. * This method never causes an error. (Ordinarily, end-of-file is not possible when reading from * standard input.) */ public static void skipBlanks() { char ch = lookChar(); while (ch != EOF && ch != '\n' && Character.isWhitespace(ch)) { readChar(); ch = lookChar(); } }
String[] getArgNames(JNIMethod method) { int n_args = method.getParameters().size(); if (n_args == 0) return new String[0]; String name = method.getName(); String params = ""; int index = 0; while (true) { index = classSource.indexOf(name, index + 1); if (!Character.isWhitespace(classSource.charAt(index - 1))) continue; if (index == -1) return null; int parantesesStart = classSource.indexOf("(", index); if (classSource.substring(index + name.length(), parantesesStart).trim().length() == 0) { int parantesesEnd = classSource.indexOf(")", parantesesStart); params = classSource.substring(parantesesStart + 1, parantesesEnd); break; } } String[] names = new String[n_args]; StringTokenizer tk = new StringTokenizer(params, ","); for (int i = 0; i < names.length; i++) { String s = tk.nextToken().trim(); StringTokenizer tk1 = new StringTokenizer(s, " "); String s1 = null; while (tk1.hasMoreTokens()) { s1 = tk1.nextToken(); } names[i] = s1.trim(); } return names; }
/** * Parse any special characters(i.e.  ); * * @return The character that was parsed. * @throws IOException If a read error occurs */ private char parseSpecialCharacter() throws IOException { char result = (char) this.source.read(); int advanceBy = 0; // is there a special character? if (result == '&') { int ch = 0; StringBuilder buffer = new StringBuilder(); // loop through and read special character do { ch = this.source.peek(advanceBy++); if ((ch != '&') && (ch != ';') && !Character.isWhitespace(ch)) { buffer.append((char) ch); } } while ((ch != ';') && (ch != -1) && !Character.isWhitespace(ch)); String b = buffer.toString().trim().toLowerCase(); // did we find a special character? if (b.length() > 0) { if (b.charAt(0) == '#') { try { result = (char) Integer.parseInt(b.substring(1)); } catch (NumberFormatException e) { advanceBy = 0; } } else { if (charMap.containsKey(b)) { result = charMap.get(b); } else { advanceBy = 0; } } } else { advanceBy = 0; } } while (advanceBy > 0) { read(); advanceBy--; } return result; }
/** 解析属性行 */ private static boolean parserAttribute(String line) { // @attribute sex {Female,Male} // @attribute capital-gain numeric if (line == null) { return true; } int index = 0; // 字符串的当前位置。 // 跳过"@attribute" index += "@attribute".length(); // 跳过空白。 while (Character.isWhitespace(line.charAt(index))) ++index; // 遇到了属性的名字 String name; int nameStart = index; // 名字开始的位置。 while (!Character.isWhitespace(line.charAt(index))) ++index; // 此时index直向属性名字后面的空格。 name = line.substring(nameStart, index); // 下面解析属性值。 // 跳过空白。 while (Character.isWhitespace(line.charAt(index))) ++index; if (line.charAt(index) == '{') { // 是categorical类型属性 // @attribute sex {Female,Male} AttributeClass ac = new AttributeClass(AttributeType.CATE, name); String cateName; int cateStart; while (line.charAt(index) != '}') { ++index; cateStart = index; while (line.charAt(index) != ',' && line.charAt(index) != '}') { ++index; } cateName = line.substring(cateStart, index); ac.addCate(cateName); } arff.addAttrClass(ac); } else { // 是numeric类型属性 arff.addAttrClass(new AttributeClass(AttributeType.NUME, name)); } return true; }
// Here is where the real work of reading the phonetics file is done. private void buildRule(String str, Vector ruleList) { if (str.length() < 1) return; for (int i = 0; i < IGNORED_KEYWORDS.length; i++) { if (str.startsWith(IGNORED_KEYWORDS[i])) return; } // A different alphabet is used for this language, will be read into // the alphabetString variable. if (str.startsWith(KEYWORD_ALPHBET)) { int start = str.indexOf(ALPHABET_START); int end = str.lastIndexOf(ALPHABET_END); if (end != -1 && start != -1) { alphabetString = str.substring(++start, end).toCharArray(); } return; } // str contains two groups of characters separated by white space(s). // The fisrt group is the "match expression". The second group is the // "replacement expression" giving the phonetic equivalent of the // "match expression". TransformationRule rule = null; StringBuffer matchExp = new StringBuffer(); StringBuffer replaceExp = new StringBuffer(); boolean start = false, end = false; int takeOutPart = 0, matchLength = 0; boolean match = true, inMulti = false; for (int i = 0; i < str.length(); i++) { if (Character.isWhitespace(str.charAt(i))) { match = false; } else { if (match) { if (!isReservedChar(str.charAt(i))) { matchExp.append(str.charAt(i)); if (!inMulti) { takeOutPart++; matchLength++; } if (str.charAt(i) == STARTMULTI || str.charAt(i) == ENDMULTI) inMulti = !inMulti; } if (str.charAt(i) == '-') takeOutPart--; if (str.charAt(i) == '^') start = true; if (str.charAt(i) == '$') end = true; } else { replaceExp.append(str.charAt(i)); } } } if (replaceExp.toString().equals(REPLACEVOID)) { replaceExp = new StringBuffer(""); // System.out.println("Changing _ to \"\" for "+matchExp.toString()); } rule = new TransformationRule( matchExp.toString(), replaceExp.toString(), takeOutPart, matchLength, start, end); // System.out.println(rule.toString()); ruleList.addElement(rule); }
private static boolean hasWhitespace(String string) { int length = string.length(); for (int i = 0; i < length; i++) { if (Character.isWhitespace(string.charAt(i))) { return true; } } return false; }
private static int getChar() // Returns the next non-whitespace character on the input stream. // Returns -1, end-of-stream, if the end of the input stream is reached. { int i = getNextChar(); while (Character.isWhitespace((char) i)) i = getNextChar(); return i; } // end getChar
/** * Skips whitespace characters and then reads one "word" from input. Any additional characters on * the current line of input are retained, and will be read by the next input operation. A word is * defined as a sequence of non-whitespace characters (not just letters!). When using standard IO, * this will not produce an error. In other cases, an IllegalArgumentException will be thrown if * an end-of-file is encountered. */ public static String getWord() { skipWhitespace(); StringBuffer str = new StringBuffer(50); char ch = lookChar(); while (ch == EOF || !Character.isWhitespace(ch)) { str.append(readChar()); ch = lookChar(); } return str.toString(); }
/** * Check to see if the ending tag is present. * * @param name The type of end tag being saught. * @return True if the ending tag was found. * @throws IOException Thrown if an IO error occurs. */ private boolean peekEndTag(String name) throws IOException { int i = 0; // pass any whitespace while ((this.source.peek(i) != -1) && Character.isWhitespace(this.source.peek(i))) { i++; } // is a tag beginning if (this.source.peek(i) != '<') { return false; } else { i++; } // pass any whitespace while ((this.source.peek(i) != -1) && Character.isWhitespace(this.source.peek(i))) { i++; } // is it an end tag if (this.source.peek(i) != '/') { return false; } else { i++; } // pass any whitespace while ((this.source.peek(i) != -1) && Character.isWhitespace(this.source.peek(i))) { i++; } // does the name match for (int j = 0; j < name.length(); j++) { if (Character.toLowerCase(this.source.peek(i)) != Character.toLowerCase(name.charAt(j))) { return false; } i++; } return true; }
/** * Skips over any whitespace characters, including for end-of-lines. After this method is called, * the next input character is either an end-of-file or a non-whitespace character. This method * never causes an error. (Ordinarily, end-of-file is not possible when reading from standard * input.) */ private static void skipWhitespace() { char ch = lookChar(); while (ch != EOF && Character.isWhitespace(ch)) { readChar(); if (ch == '\n' && readingStandardInput && writingStandardOutput) { out.print("? "); out.flush(); } ch = lookChar(); } }
private static String stripTrailing(String s) { if (s == null) return null; int lastChar; int semi; if ((semi = s.lastIndexOf(';')) < 0) lastChar = s.length() - 1; else lastChar = semi - 1; for (int i = lastChar; i >= 0; i--) { if (!Character.isWhitespace(s.charAt(i))) return s.substring(0, i + 1); } return ""; }
boolean skipSpaces() { int _c; do { _c = getNext(); if (_c == Integer.MIN_VALUE) { return false; } } while (Character.isWhitespace((char) _c)); this.idx--; return true; }
/** * Create a Map with the frequency of each symbol that appears in the string. * * @param text - string from which to generate the map */ public Map<Character, Double> getSymbolFrequencyMap() { Map<Character, Double> letterFrequency = new TreeMap<Character, Double>(); Double freq = new Double(0); Character textCharacter; for (int i = 0; i < text.length(); i++) { textCharacter = new Character(text.charAt(i)); if (!Character.isWhitespace(textCharacter)) { freq = (letterFrequency.get(textCharacter)); letterFrequency.put(textCharacter, (freq == null) ? 1 : freq + 1); } } normalizeSymbolMap(letterFrequency); return letterFrequency; }
/** Converts all whitespace characters in a String to space characters (\u0020). */ public static String cleanWhitespace(String str) { int length = str.length(); StringBuffer buf = new StringBuffer(length); for (int i = 0; i < length; i++) { char c = str.charAt(i); if (Character.isWhitespace(c)) { buf.append(' '); } else { buf.append(c); } } return buf.toString(); }
private void skipWhiteSpace() throws ParseException, IOException { int currentChar = -1; do { try { currentChar = input.read(); } catch (IOException e) { throw new ParseException(0); } } while (Character.isWhitespace((char) currentChar)); try { input.unread(currentChar); } catch (IOException e2) { throw new ParseException(0); } return; }
/** * This is the write() method of the stream. All Writer subclasses implement this. All other * versions of write() are variants of this one */ public void write(char[] buffer, int index, int len) { synchronized (this.lock) { // Loop through all the characters passed to us for (int i = index; i < index + len; i++) { // If we haven't begun a page (or a new page), do that now. if (page == null) newpage(); // If the character is a line terminator, then begin new line, // unless it is a \n immediately after a \r. if (buffer[i] == '\n') { if (!last_char_was_return) newline(); continue; } if (buffer[i] == '\r') { newline(); last_char_was_return = true; continue; } else last_char_was_return = false; // If it some other non-printing character, ignore it. if (Character.isWhitespace(buffer[i]) && !Character.isSpaceChar(buffer[i]) && (buffer[i] != '\t')) continue; // If no more characters will fit on the line, start a new line. if (charnum >= chars_per_line) { newline(); if (page == null) newpage(); // and start a new page, if necessary } // Now print the character: // If it is a space, skip one space, without output. // If it is a tab, skip the necessary number of spaces. // Otherwise, print the character. // It is inefficient to draw only one character at a time, but // because our FontMetrics don't match up exactly to what the // printer uses we need to position each character individually. if (Character.isSpaceChar(buffer[i])) charnum++; else if (buffer[i] == '\t') charnum += 8 - (charnum % 8); else { page.drawChars( buffer, i, 1, x0 + charnum * charwidth, y0 + (linenum * lineheight) + lineascent); charnum++; } } } }
/** * Parse an attribute name, if one is present. * * @throws IOException If an I/O exception occurs. */ protected String parseAttributeName() throws IOException { eatWhitespace(); if ("\"\'".indexOf(this.source.peek()) == -1) { StringBuilder buffer = new StringBuilder(); while (!Character.isWhitespace(this.source.peek()) && (this.source.peek() != '=') && (this.source.peek() != '>') && (this.source.peek() != -1)) { int ch = parseSpecialCharacter(); buffer.append((char) ch); } return buffer.toString(); } else { return (parseString()); } }
boolean readIdentifier() { int _c; int _start_idx = this.idx; do { _c = getNext(); // abort on incomplete string if (_c == Integer.MIN_VALUE) { return false; } } while (!Character.isWhitespace((char) _c)); this.idx--; this.lastIdentifier = new byte[this.idx - _start_idx]; System.arraycopy(this.buffer, _start_idx, this.lastIdentifier, 0, this.idx - _start_idx); return true; }