You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by el...@apache.org on 2021/05/02 16:24:19 UTC

[maven-shared-utils] 01/01: cleanup large chunks of unused YAGNI code and support accurate exception handling

This is an automated email from the ASF dual-hosted git repository.

elharo pushed a commit to branch encode
in repository https://gitbox.apache.org/repos/asf/maven-shared-utils.git

commit 5b1706211be0be129952aa65d6655a93212c970b
Author: Elliotte Rusty Harold <el...@ibiblio.org>
AuthorDate: Sun May 2 12:23:55 2021 -0400

    cleanup large chunks of unused YAGNI code and support accurate exception handling
---
 .../apache/maven/shared/utils/xml/XMLEncode.java   | 350 +++++----------------
 1 file changed, 80 insertions(+), 270 deletions(-)

diff --git a/src/main/java/org/apache/maven/shared/utils/xml/XMLEncode.java b/src/main/java/org/apache/maven/shared/utils/xml/XMLEncode.java
index 0d6ecd1..5c2d10f 100644
--- a/src/main/java/org/apache/maven/shared/utils/xml/XMLEncode.java
+++ b/src/main/java/org/apache/maven/shared/utils/xml/XMLEncode.java
@@ -20,7 +20,6 @@ package org.apache.maven.shared.utils.xml;
  */
 
 import java.io.IOException;
-import java.io.StringWriter;
 import java.io.Writer;
 
 /**
@@ -35,210 +34,114 @@ final class XMLEncode
 
     private static final char DEFAULT_QUOTE_CHAR = '"';
 
-    /**
-     * Checks if this text purely consists of the white space characters
-     * ' ',  TAB, NEWLINE.
-     */
-    public static boolean isWhiteSpace( String text )
-    {
-        for ( int i = 0; i < text.length(); i++ )
-        {
-            char c = text.charAt( i );
-            if ( !Character.isWhitespace( c ) )
-            {
-                return false;
-            }
-        }
-        return true;
-    }
-
-    /**
-     * Makes any text fit into XML attributes.
-     */
-    public static String xmlEncodeTextForAttribute( String text, char quoteChar )
-    {
-        if ( text == null )
-        {
-            return null;
-        }
-        return xmlEncodeTextAsPCDATA( text, true, quoteChar );
-    }
-
-    /**
-     * Encodes text as XML in the most suitable way, either CDATA block or PCDATA.
-     */
-    public static String xmlEncodeText( String text )
+    static void xmlEncodeText( String text, Writer writer ) throws IOException
     {
         if ( text == null )
         {
-            return null;
+            return;
         }
-        StringWriter writer = new StringWriter( text.length() * 2 );
-        xmlEncodeText( text, writer );
-        return writer.toString();
-    }
 
-    public static void xmlEncodeText( String text, Writer writer )
-    {
-        if ( text == null )
+        if ( !needsEncoding( text ) )
         {
+            writer.write( text );
             return;
         }
-        try
+        else
         {
-            if ( !needsEncoding( text ) )
-            {
-                writer.write( text );
-                return;
-            }
-            else
+            // only encode as cdata if is is longer than CDATA block overhead:
+            if ( text.length() > CDATA_BLOCK_THRESHOLD_LENGTH )
             {
-                // only encode as cdata if is is longer than CDATA block overhead:
-                if ( text.length() > CDATA_BLOCK_THRESHOLD_LENGTH )
+                String cdata = xmlEncodeTextAsCDATABlock( text );
+                if ( cdata != null )
                 {
-                    String cdata = xmlEncodeTextAsCDATABlock( text );
-                    if ( cdata != null )
-                    {
-                        writer.write( cdata );
-                        return;
-                    }
+                    writer.write( cdata );
+                    return;
                 }
             }
         }
-        catch ( IOException e )
-        {
-            throw new RuntimeException( e );
-        }
+
         // if every thing else fails, do it the save way...
         xmlEncodeTextAsPCDATA( text, false, DEFAULT_QUOTE_CHAR, writer );
     }
 
