From 2a7ae81217a4aaa80525f4b985335467d4721008 Mon Sep 17 00:00:00 2001 From: Dieter von Holten Date: Fri, 2 Feb 2024 16:19:24 +0100 Subject: [PATCH 1/6] - fix wrong encoding - add some @Override - replace some x.length() > 0 with !x.isEmpty() - StringBuffer -> StringBuilder - drop some trailing blanks - add some types to List<>, Set<> etc --- .../bridge/BaseScriptingEnvironment.java | 121 ++++++++---------- 1 file changed, 54 insertions(+), 67 deletions(-) diff --git a/batik-bridge/src/main/java/org/apache/batik/bridge/BaseScriptingEnvironment.java b/batik-bridge/src/main/java/org/apache/batik/bridge/BaseScriptingEnvironment.java index 72eb7ac944..f4c9d7b495 100644 --- a/batik-bridge/src/main/java/org/apache/batik/bridge/BaseScriptingEnvironment.java +++ b/batik-bridge/src/main/java/org/apache/batik/bridge/BaseScriptingEnvironment.java @@ -70,7 +70,7 @@ public class BaseScriptingEnvironment { * Constant used to describe inline scripts. *
      * {0} - URL of document containing script.
-     * {1} - Element tag
+     * {1} - Element tag.
      * {2} - line number of element.
      * 
*/ @@ -81,7 +81,7 @@ public class BaseScriptingEnvironment { * Constant used to describe inline scripts. *
      * {0} - URL of document containing script.
-     * {1} - Event attribute name
+     * {1} - Event attribute name.
      * {2} - line number of element.
      * 
*/ @@ -95,28 +95,22 @@ 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() ) { return true; } - if (elt.getAttributeNS - (null, SVGConstants.SVG_ONERROR_ATTRIBUTE).length() > 0) { + if ( !elt.getAttributeNS( null, SVGConstants.SVG_ONERROR_ATTRIBUTE ).isEmpty() ) { return true; } - if (elt.getAttributeNS - (null, SVGConstants.SVG_ONRESIZE_ATTRIBUTE).length() > 0) { + if ( !elt.getAttributeNS( null, SVGConstants.SVG_ONRESIZE_ATTRIBUTE ).isEmpty() ) { return true; } - if (elt.getAttributeNS - (null, SVGConstants.SVG_ONUNLOAD_ATTRIBUTE).length() > 0) { + if ( !elt.getAttributeNS( null, SVGConstants.SVG_ONUNLOAD_ATTRIBUTE ).isEmpty() ) { return true; } - if (elt.getAttributeNS - (null, SVGConstants.SVG_ONSCROLL_ATTRIBUTE).length() > 0) { + if ( !elt.getAttributeNS( null, SVGConstants.SVG_ONSCROLL_ATTRIBUTE ).isEmpty() ) { return true; } - if (elt.getAttributeNS - (null, SVGConstants.SVG_ONZOOM_ATTRIBUTE).length() > 0) { + if ( !elt.getAttributeNS( null, SVGConstants.SVG_ONZOOM_ATTRIBUTE ).isEmpty() ) { return true; } return isDynamicElement(ctx, doc.getDocumentElement()); @@ -141,60 +135,46 @@ public static boolean isDynamicElement(BridgeContext ctx, Element elt) { } } if (SVGConstants.SVG_NAMESPACE_URI.equals(elt.getNamespaceURI())) { - if (elt.getAttributeNS - (null, SVGConstants.SVG_ONKEYUP_ATTRIBUTE).length() > 0) { + if ( !elt.getAttributeNS( null, SVGConstants.SVG_ONKEYUP_ATTRIBUTE ).isEmpty() ) { return true; } - if (elt.getAttributeNS - (null, SVGConstants.SVG_ONKEYDOWN_ATTRIBUTE).length() > 0) { + if ( !elt.getAttributeNS( null, SVGConstants.SVG_ONKEYDOWN_ATTRIBUTE ).isEmpty() ) { return true; } - if (elt.getAttributeNS - (null, SVGConstants.SVG_ONKEYPRESS_ATTRIBUTE).length() > 0) { + if ( !elt.getAttributeNS( null, SVGConstants.SVG_ONKEYPRESS_ATTRIBUTE ).isEmpty() ) { return true; } - if (elt.getAttributeNS - (null, SVGConstants.SVG_ONLOAD_ATTRIBUTE).length() > 0) { + if ( !elt.getAttributeNS( null, SVGConstants.SVG_ONLOAD_ATTRIBUTE ).isEmpty() ) { return true; } - if (elt.getAttributeNS - (null, SVGConstants.SVG_ONERROR_ATTRIBUTE).length() > 0) { + if ( !elt.getAttributeNS( null, SVGConstants.SVG_ONERROR_ATTRIBUTE ).isEmpty() ) { return true; } - if (elt.getAttributeNS - (null, SVGConstants.SVG_ONACTIVATE_ATTRIBUTE).length() > 0) { + if ( !elt.getAttributeNS( null, SVGConstants.SVG_ONACTIVATE_ATTRIBUTE ).isEmpty() ) { return true; } - if (elt.getAttributeNS - (null, SVGConstants.SVG_ONCLICK_ATTRIBUTE).length() > 0) { + if ( !elt.getAttributeNS( null, SVGConstants.SVG_ONCLICK_ATTRIBUTE ).isEmpty() ) { return true; } - if (elt.getAttributeNS - (null, SVGConstants.SVG_ONFOCUSIN_ATTRIBUTE).length() > 0) { + if ( !elt.getAttributeNS( null, SVGConstants.SVG_ONFOCUSIN_ATTRIBUTE ).isEmpty() ) { return true; } - if (elt.getAttributeNS - (null, SVGConstants.SVG_ONFOCUSOUT_ATTRIBUTE).length() > 0) { + if ( !elt.getAttributeNS( null, SVGConstants.SVG_ONFOCUSOUT_ATTRIBUTE ).isEmpty() ) { return true; } - if (elt.getAttributeNS - (null, SVGConstants.SVG_ONMOUSEDOWN_ATTRIBUTE).length() > 0) { + if ( !elt.getAttributeNS( null, SVGConstants.SVG_ONMOUSEDOWN_ATTRIBUTE ).isEmpty() ) { return true; } - if (elt.getAttributeNS - (null, SVGConstants.SVG_ONMOUSEMOVE_ATTRIBUTE).length() > 0) { + if ( !elt.getAttributeNS( null, SVGConstants.SVG_ONMOUSEMOVE_ATTRIBUTE ).isEmpty() ) { return true; } - if (elt.getAttributeNS - (null, SVGConstants.SVG_ONMOUSEOUT_ATTRIBUTE).length() > 0) { + if ( !elt.getAttributeNS( null, SVGConstants.SVG_ONMOUSEOUT_ATTRIBUTE ).isEmpty() ) { return true; } - if (elt.getAttributeNS - (null, SVGConstants.SVG_ONMOUSEOVER_ATTRIBUTE).length() > 0) { + if ( !elt.getAttributeNS( null, SVGConstants.SVG_ONMOUSEOVER_ATTRIBUTE ).isEmpty() ) { return true; } - if (elt.getAttributeNS - (null, SVGConstants.SVG_ONMOUSEUP_ATTRIBUTE).length() > 0) { + if ( !elt.getAttributeNS( null, SVGConstants.SVG_ONMOUSEUP_ATTRIBUTE ).isEmpty() ) { return true; } } @@ -241,7 +221,7 @@ public static boolean isDynamicElement(BridgeContext ctx, Element elt) { */ protected ParsedURL docPURL; - protected Set languages = new HashSet(); + protected Set languages = new HashSet<>(); /** * The default Interpreter for the document @@ -252,7 +232,7 @@ public static boolean isDynamicElement(BridgeContext ctx, Element elt) { * Map of {@link Interpreter} to {@link org.apache.batik.bridge.Window} * objects. */ - protected Map windowObjects = new HashMap(); + protected Map windowObjects = new HashMap<>(); /** * Set of <script> elements that have already been executed. @@ -273,10 +253,8 @@ public BaseScriptingEnvironment(BridgeContext ctx) { /** * Returns the Window object for the specified {@link Interpreter}. */ - public org.apache.batik.bridge.Window getWindow(Interpreter interp, - String lang) { - org.apache.batik.bridge.Window w = - (org.apache.batik.bridge.Window) windowObjects.get(interp); + public org.apache.batik.bridge.Window getWindow(Interpreter interp, String lang) { + org.apache.batik.bridge.Window w = windowObjects.get(interp); if (w == null) { w = interp == null ? new Window(null, null) : createWindow(interp, lang); @@ -296,9 +274,8 @@ public org.apache.batik.bridge.Window getWindow() { /** * Creates a new Window object. */ - protected org.apache.batik.bridge.Window createWindow(Interpreter interp, - String lang) { - + protected org.apache.batik.bridge.Window createWindow(Interpreter interp, String lang) { + return new Window(interp, lang); } @@ -377,10 +354,9 @@ protected void loadScript(AbstractElement script) { } while (n.getNodeType() != Node.DOCUMENT_NODE); } - String type = script.getAttributeNS - (null, SVGConstants.SVG_TYPE_ATTRIBUTE); + String type = script.getAttributeNS(null, SVGConstants.SVG_TYPE_ATTRIBUTE); - if (type.length() == 0) { + if ( type.isEmpty() ) { type = SVGConstants.SVG_SCRIPT_TYPE_DEFAULT_VALUE; } @@ -458,7 +434,7 @@ protected void loadScript(AbstractElement script) { String desc = null; Reader reader = null; - if (href.length() > 0) { + if ( !href.isEmpty() ) { desc = href; // External script. @@ -480,8 +456,8 @@ protected void loadScript(AbstractElement script) { // No encoding was specified in the MIME type, so // infer it according to RFC 4329. if (purl.hasContentTypeParameter("version")) { - // Future versions of application/ecmascript - // are not supported, so skip this script + // Future versions of application/ecmascript + // are not supported, so skip this script // element if the version parameter is present. return; } @@ -497,7 +473,7 @@ protected void loadScript(AbstractElement script) { buf[1] == (byte)0xfe) { if (read >= 4 && buf[2] == 0 && buf[3] == 0) { - enc = "UTF32-LE"; + enc = "UTF-32LE"; pbis.skip(4); } else { enc = "UTF-16LE"; @@ -508,7 +484,7 @@ protected void loadScript(AbstractElement script) { enc = "UTF-16BE"; pbis.skip(2); } else if (read >= 3 - && buf[0] == (byte)0xef + && buf[0] == (byte)0xef && buf[1] == (byte)0xbb && buf[2] == (byte)0xbf) { enc = "UTF-8"; @@ -544,7 +520,7 @@ protected void loadScript(AbstractElement script) { // Inline script. Node n = script.getFirstChild(); if (n != null) { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); while (n != null) { if (n.getNodeType() == Node.CDATA_SECTION_NODE || n.getNodeType() == Node.TEXT_NODE) @@ -625,9 +601,8 @@ protected void dispatchSVGLoad(Element elt, false); NodeEventTarget t = (NodeEventTarget)elt; - final String s = - elt.getAttributeNS(null, SVGConstants.SVG_ONLOAD_ATTRIBUTE); - if (s.length() == 0) { + final String s = elt.getAttributeNS(null, SVGConstants.SVG_ONLOAD_ATTRIBUTE); + if ( s.isEmpty() ) { // No script to run so just dispatch the event to DOM // (For java presumably). t.dispatchEvent(ev); @@ -643,7 +618,7 @@ protected void dispatchSVGLoad(Element elt, } if (checkCanRun) { - // Check that it is ok to run embeded scripts + // Check that it is ok to run embedded scripts checkCompatibleScriptURL(lang, docPURL); checkCanRun = false; // we only check once for onload handlers } @@ -718,8 +693,7 @@ protected void dispatchSVGResizeEvent() { } protected void dispatchSVGDocEvent(String eventType) { - SVGSVGElement root = - (SVGSVGElement)document.getDocumentElement(); + SVGSVGElement root = (SVGSVGElement)document.getDocumentElement(); // Event is dispatched on outermost SVG element. EventTarget t = root; @@ -778,6 +752,7 @@ public Window(Interpreter interp, String lang) { * Implements {@link * org.apache.batik.bridge.Window#setInterval(String,long)}. */ + @Override public Object setInterval(final String script, long interval) { return null; } @@ -786,6 +761,7 @@ public Object setInterval(final String script, long interval) { * Implements {@link * org.apache.batik.bridge.Window#setInterval(Runnable,long)}. */ + @Override public Object setInterval(final Runnable r, long interval) { return null; } @@ -794,6 +770,7 @@ public Object setInterval(final Runnable r, long interval) { * Implements {@link * org.apache.batik.bridge.Window#clearInterval(Object)}. */ + @Override public void clearInterval(Object interval) { } @@ -801,6 +778,7 @@ public void clearInterval(Object interval) { * Implements {@link * org.apache.batik.bridge.Window#setTimeout(String,long)}. */ + @Override public Object setTimeout(final String script, long timeout) { return null; } @@ -817,13 +795,14 @@ public Object setTimeout(final Runnable r, long timeout) { * Implements {@link * org.apache.batik.bridge.Window#clearTimeout(Object)}. */ + @Override public void clearTimeout(Object timeout) { } /** * Parses the given XML string into a DocumentFragment of the * given document or a new document if 'doc' is null. - * The implementation in this class always returns 'null' + * The implementation in this class always returns 'null'. * @return The document/document fragment or null on error. */ public Node parseXML(String text, Document doc) { @@ -880,12 +859,14 @@ public void postURL(String uri, /** * Displays an alert dialog box. */ + @Override public void alert(String message) { } /** * Displays a confirm dialog box. */ + @Override public boolean confirm(String message) { return false; } @@ -893,6 +874,7 @@ public boolean confirm(String message) { /** * Displays an input dialog box. */ + @Override public String prompt(String message) { return null; } @@ -900,6 +882,7 @@ public String prompt(String message) { /** * Displays an input dialog box, given the default value. */ + @Override public String prompt(String message, String defVal) { return null; } @@ -907,6 +890,7 @@ public String prompt(String message, String defVal) { /** * Returns the current BridgeContext. */ + @Override public BridgeContext getBridgeContext() { return bridgeContext; } @@ -914,6 +898,7 @@ public BridgeContext getBridgeContext() { /** * Returns the associated interpreter. */ + @Override public Interpreter getInterpreter() { return interpreter; } @@ -921,6 +906,7 @@ public Interpreter getInterpreter() { /** * Returns the Location. */ + @Override public Location getLocation() { return null; } @@ -928,6 +914,7 @@ public Location getLocation() { /** * Returns the parent Window object. */ + @Override public org.apache.batik.w3c.dom.Window getParent() { return null; } From 612bdd24af9d2dd9208b2fc1a6d7a8be2f3e532b Mon Sep 17 00:00:00 2001 From: Dieter von Holten Date: Fri, 2 Feb 2024 16:40:08 +0100 Subject: [PATCH 2/6] - use StandardCharset - StringBuffer -> StringBuilder - fix some typos --- .../java/org/apache/batik/xml/XMLUtilities.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/batik-xml/src/main/java/org/apache/batik/xml/XMLUtilities.java b/batik-xml/src/main/java/org/apache/batik/xml/XMLUtilities.java index daf72a4e28..1da9162356 100644 --- a/batik-xml/src/main/java/org/apache/batik/xml/XMLUtilities.java +++ b/batik-xml/src/main/java/org/apache/batik/xml/XMLUtilities.java @@ -25,6 +25,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.InputStreamReader; import java.io.PushbackInputStream; import java.io.Reader; +import java.nio.charset.StandardCharsets; import org.apache.batik.util.EncodingUtilities; @@ -125,7 +126,7 @@ public static boolean isXMLVersionCharacter(char c) { } /** - * Tests whether the given character is a valid aphabetic character. + * Tests whether the given character is a valid alphabetic character. */ public static boolean isXMLAlphabeticCharacter(char c) { return (c < 128) && @@ -170,12 +171,12 @@ public static int testXMLQName(String s) { /** * Creates a Reader initialized to scan the characters in the given * XML document's InputStream. - * @param is The input stream positionned at the beginning of an + * @param is The input stream positioned at the beginning of an * XML document. - * @return a Reader positionned at the beginning of the XML document + * @return a Reader positioned at the beginning of the XML document * It is created from an encoding figured out from the first * few bytes of the document. As a consequence the given - * input stream is not positionned anymore at the beginning + * input stream is not positioned anymore at the beginning * of the document when this method returns. */ public static Reader createXMLDocumentReader(InputStream is) @@ -236,7 +237,7 @@ public static Reader createXMLDocumentReader(InputStream is) } } - return new InputStreamReader(pbis, "UTF8"); + return new InputStreamReader(pbis, StandardCharsets.UTF_8 ); } /** @@ -379,7 +380,7 @@ protected static String getXMLDeclarationEncoding(Reader r, String e) } sc = (char)c; - StringBuffer enc = new StringBuffer(); + StringBuilder enc = new StringBuilder(); for (;;) { c = r.read(); if (c == -1) { From ca4c70752dfbe9e36415e8beac1cc08339318d34 Mon Sep 17 00:00:00 2001 From: Dieter von Holten Date: Fri, 2 Feb 2024 16:54:44 +0100 Subject: [PATCH 3/6] - use Arrays.fill() - fix some typos --- .../org/apache/batik/ext/awt/image/rendered/AffineRed.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/batik-awt-util/src/main/java/org/apache/batik/ext/awt/image/rendered/AffineRed.java b/batik-awt-util/src/main/java/org/apache/batik/ext/awt/image/rendered/AffineRed.java index e90597b044..820be58522 100644 --- a/batik-awt-util/src/main/java/org/apache/batik/ext/awt/image/rendered/AffineRed.java +++ b/batik-awt-util/src/main/java/org/apache/batik/ext/awt/image/rendered/AffineRed.java @@ -35,6 +35,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.awt.image.Raster; import java.awt.image.SampleModel; import java.awt.image.WritableRaster; +import java.util.Arrays; import org.apache.batik.ext.awt.image.GraphicsUtil; @@ -86,7 +87,7 @@ public AffineRed(CachableRed src, // back aliasing effects... ColorModel cm = fixColorModel(src); - // fix my sample model so it makes sense given my size. + // fix my sample model, so it makes sense given my size. SampleModel sm = fixSampleModel(src, cm, myBounds); Point2D pt = new Point2D.Float(src.getTileGridXOffset(), @@ -225,8 +226,7 @@ protected static ColorModel fixColorModel(CachableRed src) { } int [] bits = new int[b]; - for (int i=0; i Date: Fri, 2 Feb 2024 16:58:03 +0100 Subject: [PATCH 4/6] - use Arrays.fill() - fix some typos --- .../main/java/org/apache/batik/util/Base64DecodeStream.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/batik-util/src/main/java/org/apache/batik/util/Base64DecodeStream.java b/batik-util/src/main/java/org/apache/batik/util/Base64DecodeStream.java index 5f4945af9d..ee6aa52a9c 100644 --- a/batik-util/src/main/java/org/apache/batik/util/Base64DecodeStream.java +++ b/batik-util/src/main/java/org/apache/batik/util/Base64DecodeStream.java @@ -20,13 +20,14 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.IOException; import java.io.InputStream; +import java.util.Arrays; /** * This class implements a Base64 Character decoder as specified in RFC1113. * Unlike some other encoding schemes there is nothing in this encoding that * tells the decoder where a buffer starts or stops, so to use it you will need * to isolate your encoded data into a single chunk and then feed them - * this decoder. The simplest way to do that is to read all of the encoded + * this decoder. The simplest way to do that is to read all the encoded * data into a string and then use: *
  *      byte    data[];
@@ -57,8 +58,7 @@ public Base64DecodeStream(InputStream src) {
     private static final byte[] pem_array = new byte[256];
 
     static {
-        for (int i=0; i
Date: Fri, 2 Feb 2024 17:00:52 +0100
Subject: [PATCH 5/6] - use Arrays.fill() - fix some typos

---
 .../org/apache/batik/ext/awt/image/rendered/FormatRed.java    | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/batik-awt-util/src/main/java/org/apache/batik/ext/awt/image/rendered/FormatRed.java b/batik-awt-util/src/main/java/org/apache/batik/ext/awt/image/rendered/FormatRed.java
index 33f7151cb9..37a757d759 100644
--- a/batik-awt-util/src/main/java/org/apache/batik/ext/awt/image/rendered/FormatRed.java
+++ b/batik-awt-util/src/main/java/org/apache/batik/ext/awt/image/rendered/FormatRed.java
@@ -32,6 +32,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
 import java.awt.image.SampleModel;
 import java.awt.image.SinglePixelPackedSampleModel;
 import java.awt.image.WritableRaster;
+import java.util.Arrays;
 
 import org.apache.batik.ext.awt.image.GraphicsUtil;
 
@@ -175,8 +176,7 @@ else if (bands != srcCM.getNumComponents())
 
         if (sm instanceof ComponentSampleModel) {
             int [] bitsPer = new int[bands];
-            for (int i=0; i
Date: Fri, 2 Feb 2024 17:07:07 +0100
Subject: [PATCH 6/6] - use Arrays.fill() - fix some typos

---
 .../src/main/java/org/apache/batik/dom/util/IntTable.java  | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/batik-dom/src/main/java/org/apache/batik/dom/util/IntTable.java b/batik-dom/src/main/java/org/apache/batik/dom/util/IntTable.java
index 35b170f608..79c710d093 100644
--- a/batik-dom/src/main/java/org/apache/batik/dom/util/IntTable.java
+++ b/batik-dom/src/main/java/org/apache/batik/dom/util/IntTable.java
@@ -19,12 +19,13 @@ Licensed to the Apache Software Foundation (ASF) under one or more
 package org.apache.batik.dom.util;
 
 import java.io.Serializable;
+import java.util.Arrays;
 
 /**
  * A simple hashtable, not synchronized, with fixed load factor,
  * that maps objects to ints.
  * This implementation is not Thread-safe.
- * 
+ *
  * @author Stephane Hillion
  * @version $Id$
  */
@@ -228,9 +229,7 @@ public int remove(Object key) {
      * Clears the table.
      */
     public void clear() {
-        for (int i = 0; i < table.length; i++) {
-            table[i] = null;
-        }
+        Arrays.fill( table, null );
         count = 0;
     }