Reformatted and refactored the fileparser to get xml from resource folder

#fix #refactor #story[377] #pair[xyi25, zyt10]
This commit is contained in:
Haoming Yin
2017-03-24 12:55:11 +13:00
parent e8b1720fee
commit 304f30ece6
12 changed files with 148 additions and 103 deletions
@@ -1,35 +1,37 @@
package seng302.models.parsers;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
/**
*
*
* Created by Haoming Yin (hyi25) on 16/3/2017
*/
public abstract class FileParser {
private String filePath;
private String filePath;
public FileParser(String path) {
this.filePath = path;
}
public FileParser(String path) {
this.filePath = path;
}
protected Document parseFile () {
try {
File file = new File(this.filePath);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(file);
// optional, in order to recover info from broken line.
doc.getDocumentElement().normalize();
return doc;
} catch (Exception e) {
e.printStackTrace();
return null;
}
protected Document parseFile() {
try {
InputStream is = getClass().getResourceAsStream(this.filePath);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(is);
// optional, in order to recover info from broken line.
doc.getDocumentElement().normalize();
return doc;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
}