Skip to content

Commit

Permalink
Simplify a number of statements and expressions, code style changes
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosame committed Oct 12, 2024
1 parent cf1a2fb commit d4870ff
Show file tree
Hide file tree
Showing 37 changed files with 142 additions and 238 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,9 @@ public static SVGElement getFarthestViewportElement(Element elt) {
public static SVGRect getBBox(Element elt) {
final SVGOMElement svgelt = (SVGOMElement) elt;
SVGContext svgctx = svgelt.getSVGContext();
if (svgctx == null)
return null;
if (svgctx.getBBox() == null)
if (svgctx == null || svgctx.getBBox() == null) {
return null;
}

return new SVGRect() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ public SVGLength getItem(int index) throws DOMException {
*/
@Override
protected String getValueAsString() {
if (itemList.size() == 0) {
if (itemList.isEmpty()) {
return "";
}
StringBuilder sb = new StringBuilder(itemList.size() * 8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ public SVGNumber getItem(int index) throws DOMException {
*/
@Override
protected String getValueAsString() {
if (itemList.size() == 0) {
if (itemList.isEmpty()) {
return "";
}
StringBuilder sb = new StringBuilder(itemList.size() * 8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ public SVGPathSeg getItem(int index) throws DOMException {
*/
@Override
protected String getValueAsString() {
if (itemList.size() == 0) {
if (itemList.isEmpty()) {
return "";
}
StringBuilder sb = new StringBuilder(itemList.size() * 8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ public SVGPoint getItem(int index) throws DOMException {
*/
@Override
protected String getValueAsString() {
if (itemList.size() == 0) {
if (itemList.isEmpty()) {
return "";
}
StringBuilder sb = new StringBuilder(itemList.size() * 8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ public SVGTransform getItem(int index) throws DOMException {
*/
@Override
protected String getValueAsString() {
if (itemList.size() == 0) {
if (itemList.isEmpty()) {
return "";
}
StringBuilder sb = new StringBuilder(itemList.size() * 8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,14 @@ public String getTitle() {

for (Node n = getDocumentElement().getFirstChild(); n != null; n = n.getNextSibling()) {
String ns = n.getNamespaceURI();
if (ns != null && ns.equals(SVG_NAMESPACE_URI)) {
if (n.getLocalName().equals(SVG_TITLE_TAG)) {
preserve = ((SVGLangSpace) n).getXMLspace().equals("preserve");
for (n = n.getFirstChild(); n != null; n = n.getNextSibling()) {
if (n.getNodeType() == Node.TEXT_NODE) {
sb.append(n.getNodeValue());
}
if (ns != null && SVG_NAMESPACE_URI.equals(ns) && SVG_TITLE_TAG.equals(n.getLocalName())) {
preserve = XML_PRESERVE_VALUE.equals(((SVGLangSpace) n).getXMLspace());
for (n = n.getFirstChild(); n != null; n = n.getNextSibling()) {
if (n.getNodeType() == Node.TEXT_NODE) {
sb.append(n.getNodeValue());
}
break;
}
break;
}
}

Expand Down Expand Up @@ -709,10 +707,7 @@ public void otherAnimationChanged(Element e, String type) {

// DocumentCSS ////////////////////////////////////////////////////////////

/**
* This does not implement {@code DocumentCSS#getOverrideStyle(Element,String)}
* anymore, as it does not support Typed OM.
*/
@Override
public CSSStyleDeclaration getOverrideStyle(Element elt, String pseudoElt) {
if (elt instanceof SVGStylableElement && pseudoElt == null) {
return ((SVGStylableElement) elt).getOverrideStyle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -708,10 +708,9 @@ protected AnimatableValue getBaseValue(SVGAnimatedNumber n, SVGAnimatedNumber on
*/
@Override
public short getPercentageInterpretation(String ns, String an, boolean isCSS) {
if (isCSS || ns == null) {
if (an.equals(CSSConstants.CSS_BASELINE_SHIFT_PROPERTY) || an.equals(CSSConstants.CSS_FONT_SIZE_PROPERTY)) {
return PERCENTAGE_FONT_SIZE;
}
if ((isCSS || ns == null) && (CSSConstants.CSS_BASELINE_SHIFT_PROPERTY.equals(an)
|| CSSConstants.CSS_FONT_SIZE_PROPERTY.equals(an))) {
return PERCENTAGE_FONT_SIZE;
}
if (!isCSS) {
DoublyIndexedTable<String, String> t = getTraitInformationTable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class SVGOMStyleElement extends SVGOMElement implements CSSStyleSheetNode

static {
attributeInitializer = new AttributeInitializer(1);
attributeInitializer.addAttribute(XMLConstants.XML_NAMESPACE_URI, "xml", "space", "preserve");
attributeInitializer.addAttribute(XML_NAMESPACE_URI, "xml", XML_SPACE_ATTRIBUTE, XML_PRESERVE_VALUE);
}

/**
Expand Down Expand Up @@ -115,31 +115,29 @@ public String getLocalName() {
*/
@Override
public StyleSheet getCSSStyleSheet() {
if (styleSheet == null) {
if (getType().equals(CSSConstants.CSS_MIME_TYPE)) {
SVGOMDocument doc = (SVGOMDocument) getOwnerDocument();
CSSEngine e = doc.getCSSEngine();
String text = "";
Node n = getFirstChild();
if (n != null) {
StringBuilder sb = new StringBuilder();
while (n != null) {
if (n.getNodeType() == Node.CDATA_SECTION_NODE || n.getNodeType() == Node.TEXT_NODE)
sb.append(n.getNodeValue());
n = n.getNextSibling();
}
text = sb.toString();
if (styleSheet == null && CSSConstants.CSS_MIME_TYPE.equals(getType())) {
SVGOMDocument doc = (SVGOMDocument) getOwnerDocument();
CSSEngine e = doc.getCSSEngine();
String text = "";
Node n = getFirstChild();
if (n != null) {
StringBuilder sb = new StringBuilder();
while (n != null) {
if (n.getNodeType() == Node.CDATA_SECTION_NODE || n.getNodeType() == Node.TEXT_NODE)
sb.append(n.getNodeValue());
n = n.getNextSibling();
}
ParsedURL burl = null;
String bu = getBaseURI();
if (bu != null) {
burl = new ParsedURL(bu);
}
String media = getAttributeNS(null, SVG_MEDIA_ATTRIBUTE);
styleSheet = e.parseStyleSheet(text, burl, media);
addEventListenerNS(XMLConstants.XML_EVENTS_NAMESPACE_URI, "DOMCharacterDataModified",
domCharacterDataModifiedListener, false, null);
text = sb.toString();
}
ParsedURL burl = null;
String bu = getBaseURI();
if (bu != null) {
burl = new ParsedURL(bu);
}
String media = getAttributeNS(null, SVG_MEDIA_ATTRIBUTE);
styleSheet = e.parseStyleSheet(text, burl, media);
addEventListenerNS(XMLConstants.XML_EVENTS_NAMESPACE_URI, "DOMCharacterDataModified",
domCharacterDataModifiedListener, false, null);
}
return styleSheet;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public boolean paintRable(Graphics2D g2d) {

@Override
public RenderedImage createRendering(RenderContext rc) {
if (getSources().size() == 0)
if (getSources().isEmpty())
return null;

// Just copy over the rendering hints.
Expand Down Expand Up @@ -194,7 +194,7 @@ public RenderedImage createRendering(RenderContext rc) {
}
}

if (srcs.size() == 0)
if (srcs.isEmpty())
return null;

// System.out.println("Done General: " + rule);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,8 @@ protected static List<Stop> extractLocalStop(Element gradientElement, float opac
if (stops == null) {
stops = new LinkedList<>();
}
if (previous != null) {
if (stop.offset < previous.offset) {
stop.offset = previous.offset;
}
if ((previous != null) && (stop.offset < previous.offset)) {
stop.offset = previous.offset;
}
stops.add(stop);
previous = stop;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,22 +96,14 @@ public class BaseScriptingEnvironment {
public static boolean isDynamicDocument(BridgeContext ctx, Document doc) {
Element elt = doc.getDocumentElement();
if ((elt != null) && SVGConstants.SVG_NAMESPACE_URI.equals(elt.getNamespaceURI())) {
if (elt.getAttributeNS(null, SVGConstants.SVG_ONABORT_ATTRIBUTE).length() > 0) {
if (!elt.getAttributeNS(null, SVGConstants.SVG_ONABORT_ATTRIBUTE).isEmpty()
|| !elt.getAttributeNS(null, SVGConstants.SVG_ONERROR_ATTRIBUTE).isEmpty()
|| !elt.getAttributeNS(null, SVGConstants.SVG_ONRESIZE_ATTRIBUTE).isEmpty()
|| !elt.getAttributeNS(null, SVGConstants.SVG_ONUNLOAD_ATTRIBUTE).isEmpty()) {
return true;
}
if (elt.getAttributeNS(null, SVGConstants.SVG_ONERROR_ATTRIBUTE).length() > 0) {
return true;
}
if (elt.getAttributeNS(null, SVGConstants.SVG_ONRESIZE_ATTRIBUTE).length() > 0) {
return true;
}
if (elt.getAttributeNS(null, SVGConstants.SVG_ONUNLOAD_ATTRIBUTE).length() > 0) {
return true;
}
if (elt.getAttributeNS(null, SVGConstants.SVG_ONSCROLL_ATTRIBUTE).length() > 0) {
return true;
}
if (elt.getAttributeNS(null, SVGConstants.SVG_ONZOOM_ATTRIBUTE).length() > 0) {
if (!elt.getAttributeNS(null, SVGConstants.SVG_ONSCROLL_ATTRIBUTE).isEmpty()
|| !elt.getAttributeNS(null, SVGConstants.SVG_ONZOOM_ATTRIBUTE).isEmpty()) {
return true;
}
return isDynamicElement(ctx, doc.getDocumentElement());
Expand All @@ -134,55 +126,33 @@ public static boolean isDynamicElement(Element elt, BridgeContext ctx, List<Brid
}
}
if (SVGConstants.SVG_NAMESPACE_URI.equals(elt.getNamespaceURI())) {
if (elt.getAttributeNS(null, SVGConstants.SVG_ONKEYUP_ATTRIBUTE).length() > 0) {
return true;
}
if (elt.getAttributeNS(null, SVGConstants.SVG_ONKEYDOWN_ATTRIBUTE).length() > 0) {
return true;
}
if (elt.getAttributeNS(null, SVGConstants.SVG_ONKEYPRESS_ATTRIBUTE).length() > 0) {
return true;
}
if (elt.getAttributeNS(null, SVGConstants.SVG_ONLOAD_ATTRIBUTE).length() > 0) {
return true;
}
if (elt.getAttributeNS(null, SVGConstants.SVG_ONERROR_ATTRIBUTE).length() > 0) {
if (!elt.getAttributeNS(null, SVGConstants.SVG_ONKEYUP_ATTRIBUTE).isEmpty()
|| !elt.getAttributeNS(null, SVGConstants.SVG_ONKEYDOWN_ATTRIBUTE).isEmpty()
|| !elt.getAttributeNS(null, SVGConstants.SVG_ONKEYPRESS_ATTRIBUTE).isEmpty()
|| !elt.getAttributeNS(null, SVGConstants.SVG_ONLOAD_ATTRIBUTE).isEmpty()) {
return true;
}
if (elt.getAttributeNS(null, SVGConstants.SVG_ONACTIVATE_ATTRIBUTE).length() > 0) {
if (!elt.getAttributeNS(null, SVGConstants.SVG_ONERROR_ATTRIBUTE).isEmpty()
|| !elt.getAttributeNS(null, SVGConstants.SVG_ONACTIVATE_ATTRIBUTE).isEmpty()
|| !elt.getAttributeNS(null, SVGConstants.SVG_ONCLICK_ATTRIBUTE).isEmpty()
|| !elt.getAttributeNS(null, SVGConstants.SVG_ONFOCUSIN_ATTRIBUTE).isEmpty()) {
return true;
}
if (elt.getAttributeNS(null, SVGConstants.SVG_ONCLICK_ATTRIBUTE).length() > 0) {
if (!elt.getAttributeNS(null, SVGConstants.SVG_ONFOCUSOUT_ATTRIBUTE).isEmpty()
|| !elt.getAttributeNS(null, SVGConstants.SVG_ONMOUSEDOWN_ATTRIBUTE).isEmpty()
|| !elt.getAttributeNS(null, SVGConstants.SVG_ONMOUSEMOVE_ATTRIBUTE).isEmpty()
|| !elt.getAttributeNS(null, SVGConstants.SVG_ONMOUSEOUT_ATTRIBUTE).isEmpty()) {
return true;
}
if (elt.getAttributeNS(null, SVGConstants.SVG_ONFOCUSIN_ATTRIBUTE).length() > 0) {
return true;
}
if (elt.getAttributeNS(null, SVGConstants.SVG_ONFOCUSOUT_ATTRIBUTE).length() > 0) {
return true;
}
if (elt.getAttributeNS(null, SVGConstants.SVG_ONMOUSEDOWN_ATTRIBUTE).length() > 0) {
return true;
}
if (elt.getAttributeNS(null, SVGConstants.SVG_ONMOUSEMOVE_ATTRIBUTE).length() > 0) {
return true;
}
if (elt.getAttributeNS(null, SVGConstants.SVG_ONMOUSEOUT_ATTRIBUTE).length() > 0) {
return true;
}
if (elt.getAttributeNS(null, SVGConstants.SVG_ONMOUSEOVER_ATTRIBUTE).length() > 0) {
return true;
}
if (elt.getAttributeNS(null, SVGConstants.SVG_ONMOUSEUP_ATTRIBUTE).length() > 0) {
if (!elt.getAttributeNS(null, SVGConstants.SVG_ONMOUSEOVER_ATTRIBUTE).isEmpty()
|| !elt.getAttributeNS(null, SVGConstants.SVG_ONMOUSEUP_ATTRIBUTE).isEmpty()) {
return true;
}
}

for (Node n = elt.getFirstChild(); n != null; n = n.getNextSibling()) {
if (n.getNodeType() == Node.ELEMENT_NODE) {
if (isDynamicElement(ctx, (Element) n)) {
return true;
}
if (n.getNodeType() == Node.ELEMENT_NODE && isDynamicElement(ctx, (Element) n)) {
return true;
}
}
return false;
Expand Down Expand Up @@ -438,7 +408,7 @@ protected void loadScript(AbstractElement script) {
String desc = null;
Reader reader = null;

if (href.length() > 0) {
if (!href.isEmpty()) {
desc = href;

// External script.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -604,10 +604,8 @@ public Interpreter getInterpreter(String language) {
}
}

if (interpreter == null) {
if (userAgent != null) {
userAgent.displayError(new Exception("Unknown language: " + language));
}
if (interpreter == null && userAgent != null) {
userAgent.displayError(new Exception("Unknown language: " + language));
}

return interpreter;
Expand Down Expand Up @@ -865,7 +863,7 @@ public void removeViewport(Element e) {
public void closeViewport(Element e) {
// viewportMap.remove(e); FIXME: potential memory leak
viewportStack.remove(0);
if (viewportStack.size() == 0) {
if (viewportStack.isEmpty()) {
viewportStack = null;
}
}
Expand Down Expand Up @@ -1769,7 +1767,7 @@ public void otherAnimationChanged(Element e, String type) {
public Viewport getViewport(Element e) {
if (viewportStack != null) {
// building time
if (viewportStack.size() == 0) {
if (viewportStack.isEmpty()) {
// outermost svg element
return viewportMap.get(userAgent);
} else {
Expand Down Expand Up @@ -1939,10 +1937,7 @@ public boolean checkInteractiveElement(SVGDocument doc, Element e) {
// This is a bit of a hack but don't count
// title and desc as children of root SVG since
// we don't show tool tips for them anyways.
if (SVGConstants.SVG_TITLE_TAG.equals(tag)) {
return (e.getParentNode() != doc.getRootElement());
}
if (SVGConstants.SVG_DESC_TAG.equals(tag)) {
if (SVGConstants.SVG_TITLE_TAG.equals(tag) || SVGConstants.SVG_DESC_TAG.equals(tag)) {
return (e.getParentNode() != doc.getRootElement());
}
if (SVGConstants.SVG_CURSOR_TAG.equals(tag))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ private static float resolveColorComponent(NumericValue item) {
} else if (f > 100f) {
f = 100f;
}
return f * 0.01f;
return f / 100f;
}
throw new IllegalArgumentException("Invalid color component: " + item.getCssText());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,7 @@ public Filter createFilter(BridgeContext ctx, Element filterElement, Element fil
*/
protected static CompositeRule convertMode(Element filterElement, BridgeContext ctx) {
String rule = filterElement.getAttributeNS(null, SVG_MODE_ATTRIBUTE);
if (rule.length() == 0) {
return CompositeRule.OVER;
}
if (SVG_NORMAL_VALUE.equals(rule)) {
if (rule.isEmpty() || SVG_NORMAL_VALUE.equals(rule)) {
return CompositeRule.OVER;
}
if (SVG_MULTIPLY_VALUE.equals(rule)) {
Expand Down
Loading

0 comments on commit d4870ff

Please sign in to comment.