here’s the parser code
an8file.java
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.util.Scanner;
import java.util.Vector;
public class an8file extends buildable {
// creates a string representation of the file
// nothing special to look at here
public static an8file build(File file)
{
Scanner scan = null;
try {
scan = new Scanner(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StringBuilder st = new StringBuilder();
while(scan.hasNext())
{
st.append(scan.nextLine()+"\n");
}
return build(st.toString());
}
//this is where it really gets ugly
public static an8file build(String file)
{
an8file an = new an8file();
Scanner scan = new Scanner(file);
// the buildables stack
Vector buildables= new Vector();
buildables.add(an);
while(scan.hasNext())
{
String s = scan.nextLine();
s.trim();
if(s.contains("{"))
{
chunk c = new chunk();
int bracket= s.indexOf("{");
c.type=s.substring(0, bracket).trim();
buildables.add(c);
((buildable)buildables.get(buildables.size()-2)).subelements.add(c);
// there is an element in with the chunk declaration
if(bracket<s.length()-1)
{
//one-liners are a special case... ugh
if(s.contains("}"))
{
((buildable)buildables.lastElement()).subelements.add(s.substring(bracket+1, s.length()-2));
buildables.remove(buildables.size()-1);
}
else
{
((buildable)buildables.lastElement()).subelements.add(s.substring(bracket+1));
}
}
}
else
if(s.contains("}"))
{
buildables.remove(buildables.size()-1);
}
else
{
((buildable)buildables.lastElement()).subelements.add(s);
}
}
// STATE MACHINES SUCK!!!
return an;
}
public String toString()
{
String text = "";
for(int x=0; x<subelements.size();x++ )
{
text+= subelements.get(x).toString()+"\n";
}
return text;
}
public static void main(String args[])
{
an8file a = an8file.build(new File("test.an8"));
System.out.println(((chunk)((chunk)a.subelements.get(0)).subelements.get(0)).subelements.get(0));
try {
a.save(new File("test2.an8"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void save(File file) throws FileNotFoundException {
PrintStream ps = new PrintStream(new FileOutputStream(file));
ps.print(toString());
}
}
buildable.java
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.util.Scanner;
import java.util.Vector;
public class an8file extends buildable {
// creates a string representation of the file
// nothing special to look at here
public static an8file build(File file)
{
Scanner scan = null;
try {
scan = new Scanner(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StringBuilder st = new StringBuilder();
while(scan.hasNext())
{
st.append(scan.nextLine()+"\n");
}
return build(st.toString());
}
//this is where it really gets ugly
public static an8file build(String file)
{
an8file an = new an8file();
Scanner scan = new Scanner(file);
// the buildables stack
Vector buildables= new Vector();
buildables.add(an);
while(scan.hasNext())
{
String s = scan.nextLine();
s.trim();
if(s.contains("{"))
{
chunk c = new chunk();
int bracket= s.indexOf("{");
c.type=s.substring(0, bracket).trim();
buildables.add(c);
((buildable)buildables.get(buildables.size()-2)).subelements.add(c);
// there is an element in with the chunk declaration
if(bracket<s.length()-1)
{
//one-liners are a special case... ugh
if(s.contains("}"))
{
((buildable)buildables.lastElement()).subelements.add(s.substring(bracket+1, s.length()-2));
buildables.remove(buildables.size()-1);
}
else
{
((buildable)buildables.lastElement()).subelements.add(s.substring(bracket+1));
}
}
}
else
if(s.contains("}"))
{
buildables.remove(buildables.size()-1);
}
else
{
((buildable)buildables.lastElement()).subelements.add(s);
}
}
// STATE MACHINES SUCK!!!
return an;
}
public String toString()
{
String text = "";
for(int x=0; x<subelements.size();x++ )
{
text+= subelements.get(x).toString()+"\n";
}
return text;
}
public static void main(String args[])
{
an8file a = an8file.build(new File("test.an8"));
System.out.println(((chunk)((chunk)a.subelements.get(0)).subelements.get(0)).subelements.get(0));
try {
a.save(new File("test2.an8"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void save(File file) throws FileNotFoundException {
PrintStream ps = new PrintStream(new FileOutputStream(file));
ps.print(toString());
}
}
chunk.java
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.util.Scanner;
import java.util.Vector;
public class an8file extends buildable {
// creates a string representation of the file
// nothing special to look at here
public static an8file build(File file)
{
Scanner scan = null;
try {
scan = new Scanner(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StringBuilder st = new StringBuilder();
while(scan.hasNext())
{
st.append(scan.nextLine()+"\n");
}
return build(st.toString());
}
//this is where it really gets ugly
public static an8file build(String file)
{
an8file an = new an8file();
Scanner scan = new Scanner(file);
// the buildables stack
Vector buildables= new Vector();
buildables.add(an);
while(scan.hasNext())
{
String s = scan.nextLine();
s.trim();
if(s.contains("{"))
{
chunk c = new chunk();
int bracket= s.indexOf("{");
c.type=s.substring(0, bracket).trim();
buildables.add(c);
((buildable)buildables.get(buildables.size()-2)).subelements.add(c);
// there is an element in with the chunk declaration
if(bracket<s.length()-1)
{
//one-liners are a special case... ugh
if(s.contains("}"))
{
((buildable)buildables.lastElement()).subelements.add(s.substring(bracket+1, s.length()-2));
buildables.remove(buildables.size()-1);
}
else
{
((buildable)buildables.lastElement()).subelements.add(s.substring(bracket+1));
}
}
}
else
if(s.contains("}"))
{
buildables.remove(buildables.size()-1);
}
else
{
((buildable)buildables.lastElement()).subelements.add(s);
}
}
// STATE MACHINES SUCK!!!
return an;
}
public String toString()
{
String text = "";
for(int x=0; x<subelements.size();x++ )
{
text+= subelements.get(x).toString()+"\n";
}
return text;
}
public static void main(String args[])
{
an8file a = an8file.build(new File("test.an8"));
System.out.println(((chunk)((chunk)a.subelements.get(0)).subelements.get(0)).subelements.get(0));
try {
a.save(new File("test2.an8"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void save(File file) throws FileNotFoundException {
PrintStream ps = new PrintStream(new FileOutputStream(file));
ps.print(toString());
}
}
you’ll still have to look up the format, but this will deal with the nested objects. everything is stored within the subelements vectors. check if it’s a string or a chunk.