Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/org/apache/xmlbeans/GDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.apache.xmlbeans;

import org.apache.xmlbeans.impl.util.ExceptionUtil;
import org.apache.xmlbeans.impl.util.MathUtil;

import java.math.BigDecimal;
import java.math.BigInteger;
Expand Down Expand Up @@ -274,7 +275,7 @@ public GDate(CharSequence string) {
}
}
try {
fs = new BigDecimal(string.subSequence(start, len).toString());
fs = MathUtil.parseAsBigDecimal(string.subSequence(start, len).toString());
} catch (Throwable e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/apache/xmlbeans/GDuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

package org.apache.xmlbeans;

import org.apache.xmlbeans.impl.util.MathUtil;

import java.math.BigDecimal;

/**
Expand Down Expand Up @@ -131,7 +133,7 @@ public GDuration(CharSequence str)
int i = start;
do i += 1;
while (i < len && GDate.isDigit(ch = str.charAt(i)));
_fs = new BigDecimal(str.subSequence(start, i).toString());
_fs = MathUtil.parseAsBigDecimal(str.subSequence(start, i).toString());
if (i >= len || ch != 'S')
throw new IllegalArgumentException("illegal duration");
start = i;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.apache.xmlbeans.*;
import org.apache.xmlbeans.impl.common.QNameHelper;
import org.apache.xmlbeans.impl.util.MathUtil;
import org.apache.xmlbeans.impl.values.XmlIntegerImpl;
import org.apache.xmlbeans.impl.values.XmlStringImpl;
import org.apache.xmlbeans.impl.values.XmlValueOutOfRangeException;
Expand Down Expand Up @@ -165,7 +166,7 @@ public static SchemaTypeSystem get()
build_wsstring(SchemaType.WS_COLLAPSE), null, null };

private final static XmlValueRef[] FACETS_UNSIGNED_LONG = new XmlValueRef[]
{ null, null, null, null, buildInteger(BigInteger.ZERO), buildInteger(new BigInteger("18446744073709551615")), null, null, buildNnInteger(BigInteger.ZERO),
{ null, null, null, null, buildInteger(BigInteger.ZERO), buildInteger(MathUtil.parseAsBigInteger("18446744073709551615")), null, null, buildNnInteger(BigInteger.ZERO),
build_wsstring(SchemaType.WS_COLLAPSE), null, null };

private final static XmlValueRef[] FACETS_UNSIGNED_INT = new XmlValueRef[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.xmlbeans.impl.common.QNameHelper;
import org.apache.xmlbeans.impl.common.XMLChar;
import org.apache.xmlbeans.impl.schema.StscImporter.SchemaToProcess;
import org.apache.xmlbeans.impl.util.MathUtil;
import org.apache.xmlbeans.impl.values.NamespaceContext;
import org.apache.xmlbeans.impl.values.XmlNonNegativeIntegerImpl;
import org.apache.xmlbeans.impl.values.XmlPositiveIntegerImpl;
Expand Down Expand Up @@ -1511,8 +1512,8 @@ static BigInteger buildBigInt(XmlAnySimpleType value) {
String text = value.getStringValue();
BigInteger bigInt;
try {
bigInt = new BigInteger(text);
} catch (NumberFormatException e) {
bigInt = MathUtil.parseAsBigInteger(text);
} catch (Exception e) {
StscState.get().error(XmlErrorCodes.INVALID_VALUE_DETAIL, new Object[]{text, "nonNegativeInteger", e.getMessage()}, value);
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.apache.xmlbeans.*;
import org.apache.xmlbeans.impl.common.QNameHelper;
import org.apache.xmlbeans.impl.util.MathUtil;
import org.apache.xmlbeans.impl.values.XmlIntegerImpl;
import org.apache.xmlbeans.impl.values.XmlStringImpl;
import org.apache.xmlbeans.impl.values.XmlValueOutOfRangeException;
Expand Down Expand Up @@ -185,7 +186,7 @@ public static SchemaTypeSystem get() {
build_wsstring(SchemaType.WS_COLLAPSE), null, null};

private final static XmlValueRef[] FACETS_UNSIGNED_LONG = new XmlValueRef[]
{null, null, null, null, buildInteger(BigInteger.ZERO), buildInteger(new BigInteger("18446744073709551615")), null, null, buildNnInteger(BigInteger.ZERO),
{null, null, null, null, buildInteger(BigInteger.ZERO), buildInteger(MathUtil.parseAsBigInteger("18446744073709551615")), null, null, buildNnInteger(BigInteger.ZERO),
build_wsstring(SchemaType.WS_COLLAPSE), null, null};

private final static XmlValueRef[] FACETS_UNSIGNED_INT = new XmlValueRef[]
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/org/apache/xmlbeans/impl/util/MathUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ Licensed to the Apache Software Foundation (ASF) under one or more
==================================================================== */
package org.apache.xmlbeans.impl.util;

import java.math.BigDecimal;
import java.math.BigInteger;

/**
* Internal Use Only. Utility methods for dealing with conversions
*/
Expand Down Expand Up @@ -47,4 +50,41 @@ public static int safeDoubleToInt(double d) {
}
return (int) d;
}

