You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ki...@apache.org on 2020/08/23 20:16:46 UTC

svn commit: r1881117 [5/6] - in /xmlbeans/trunk/src: main/java/org/apache/xmlbeans/impl/repackage/ main/java/org/apache/xmlbeans/impl/richParser/ main/java/org/apache/xmlbeans/impl/schema/ main/java/org/apache/xmlbeans/impl/tool/ main/java/org/apache/x...

Modified: xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/util/XsTypeConverter.java
URL: http://svn.apache.org/viewvc/xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/util/XsTypeConverter.java?rev=1881117&r1=1881116&r2=1881117&view=diff
==============================================================================
--- xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/util/XsTypeConverter.java (original)
+++ xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/util/XsTypeConverter.java Sun Aug 23 20:16:45 2020
@@ -15,25 +15,19 @@
 
 package org.apache.xmlbeans.impl.util;
 
-import org.apache.xmlbeans.GDate;
-import org.apache.xmlbeans.GDateBuilder;
-import org.apache.xmlbeans.GDateSpecification;
-import org.apache.xmlbeans.SchemaType;
-import org.apache.xmlbeans.XmlCalendar;
-import org.apache.xmlbeans.XmlError;
+import org.apache.xmlbeans.*;
 import org.apache.xmlbeans.impl.common.InvalidLexicalValueException;
 
 import javax.xml.namespace.NamespaceContext;
 import javax.xml.namespace.QName;
 import java.math.BigDecimal;
 import java.math.BigInteger;
+import java.net.URI;
 import java.util.Calendar;
 import java.util.Collection;
 import java.util.Date;
-import java.net.URI;
 
