Za tiste, ki ste se odločili, da boste zadomačo nalogo izbrali zahodne
filozofe, sem napisal en parser, ki poparsa tisto vhodno datoteko iz
neta v java.util.List, kjer so objekti tipa Person. Person je pa inner
class v projektu. Parse metoda je nepregledno napisana vendar dela :).

Vse kar morate spremeniti je vaš file location: (ni mi še uspelo, da
bi jih relativno na projekt bral)
final String FILE_LOCATION = "C:\\phil_tline.txt";

Še koda:

import java.util.List;
import java.util.LinkedList;

public class MyDemo extends PApplet {  // Objektno dedovan applet

  void setup()
  {
    final String FILE_LOCATION = "C:\\phil_tline.txt";
    size(640, 480);
    List personList = parseFile(FILE_LOCATION);
    this.printOutPersonList(personList);

  }

  void draw()
  {
    background(255);

  }

  public List parseFile(String fileLocation){
          List personList = new LinkedList();
          InputStream is = null;

    try {
          is = new FileInputStream(new File(fileLocation));
        
          BufferedReader br = new BufferedReader(new InputStreamReader(is));
          String line;
                while((line=br.readLine()) != null)
                        {
                         boolean bce = false;
                         boolean bceCe = false;
                        //Author regex
                        Pattern pat = Pattern.compile("^(\\D*)(c\\.)");
                        Matcher mat = pat.matcher(line);

                        //Birth regex
                        Pattern pat1 = Pattern.compile("(\\d*)");

                        //Death regex
                        Pattern pat2 = Pattern.compile("\\b(\\d*)");
                        //Get Living 1
                        Pattern pat3 = Pattern.compile("BCE");
                        //Get Living 2
                        Pattern pat4 = Pattern.compile("CE");

                        //Get description
                        Pattern pat5 = Pattern.compile("(\\.)(.*)(\\.)");


                          if(mat.find())
                          {
                            String person = mat.group();
                            //Get the ".c" out
                            person = person.substring(0, person.length() - 2);
                            //Delete person from line
                            String startAge = mat.replaceFirst("");
                            //Get wrid of whitespace
                            if(startAge.charAt(0) == ' ')
                            startAge = startAge.substring(1, startAge.length());

                            String ageStr = "";
                            mat = pat1.matcher(startAge);
                            String deathStr = "";
                            if(mat.find()){
                              ageStr = mat.group();
                              deathStr = mat.replaceFirst("");

                            }
                            int age = Integer.valueOf(ageStr).intValue();

                            mat = pat3.matcher(deathStr);
                            if(mat.find()){
                              bce = true;
                              deathStr = mat.replaceAll("");
                              }

                            mat = pat4.matcher(deathStr);
                            if(mat.find()){
                              bceCe = true;
                              }

                            mat = pat2.matcher(deathStr);

                            String deathS = "";
                            if(mat.find()){
                              deathS = mat.group();
                              deathStr = mat.replaceFirst("");
                            }
                            int death = Integer.valueOf(deathS).intValue();

                            mat = pat5.matcher(deathStr);
                            String desc = "";
                            if(mat.find()){
                                desc = mat.group();
                                desc = desc.substring(1, desc.length()-1);
                              }
                            //System.out.println("START" + deathStr + "END");
                            if(bce){
                              age = 0 -age;
                              death = 0 -death;
                              if(bceCe)
                                death = Math.abs(death);
                              }
                            //System.out.println("DATA: PERSON" +
person + " Birth:" + age + " Death:" + death + " Desc:" + desc);
                            Person per = new Person(person, age, death, desc);
                          personList.add(per);
                          }
                        //System.out.println(line);
                        }
         }
         catch (IOException e) {
                e.printStackTrace();
        }

      return personList;
    }

  public void printOutPersonList(List list){
        for(int i = 0; i < list.size() ; i++){
                Person temp = (Person)list.get(i);
                System.out.println("Name: " + temp.getName() + " Birth: " +
temp.getBorn() + " Died: "  +
                                   temp.getDied() + " Description: " +
temp.getDescription());
        }
  }

 public class Person{
        private String name;
        private int born;
        private int died;
        private String description;

        public Person(String name,
                      int born,
                      int died,
                      String description){

        this.name = name;
        this.born = born;
        this.died = died;
        this.description = description;
        }

        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
        }
        public int getBorn() {
                return born;
        }
        public void setBorn(int born) {
                this.born = born;
        }
        public int getDied() {
                return died;
        }
        public void setDied(int died) {
                this.died = died;
        }
        public String getDescription() {
                return description;
        }
        public void setDescription(String description) {
                this.description = description;
        }
        }
}

LP, Domen

Reply via email to