private static final Pattern PATH_REGEX = Pattern.compile("[a-zA-Z]|[-0-9.]+");
public static AbstractCanvasObject createShape(Element elt) {
String name = elt.getTagName();
AbstractCanvasObject ret;
if (name.equals("ellipse")) {
ret = createOval(elt);
} else if (name.equals("line")) {
ret = createLine(elt);
} else if (name.equals("path")) {
ret = createPath(elt);
} else if (name.equals("polyline")) {
ret = createPolyline(elt);
} else if (name.equals("polygon")) {
ret = createPolygon(elt);
} else if (name.equals("rect")) {
ret = createRectangle(elt);
} else if (name.equals("text")) {
ret = createText(elt);
} else {
return null;
}
List<Attribute<?>> attrs = ret.getAttributes();
if (attrs.contains(DrawAttr.PAINT_TYPE)) {
String stroke = elt.getAttribute("stroke");
String fill = elt.getAttribute("fill");
if (stroke.equals("") || stroke.equals("none")) {
ret.setValue(DrawAttr.PAINT_TYPE, DrawAttr.PAINT_FILL);
} else if (fill.equals("none")) {
ret.setValue(DrawAttr.PAINT_TYPE, DrawAttr.PAINT_STROKE);
} else {
ret.setValue(DrawAttr.PAINT_TYPE, DrawAttr.PAINT_STROKE_FILL);
}
}
attrs = ret.getAttributes(); // since changing paintType could change it
if (attrs.contains(DrawAttr.STROKE_WIDTH) && elt.hasAttribute("stroke-width")) {
Integer width = Integer.valueOf(elt.getAttribute("stroke-width"));
ret.setValue(DrawAttr.STROKE_WIDTH, width);
}
if (attrs.contains(DrawAttr.STROKE_COLOR)) {
String color = elt.getAttribute("stroke");
String opacity = elt.getAttribute("stroke-opacity");
if (!color.equals("none")) {
ret.setValue(DrawAttr.STROKE_COLOR, getColor(color, opacity));
}
}
if (attrs.contains(DrawAttr.FILL_COLOR)) {
String color = elt.getAttribute("fill");
if (color.equals("")) color = "#000000";
String opacity = elt.getAttribute("fill-opacity");
if (!color.equals("none")) {
ret.setValue(DrawAttr.FILL_COLOR, getColor(color, opacity));
}
}
return ret;
}