/* * Parser.java * Klass som laddar och parsar en textfil * Med utseendet: * * <|component|> * COMPONENT NAME * <|TEXT|> * blahblahblah * * * texttarget * texttarget * * <|component|> * *etcetc..... * * Karl-Adam Karlsson * 06-11-15 * */ import java.io.*; import java.util.*; public class Parser { BufferedReader myReader; LinkedList components; Interactiveator myIact; public Parser(String filename, Interactiveator iact){ myIact = iact; try{ components = new LinkedList(); FileReader fR = new FileReader( iact.getContext("docs/"+filename) ); myReader = new BufferedReader(fR); }catch(FileNotFoundException fnof){ fnof.printStackTrace(); } }//end of konstruktor /* * Parses the file that was assigned to myReader in the constructor. * Returns a LinkedList with the Komponent components. * The first Komponent in the list is considered the start. * * @return LinkedList A list of the Komponent components resulting from the parsing. * */ public LinkedList parse(){ String line = new String(); try{ while( ( line = myReader.readLine() ) != null ){ line = line.trim(); if( line.equals("<|component|>") ){ //Skapa en component, och lägg till sist Komponent k = new Komponent( myReader.readLine().trim() ); components.addLast(k); // De andra kommer alltså att manipulera den sista komponenten i // listan. }else if( line.equals("<|TEXT|>") ){ // Text, mums. Läs in och lägg till komponenten. LinkedList text = new LinkedList(); while( !line.equals("") ){ line = myReader.readLine(); line = line.trim(); //someText = someText.concat( line ); text.addLast(line); }//end of subwhile //Lägg till texten i sista komponenten ((Komponent)components.getLast()).setText(text); }else if( line.startsWith("") ){ //choicetag. choice och target. line = line.split("")[1]; //System.out.println("Line: "+line); String tmpLines[] = line.split(""); //tmplines[0] är choice strängen, tmpLines[1] är target. ((Komponent)components.getLast()).addChoice(tmpLines[0].trim(), tmpLines[1].trim()); } }//end of while myReader.close(); }catch(IOException ioe){ ioe.printStackTrace(); //System.exit(1); } return components; }//end of parse() //TEST /* public static void main(String args[]){ Parser p = new Parser("test.txt"); LinkedList ll = p.parse(); for(int a=0;a