diff --git a/src/parser/FileParser.java b/src/parser/FileParser.java index f987965..73180aa 100644 --- a/src/parser/FileParser.java +++ b/src/parser/FileParser.java @@ -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 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 getShapes(); } diff --git a/src/parser/SvgParser.java b/src/parser/SvgParser.java index 493fe4b..72b31c4 100644 --- a/src/parser/SvgParser.java +++ b/src/parser/SvgParser.java @@ -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 shapes; + + static { + fileExtension = "svg"; + } + + public SvgParser(String path) throws IOException, SAXException, ParserConfigurationException { + super(path); + shapes = new ArrayList<>(); } @Override - public List 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 getShapes() { + return shapes; } }