Implement SVG pre-processing

pull/35/head
James Ball 2020-10-24 16:54:38 +01:00
rodzic 0b2565d981
commit 9361e8bdea
3 zmienionych plików z 50 dodań i 6 usunięć

Wyświetl plik

@ -1,8 +1,11 @@
package parser;
import static parser.XmlUtil.asList;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@ -25,17 +28,57 @@ public class SvgParser extends FileParser {
shapes = new ArrayList<>();
}
@Override
protected void parseFile(String path)
throws ParserConfigurationException, IOException, SAXException, IllegalArgumentException {
private Document getSvgDocument(String path)
throws IOException, SAXException, ParserConfigurationException {
// opens XML reader for svg file.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder builder = factory.newDocumentBuilder();
File file = new File(path);
Document doc = builder.parse(file);
for (Node elem : doc.getElementsByTagName("path"))
return builder.parse(file);
}
private String[] preProcessPath(String path) throws IllegalArgumentException {
// Replace all commas with spaces and then remove unnecessary whitespace
path = path.replace(',', ' ');
path = path.replaceAll("\\s+", " ");
path = path.replaceAll("(^\\s|\\s$)", "");
// If there are any characters in the path that are illegal
if (path.matches("[^mlhvcsqtazMLHVCSQTAZ\\-.\\d\\s]")) {
throw new IllegalArgumentException("Illegal characters in SVG path.");
// If there are more than 1 letters or delimiters next to one another
} else if (path.matches("[a-zA-Z.\\-]{2,}")) {
throw new IllegalArgumentException("Multiple letters or delimiters found next to one another in SVG path.");
// First character in path must be a command
} else if (path.matches("^[a-zA-Z]")) {
throw new IllegalArgumentException("Start of SVG path is not a letter.");
}
// Split on SVG path characters to get a list of instructions, keeping the SVG commands
return path.split("(?=[mlhvcsqtazMLHVCSQTAZ])");
}
private List<String> getSvgPathAttributes(Document svg) {
List<String> paths = new ArrayList<>();
for (Node elem : asList(svg.getElementsByTagName("path"))) {
paths.add(elem.getAttributes().getNamedItem("d").getNodeValue());
}
return paths;
}
@Override
protected void parseFile(String filePath)
throws ParserConfigurationException, IOException, SAXException, IllegalArgumentException {
Document svg = getSvgDocument(filePath);
for (String path : getSvgPathAttributes(svg)) {
preProcessPath(path);
}
}
@Override

Wyświetl plik

@ -14,6 +14,7 @@ public final class XmlUtil {
return n.getLength()==0?
Collections.emptyList(): new NodeListWrapper(n);
}
static final class NodeListWrapper extends AbstractList<Node>
implements RandomAccess {
private final NodeList list;

Wyświetl plik

@ -3,7 +3,7 @@ import shapes.Line;
import static org.junit.Assert.*;
public class TestSuite {
public class LineTest {
// TODO: Create tests for shapes.Shapes class.
@Test