kopia lustrzana https://github.com/jameshball/osci-render
Refactor FileParser
rodzic
5e13ea4595
commit
cc8b7dd39a
|
@ -1,11 +1,35 @@
|
||||||
package parser;
|
package parser;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
import javax.xml.parsers.ParserConfigurationException;
|
||||||
|
import org.xml.sax.SAXException;
|
||||||
import shapes.Shape;
|
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;
|
package parser;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
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;
|
import shapes.Shape;
|
||||||
|
|
||||||
public class SvgParser implements FileParser {
|
public class SvgParser extends FileParser {
|
||||||
|
|
||||||
@Override
|
private final List<? extends Shape> shapes;
|
||||||
public String getFileExtension() {
|
|
||||||
return ".svg";
|
static {
|
||||||
|
fileExtension = "svg";
|
||||||
|
}
|
||||||
|
|
||||||
|
public SvgParser(String path) throws IOException, SAXException, ParserConfigurationException {
|
||||||
|
super(path);
|
||||||
|
shapes = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<? extends Shape> parseFile(String path) {
|
protected void parseFile(String path)
|
||||||
return null;
|
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