-    /**
-     * Encodes any text as PCDATA.
-     */
-    public static String xmlEncodeTextAsPCDATA( String text )
+    static void xmlEncodeTextAsPCDATA( String text, boolean forAttribute, char quoteChar, Writer n ) throws IOException
     {
         if ( text == null )
         {
-            return null;
+            return;
         }
-        return xmlEncodeTextAsPCDATA( text, false );
-    }
-
-    /**
-     * Encodes any text as PCDATA.
-     *
-     * @param forAttribute if you want
-     *                     quotes and apostrophes specially treated for attributes
-     */
-    public static String xmlEncodeTextAsPCDATA( String text, boolean forAttribute )
-    {
-        return xmlEncodeTextAsPCDATA( text, forAttribute, DEFAULT_QUOTE_CHAR );
-    }
 
-    /**
-     * Encodes any text as PCDATA.
-     *
-     * @param forAttribute if you want
-     *                     quotes and apostrophes specially treated for attributes
-     * @param quoteChar    if this is for attributes this <code>char</code> is used to quote the attribute value
-     */
-    public static String xmlEncodeTextAsPCDATA( String text, boolean forAttribute, char quoteChar )
-    {
-        if ( text == null )
+        int length = text.length();
+        if ( forAttribute )
         {
-            return null;
+            n.append( quoteChar );
         }
-        StringWriter writer = new StringWriter( text.length() * 2 );
-        xmlEncodeTextAsPCDATA( text, forAttribute, quoteChar, writer );
-        return writer.toString();
-    }
 
-    public static void xmlEncodeTextAsPCDATA( String text, boolean forAttribute, char quoteChar, Writer n )
-    {
-        if ( text == null )
-        {
-            return;
-        }
-        try
+        for ( int i = 0; i < length; i++ )
         {
-            char c;
-            int length = text.length();
-            if ( forAttribute )
-            {
-                n.append( quoteChar );
-            }
-
-            for ( int i = 0; i < length; i++ )
+            char c = text.charAt( i );
+            switch ( c )
             {
-                c = text.charAt( i );
-                switch ( c )
-                {
-                    case '&':
-                        n.append( "&amp;" );
-                        break;
-                    case '<':
-                        n.append( "&lt;" );
-                        break;
-                    case '>': // FIX for sourceforge bug #802520 ("]]>" needs encoding)
-                        n.append( "&gt;" );
-                        break;
-                    case '"':
-                        if ( forAttribute )
-                        {
-                            n.append( "&quot;" );
-                        }
-                        else
-                        {
-                            n.append( c );
-                        }
-                        break;
-                    case '\'':
-                        if ( forAttribute )
-                        {
-                            n.append( "&apos;" );
-                        }
-                        else
-                        {
-                            n.append( c );
-                        }
-                        break;
-                    case '\r':
-                        if ( forAttribute )
-                        {
-                            if ( i == ( length - 1 ) || text.charAt( i + 1 ) != '\n' )
-                            {
-                                n.append( "&#13;" );
-                            }
-                        }
-                        else
-                        {
-                            n.append( c );
-                        }
-                        // but skip the \r in \r\n
-
-                        break;
-                    case '\n':
-                        if ( forAttribute )
+                case '&':
+                    n.append( "&amp;" );
+                    break;
+                case '<':
+                    n.append( "&lt;" );
+                    break;
+                case '>': // FIX for sourceforge bug #802520 ("]]>" needs encoding)
+                    n.append( "&gt;" );
+                    break;
+                case '"':
+                    if ( forAttribute )
+                    {
+                        n.append( "&quot;" );
+                    }
+                    else
+                    {
+                        n.append( c );
+                    }
+                    break;
+                case '\'':
+                    if ( forAttribute )
+                    {
+                        n.append( "&apos;" );
+                    }
+                    else
+                    {
+                        n.append( c );
+                    }
+                    break;
+                case '\r':
+                    if ( forAttribute )
+                    {
+                        if ( i == ( length - 1 ) || text.charAt( i + 1 ) != '\n' )
                         {
-                            n.append( "&#10;" );
+                            n.append( "&#13;" );
                         }
-                        break;
-
-                    default:
+                    }
+                    else
+                    {
                         n.append( c );
-                        break;
-                }
-            }
+                    }
+                    // but skip the \r in \r\n
 
-            if ( forAttribute )
-            {
-                n.append( quoteChar );
+                    break;
+                case '\n':
+                    if ( forAttribute )
+                    {
+                        n.append( "&#10;" );
+                    }
+                    break;
+
+                default:
+                    n.append( c );
+                    break;
             }
         }
-        catch ( IOException e )
+
+        if ( forAttribute )
         {
-            throw new RuntimeException( e );
+            n.append( quoteChar );
         }
 
     }