// TODO try to make this configurable
private static final int MAX_NUMBER_LENGTH = 1024;

/**
* @param s string to parse
* @return valid BigDecimal
* @throws NumberFormatException if parse fails
* @throws IllegalArgumentException if string is too long
* @throws NullPointerException if string is null
*/
public static BigDecimal parseAsBigDecimal(String s) {
if (s == null) {
throw new NullPointerException("Cannot parse null as BigDecimal");
}
if (s.length() > MAX_NUMBER_LENGTH) {
throw new IllegalArgumentException("Number has more than " + MAX_NUMBER_LENGTH + " characters");
}
return new BigDecimal(s);
}

/**
* @param s string to parse
* @return valid BigInteger
* @throws NumberFormatException if parse fails
* @throws IllegalArgumentException if string is too long
* @throws NullPointerException if string is null
*/
public static BigInteger parseAsBigInteger(String s) {
if (s == null) {
throw new NullPointerException("Cannot parse null as BigInteger");
}
if (s.length() > MAX_NUMBER_LENGTH) {
throw new IllegalArgumentException("Number has more than " + MAX_NUMBER_LENGTH + " characters");
}
return new BigInteger(s);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public static BigDecimal lexDecimal(CharSequence cs, boolean allowExponent)
//equals() method, but the xml value
//space does not consider them significant.
//See http://www.w3.org/2001/05/xmlschema-errata#e2-44
return new BigDecimal(trimTrailingZeros(v));
return MathUtil.parseAsBigDecimal(trimTrailingZeros(v));
}

private static final char[] CH_ZEROS = new char[]{'0', '0', '0', '0', '0', '0', '0', '0',
Expand Down Expand Up @@ -308,7 +308,7 @@ public static BigInteger lexInteger(CharSequence cs)

//TODO: consider special casing zero and one to return static values
//from BigInteger to avoid object creation.
return new BigInteger(trimInitialPlus(v));
return MathUtil.parseAsBigInteger(trimInitialPlus(v));
}

public static BigInteger lexInteger(CharSequence cs, Collection<XmlError> errors) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.xmlbeans.impl.schema.SchemaTypeImpl;
import org.apache.xmlbeans.impl.schema.SchemaTypeVisitorImpl;
import org.apache.xmlbeans.impl.util.ExceptionUtil;
import org.apache.xmlbeans.impl.util.MathUtil;
import org.apache.xmlbeans.impl.util.XsTypeConverter;
import org.apache.xmlbeans.impl.values.*;

Expand Down Expand Up @@ -1108,7 +1109,7 @@ private void validateAtomicType(
}

