
com.brettonw.bag.BagParser Maven / Gradle / Ivy
package com.brettonw.bag;
// The BagParser is loosely modeled after a JSON parser grammar from the site (http://www.json.org).
// The main difference is that we ignore differences between value types (all of them will be
// strings internally), and assume the input is a well formed string representation of a BagObject
// or BagArray in JSON-ish format
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.*;
class BagParser {
private static final Logger log = LogManager.getLogger (BagParser.class);
private int index;
private final String input;
private String readInputStream (InputStream inputStream) throws IOException {
InputStreamReader inputStreamReader = new InputStreamReader (inputStream);
BufferedReader bufferedReader = new BufferedReader (inputStreamReader);
StringBuilder stringBuilder = new StringBuilder ();
String line;
while ((line = bufferedReader.readLine ()) != null) {
stringBuilder.append (line);
}
bufferedReader.close ();
return stringBuilder.toString ();
}
BagParser(String input) {
this.input = input;
index = 0;
}
public BagParser(InputStream inputStream) throws IOException {
input = readInputStream (inputStream);
index = 0;
}
public BagParser(File file) throws IOException {
InputStream inputStream = new FileInputStream (file);
input = readInputStream (inputStream);
index = 0;
}
BagArray ReadBagArray() {
// :: [ ] | [ ]
BagArray bagArray = new BagArray();
return (Expect('[') && ReadElements(bagArray) && Expect(']')) ? bagArray : null;
}
BagObject ReadBagObject() {
//
© 2015 - 2025 Weber Informatics LLC | Privacy Policy