-public final class XsTypeConverter
-{
+public final class XsTypeConverter {
     private static final String POS_INF_LEX = "INF";
     private static final String NEG_INF_LEX = "-INF";
     private static final String NAN_LEX = "NaN";
@@ -43,13 +37,12 @@ public final class XsTypeConverter
     private static final BigDecimal DECIMAL__ZERO = new BigDecimal(0.0);
 
     // See Section 2.4.3 of FRC2396  http://www.ietf.org/rfc/rfc2396.txt
-    private static final String[] URI_CHARS_TO_BE_REPLACED = {" "  , "{"  , "}"  , "|"  , "\\" , "^"  , "["  , "]"  , "`"  };
-    private static final String[] URI_CHARS_REPLACED_WITH  = {"%20", "%7b", "%7d", "%7c", "%5c", "%5e", "%5b", "%5d", "%60"};
+    private static final String[] URI_CHARS_TO_BE_REPLACED = {" ", "{", "}", "|", "\\", "^", "[", "]", "`"};
+    private static final String[] URI_CHARS_REPLACED_WITH = {"%20", "%7b", "%7d", "%7c", "%5c", "%5e", "%5b", "%5d", "%60"};
 
     // ======================== float ========================
     public static float lexFloat(CharSequence cs)
-        throws NumberFormatException
-    {
+        throws NumberFormatException {
         final String v = cs.toString();
         try {
             //current jdk impl of parseFloat calls trim() on the string.
@@ -58,27 +51,31 @@ public final class XsTypeConverter
             if (cs.length() > 0) {
                 char ch = cs.charAt(cs.length() - 1);
                 if (ch == 'f' || ch == 'F') {
-                    if (cs.charAt(cs.length() - 2) != 'N')
+                    if (cs.charAt(cs.length() - 2) != 'N') {
                         throw new NumberFormatException("Invalid char '" + ch + "' in float.");
+                    }
                 }
             }
             return Float.parseFloat(v);
-        }
-        catch (NumberFormatException e) {
-            if (v.equals(POS_INF_LEX)) return Float.POSITIVE_INFINITY;
-            if (v.equals(NEG_INF_LEX)) return Float.NEGATIVE_INFINITY;
-            if (v.equals(NAN_LEX)) return Float.NaN;
+        } catch (NumberFormatException e) {
+            if (v.equals(POS_INF_LEX)) {
+                return Float.POSITIVE_INFINITY;
+            }
+            if (v.equals(NEG_INF_LEX)) {
+                return Float.NEGATIVE_INFINITY;
+            }
+            if (v.equals(NAN_LEX)) {
+                return Float.NaN;
+            }
 
             throw e;
         }
     }
 
-    public static float lexFloat(CharSequence cs, Collection errors)
-    {
+    public static float lexFloat(CharSequence cs, Collection errors) {
         try {
             return lexFloat(cs);
-        }
-        catch (NumberFormatException e) {
+        } catch (NumberFormatException e) {
             String msg = "invalid float: " + cs;
             errors.add(XmlError.forMessage(msg));
 
@@ -86,23 +83,22 @@ public final class XsTypeConverter
         }
     }
 
-    public static String printFloat(float value)
-    {
-        if (value == Float.POSITIVE_INFINITY)
+    public static String printFloat(float value) {
+        if (value == Float.POSITIVE_INFINITY) {
             return POS_INF_LEX;
-        else if (value == Float.NEGATIVE_INFINITY)
+        } else if (value == Float.NEGATIVE_INFINITY) {
             return NEG_INF_LEX;
-        else if (Float.isNaN(value))
+        } else if (Float.isNaN(value)) {
             return NAN_LEX;
-        else
+        } else {
             return Float.toString(value);
+        }
     }
 
 
     // ======================== double ========================
     public static double lexDouble(CharSequence cs)
-        throws NumberFormatException
-    {
+        throws NumberFormatException {
         final String v = cs.toString();
 
         try {
@@ -111,26 +107,30 @@ public final class XsTypeConverter
             //so no need to do a collapse pass through the string.
             if (cs.length() > 0) {
                 char ch = cs.charAt(cs.length() - 1);
-                if (ch == 'd' || ch == 'D')
+                if (ch == 'd' || ch == 'D') {
                     throw new NumberFormatException("Invalid char '" + ch + "' in double.");
+                }
             }
             return Double.parseDouble(v);
-        }
-        catch (NumberFormatException e) {
-            if (v.equals(POS_INF_LEX)) return Double.POSITIVE_INFINITY;
-            if (v.equals(NEG_INF_LEX)) return Double.NEGATIVE_INFINITY;
-            if (v.equals(NAN_LEX)) return Double.NaN;
+        } catch (NumberFormatException e) {
+            if (v.equals(POS_INF_LEX)) {
+                return Double.POSITIVE_INFINITY;
+            }
+            if (v.equals(NEG_INF_LEX)) {
+                return Double.NEGATIVE_INFINITY;
+            }
+            if (v.equals(NAN_LEX)) {
+                return Double.NaN;
+            }
 
             throw e;
         }
     }
 
-    public static double lexDouble(CharSequence cs, Collection errors)
-    {
+    public static double lexDouble(CharSequence cs, Collection errors) {
         try {
             return lexDouble(cs);
-        }
-        catch (NumberFormatException e) {
+        } catch (NumberFormatException e) {
             String msg = "invalid double: " + cs;
             errors.add(XmlError.forMessage(msg));
 
@@ -138,23 +138,22 @@ public final class XsTypeConverter
         }
     }
 
-    public static String printDouble(double value)
-    {
-        if (value == Double.POSITIVE_INFINITY)
+    public static String printDouble(double value) {
+        if (value == Double.POSITIVE_INFINITY) {
             return POS_INF_LEX;
-        else if (value == Double.NEGATIVE_INFINITY)
+        } else if (value == Double.NEGATIVE_INFINITY) {
             return NEG_INF_LEX;
-        else if (Double.isNaN(value))
+        } else if (Double.isNaN(value)) {
             return NAN_LEX;
-        else
+        } else {
             return Double.toString(value);
+        }
     }
 
 
     // ======================== decimal ========================
     public static BigDecimal lexDecimal(CharSequence cs)
-        throws NumberFormatException
-    {
+        throws NumberFormatException {
         final String v = cs.toString();
 
         //TODO: review this
@@ -166,67 +165,50 @@ public final class XsTypeConverter
         return new BigDecimal(trimTrailingZeros(v));
     }
 
-    public static BigDecimal lexDecimal(CharSequence cs, Collection errors)
-    {
-        try {
-            return lexDecimal(cs);
-        }
-        catch (NumberFormatException e) {
-            String msg = "invalid long: " + cs;
-            errors.add(XmlError.forMessage(msg));
-            return DECIMAL__ZERO;
-        }
-    }
-
-    private static final char[] CH_ZEROS = new char[] {'0', '0', '0', '0', '0', '0', '0', '0',
+    private static final char[] CH_ZEROS = new char[]{'0', '0', '0', '0', '0', '0', '0', '0',
         '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'};
 
-    public static String printDecimal(BigDecimal value)
-    {
+    public static String printDecimal(BigDecimal value) {
         // We can't simply use value.toString() here, because in JDK1.5 that returns an
         // exponent String and exponents are not allowed in XMLSchema decimal values
         // The following code comes from Apache Harmony
         String intStr = value.unscaledValue().toString();
         int scale = value.scale();
-        if ((scale == 0) || ((value.longValue() == 0) && (scale < 0)))
+        if ((scale == 0) || ((value.longValue() == 0) && (scale < 0))) {
             return intStr;
+        }
 
         int begin = (value.signum() < 0) ? 1 : 0;
         int delta = scale;
         // We take space for all digits, plus a possible decimal point, plus 'scale'
         StringBuilder result = new StringBuilder(intStr.length() + 1 + Math.abs(scale));
 
-        if (begin == 1)
-        {
-            // If the number is negative, we insert a '-' character at front 
+        if (begin == 1) {
+            // If the number is negative, we insert a '-' character at front
             result.append('-');
         }
-        if (scale > 0)
-        {
+        if (scale > 0) {
             delta -= (intStr.length() - begin);
-            if (delta >= 0)
-            {
+            if (delta >= 0) {
                 result.append("0."); //$NON-NLS-1$
                 // To append zeros after the decimal point
-                for (; delta > CH_ZEROS.length; delta -= CH_ZEROS.length)
+                for (; delta > CH_ZEROS.length; delta -= CH_ZEROS.length) {
                     result.append(CH_ZEROS);
+                }
                 result.append(CH_ZEROS, 0, delta);
                 result.append(intStr.substring(begin));
-            }
-            else
-            {
+            } else {
                 delta = begin - delta;
                 result.append(intStr.substring(begin, delta));
                 result.append('.');
                 result.append(intStr.substring(delta));
             }
-        }
-        else
-        {// (scale <= 0)
+        } else {// (scale <= 0)
             result.append(intStr.substring(begin));
             // To append trailing zeros
-            for (; delta < -CH_ZEROS.length; delta += CH_ZEROS.length)
+            for (; delta < -CH_ZEROS.length; delta += CH_ZEROS.length) {
                 result.append(CH_ZEROS);
+            }
             result.append(CH_ZEROS, 0, -delta);
         }
         return result.toString();
@@ -234,11 +216,11 @@ public final class XsTypeConverter
 
     // ======================== integer ========================
     public static BigInteger lexInteger(CharSequence cs)
-        throws NumberFormatException
-    {
+        throws NumberFormatException {
         if (cs.length() > 1) {
-            if (cs.charAt(0) == '+' && cs.charAt(1) == '-')
+            if (cs.charAt(0) == '+' && cs.charAt(1) == '-') {
                 throw new NumberFormatException("Illegal char sequence '+-'");
+            }
         }
         final String v = cs.toString();
 
@@ -247,132 +229,116 @@ public final class XsTypeConverter
         return new BigInteger(trimInitialPlus(v));
     }
 
-    public static BigInteger lexInteger(CharSequence cs, Collection errors)
-    {
+    public static BigInteger lexInteger(CharSequence cs, Collection errors) {
         try {
             return lexInteger(cs);
-        }
-        catch (NumberFormatException e) {
+        } catch (NumberFormatException e) {
             String msg = "invalid long: " + cs;
             errors.add(XmlError.forMessage(msg));
             return BigInteger.ZERO;
         }
     }
 
-    public static String printInteger(BigInteger value)
-    {
+    public static String printInteger(BigInteger value) {
         return value.toString();
     }
 
     // ======================== long ========================
     public static long lexLong(CharSequence cs)
-        throws NumberFormatException
-    {
+        throws NumberFormatException {
         final String v = cs.toString();
         return Long.parseLong(trimInitialPlus(v));
     }
 
-    public static long lexLong(CharSequence cs, Collection errors)
-    {
+    public static long lexLong(CharSequence cs, Collection errors) {
         try {
             return lexLong(cs);
-        }
-        catch (NumberFormatException e) {
+        } catch (NumberFormatException e) {
             String msg = "invalid long: " + cs;
             errors.add(XmlError.forMessage(msg));
             return 0L;
         }
     }
 
-    public static String printLong(long value)
-    {
+    public static String printLong(long value) {
         return Long.toString(value);
     }
 
 
     // ======================== short ========================
     public static short lexShort(CharSequence cs)
-        throws NumberFormatException
-    {
+        throws NumberFormatException {
         return parseShort(cs);
     }
 
-    public static short lexShort(CharSequence cs, Collection errors)
-    {
+    public static short lexShort(CharSequence cs, Collection errors) {
         try {
             return lexShort(cs);
-        }
-        catch (NumberFormatException e) {
+        } catch (NumberFormatException e) {
             String msg = "invalid short: " + cs;
             errors.add(XmlError.forMessage(msg));
             return 0;
         }
     }
 
-    public static String printShort(short value)
-    {
+    public static String printShort(short value) {
         return Short.toString(value);
     }
 
 
     // ======================== int ========================
     public static int lexInt(CharSequence cs)
-        throws NumberFormatException
-    {
+        throws NumberFormatException {
         return parseInt(cs);
     }
 
-    public static int lexInt(CharSequence cs, Collection errors)
-    {
+    public static int lexInt(CharSequence cs, Collection errors) {
         try {
             return lexInt(cs);
-        }
-        catch (NumberFormatException e) {
+        } catch (NumberFormatException e) {
             String msg = "invalid int:" + cs;
             errors.add(XmlError.forMessage(msg));
             return 0;
         }
     }
 
-    public static String printInt(int value)
-    {
+    public static String printInt(int value) {
         return Integer.toString(value);
     }
 
 
     // ======================== byte ========================
     public static byte lexByte(CharSequence cs)
-        throws NumberFormatException
-    {
+        throws NumberFormatException {
         return parseByte(cs);
     }
 
-    public static byte lexByte(CharSequence cs, Collection errors)
-    {
+    public static byte lexByte(CharSequence cs, Collection errors) {
         try {
             return lexByte(cs);
-        }
-        catch (NumberFormatException e) {
+        } catch (NumberFormatException e) {
             String msg = "invalid byte: " + cs;
             errors.add(XmlError.forMessage(msg));
             return 0;
         }
     }
 
-    public static String printByte(byte value)
-    {
+    public static String printByte(byte value) {
         return Byte.toString(value);
     }
 
 
     // ======================== boolean ========================
-    public static boolean lexBoolean(CharSequence v)
-    {
+    public static boolean lexBoolean(CharSequence v) {
         switch (v.length()) {
             case 1:  // "0" or "1"
                 final char c = v.charAt(0);
-                if ('0' == c) return false;
-                if ('1' == c) return true;
+                if ('0' == c) {
+                    return false;
+                }
+                if ('1' == c) {
+                    return true;
+                }
                 break;
             case 4:  //"true"
                 if ('t' == v.charAt(0) &&
@@ -398,55 +364,38 @@ public final class XsTypeConverter
         throw new InvalidLexicalValueException(msg);
     }
 
-    public static boolean lexBoolean(CharSequence value, Collection errors)
-    {
+    public static boolean lexBoolean(CharSequence value, Collection<XmlError> errors) {
         try {
             return lexBoolean(value);
-        }
-        catch (InvalidLexicalValueException e) {
+        } catch (InvalidLexicalValueException e) {
             errors.add(XmlError.forMessage(e.getMessage()));
             return false;
         }
     }
 
-    public static String printBoolean(boolean value)
-    {
+    public static String printBoolean(boolean value) {
         return (value ? "true" : "false");
     }
 
 
     // ======================== string ========================
-    public static String lexString(CharSequence cs, Collection errors)
-    {
-        final String v = cs.toString();
-
-        return v;
-    }
-
-
-    public static String lexString(CharSequence lexical_value)
-    {
-        return lexical_value.toString();
-    }
-
-    public static String printString(String value)
-    {
+    public static String printString(String value) {
         return value;
     }
 
 
     // ======================== QName ========================
-    public static QName lexQName(CharSequence charSeq, NamespaceContext nscontext)
-    {
+    public static QName lexQName(CharSequence charSeq, NamespaceContext nscontext) {
         String prefix, localname;
 
         int firstcolon;
         boolean hasFirstCollon = false;
-        for (firstcolon = 0; firstcolon < charSeq.length(); firstcolon++)
+        for (firstcolon = 0; firstcolon < charSeq.length(); firstcolon++) {
             if (charSeq.charAt(firstcolon) == NAMESPACE_SEP) {
                 hasFirstCollon = true;
                 break;
             }
+        }
 
         if (hasFirstCollon) {
             prefix = charSeq.subSequence(0, firstcolon).toString();
@@ -462,8 +411,9 @@ public final class XsTypeConverter
         String uri = nscontext.getNamespaceURI(prefix);
 
         if (uri == null) {
-            if (prefix != null && prefix.length() > 0)
+            if (prefix != null && prefix.length() > 0) {
                 throw new InvalidLexicalValueException("Can't resolve prefix: " + prefix);
+            }
 
             uri = "";
         }
@@ -472,12 +422,10 @@ public final class XsTypeConverter
     }
 
     public static QName lexQName(String xsd_qname, Collection errors,
-                                 NamespaceContext nscontext)
-    {
+                                 NamespaceContext nscontext) {
         try {
             return lexQName(xsd_qname, nscontext);
-        }
-        catch (InvalidLexicalValueException e) {
+        } catch (InvalidLexicalValueException e) {
             errors.add(XmlError.forMessage(e.getMessage()));
             final int idx = xsd_qname.indexOf(NAMESPACE_SEP);
             return new QName(null, xsd_qname.substring(idx));
@@ -485,8 +433,7 @@ public final class XsTypeConverter
     }
 
     public static String printQName(QName qname, NamespaceContext nsContext,
-                                    Collection errors)
-    {
+                                    Collection errors) {
         final String uri = qname.getNamespaceURI();
         assert uri != null; //qname is not allowed to have null uri values
         final String prefix;
@@ -494,7 +441,7 @@ public final class XsTypeConverter
             prefix = nsContext.getPrefix(uri);
             if (prefix == null) {
                 String msg = "NamespaceContext does not provide" +
-                    " prefix for namespaceURI " + uri;
+                             " prefix for namespaceURI " + uri;
                 errors.add(XmlError.forMessage(msg));
             }
         } else {
@@ -506,8 +453,7 @@ public final class XsTypeConverter
 
     public static String getQNameString(String uri,
                                         String localpart,
-                                        String prefix)
-    {
+                                        String prefix) {
         if (prefix != null &&
             uri != null &&
             uri.length() > 0 &&
@@ -519,107 +465,68 @@ public final class XsTypeConverter
     }
 
     // ======================== GDate ========================
-    public static GDate lexGDate(CharSequence charSeq)
-    {
+    public static GDate lexGDate(CharSequence charSeq) {
         return new GDate(charSeq);
     }
 
-    public static GDate lexGDate(String xsd_gdate, Collection errors)
-    {
+    public static GDate lexGDate(String xsd_gdate, Collection errors) {
         try {
             return lexGDate(xsd_gdate);
-        }
-        catch (IllegalArgumentException e) {
+        } catch (IllegalArgumentException e) {
             errors.add(XmlError.forMessage(e.getMessage()));
             return new GDateBuilder().toGDate();
         }
     }
 
-    public static String printGDate(GDate gdate, Collection errors)
-    {
+    public static String printGDate(GDate gdate, Collection errors) {
         return gdate.toString();
     }
 
 
     // ======================== dateTime ========================
-    public static XmlCalendar lexDateTime(CharSequence v)
-    {
+    public static XmlCalendar lexDateTime(CharSequence v) {
         GDateSpecification value = getGDateValue(v, SchemaType.BTC_DATE_TIME);
         return value.getCalendar();
     }
 
 
-    public static String printDateTime(Calendar c)
-    {
+    public static String printDateTime(Calendar c) {
         return printDateTime(c, SchemaType.BTC_DATE_TIME);
     }
 
-    public static String printTime(Calendar c)
-    {
+    public static String printTime(Calendar c) {
         return printDateTime(c, SchemaType.BTC_TIME);
     }
 
-    public static String printDate(Calendar c)
-    {
+    public static String printDate(Calendar c) {
         return printDateTime(c, SchemaType.BTC_DATE);
     }
 
-    public static String printDate(Date d)
-    {
+    public static String printDate(Date d) {
         GDateSpecification value = getGDateValue(d, SchemaType.BTC_DATE);
         return value.toString();
     }
 
-    public static String printDateTime(Calendar c, int type_code)
-    {
+    public static String printDateTime(Calendar c, int type_code) {
         GDateSpecification value = getGDateValue(c, type_code);
         return value.toString();
     }
 
-    public static String printDateTime(Date c)
-    {
+    public static String printDateTime(Date c) {
         GDateSpecification value = getGDateValue(c, SchemaType.BTC_DATE_TIME);
         return value.toString();
     }
 
 
     // ======================== hexBinary ========================
-    public static CharSequence printHexBinary(byte[] val)
-    {
+    public static CharSequence printHexBinary(byte[] val) {
         return HexBin.bytesToString(val);
     }
 
-    public static byte[] lexHexBinary(CharSequence lexical_value)
-    {
-        byte[] buf = HexBin.decode(lexical_value.toString().getBytes());
-        if (buf != null)
-            return buf;
-        else
-            throw new InvalidLexicalValueException("invalid hexBinary value");
-    }
-
-
-    // ======================== base64binary ========================
-    public static CharSequence printBase64Binary(byte[] val)
-    {
-        final byte[] bytes = Base64.encode(val);
-        return new String(bytes);
-    }
-
-    public static byte[] lexBase64Binary(CharSequence lexical_value)
-    {
-        byte[] buf = Base64.decode(lexical_value.toString().getBytes());
-        if (buf != null)
-            return buf;
-        else
-            throw new InvalidLexicalValueException("invalid base64Binary value");
-    }
-
 
     // date utils
     public static GDateSpecification getGDateValue(Date d,
-                                                   int builtin_type_code)
-    {
+                                                   int builtin_type_code) {
         GDateBuilder gDateBuilder = new GDateBuilder(d);
         gDateBuilder.setBuiltinTypeCode(builtin_type_code);
         GDate value = gDateBuilder.toGDate();
@@ -628,8 +535,7 @@ public final class XsTypeConverter
 
 
     public static GDateSpecification getGDateValue(Calendar c,
-                                                   int builtin_type_code)
-    {
+                                                   int builtin_type_code) {
         GDateBuilder gDateBuilder = new GDateBuilder(c);
         gDateBuilder.setBuiltinTypeCode(builtin_type_code);
         GDate value = gDateBuilder.toGDate();
@@ -637,16 +543,14 @@ public final class XsTypeConverter
     }
 
     public static GDateSpecification getGDateValue(CharSequence v,
-                                                   int builtin_type_code)
-    {
+                                                   int builtin_type_code) {
         GDateBuilder gDateBuilder = new GDateBuilder(v);
         gDateBuilder.setBuiltinTypeCode(builtin_type_code);
         GDate value = gDateBuilder.toGDate();
         return value;
     }
 
-    private static String trimInitialPlus(String xml)
-    {
+    private static String trimInitialPlus(String xml) {
         if (xml.length() > 0 && xml.charAt(0) == '+') {
             return xml.substring(1);
         } else {
@@ -654,11 +558,9 @@ public final class XsTypeConverter
         }
     }
 
-    private static String trimTrailingZeros(String xsd_decimal)
-    {
+    private static String trimTrailingZeros(String xsd_decimal) {
         final int last_char_idx = xsd_decimal.length() - 1;
-        if (xsd_decimal.charAt(last_char_idx) == '0')
-        {
+        if (xsd_decimal.charAt(last_char_idx) == '0') {
             final int last_point = xsd_decimal.lastIndexOf('.');
             if (last_point >= 0) {
                 //find last trailing zero
@@ -674,27 +576,24 @@ public final class XsTypeConverter
         return xsd_decimal;
     }
 
-    private static int parseInt(CharSequence cs)
-    {
+    private static int parseInt(CharSequence cs) {
         return parseIntXsdNumber(cs, Integer.MIN_VALUE, Integer.MAX_VALUE);
     }
 
-    private static short parseShort(CharSequence cs)
-    {
-        return (short)parseIntXsdNumber(cs, Short.MIN_VALUE, Short.MAX_VALUE);
+    private static short parseShort(CharSequence cs) {
+        return (short) parseIntXsdNumber(cs, Short.MIN_VALUE, Short.MAX_VALUE);
     }
 
-    private static byte parseByte(CharSequence cs)
-    {
-        return (byte)parseIntXsdNumber(cs, Byte.MIN_VALUE, Byte.MAX_VALUE);
+    private static byte parseByte(CharSequence cs) {
+        return (byte) parseIntXsdNumber(cs, Byte.MIN_VALUE, Byte.MAX_VALUE);
     }
 
-    private static int parseIntXsdNumber(CharSequence ch, int min_value, int max_value)
-    {
+    private static int parseIntXsdNumber(CharSequence ch, int min_value, int max_value) {
         // int parser on a CharSequence
         int length = ch.length();
-        if (length < 1)
+        if (length < 1) {
             throw new NumberFormatException("For input string: \"" + ch.toString() + "\"");
+        }
 
         int sign = 1;
         int result = 0;
@@ -722,11 +621,13 @@ public final class XsTypeConverter
             c = ch.charAt(i + start);
             int v = Character.digit(c, 10);
 
-            if (v < 0)
+            if (v < 0) {
                 throw new NumberFormatException("For input string: \"" + ch.toString() + "\"");
+            }
 
-            if (result < limit || (result==limit && v > limit2))
+            if (result < limit || (result == limit && v > limit2)) {
                 throw new NumberFormatException("For input string: \"" + ch.toString() + "\"");
+            }
 
             result = result * 10 - v;
         }
@@ -735,20 +636,16 @@ public final class XsTypeConverter
     }
 
     // ======================== anyURI ========================
-    public static CharSequence printAnyURI(CharSequence val)
-    {
-        return val;
-    }
 
     /**
      * Checkes the regular expression of URI, defined by RFC2369 http://www.ietf.org/rfc/rfc2396.txt Appendix B.
      * Note: The whitespace normalization rule collapse must be applied priot to calling this method.
+     *
      * @param lexical_value the lexical value
      * @return same input value if input value is in the lexical space
      * @throws InvalidLexicalValueException
      */
-    public static CharSequence lexAnyURI(CharSequence lexical_value)
-    {
+    public static CharSequence lexAnyURI(CharSequence lexical_value) {
         /*  // Reg exp from RFC2396, but it's too forgiving for XQTS
         Pattern p = Pattern.compile("^([^:/?#]+:)?(//[^/?#]*)?([^?#]*)(\\?[^#]*)?(#.*)?");
         Matcher m = p.matcher(lexical_value);
@@ -766,22 +663,17 @@ public final class XsTypeConverter
 
         // Per XMLSchema spec allow spaces inside URIs
         StringBuilder s = new StringBuilder(lexical_value.toString());
-        for (int ic = 0; ic<URI_CHARS_TO_BE_REPLACED.length; ic++)
-        {
+        for (int ic = 0; ic < URI_CHARS_TO_BE_REPLACED.length; ic++) {
             int i = 0;
-            while ((i = s.indexOf(URI_CHARS_TO_BE_REPLACED[ic], i)) >= 0)
-            {
+            while ((i = s.indexOf(URI_CHARS_TO_BE_REPLACED[ic], i)) >= 0) {
                 s.replace(i, i + 1, URI_CHARS_REPLACED_WITH[ic]);
                 i += 3;
             }
         }
 
-        try
-        {
+        try {
             URI.create(s.toString());
-        }
-        catch (IllegalArgumentException e)
-        {
+        } catch (IllegalArgumentException e) {
             throw new InvalidLexicalValueException("invalid anyURI value: " + lexical_value, e);
         }
 

Modified: xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/JavaBase64Holder.java
URL: http://svn.apache.org/viewvc/xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/JavaBase64Holder.java?rev=1881117&r1=1881116&r2=1881117&view=diff
==============================================================================
--- xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/JavaBase64Holder.java (original)
+++ xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/JavaBase64Holder.java Sun Aug 23 20:16:45 2020
@@ -15,91 +15,79 @@
 
 package org.apache.xmlbeans.impl.values;
 
-import org.apache.xmlbeans.impl.schema.BuiltinSchemaTypeSystem;
-
-import org.apache.xmlbeans.impl.util.Base64;
-import org.apache.xmlbeans.XmlObject;
-import org.apache.xmlbeans.XmlErrorCodes;
-import org.apache.xmlbeans.XmlBase64Binary;
 import org.apache.xmlbeans.SchemaType;
-import org.apache.xmlbeans.impl.common.ValidationContext;
+import org.apache.xmlbeans.XmlBase64Binary;
+import org.apache.xmlbeans.XmlErrorCodes;
+import org.apache.xmlbeans.XmlObject;
 import org.apache.xmlbeans.impl.common.QNameHelper;
+import org.apache.xmlbeans.impl.common.ValidationContext;
+import org.apache.xmlbeans.impl.schema.BuiltinSchemaTypeSystem;
 
+import java.nio.charset.StandardCharsets;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.util.Arrays;
-import java.io.UnsupportedEncodingException;
+import java.util.Base64;
 
-public abstract class JavaBase64Holder extends XmlObjectBase
-{
-    public SchemaType schemaType()
-        { return BuiltinSchemaTypeSystem.ST_BASE_64_BINARY; }
+public abstract class JavaBase64Holder extends XmlObjectBase {
+    public SchemaType schemaType() {
+        return BuiltinSchemaTypeSystem.ST_BASE_64_BINARY;
+    }
 
     protected byte[] _value;
 
     // SIMPLE VALUE ACCESSORS BELOW -------------------------------------------
 
     // gets raw text value
-    protected String compute_text(NamespaceManager nsm)
-    {
-        return new String(Base64.encode(_value));
+    protected String compute_text(NamespaceManager nsm) {
+        return Base64.getEncoder().encodeToString(_value);
     }
-    protected void set_text(String s)
-    {
+
+    protected void set_text(String s) {
         _hashcached = false;
-        if (_validateOnSet())
+        if (_validateOnSet()) {
             _value = validateLexical(s, schemaType(), _voorVc);
-        else
+        } else {
             _value = lex(s, _voorVc);
+        }
     }
-    protected void set_nil()
-    {
+
+    protected void set_nil() {
         _hashcached = false;
         _value = null;
     }
 
-    public static byte[] lex(String v, ValidationContext c)
-    {
-        byte[] vBytes = null;
-        try
-        {
-            vBytes = v.getBytes("UTF-8");
-        }
-        catch(UnsupportedEncodingException uee)
-        {
-            // should never happen - UTF-8 is always supported
-        }
-        final byte[] bytes = Base64.decode(vBytes);
-
-        if (bytes == null)
-        {
+    public static byte[] lex(String v, ValidationContext c) {
+        try {
+            byte[] vBytes = v.getBytes(StandardCharsets.UTF_8);
+            return Base64.getDecoder().decode(vBytes);
+        } catch (IllegalArgumentException e) {
             // TODO - get a decent error with line numbers and such here
-            c.invalid(XmlErrorCodes.BASE64BINARY, new Object[] { "not encoded properly" });
+            c.invalid(XmlErrorCodes.BASE64BINARY, new Object[]{"not encoded properly"});
+            return null;
         }
-
-        return bytes;
     }
 
-    public static byte[] validateLexical(String v, SchemaType sType, ValidationContext context)
-    {
+    public static byte[] validateLexical(String v, SchemaType sType, ValidationContext context) {
         final byte[] bytes = lex(v, context);
-        if (bytes == null) return null;
+        if (bytes == null) {
+            return null;
+        }
 
-        if (!sType.matchPatternFacet(v))
-        {
+        if (!sType.matchPatternFacet(v)) {
             context.invalid(XmlErrorCodes.DATATYPE_VALID$PATTERN_VALID$NO_VALUE,
-                new Object[] { "base 64", QNameHelper.readable(sType) });
+                new Object[]{"base 64", QNameHelper.readable(sType)});
             return null;
         }
 
         return bytes;
     }
 
-    public byte[] getByteArrayValue()
-    {
+    public byte[] getByteArrayValue() {
         check_dated();
-        if (_value == null)
+        if (_value == null) {
             return null;
+        }
 
         byte[] result = new byte[_value.length];
         System.arraycopy(_value, 0, result, 0, _value.length);
@@ -107,16 +95,14 @@ public abstract class JavaBase64Holder e
     }
 
     // setters
-    protected void set_ByteArray(byte[] ba)
-    {
+    protected void set_ByteArray(byte[] ba) {
         _hashcached = false;
         _value = new byte[ba.length];
         System.arraycopy(ba, 0, _value, 0, ba.length);
     }
 
     // comparators
-    protected boolean equal_to(XmlObject i)
-    {
+    protected boolean equal_to(XmlObject i) {
         byte[] ival = ((XmlBase64Binary) i).getByteArrayValue();
         return Arrays.equals(_value, ival);
     }
@@ -125,29 +111,27 @@ public abstract class JavaBase64Holder e
     protected boolean _hashcached = false;
     protected int hashcode = 0;
     protected static MessageDigest md5;
-    static
-    {
-        try
-        {
+
+    static {
+        try {
             md5 = MessageDigest.getInstance("MD5");
-        }
-        catch( NoSuchAlgorithmException e )
-        {
+        } catch (NoSuchAlgorithmException e) {
             throw new IllegalStateException("Cannot find MD5 hash Algorithm");
         }
     }
 
-    protected int value_hash_code()
-    {
-        if( _hashcached )
+    protected int value_hash_code() {
+        if (_hashcached) {
             return hashcode;
+        }
 
         _hashcached = true;
 
-        if( _value == null )
+        if (_value == null) {
             return hashcode = 0;
+        }
 
         byte[] res = md5.digest(_value);
-        return hashcode = res[0]<<24 + res[1]<<16 + res[2]<<8 + res[3];
+        return hashcode = res[0] << 24 + res[1] << 16 + res[2] << 8 + res[3];
     }
 }

Modified: xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/JavaHexBinaryHolder.java
URL: http://svn.apache.org/viewvc/xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/JavaHexBinaryHolder.java?rev=1881117&r1=1881116&r2=1881117&view=diff
==============================================================================
--- xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/JavaHexBinaryHolder.java (original)
+++ xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/JavaHexBinaryHolder.java Sun Aug 23 20:16:45 2020
@@ -15,92 +15,80 @@
 
 package org.apache.xmlbeans.impl.values;
 
-import org.apache.xmlbeans.impl.schema.BuiltinSchemaTypeSystem;
-
-import org.apache.xmlbeans.impl.util.HexBin;
-import org.apache.xmlbeans.XmlObject;
+import org.apache.xmlbeans.SchemaType;
 import org.apache.xmlbeans.XmlErrorCodes;
 import org.apache.xmlbeans.XmlHexBinary;
-import org.apache.xmlbeans.SchemaType;
-import org.apache.xmlbeans.impl.common.ValidationContext;
+import org.apache.xmlbeans.XmlObject;
 import org.apache.xmlbeans.impl.common.QNameHelper;
+import org.apache.xmlbeans.impl.common.ValidationContext;
+import org.apache.xmlbeans.impl.schema.BuiltinSchemaTypeSystem;
+import org.apache.xmlbeans.impl.util.HexBin;
 
-import java.security.NoSuchAlgorithmException;
+import java.nio.charset.StandardCharsets;
 import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
 import java.util.Arrays;
-import java.io.UnsupportedEncodingException;
 
-public abstract class JavaHexBinaryHolder extends XmlObjectBase
-{
-    public SchemaType schemaType()
-        { return BuiltinSchemaTypeSystem.ST_HEX_BINARY; }
+public abstract class JavaHexBinaryHolder extends XmlObjectBase {
+    public SchemaType schemaType() {
+        return BuiltinSchemaTypeSystem.ST_HEX_BINARY;
+    }
 
     protected byte[] _value;
 
     // SIMPLE VALUE ACCESSORS BELOW -------------------------------------------
 
     // gets raw text value
-    protected String compute_text(NamespaceManager nsm)
-    {
-        return new String(HexBin.encode(_value));
+    protected String compute_text(NamespaceManager nsm) {
+        return new String(HexBin.encode(_value), StandardCharsets.ISO_8859_1);
     }
-    protected void set_text(String s)
-    {
+
+    protected void set_text(String s) {
         _hashcached = false;
-        if (_validateOnSet())
+        if (_validateOnSet()) {
             _value = validateLexical(s, schemaType(), _voorVc);
-        else
+        } else {
             _value = lex(s, _voorVc);
+        }
     }
-    protected void set_nil()
-    {
+
+    protected void set_nil() {
         _hashcached = false;
         _value = null;
     }
 
-    public static byte[] lex(String v, ValidationContext context)
-    {
-        byte[] vBytes = null;
-        try
-        {
-            vBytes = v.getBytes("UTF-8");
-        }
-        catch(UnsupportedEncodingException uee)
-        {
-            // should never happen - UTF-8 is always supported
-        }
+    public static byte[] lex(String v, ValidationContext context) {
+        byte[] vBytes = v.getBytes(StandardCharsets.ISO_8859_1);
         byte[] bytes = HexBin.decode(vBytes);
 
-        if (bytes == null)
-        {
+        if (bytes == null) {
             // TODO - get a decent error with line numbers and such here
-            context.invalid(XmlErrorCodes.HEXBINARY, new Object[] { "not encoded properly" });
+            context.invalid(XmlErrorCodes.HEXBINARY, new Object[]{"not encoded properly"});
         }
 
         return bytes;
     }
 
-    public static byte[] validateLexical(String v, SchemaType sType, ValidationContext context)
-    {
+    public static byte[] validateLexical(String v, SchemaType sType, ValidationContext context) {
         byte[] bytes = lex(v, context);
 
-        if (bytes == null)
+        if (bytes == null) {
             return null;
+        }
 
-        if (!sType.matchPatternFacet(v))
-        {
-            context.invalid( "Hex encoded data does not match pattern for " + QNameHelper.readable(sType));
+        if (!sType.matchPatternFacet(v)) {
+            context.invalid("Hex encoded data does not match pattern for " + QNameHelper.readable(sType));
             return null;
         }
 
         return bytes;
     }
 
-    public byte[] getByteArrayValue()
-    {
+    public byte[] getByteArrayValue() {
         check_dated();
-        if (_value == null)
+        if (_value == null) {
             return null;
+        }
 
         byte[] result = new byte[_value.length];
         System.arraycopy(_value, 0, result, 0, _value.length);
@@ -108,16 +96,14 @@ public abstract class JavaHexBinaryHolde
     }
 
     // setters
-    protected void set_ByteArray(byte[] ba)
-    {
+    protected void set_ByteArray(byte[] ba) {
         _hashcached = false;
         _value = new byte[ba.length];
         System.arraycopy(ba, 0, _value, 0, ba.length);
     }
 
     // comparators
-    protected boolean equal_to(XmlObject i)
-    {
+    protected boolean equal_to(XmlObject i) {
         byte[] ival = ((XmlHexBinary) i).getByteArrayValue();
         return Arrays.equals(_value, ival);
     }
@@ -126,30 +112,28 @@ public abstract class JavaHexBinaryHolde
     protected boolean _hashcached = false;
     protected int hashcode = 0;
     protected static MessageDigest md5;
-    static
-    {
-        try
-        {
+
+    static {
+        try {
             md5 = MessageDigest.getInstance("MD5");
-        }
-        catch( NoSuchAlgorithmException e )
-        {
+        } catch (NoSuchAlgorithmException e) {
             throw new IllegalStateException("Cannot find MD5 hash Algorithm");
         }
     }
 
-    protected int value_hash_code()
-    {
-        if( _hashcached )
+    protected int value_hash_code() {
+        if (_hashcached) {
             return hashcode;
+        }
 
         _hashcached = true;
 
-        if( _value == null )
+        if (_value == null) {
             return hashcode = 0;
+        }
 
         byte[] res = md5.digest(_value);
-        return hashcode = res[0]<<24 + res[1]<<16 + res[2]<<8 + res[3];
+        return hashcode = res[0] << 24 + res[1] << 16 + res[2] << 8 + res[3];
     }
 
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org