com.rapiddweller.common.ParseUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rd-lib-common Show documentation
Show all versions of rd-lib-common Show documentation
'rapiddweller Common' is an open source Java library
forked from Databene Commons by Volker Bergmann.
It provides extensions to the Java core library by utility classes, abstract concepts
and concrete implementations.
/*
* Copyright (C) 2004-2015 Volker Bergmann ([email protected]).
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.rapiddweller.common;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PushbackReader;
import java.io.Reader;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.ParsePosition;
import java.util.ArrayList;
import java.util.List;
/**
* Provides methods for parsing PushbackReaders and Strings.
* Created: 20.03.2005 16:32:00
*
* @author Volker Bergmann
*/
public final class ParseUtil {
/**
* Parse word string.
*
* @param reader the reader
* @return the string
* @throws IOException the io exception
*/
public static String parseWord(PushbackReader reader) throws IOException {
StringBuilder builder = new StringBuilder();
int c;
while ((c = reader.read()) != -1 && Character.isAlphabetic(c)) {
builder.append((char) c);
}
if (c != -1) {
reader.unread(c);
}
return builder.toString();
}
/**
* Parse double quoted string string.
*
* @param reader the reader
* @return the string
* @throws IOException the io exception
* @throws ParseException the parse exception
*/
public static String parseDoubleQuotedString(PushbackReader reader) throws IOException, ParseException {
StringBuilder builder = new StringBuilder();
int c;
if ((c = reader.read()) != '"') {
throw new ParseException("Opening quote (\") expected", 0);
}
while ((c = reader.read()) != -1 && c != '"') {
builder.append((char) c);
}
if (c != '"') {
throw new ParseException("Closing quote (\") expected", 0);
}
return builder.toString();
}
/**
* Parse decimal double.
*
* @param reader the reader
* @return the double
* @throws IOException the io exception
* @throws ParseException the parse exception
*/
public static double parseDecimal(PushbackReader reader) throws IOException, ParseException {
double result = parseInteger(reader);
double postfix = parseOptionalPostfix(reader);
if (result >= 0) {
result += postfix;
} else {
result -= postfix;
}
return result;
}
/**
* Parse optional postfix double.
*
* @param reader the reader
* @return the double
* @throws IOException the io exception
*/
public static double parseOptionalPostfix(PushbackReader reader) throws IOException {
int c = reader.read();
if (c != '.') {
if (c != -1) {
reader.unread(c);
}
return 0.;
}
double result = 0;
double base = 0.1;
while ((c = reader.read()) != -1 && Character.isDigit((char) c)) {
result += (c - '0') * base;
base *= 0.1;
}
if (c != -1) {
reader.unread(c);
}
return result;
}
/**
* Parse integer long.
*
* @param reader the reader
* @return the long
* @throws IOException the io exception
* @throws ParseException the parse exception
*/
public static long parseInteger(PushbackReader reader) throws IOException, ParseException {
boolean negative = parseOptionalSign(reader);
return parseNonNegativeInteger(reader) * (negative ? -1 : 1);
}
/**
* Parse non negative integer long.
*
* @param source the source
* @param pos the pos
* @return the long
* @throws ParseException the parse exception
*/
public static long parseNonNegativeInteger(String source, ParsePosition pos) throws ParseException {
int digit;
if (pos.getIndex() > source.length() || !Character.isDigit(digit = source.charAt(pos.getIndex()))) {
throw new ParseException("Number expected", 0);
}
pos.setIndex(pos.getIndex() + 1);
long result = digit - '0';
while (pos.getIndex() < source.length() && Character.isDigit(digit = source.charAt(pos.getIndex()))) {
result = result * 10 + digit - '0';
pos.setIndex(pos.getIndex() + 1);
}
return result;
}
/**
* Parse non negative integer long.
*
* @param reader the reader
* @return the long
* @throws IOException the io exception
* @throws ParseException the parse exception
*/
public static long parseNonNegativeInteger(PushbackReader reader) throws IOException, ParseException {
int digit;
if ((digit = reader.read()) == -1 || !Character.isDigit((char) digit)) {
throw new ParseException("Long expected", 0);
}
long result = digit - '0';
while ((digit = reader.read()) != -1 && Character.isDigit((char) digit)) {
result = result * 10 + digit - '0';
}
if (digit != -1) {
reader.unread(digit);
}
return result;
}
/**
* Parse optional sign boolean.
*
* @param reader the reader
* @return the boolean
* @throws IOException the io exception
*/
public static boolean parseOptionalSign(PushbackReader reader) throws IOException {
skipWhitespace(reader);
int optionalSign = reader.read();
if (optionalSign == '-') {
return true;
}
if (optionalSign != -1) {
reader.unread(optionalSign);
}
return false;
}
/**
* Skip whitespace.
*
* @param reader the reader
* @throws IOException the io exception
*/
public static void skipWhitespace(PushbackReader reader) throws IOException {
int c;
do {
c = reader.read();
} while (c != -1 && Character.isWhitespace((char) c));
if (c != -1) {
reader.unread(c);
}
}
/**
* Parse unit string.
*
* @param reader the reader
* @return the string
* @throws IOException the io exception
*/
public static String parseUnit(PushbackReader reader) throws IOException {
StringBuilder result = new StringBuilder();
int c;
while ((c = reader.read()) != -1 && Character.isUpperCase((char) c)) {
result.append((char) c);
}
if (c != -1) {
reader.unread(c);
}
return (result.length() > 0 ? result.toString() : null);
}
/**
* Parse estimated boolean.
*
* @param reader the reader
* @return the boolean
* @throws IOException the io exception
*/
public static boolean parseEstimated(PushbackReader reader) throws IOException {
int c = reader.read();
if (c == 'e') {
return true;
}
if (c != -1) {
reader.unread(c);
}
return false;
}
/**
* Is empty boolean.
*
* @param object the object
* @return the boolean
*/
public static boolean isEmpty(Object object) {
return (object == null || StringUtil.isEmpty((String) object));
}
/**
* Next non whitespace index int.
*
* @param source the source
* @param startIndex the start index
* @return the int
*/
public static int nextNonWhitespaceIndex(String source, int startIndex) {
int i;
for (i = startIndex; i < source.length() && Character.isWhitespace(source.charAt(i)); i++) {
}
if (i >= source.length()) {
i = -1;
}
return i;
}
/**
* Parse empty line separated file string [ ] [ ].
*
* @param src the src
* @return the string [ ] [ ]
* @throws IOException the io exception
*/
public static String[][] parseEmptyLineSeparatedFile(Reader src) throws IOException {
BufferedReader reader = null;
List> sections = new ArrayList<>();
List lines = null;
try {
reader = new BufferedReader(src);
String line;
while ((line = reader.readLine()) != null) {
if (line.length() > 0) {
if (lines == null) {
// start a new section
lines = new ArrayList<>();
sections.add(lines);
}
lines.add(line);
} else {
// end of the section
if (lines != null) {
lines = null;
} else {
// add empty section
sections.add(new ArrayList<>());
}
}
}
return StringUtil.toArrayArray(sections);
} finally {
IOUtil.close(reader);
}
}
/**
* Split numbers object [ ].
*
* @param text the text
* @return the object [ ]
*/
public static Object[] splitNumbers(String text) {
List