@@ -246,13 +149,13 @@ final class XMLEncode
     /**
      * Returns string as CDATA block if possible, otherwise null.
      */
-    public static String xmlEncodeTextAsCDATABlock( String text )
+    private static String xmlEncodeTextAsCDATABlock( String text )
     {
         if ( text == null )
         {
             return null;
         }
-        if ( isCompatibleWithCDATABlock( text ) )
+        if ( !text.contains( "]]>" ) )
         {
             return "<![CDATA[" + text + "]]>";
         }
@@ -265,28 +168,16 @@ final class XMLEncode
     /**
      * Checks if this text needs encoding in order to be represented in XML.
      */
-    public static boolean needsEncoding( String text )
-    {
-        return needsEncoding( text, false );
-    }
-
-    /**
-     * Checks if this text needs encoding in order to be represented in XML.
-     * <p/>
-     * Set <code>checkForAttr</code> if you want to check for storability in
-     * an attribute.
-     */
-    public static boolean needsEncoding( String data, boolean checkForAttr )
+    private static boolean needsEncoding( String text )
     {
-        if ( data == null )
+        if ( text == null )
         {
             return false;
         }
-        char c;
-        for ( int i = 0; i < data.length(); i++ )
+        for ( int i = 0; i < text.length(); i++ )
         {
-            c = data.charAt( i );
-            if ( c == '&' || c == '<' || ( checkForAttr && ( c == '"' || c == '\'' ) ) )
+            char c = text.charAt( i );
+            if ( c == '&' || c == '<' )
             {
                 return true;
             }
@@ -294,85 +185,4 @@ final class XMLEncode
         return false;
     }
 
-    /**
-     * Can this text be stored into a CDATA block?
-     */
-    public static boolean isCompatibleWithCDATABlock( String text )
-    {
-        return text != null && ( !text.contains( "]]>" ) );
-    }
-
-    /**
-     * Make CDATA out of possibly encoded PCDATA. <br>
-     * E.g. make '&amp;' out of '&amp;amp;'
-     */
-    public static String xmlDecodeTextToCDATA( String pcdata )
-    {
-        if ( pcdata == null )
-        {
-            return null;
-        }
-        char c, c1, c2, c3, c4, c5;
-        StringBuilder n = new StringBuilder( pcdata.length() );
-        for ( int i = 0; i < pcdata.length(); i++ )
-        {
-            c = pcdata.charAt( i );
-            if ( c == '&' )
-            {
-                c1 = lookAhead( 1, i, pcdata );
-                c2 = lookAhead( 2, i, pcdata );
-                c3 = lookAhead( 3, i, pcdata );
-                c4 = lookAhead( 4, i, pcdata );
-                c5 = lookAhead( 5, i, pcdata );
-
-                if ( c1 == 'a' && c2 == 'm' && c3 == 'p' && c4 == ';' )
-                {
-                    n.append( "&" );
-                    i += 4;
-                }
-                else if ( c1 == 'l' && c2 == 't' && c3 == ';' )
-                {
-                    n.append( "<" );
-                    i += 3;
-                }
-                else if ( c1 == 'g' && c2 == 't' && c3 == ';' )
-                {
-                    n.append( ">" );
-                    i += 3;
-                }
-                else if ( c1 == 'q' && c2 == 'u' && c3 == 'o' && c4 == 't' && c5 == ';' )
-                {
-                    n.append( "\"" );
-                    i += 5;
-                }
-                else if ( c1 == 'a' && c2 == 'p' && c3 == 'o' && c4 == 's' && c5 == ';' )
-                {
-                    n.append( "'" );
-                    i += 5;
-                }
-                else
-                {
-                    n.append( "&" );
-                }
-            }
-            else
-            {
-                n.append( c );
-            }
-        }
-        return n.toString();
-    }
-
-    private static char lookAhead( int la, int offset, String data )
-    {
-        try
-        {
-            return data.charAt( offset + la );
-        }
-        catch ( StringIndexOutOfBoundsException e )
-        {
-            return 0x0;
-        }
-    }
-
 }