Skip to content

SVGElement: store String instead of StyleAttribute in map #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 17 additions & 63 deletions svg-core/src/main/java/com/kitfox/svg/SVGElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ abstract public class SVGElement implements Serializable
/**
* Styles defined for this elemnt via the <b>style</b> attribute.
*/
protected final HashMap<String, StyleAttribute> inlineStyles = new HashMap<String, StyleAttribute>();
protected final HashMap<String, String> inlineStyles = new HashMap<String, String>();
/**
* Presentation attributes set for this element. Ie, any attribute other
* than the <b>style</b> attribute.
*/
protected final HashMap<String, StyleAttribute> presAttribs = new HashMap<String, StyleAttribute>();
protected final HashMap<String, String> presAttribs = new HashMap<String, String>();
/**
* A list of presentation attributes to not include in the presentation
* attribute set.
Expand Down Expand Up @@ -303,7 +303,7 @@ public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElem
}
String value = attrs.getValue(i);

presAttribs.put(name, new StyleAttribute(name, value));
presAttribs.put(name, value);
}
}

Expand Down Expand Up @@ -341,10 +341,10 @@ public void addAttribute(String name, int attribType, String value) throws SVGEl
switch (attribType)
{
case AnimationElement.AT_CSS:
inlineStyles.put(name, new StyleAttribute(name, value));
inlineStyles.put(name, value);
return;
case AnimationElement.AT_XML:
presAttribs.put(name, new StyleAttribute(name, value));
presAttribs.put(name, value);
return;
}

Expand Down Expand Up @@ -538,56 +538,6 @@ public boolean getStyle(StyleAttribute attrib) throws SVGException
return getStyle(attrib, true);
}

public void setAttribute(String name, int attribType, String value) throws SVGElementException
{
StyleAttribute styAttr;


switch (attribType)
{
case AnimationElement.AT_CSS:
{
styAttr = (StyleAttribute) inlineStyles.get(name);
break;
}
case AnimationElement.AT_XML:
{
styAttr = (StyleAttribute) presAttribs.get(name);
break;
}
case AnimationElement.AT_AUTO:
{
styAttr = (StyleAttribute) inlineStyles.get(name);

if (styAttr == null)
{
styAttr = (StyleAttribute) presAttribs.get(name);
}
break;
}
default:
throw new SVGElementException(this, "Invalid attribute type " + attribType);
}

if (styAttr == null)
{
throw new SVGElementException(this, "Could not find attribute " + name + "(" + AnimationElement.animationElementToString(attribType) + "). Make sure to create attribute before setting it.");
}

//Alter layout for relevant attributes
if ("id".equals(styAttr.getName()))
{
if (diagram != null)
{
diagram.removeElement(this.id);
diagram.setElement(value, this);
}
this.id = value;
}

styAttr.setStringValue(value);
}

public boolean getStyle(StyleAttribute attrib, boolean recursive) throws SVGException
{
return getStyle(attrib, recursive, true);
Expand All @@ -612,9 +562,9 @@ public boolean getStyle(StyleAttribute attrib, boolean recursive, boolean evalAn
String styName = attrib.getName();

//Check for local inline styles
StyleAttribute styAttr = (StyleAttribute)inlineStyles.get(styName);
String styAttr = inlineStyles.get(styName);

attrib.setStringValue(styAttr == null ? "" : styAttr.getStringValue());
attrib.setStringValue(styAttr == null ? "" : styAttr);

//Evalutate coresponding track, if one exists
if (evalAnimation)
Expand All @@ -635,9 +585,9 @@ public boolean getStyle(StyleAttribute attrib, boolean recursive, boolean evalAn


//Check for presentation attribute
StyleAttribute presAttr = (StyleAttribute)presAttribs.get(styName);
String presAttr = presAttribs.get(styName);

attrib.setStringValue(presAttr == null ? "" : presAttr.getStringValue());
attrib.setStringValue(presAttr == null ? "" : presAttr);

//Evalutate coresponding track, if one exists
if (evalAnimation)
Expand Down Expand Up @@ -694,7 +644,9 @@ public boolean getStyle(StyleAttribute attrib, boolean recursive, boolean evalAn
public StyleAttribute getStyleAbsolute(String styName)
{
//Check for local inline styles
return (StyleAttribute) inlineStyles.get(styName);
final String value = inlineStyles.get(styName);

return value != null ? new StyleAttribute(styName, value) : null;
}

/**
Expand All @@ -709,10 +661,10 @@ public boolean getPres(StyleAttribute attrib) throws SVGException
String presName = attrib.getName();

//Make sure we have a coresponding presentation attribute
StyleAttribute presAttr = (StyleAttribute) presAttribs.get(presName);
String presAttr = presAttribs.get(presName);

//Copy presentation value directly
attrib.setStringValue(presAttr == null ? "" : presAttr.getStringValue());
attrib.setStringValue(presAttr == null ? "" : presAttr);

//Evalutate coresponding track, if one exists
TrackBase track = trackManager.getTrack(presName, AnimationElement.AT_XML);
Expand Down Expand Up @@ -740,7 +692,9 @@ public boolean getPres(StyleAttribute attrib) throws SVGException
public StyleAttribute getPresAbsolute(String styName)
{
//Check for local inline styles
return (StyleAttribute) presAttribs.get(styName);
final String value = presAttribs.get(styName);

return value != null ? new StyleAttribute(styName, value) : null;
}

private static final Pattern TRANSFORM_PATTERN = Pattern.compile("\\w+\\([^)]*\\)");
Expand Down
8 changes: 4 additions & 4 deletions svg-core/src/main/java/com/kitfox/svg/xml/XMLParseUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -794,8 +794,8 @@ public static String[] getElementArrayString(Element root, String name, String a
* @param styleString - A CSS formatted string of styles. Eg,
* "font-size:12;fill:#d32c27;fill-rule:evenodd;stroke-width:1pt;"
*/
public static HashMap<String, StyleAttribute> parseStyle(String styleString) {
return parseStyle(styleString, new HashMap<String, StyleAttribute>());
public static HashMap<String, String> parseStyle(String styleString) {
return parseStyle(styleString, new HashMap<String, String>());
}

/**
Expand All @@ -804,7 +804,7 @@ public static HashMap<String, StyleAttribute> parseStyle(String styleString) {
* "font-size:12;fill:#d32c27;fill-rule:evenodd;stroke-width:1pt;"
* @param map - A map to which these styles will be added
*/
public static HashMap<String, StyleAttribute> parseStyle(String styleString, HashMap<String, StyleAttribute> map) {
public static HashMap<String, String> parseStyle(String styleString, HashMap<String, String> map) {
final Pattern patSemi = Pattern.compile(";");

String[] styles = patSemi.split(styleString);
Expand All @@ -825,7 +825,7 @@ public static HashMap<String, StyleAttribute> parseStyle(String styleString, Has
String key = styles[i].substring(0, colon).trim();
String value = quoteMatch.reset(styles[i].substring(colon + 1).trim()).replaceAll("");

map.put(key, new StyleAttribute(key, value));
map.put(key, value);
}

return map;
Expand Down