Search in sources :

Example 1 with GenericParserSerialize

use of org.snt.inmemantlr.memobjects.GenericParserSerialize in project inmemantlr by julianthome.

the class GenericParser method store.

/**
 * serialize generic parser
 *
 * @param file path where generic parser is supposed to be stored
 * @param overwrite overwrite file
 * @throws SerializationException generic parser is not serializable
 */
public void store(String file, boolean overwrite) throws SerializationException {
    File loc = new File(file);
    File path = loc.getParentFile();
    LOGGER.debug("store file {}", loc.getAbsolutePath());
    if (loc.exists() && !overwrite) {
        throw new SerializationException("File " + file + " already exists");
    }
    if (!path.exists()) {
        throw new SerializationException("Cannot find path " + path.getAbsolutePath());
    }
    if (!antrlObjectsAvailable()) {
        throw new SerializationException("You have not compiled your grammar yet - there are no antlr objects available");
    }
    FileOutputStream f_out;
    ObjectOutputStream o_out;
    try {
        f_out = new FileOutputStream(file);
    } catch (FileNotFoundException e) {
        throw new SerializationException("output file cannot be found");
    }
    try {
        o_out = new ObjectOutputStream(f_out);
    } catch (IOException e) {
        throw new SerializationException("object output stream cannot be created");
    }
    GenericParserSerialize towrite = new GenericParserSerialize(getAllCompiledObjects(), parserName, lexerName);
    try {
        o_out.writeObject(towrite);
    } catch (NotSerializableException e) {
        LOGGER.error("Not serializable {}", e.getMessage());
    } catch (IOException e) {
        throw new SerializationException("error occurred while writing object", e);
    } finally {
        closeQuietly(o_out);
        closeQuietly(f_out);
    }
}
Also used : GenericParserSerialize(org.snt.inmemantlr.memobjects.GenericParserSerialize)

Example 2 with GenericParserSerialize

use of org.snt.inmemantlr.memobjects.GenericParserSerialize in project inmemantlr by julianthome.

the class GenericParser method load.

/**
 * load serialized generic parser
 *
 * @param file file of serialized generic parser file
 * @return the deserialized generic parser
 * @throws DeserializationException generic parser is not de-serializable
 */
public static GenericParser load(String file) throws DeserializationException {
    File loc = new File(file);
    File path = loc.getParentFile();
    if (!path.exists()) {
        throw new DeserializationException("Cannot find path " + path.getAbsolutePath());
    }
    if (!loc.exists()) {
        throw new DeserializationException("File " + file + " does not exist");
    }
    LOGGER.debug("load file {}", loc.getAbsolutePath());
    FileInputStream f_in;
    ObjectInputStream o_in;
    try {
        f_in = new FileInputStream(file);
    } catch (FileNotFoundException e) {
        throw new DeserializationException("input file " + file + " cannot be found");
    }
    try {
        o_in = new ObjectInputStream(f_in);
    } catch (IOException e) {
        throw new DeserializationException("object input stream cannot be found", e);
    }
    Object toread;
    try {
        toread = o_in.readObject();
    } catch (NotSerializableException e) {
        throw new DeserializationException("cannot read object", e);
    } catch (ClassNotFoundException e) {
        throw new DeserializationException("cannot find class", e);
    } catch (IOException e) {
        throw new DeserializationException(e.getMessage(), e);
    } finally {
        try {
            o_in.close();
            f_in.close();
        } catch (IOException e) {
            ;
        }
    }
    if (!(toread instanceof GenericParserSerialize))
        throw new IllegalArgumentException("toread must be an instance of GenericParserSerialize");
    GenericParserSerialize gin = (GenericParserSerialize) toread;
    GenericParser gp = new GenericParser(gin.getMemoryTupleSet(), gin.getParserName(), gin.getLexerName());
    if (!gp.antrlObjectsAvailable()) {
        throw new DeserializationException("there are no antlr objects available in " + file);
    }
    return gp;
}
Also used : GenericParserSerialize(org.snt.inmemantlr.memobjects.GenericParserSerialize)

Aggregations

GenericParserSerialize (org.snt.inmemantlr.memobjects.GenericParserSerialize)2