-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSVGfix.java
98 lines (93 loc) · 3.3 KB
/
SVGfix.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import java.util.TreeMap;
import java.util.Set;
import java.util.Iterator;
import java.util.ArrayList;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.regex.Pattern;
public class SVGfix {
static private String msgNoFix = "File \"input.svg\" doesn't have colors in CSS format - no output generated";
static private TreeMap<String,String> tree = new TreeMap<String,String>();
public static void main(String[] args) throws FileNotFoundException {
Pattern path = Pattern.compile("<path");
FileInputStream fileIn = new FileInputStream("input.svg");
Scanner in = new Scanner(fileIn);
String previous = parseSymbols(in);
System.out.println(previous);
in.useDelimiter(path);
System.out.println(in.next());
do {
in.skip(path);
System.out.println(fixPath(in.next()));
} while (in.hasNext());
}
static private String parseSymbols(Scanner in) {
Pattern defs = Pattern.compile("<defs>");
Pattern style = Pattern.compile("</style>");
String previous = null;
String styles = null;
in.useDelimiter(defs);
try {
previous = in.next();
in.skip(defs);
in.skip(Pattern.compile("<style>"));
in.useDelimiter(style);
styles = in.next();
in.skip(style);
in.skip(Pattern.compile("</defs>"));
}
catch (java.util.NoSuchElementException e) {
System.out.println(msgNoFix);
System.exit(1);
}
fillTree(styles);
return previous;
}
static private void fillTree(String styles) {
String name;
String value;
int j;
for ( int i = 0; i < styles.length(); i++) {
while( styles.charAt(i) == ' ') i++;
if (styles.charAt(i) != '.' ){
System.out.println(
"Expected '.' but found '" + styles.charAt(i) +
"' at position " + (i) + " in <styles>"
);
System.exit(1);
}
i++;
for (j = i; styles.charAt(j) != '{'; j++);
name = styles.substring(i, j);
for (i = j+1; styles.charAt(i) != '}'; i++);
value = styles.substring(j+1, i);
tree.put(name, value);
}
}
static private String fixPath(String path) {
String name;
String value;
int i = 0;
int j, k;
while ( path.charAt(i) == ' ' ) i++;
if ( (path.charAt(i) == 'c') && (path.charAt(i+5) == '=') ) {
for (j = i + 8; path.charAt(j) != '"'; j++ );
name = path.substring(i+7, j);
if (!tree.containsKey(name)) {
System.out.println(
"Class named \"" + name + "\" doesn't exit in <styles>"
);
Set<String> keys = tree.keySet();
Iterator<String> s = keys.iterator();
while ( s.hasNext() ) System.out.println(s.next());
System.exit(1);
}
value = tree.get(name);
for (k = 0; value.charAt(k) != ':'; k++ );
return "<path " + value.substring(0,k) + "=\"" + value.substring(k+1) +
"\"" + path.substring(j+1);
}
return "<path " + path.substring(i);
}
}