Skip to content

Commit cfbf64a

Browse files
committed
add more attribute validators
Signed-off-by: Stefan Niederhauser <[email protected]>
1 parent 004d75b commit cfbf64a

File tree

15 files changed

+489
-210
lines changed

15 files changed

+489
-210
lines changed

graphviz-java/src/main/java/guru/nidi/graphviz/attribute/SingleAttributes.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@
2020

2121
class SingleAttributes<T, F extends For> implements Attributes<F> {
2222
protected final String key;
23-
protected final T value;
23+
public final T value;
2424

2525
protected SingleAttributes(String key, T value) {
2626
this.key = key;
2727
this.value = value;
2828
}
2929

30-
public <E> E key(String key) {
30+
protected <E> E key(String key) {
3131
return newInstance(key, value);
3232
}
3333

34-
public <E> E value(T value) {
34+
protected <E> E value(T value) {
3535
return newInstance(key, value);
3636
}
3737

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright © 2015 Stefan Niederhauser ([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package guru.nidi.graphviz.attribute.validate;
17+
18+
class AnyDatatype extends Datatype {
19+
AnyDatatype(String name) {
20+
super(name);
21+
}
22+
23+
@Override
24+
ValidatorMessage validate(Object value) {
25+
return null;
26+
}
27+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright © 2015 Stefan Niederhauser ([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package guru.nidi.graphviz.attribute.validate;
17+
18+
import java.util.HashSet;
19+
import java.util.Set;
20+
21+
import static guru.nidi.graphviz.attribute.validate.ValidatorMessage.Severity.ERROR;
22+
import static java.util.Arrays.asList;
23+
24+
class ArrowDatatype extends Datatype {
25+
ArrowDatatype() {
26+
super("arrow");
27+
}
28+
29+
@Override
30+
ValidatorMessage validate(Object value) {
31+
return new ArrowTypeValidator(value).validate();
32+
}
33+
34+
static class ArrowTypeValidator {
35+
private static final Set<Shape> SHAPES = new HashSet<>(asList(
36+
new Shape("box", true, true), new Shape("crow", true, false), new Shape("curve", true, false),
37+
new Shape("icurve", true, false), new Shape("diamond", true, true), new Shape("dot", false, true),
38+
new Shape("inv", true, true), new Shape("none", false, false), new Shape("normal", true, true),
39+
new Shape("tee", true, false), new Shape("vee", true, false)));
40+
41+
private final String s;
42+
private int pos;
43+
44+
ArrowTypeValidator(Object value) {
45+
s = value.toString();
46+
pos = 0;
47+
}
48+
49+
ValidatorMessage validate() {
50+
int shapes = 0;
51+
do {
52+
final ValidatorMessage message = validateShape();
53+
if (message != null) {
54+
return message;
55+
}
56+
shapes++;
57+
} while (pos < s.length() && shapes < 4);
58+
if (pos < s.length() - 1) {
59+
return new ValidatorMessage(ERROR, "More than 4 shapes in '" + s + "'.");
60+
}
61+
if (shapes > 1 && s.endsWith("none")) {
62+
return new ValidatorMessage(ERROR, "Last shape cannot be 'none' in '" + s + "'.");
63+
}
64+
return null;
65+
}
66+
67+
private ValidatorMessage validateShape() {
68+
boolean o = false;
69+
if (pos < s.length() && s.charAt(pos) == 'o') {
70+
o = true;
71+
pos++;
72+
}
73+
boolean lr = false;
74+
if (pos < s.length() && (s.charAt(pos) == 'l' || s.charAt(pos) == 'r')) {
75+
lr = true;
76+
pos++;
77+
}
78+
return validateShape(o, lr);
79+
}
80+
81+
private ValidatorMessage validateShape(boolean o, boolean lr) {
82+
final Shape shape = findShape();
83+
if (shape == null) {
84+
return new ValidatorMessage(ERROR, "Unknown shape '" + s.substring(pos) + "'.");
85+
}
86+
if (o && !shape.o) {
87+
return new ValidatorMessage(ERROR, "Shape '" + shape.name + "' is not allowed a 'o' prefix.");
88+
}
89+
if (lr && !shape.lr) {
90+
return new ValidatorMessage(ERROR, "Shape '" + shape.name + "' is not allowed a 'l'/'r' prefix.");
91+
}
92+
return null;
93+
}
94+
95+
private Shape findShape() {
96+
for (final Shape shape : SHAPES) {
97+
if (s.substring(pos, Math.min(s.length(), pos + shape.name.length())).equals(shape.name)) {
98+
pos += shape.name.length();
99+
return shape;
100+
}
101+
}
102+
return null;
103+
}
104+
105+
static class Shape {
106+
final String name;
107+
final boolean lr;
108+
final boolean o;
109+
110+
Shape(String name, boolean lr, boolean o) {
111+
this.name = name;
112+
this.lr = lr;
113+
this.o = o;
114+
}
115+
}
116+
}
117+
}

graphviz-java/src/main/java/guru/nidi/graphviz/attribute/validate/ArrowTypeValidator.java

Lines changed: 0 additions & 106 deletions
This file was deleted.

graphviz-java/src/main/java/guru/nidi/graphviz/attribute/validate/AttributeConfigs.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,25 +56,25 @@ static List<AttributeConfig> get(String name) {
5656
add("concentrate", entry("G", BOOL, false));
5757
add("constraint", entry("E", BOOL, true).engines(DOT));
5858
add("decorate", entry("E", BOOL, false));
59-
add("defaultdist", entry("G", DOUBLE, "1+(avg. len)*sqrt(|V|)", EPSILON).engines(NEATO));
59+
add("defaultdist", entry("G", DOUBLE, "1+(avg. len)*sqrt(|V|)", EPSILON).engines(NEATO)); //TODO
6060
add("dim", entry("G", INT, 2, 2.0).engines(SFDP, FDP, NEATO));
6161
add("dimen", entry("G", INT, 2, 2.0).engines(SFDP, FDP, NEATO));
62-
add("dir", entry("E", DIR_TYPE, "forward(directed)<BR>none(undirected)"));
62+
add("dir", entry("E", DIR_TYPE, "forward(directed)<BR>none(undirected)")); //TODO
6363
add("diredgeconstraints", entry("G", asList(STRING, BOOL), false).engines(NEATO));
6464
add("distortion", entry("N", DOUBLE, 0.0, -100.0));
6565
add("dpi", entry("G", DOUBLE, 72.0).formats(SVG, BITMAP));
6666
add("edgeURL", entry("E", ESC_STRING, "").formats(SVG, MAP));
6767
add("edgehref", entry("E", ESC_STRING, "").formats(SVG, MAP));
6868
add("edgetarget", entry("E", ESC_STRING).formats(SVG, MAP));
6969
add("edgetooltip", entry("E", ESC_STRING, "").formats(SVG, CMAP));
70-
add("epsilon", entry("G", DOUBLE, ".0001 * # nodes(mode == KK)<BR>.0001(mode == major)").engines(NEATO));
71-
add("esep", entry("G", asList(ADD_DOUBLE, ADD_POINT), "+3").engines(NOT_DOT));
72-
add("fillcolor", entry("NEC", asList(COLOR, COLOR_LIST), "lightgrey(nodes)<BR>black(clusters"));
70+
add("epsilon", entry("G", DOUBLE, ".0001 * # nodes(mode == KK)<BR>.0001(mode == major)").engines(NEATO)); //TODO
71+
add("esep", entry("G", asList(ADD_DOUBLE, ADD_POINT), "+3").engines(NOT_DOT)); //TODO
72+
add("fillcolor", entry("NEC", asList(COLOR, COLOR_LIST), "lightgrey(nodes)<BR>black(clusters")); //TODO
7373
add("fixedsize", entry("N", asList(BOOL, STRING), false));
7474
add("fontcolor", entry("ENGC", COLOR, "black"));
75-
add("fontname", entry("ENGC", STRING, "Times-Roman"));
75+
add("fontname", entry("ENGC", STRING, "Times-Roman")); //TODO
7676
add("fontnames", entry("G", STRING, "").formats(SVG));
77-
add("fontpath", entry("G", STRING, "system-dependent"));
77+
add("fontpath", entry("G", STRING, "system-dependent")); //TODO
7878
add("fontsize", entry("ENGC", DOUBLE, 14.0, 1.0));
7979
add("forcelabels", entry("G", BOOL, true));
8080
add("gradientangle", entry("NCG", INT, 0));
@@ -95,37 +95,38 @@ static List<AttributeConfig> get(String name) {
9595
add("imagepos", entry("N", STRING, "mc"));
9696
add("imagescale", entry("N", BOOL, false));
9797
add("inputscale", entry("G", DOUBLE).engines(FDP, NEATO));
98-
add("label", entry("ENGC", LBL_STRING, "\\N(nodes)<BR>''(otherwise)"));
98+
add("label", entry("ENGC", LBL_STRING, "\\N(nodes)<BR>''(otherwise)")); //TODO
9999
add("labelURL", entry("E", ESC_STRING, "").formats(SVG, MAP));
100100
add("label_scheme", entry("G", INT, 0, 0.0).engines(SFDP));
101101
add("labelangle", entry("E", DOUBLE, -25.0, -180.0));
102102
add("labeldistance", entry("E", DOUBLE, 1.0, 0.0));
103103
add("labelfloat", entry("E", BOOL, false));
104104
add("labelfontcolor", entry("E", COLOR, "black"));
105-
add("labelfontname", entry("E", STRING, "Times-Roman"));
105+
add("labelfontname", entry("E", STRING, "Times-Roman")); //TODO
106106
add("labelfontsize", entry("E", DOUBLE, 14.0, 1.0));
107107
add("labelhref", entry("E", ESC_STRING, "").formats(SVG, MAP));
108108
add("labeljust", entry("GC", STRING, "c"));
109-
add("labelloc", entry("NGC", STRING, "'t'(clusters)<BR>'b'(root graphs)<BR>'c'(nodes)"));
109+
add("labelloc", entry("NGC", STRING, "'t'(clusters)<BR>'b'(root graphs)<BR>'c'(nodes)")); //TODO
110110
add("labeltarget", entry("E", ESC_STRING).formats(SVG, MAP));
111111
add("labeltooltip", entry("E", ESC_STRING, "").formats(SVG, CMAP));
112112
add("landscape", entry("G", BOOL, false));
113113
add("layer", entry("ENC", LAYER_RANGE, ""));
114114
add("layerlistsep", entry("G", STRING, ","));
115115
add("layers", entry("G", LAYER_LIST, ""));
116116
add("layerselect", entry("G", LAYER_RANGE, ""));
117-
add("layersep", entry("G", STRING, ":\\t"));
117+
add("layersep", entry("G", STRING, ":\\t")); //TODO
118118
add("layout", entry("G", STRING, ""));
119-
add("len", entry("E", DOUBLE, "1.0(neato)<BR>0.3(fdp)").engines(FDP, NEATO));
119+
add("len", entry("E", DOUBLE, "1.0(neato)<BR>0.3(fdp)").engines(FDP, NEATO)); //TODO
120120
add("levels", entry("G", INT, Integer.MAX_VALUE, 0.0).engines(SFDP));
121121
add("levelsgap", entry("G", DOUBLE, 0.0).engines(NEATO));
122122
add("lhead", entry("E", STRING, "").engines(DOT));
123123
add("lheight", entry("GC", DOUBLE).formats(WRITE));
124124
add("lp", entry("EGC", POINT).formats(WRITE));
125125
add("ltail", entry("E", STRING, "").engines(DOT));
126126
add("lwidth", entry("GC", DOUBLE).formats(WRITE));
127-
add("margin", entry("NCG", asList(DOUBLE, POINT), "&#60;device-dependent&#62;"));
127+
add("margin", entry("NCG", asList(DOUBLE, POINT), "&#60;device-dependent&#62;")); //TODO
128128
add("maxiter", entry("G", INT, "100 nodes(mode == KK)<BR>200(mode == major)<BR>600(fdp)").engines(FDP, NEATO));
129+
//TODO
129130
add("mclimit", entry("G", DOUBLE, 1.0).engines(DOT));
130131
add("mindist", entry("G", DOUBLE, 1.0, 0.0).engines(CIRCO));
131132
add("minlen", entry("E", INT, 1, 0.0).engines(DOT));
@@ -146,37 +147,39 @@ static List<AttributeConfig> get(String name) {
146147
add("outputorder", entry("G", OUTPUT_MODE, "breadthfirst"));
147148
add("overlap", entry("G", asList(STRING, BOOL), true).engines(NOT_DOT));
148149
add("overlap_scaling", entry("G", DOUBLE, -4.0, -1.0e10)); //TODO prism only
149-
add("overlap_shrink", entry("G", BOOL, true)); //TOOO prism only
150+
add("overlap_shrink", entry("G", BOOL, true)); //TODO prism only
150151
add("pack", entry("G", asList(BOOL, INT), false));
151152
add("packmode", entry("G", PACK_MODE, "node"));
152153
add("pad", entry("G", asList(DOUBLE, POINT), 0.0555));
153154
add("page", entry("G", asList(DOUBLE, POINT)));
154155
add("pagedir", entry("G", PAGE_DIR, "BL"));
155156
add("pencolor", entry("C", COLOR, "black"));
156157
add("penwidth", entry("CNE", DOUBLE, 1.0, 0.0));
157-
add("peripheries", entry("NC", INT, "shape default(nodes)<BR>1(clusters)", 0.0));
158+
add("peripheries", entry("NC", INT, "shape default(nodes)<BR>1(clusters)", 0.0)); //TODO
158159
add("pin", entry("N", BOOL, false).engines(FDP, NEATO));
159160
add("pos", entry("EN", asList(POINT, SPLINE_TYPE)));
160161
add("quadtree", entry("G", asList(QUAD_TYPE, BOOL), "normal").engines(SFDP));
161162
add("quantum", entry("G", DOUBLE, 0.0, 0.0));
162163
add("rank", entry("S", RANK_TYPE).engines(DOT));
163164
add("rankdir", entry("G", RANK_DIR, "TB").engines(DOT));
164165
add("ranksep", entry("G", asList(DOUBLE, DOUBLE_LIST), "0.5(dot)<BR>1.0(twopi)", 0.02).engines(TWOPI, DOT));
166+
//TODO
165167
add("ratio", entry("G", asList(DOUBLE, STRING)));
166168
add("rects", entry("N", RECT).formats(WRITE));
167169
add("regular", entry("N", BOOL, false));
168170
add("remincross", entry("G", BOOL, true).engines(DOT));
169171
add("repulsiveforce", entry("G", DOUBLE, 1.0, 0.0).engines(SFDP));
170172
add("resolution", entry("G", DOUBLE, 72.0).formats(SVG, BITMAP));
171173
add("root", entry("GN", asList(STRING, BOOL), "&#60;none&#62;(graphs)<BR>false(nodes)").engines(CIRCO, TWOPI));
174+
//TODO
172175
add("rotate", entry("G", INT, 0));
173176
add("rotation", entry("G", DOUBLE, 0).engines(SFDP));
174177
add("samehead", entry("E", STRING, "").engines(DOT));
175178
add("sametail", entry("E", STRING, "").engines(DOT));
176-
add("samplepoints", entry("N", INT, "8(output)<BR>20(overlap and image maps)"));
179+
add("samplepoints", entry("N", INT, "8(output)<BR>20(overlap and image maps)")); //TODO
177180
add("scale", entry("G", asList(DOUBLE, POINT)).engines(NOT_DOT));
178181
add("searchsize", entry("G", INT, 30).engines(DOT));
179-
add("sep", entry("G", asList(ADD_DOUBLE, ADD_POINT), "+4").engines(NOT_DOT));
182+
add("sep", entry("G", asList(ADD_DOUBLE, ADD_POINT), "+4").engines(NOT_DOT)); //TODO
180183
add("shape", entry("N", SHAPE, "ellipse"));
181184
add("shapefile", entry("N", STRING, ""));
182185
add("showboxes", entry("ENG", INT, 0, 0.0).engines(DOT));

0 commit comments

Comments
 (0)