kopia lustrzana https://github.com/jameshball/osci-render
Refactor FileParser
rodzic
5e13ea4595
commit
cc8b7dd39a
|
@ -1,11 +1,35 @@
|
|||
package parser;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import org.xml.sax.SAXException;
|
||||
import shapes.Shape;
|
||||
|
||||
public interface FileParser {
|
||||
public abstract class FileParser {
|
||||
|
||||
String getFileExtension();
|
||||
public static String fileExtension;
|
||||
|
||||
List<? extends Shape> parseFile(String path);
|
||||
public static String getFileExtension() {
|
||||
return fileExtension;
|
||||
}
|
||||
|
||||
public FileParser(String path) throws IOException, SAXException, ParserConfigurationException {
|
||||
checkFileExtension(path);
|
||||
parseFile(path);
|
||||
}
|
||||
|
||||
private static void checkFileExtension(String path) throws IllegalArgumentException {
|
||||
Pattern pattern = Pattern.compile("\\." + getFileExtension() + "$");
|
||||
if (!pattern.matcher(path).find()) {
|
||||
throw new IllegalArgumentException(
|
||||
"File to parse is not a ." + getFileExtension() + " file.");
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void parseFile(String path)
|
||||
throws ParserConfigurationException, IOException, SAXException, IllegalArgumentException;
|
||||
|
||||
public abstract List<? extends Shape> getShapes();
|
||||
}
|
||||
|
|
|
@ -1,17 +1,42 @@
|
|||
package parser;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import org.w3c.dom.Document;
|
||||
import org.xml.sax.SAXException;
|
||||
import shapes.Shape;
|
||||
|
||||
public class SvgParser implements FileParser {
|
||||
public class SvgParser extends FileParser {
|
||||
|
||||
@Override
|
||||
public String getFileExtension() {
|
||||
return ".svg";
|
||||
private final List<? extends Shape> shapes;
|
||||
|
||||
static {
|
||||
fileExtension = "svg";
|
||||
}
|
||||
|
||||
public SvgParser(String path) throws IOException, SAXException, ParserConfigurationException {
|
||||
super(path);
|
||||
shapes = new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<? extends Shape> parseFile(String path) {
|
||||
return null;
|
||||
protected void parseFile(String path)
|
||||
throws ParserConfigurationException, IOException, SAXException, IllegalArgumentException {
|
||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
factory.setValidating(true);
|
||||
factory.setIgnoringElementContentWhitespace(true);
|
||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
||||
File file = new File(path);
|
||||
Document doc = builder.parse(file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<? extends Shape> getShapes() {
|
||||
return shapes;
|
||||
}
|
||||
}
|
||||
|
|
Ładowanie…
Reference in New Issue