if (errorState == _errorState) {
_decimalValue = new BigDecimal(value);
_decimalValue = MathUtil.parseAsBigDecimal(value);
JavaDecimalHolderEx.validateValue(_decimalValue, type, _vc);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.impl.common.ValidationContext;
import org.apache.xmlbeans.impl.schema.BuiltinSchemaTypeSystem;
import org.apache.xmlbeans.impl.util.MathUtil;
import org.apache.xmlbeans.impl.util.XsTypeConverter;

import java.math.BigDecimal;
Expand Down Expand Up @@ -47,8 +48,8 @@ protected void set_text(String s) {
}

try {
set_BigDecimal(new BigDecimal(s));
} catch (NumberFormatException e) {
set_BigDecimal(MathUtil.parseAsBigDecimal(s));
} catch (Exception e) {
_voorVc.invalid(XmlErrorCodes.DECIMAL, new Object[]{s});
}
}
Expand All @@ -70,8 +71,8 @@ public static void validateLexical(String v, ValidationContext context, boolean
// long-standing lenient behaviour: accept whatever BigDecimal accepts,
// which includes scientific/exponent notation such as "1E5".
try {
new BigDecimal(v);
} catch (NumberFormatException e) {
MathUtil.parseAsBigDecimal(v);
} catch (Exception e) {
context.invalid(XmlErrorCodes.DECIMAL, new Object[]{v});
}
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.impl.common.QNameHelper;
import org.apache.xmlbeans.impl.common.ValidationContext;
import org.apache.xmlbeans.impl.util.MathUtil;

import java.math.BigDecimal;
import java.math.RoundingMode;
Expand All @@ -44,8 +45,8 @@ protected void set_text(String s) {

BigDecimal v = null;
try {
v = new BigDecimal(s);
} catch (NumberFormatException e) {
v = MathUtil.parseAsBigDecimal(s);
} catch (Exception e) {
_voorVc.invalid(XmlErrorCodes.DECIMAL, new Object[]{s});
}

Expand Down Expand Up @@ -83,7 +84,6 @@ public static void validateLexical(String v, SchemaType sType, ValidationContext
/**
* Performs facet validation only.
*/

public static void validateValue(BigDecimal v, SchemaType sType, ValidationContext context) {
// fractional digits
XmlObject fd = sType.getFacet(SchemaType.FACET_FRACTION_DIGITS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.impl.common.ValidationContext;
import org.apache.xmlbeans.impl.schema.BuiltinSchemaTypeSystem;
import org.apache.xmlbeans.impl.util.MathUtil;

import java.math.BigDecimal;
import java.math.BigInteger;
Expand Down Expand Up @@ -49,7 +50,7 @@ public static BigInteger lex(String s, ValidationContext vc) {
}

try {
return new BigInteger(s);
return MathUtil.parseAsBigInteger(s);
} catch (Exception e) {
vc.invalid(XmlErrorCodes.INTEGER, new Object[]{s});
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import org.apache.xmlbeans.*;
import org.apache.xmlbeans.impl.util.HexBin;
import org.apache.xmlbeans.impl.util.MathUtil;
import org.apache.xmlbeans.soap.SOAPArrayType;
import org.apache.xmlbeans.soap.SchemaWSDLArrayType;

Expand Down Expand Up @@ -382,7 +383,7 @@ private String formatToLength(String s, SchemaType sType) {
}

private String formatDecimal(String start, SchemaType sType) {
BigDecimal result = new BigDecimal(start);
BigDecimal result = MathUtil.parseAsBigDecimal(start);
XmlDecimal xmlD;
xmlD = (XmlDecimal) sType.getFacet(SchemaType.FACET_MIN_INCLUSIVE);
BigDecimal min = xmlD != null ? xmlD.getBigDecimalValue() : null;
Expand Down Expand Up @@ -414,7 +415,7 @@ private String formatDecimal(String start, SchemaType sType) {
for (int i = 0; i < totalDigits; i++) {
sb.append('9');
}
BigDecimal digitsLimit = new BigDecimal(sb.toString());
BigDecimal digitsLimit = MathUtil.parseAsBigDecimal(sb.toString());
if (max != null && max.compareTo(digitsLimit) > 0) {
max = digitsLimit;
maxInclusive = true;
Expand Down Expand Up @@ -445,7 +446,7 @@ private String formatDecimal(String start, SchemaType sType) {
sb.append('0');
}
sb.append('1');
increment = new BigDecimal(sb.toString());
increment = MathUtil.parseAsBigDecimal(sb.toString());
} else {
increment = BigDecimal.ONE;
}
Expand Down
Loading