Advertisement
Guest User

Untitled

a guest
Nov 28th, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. package edt;
  2.  
  3. import edt.core.Author;
  4. import edt.core.Section;
  5. import edt.core.Document;
  6. import edt.core.Paragraph;
  7.  
  8. import java.io.*;
  9.  
  10. public class Parser {
  11.  
  12.     private Document _document;
  13.     private Section _curSection;
  14.  
  15.     public Parser() {
  16.     }
  17.  
  18.     public static void main(String[] args) throws Exception {
  19.         String fileName = System.getProperty("import");
  20.        
  21.         Parser parser = new Parser();
  22.  
  23.         Document doc = parser.parse(fileName);
  24.  
  25.         System.out.println(doc.getContent());
  26.     }
  27.  
  28.     public Document parse(String filename) {
  29.         BufferedReader reader = null;
  30.  
  31.         try {
  32.             reader = new BufferedReader(new FileReader(filename));
  33.             _document = new Document();
  34.             _curSection = _document;
  35.  
  36.             parseTitle(reader.readLine());
  37.             parseAuthors(reader.readLine());
  38.  
  39.             String line;
  40.  
  41.             while ((line = reader.readLine()) != null)
  42.                 parseLineContent(line);
  43.  
  44.             //return _document;
  45.         }
  46.         catch (IOException ex) {
  47.             ex.printStackTrace();
  48.         }
  49.         finally {
  50.             try {
  51.                 if (reader != null)
  52.                     reader.close();
  53.             }
  54.             catch (IOException ex) {
  55.                 ex.printStackTrace();
  56.             }
  57.         }
  58.        
  59.         return _document;
  60.     }
  61.  
  62.     private void parseTitle(String title) {
  63.         _document.setTitle(title);
  64.     }
  65.  
  66.     private void parseAuthors(String authors) {
  67.         for(String author : authors.split("\\|")) {
  68.                 String[] str = author.split("\\/");
  69.                 _document.addAuthor(new Author(str[0], str[1]));
  70.             }
  71.     }
  72.            
  73.     private void parseLineContent(String line) {
  74.         String[] content = line.split("\\|");
  75.  
  76.         switch(content[0]) {
  77.         case "SECTION":  // create new section
  78.             _curSection = new Section(content[2]);
  79.             _document.addSection(-1, _curSection);
  80.             if (content[1].length() > 0)
  81.                 _document.indexElement(content[1], _curSection);
  82.             break;
  83.            
  84.         case "PARAGRAPH":  // create new paragraph
  85.             Paragraph par = new Paragraph(content[1]);
  86.             _curSection.addParagraph(-1, par);
  87.             break;
  88.  
  89.         default:
  90.             System.err.println("Error in parsing content. Invalid Token " + line);
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement