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
2 changes: 1 addition & 1 deletion src/main/java/org/apache/xmlbeans/XmlCalendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public static int getDefaultYear()
{
String yearstring = SystemProperties.getProperty("user.defaultyear");
if (yearstring != null)
defaultYear = MathUtil.parseAsInteger(yearstring);
defaultYear = MathUtil.parseAsInt(yearstring);
else
defaultYear = DEFAULT_DEFAULT_YEAR;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ else if (enumerations.equals("never"))
{
try
{
int intVal = MathUtil.parseAsInteger(enumerations);
int intVal = MathUtil.parseAsInt(enumerations);
inst2XsdOptions.setUseEnumerations(intVal);
}
catch (NumberFormatException e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ public SchemaType typeForSignature(String signature) {
if (curType == null) {
throw new IllegalArgumentException();
} else {
int index = MathUtil.parseAsInteger(part.substring(offset));
int index = MathUtil.parseAsInt(part.substring(offset));

if (curType.getSimpleVariety() != SchemaType.UNION) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static void main(String[] args)
if (indentStr == null)
indent = DEFAULT_INDENT;
else
indent = MathUtil.parseAsInteger(indentStr);
indent = MathUtil.parseAsInt(indentStr);

File[] files = cl.getFiles();

Expand Down
46 changes: 35 additions & 11 deletions src/main/java/org/apache/xmlbeans/impl/util/MathUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,41 +106,65 @@ public static BigInteger parseAsBigInteger(String s) {
* @throws IllegalArgumentException if string is too long
* @throws NullPointerException if string is null
*/
public static Float parseAsFloat(String s) {
public static float parseAsFloat(String s) {
return parseAsFloat(s, DEFAULT_MAX_NUMBER_CHARS);
}

/**
* @param s string to parse
* @param maxNumberOfChars maximum number of characters allowed in the string
* @return valid float
* @throws NumberFormatException if parse fails
* @throws IllegalArgumentException if string is too long
* @throws NullPointerException if string is null
*/
public static float parseAsFloat(String s, int maxNumberOfChars) {
if (s == null) {
throw new NullPointerException("Cannot parse null as Float");
}
if (s.length() > DEFAULT_MAX_NUMBER_CHARS) {
throw new IllegalArgumentException("Number has more than " + DEFAULT_MAX_NUMBER_CHARS + " characters");
if (s.length() > maxNumberOfChars) {
throw new IllegalArgumentException("Number has more than " + maxNumberOfChars + " characters");
}
return Float.parseFloat(s);
}

/**
* @param s string to parse
* @return valid Float
* @return valid float
* @throws NumberFormatException if parse fails
* @throws IllegalArgumentException if string is too long
* @throws NullPointerException if string is null
*/
public static Double parseAsDouble(String s) {
public static double parseAsDouble(String s) {
return parseAsDouble(s, DEFAULT_MAX_NUMBER_CHARS);
}

/**
* @param s string to parse
* @param maxNumberOfChars maximum number of characters allowed in the string
* @return valid float
* @throws NumberFormatException if parse fails
* @throws IllegalArgumentException if string is too long
* @throws NullPointerException if string is null
*/
public static double parseAsDouble(String s, int maxNumberOfChars) {
if (s == null) {
throw new NullPointerException("Cannot parse null as Double");
}
if (s.length() > DEFAULT_MAX_NUMBER_CHARS) {
throw new IllegalArgumentException("Number has more than " + DEFAULT_MAX_NUMBER_CHARS + " characters");
if (s.length() > maxNumberOfChars) {
throw new IllegalArgumentException("Number has more than " + maxNumberOfChars + " characters");
}
return Double.parseDouble(s);
}

/**
* @param s string to parse
* @return valid Long
* @return valid long
* @throws NumberFormatException if parse fails
* @throws IllegalArgumentException if string is too long
* @throws NullPointerException if string is null
*/
public static Long parseAsLong(String s) {
public static long parseAsLong(String s) {
if (s == null) {
throw new NullPointerException("Cannot parse null as Long");
}
Expand All @@ -152,12 +176,12 @@ public static Long parseAsLong(String s) {

/**
* @param s string to parse
* @return valid Integer
* @return valid int
* @throws NumberFormatException if parse fails
* @throws IllegalArgumentException if string is too long
* @throws NullPointerException if string is null
*/
public static Integer parseAsInteger(String s) {
public static int parseAsInt(String s) {
if (s == null) {
throw new NullPointerException("Cannot parse null as Integer");
}
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/org/apache/xmlbeans/impl/util/XsTypeConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private static void checkFloatingPointLexical(CharSequence cs) {
// ======================== float ========================
public static float lexFloat(CharSequence cs)
throws NumberFormatException {
return lexFloat(cs, false);
return lexFloat(cs, false, XmlOptions.DEFAULT_MAX_NUMBER_CHARS);
}

/**
Expand All @@ -89,11 +89,13 @@ public static float lexFloat(CharSequence cs)
* ({@code f}/{@code F}/{@code d}/{@code D}). When {@code false} the
* long-standing lenient behaviour applies. Driven by
* {@link org.apache.xmlbeans.XmlOptions#setLoadStrictFloatingPoint()}.
* @param maxNumberOfChars the maximum number of characters allowed in the string
* @return the parsed float
* @throws NumberFormatException if the value is not a valid xsd:float
* @since 5.4.0
*/
public static float lexFloat(CharSequence cs, boolean strict)
public static float lexFloat(CharSequence cs, boolean strict,
int maxNumberOfChars)
throws NumberFormatException {
rejectInvalidNumber(cs);
final String v = cs.toString();
Expand All @@ -116,12 +118,12 @@ public static float lexFloat(CharSequence cs, boolean strict)
throw new NumberFormatException("Invalid char '" + ch + "' in float.");
}
}
return MathUtil.parseAsFloat(v);
return MathUtil.parseAsFloat(v, maxNumberOfChars);
}

public static float lexFloat(CharSequence cs, Collection<XmlError> errors) {
try {
return lexFloat(cs);
return lexFloat(cs, false, XmlOptions.DEFAULT_MAX_NUMBER_CHARS);
} catch (NumberFormatException e) {
String msg = "invalid float: " + cs;
errors.add(XmlError.forMessage(msg));
Expand All @@ -146,7 +148,7 @@ public static String printFloat(float value) {
// ======================== double ========================
public static double lexDouble(CharSequence cs)
throws NumberFormatException {
return lexDouble(cs, false);
return lexDouble(cs, false, XmlOptions.DEFAULT_MAX_NUMBER_CHARS);
}

/**
Expand All @@ -159,11 +161,13 @@ public static double lexDouble(CharSequence cs)
* ({@code f}/{@code F}/{@code d}/{@code D}). When {@code false} the
* long-standing lenient behaviour applies. Driven by
* {@link org.apache.xmlbeans.XmlOptions#setLoadStrictFloatingPoint()}.
* @param maxNumberOfChars the maximum number of characters allowed in the string
* @return the parsed double
* @throws NumberFormatException if the value is not a valid xsd:double
* @since 5.4.0
*/
public static double lexDouble(CharSequence cs, boolean strict)
public static double lexDouble(CharSequence cs, boolean strict,
int maxNumberOfChars)
throws NumberFormatException {
rejectInvalidNumber(cs);
final String v = cs.toString();
Expand All @@ -186,12 +190,12 @@ public static double lexDouble(CharSequence cs, boolean strict)
throw new NumberFormatException("Invalid char '" + ch + "' in double.");
}
}
return MathUtil.parseAsDouble(v);
return MathUtil.parseAsDouble(v, maxNumberOfChars);
}

public static double lexDouble(CharSequence cs, Collection<XmlError> errors) {
try {
return lexDouble(cs);
return lexDouble(cs, false, XmlOptions.DEFAULT_MAX_NUMBER_CHARS);
} catch (NumberFormatException e) {
String msg = "invalid double: " + cs;
errors.add(XmlError.forMessage(msg));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,7 @@ private void validateAtomicType(
}
case SchemaType.BTC_FLOAT: {
float f =
JavaFloatHolderEx.validateLexical(value, type, _vc);
JavaFloatHolderEx.validateLexical(value, type, _vc, _options.getMaxNumberOfCharsForNumbers());

if (errorState == _errorState) {
JavaFloatHolderEx.validateValue(f, type, _vc);
Expand All @@ -1133,7 +1133,7 @@ private void validateAtomicType(
}
case SchemaType.BTC_DOUBLE: {
double d =
JavaDoubleHolderEx.validateLexical(value, type, _vc);
JavaDoubleHolderEx.validateLexical(value, type, _vc, _options.getMaxNumberOfCharsForNumbers());

if (errorState == _errorState) {
JavaDoubleHolderEx.validateValue(d, type, _vc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.apache.xmlbeans.SchemaType;
import org.apache.xmlbeans.XmlErrorCodes;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import org.apache.xmlbeans.impl.common.ValidationContext;
import org.apache.xmlbeans.impl.schema.BuiltinSchemaTypeSystem;
import org.apache.xmlbeans.impl.util.XsTypeConverter;
Expand Down Expand Up @@ -61,8 +62,13 @@ public static double validateLexical(String v, ValidationContext context) {
}

public static double validateLexical(String v, ValidationContext context, boolean strict) {
return validateLexical(v, context, strict, XmlOptions.DEFAULT_MAX_NUMBER_CHARS);
}

public static double validateLexical(String v, ValidationContext context, boolean strict,
int maxNumberOfCharsForNumbers) {
try {
return XsTypeConverter.lexDouble(v, strict);
return XsTypeConverter.lexDouble(v, strict, maxNumberOfCharsForNumbers);
} catch (NumberFormatException e) {
context.invalid(XmlErrorCodes.DOUBLE, new Object[]{v});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.apache.xmlbeans.SchemaType;
import org.apache.xmlbeans.XmlErrorCodes;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import org.apache.xmlbeans.impl.common.QNameHelper;
import org.apache.xmlbeans.impl.common.ValidationContext;

Expand All @@ -42,7 +43,12 @@ protected void set_double(double v) {
}

public static double validateLexical(String v, SchemaType sType, ValidationContext context) {
double d = JavaDoubleHolder.validateLexical(v, context);
return validateLexical(v, sType, context, XmlOptions.DEFAULT_MAX_NUMBER_CHARS);
}

public static double validateLexical(String v, SchemaType sType, ValidationContext context,
int maxNumberOfChars) {
double d = JavaDoubleHolder.validateLexical(v, context, false, maxNumberOfChars);

if (!sType.matchPatternFacet(v)) {
context.invalid(XmlErrorCodes.DATATYPE_VALID$PATTERN_VALID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.apache.xmlbeans.SchemaType;
import org.apache.xmlbeans.XmlErrorCodes;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import org.apache.xmlbeans.impl.common.ValidationContext;
import org.apache.xmlbeans.impl.schema.BuiltinSchemaTypeSystem;
import org.apache.xmlbeans.impl.util.XsTypeConverter;
Expand Down Expand Up @@ -61,8 +62,13 @@ public static float validateLexical(String v, ValidationContext context) {
}

public static float validateLexical(String v, ValidationContext context, boolean strict) {
return validateLexical(v, context, strict, XmlOptions.DEFAULT_MAX_NUMBER_CHARS);
}

public static float validateLexical(String v, ValidationContext context, boolean strict,
int maxNumberOfChars) {
try {
return XsTypeConverter.lexFloat(v, strict);
return XsTypeConverter.lexFloat(v, strict, maxNumberOfChars);
} catch (NumberFormatException e) {
context.invalid(XmlErrorCodes.FLOAT, new Object[]{v});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.apache.xmlbeans.SchemaType;
import org.apache.xmlbeans.XmlErrorCodes;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import org.apache.xmlbeans.impl.common.QNameHelper;
import org.apache.xmlbeans.impl.common.ValidationContext;

Expand All @@ -42,11 +43,16 @@ protected void set_float(float v) {
}

public static float validateLexical(String v, SchemaType sType, ValidationContext context) {
float f = JavaFloatHolder.validateLexical(v, context);
return validateLexical(v, sType, context, XmlOptions.DEFAULT_MAX_NUMBER_CHARS);
}

public static float validateLexical(String v, SchemaType sType, ValidationContext context,
int maxNumberOfChars) {
float f = JavaFloatHolder.validateLexical(v, context, false, maxNumberOfChars);

if (!sType.matchPatternFacet(v)) {
context.invalid(XmlErrorCodes.DATATYPE_VALID$PATTERN_VALID,
new Object[]{"float", v, QNameHelper.readable(sType)});
new Object[]{"float", v, QNameHelper.readable(sType)});
}

return f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public static Path getCompiledPathSaxon(String pathExpr, String currentVar, Map<
}


int offset = MathUtil.parseAsInteger(namespaces.getOrDefault(XPath._NS_BOUNDARY, "0"));
int offset = MathUtil.parseAsInt(namespaces.getOrDefault(XPath._NS_BOUNDARY, "0"));
namespaces.remove(XPath._NS_BOUNDARY);

return new SaxonXPath(pathExpr.substring(offset), currentVar, namespaces);
Expand Down Expand Up @@ -177,7 +177,7 @@ static synchronized XQuery getCompiledQuery(String queryExpr, String currentVar,
} catch (XPath.XPathCompileException e) {
//don't care if it fails, just care about boundary
} finally {
boundaryVal = MathUtil.parseAsInteger(boundary.getOrDefault(XPath._NS_BOUNDARY, "0"));
boundaryVal = MathUtil.parseAsInt(boundary.getOrDefault(XPath._NS_BOUNDARY, "0"));
}

return new SaxonXQuery(queryExpr, currentVar, boundaryVal, options);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/apache/xmlbeans/soap/SOAPArrayType.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private static int[] internalParseCommaIntString(String csl) {
private static int collapseDimString(String dimString2) {
String dimString = XmlWhitespace.collapse(dimString2, XmlWhitespace.WS_COLLAPSE);
try {
return ("*".equals(dimString) || dimString.isEmpty()) ? -1 : MathUtil.parseAsInteger(dimString);
return ("*".equals(dimString) || dimString.isEmpty()) ? -1 : MathUtil.parseAsInt(dimString);
} catch (Exception e) {
throw new XmlValueOutOfRangeException("Malformed integer in SOAP array index");
}
Expand Down Expand Up @@ -127,7 +127,7 @@ public SOAPArrayType(QName name, String dimensions) {
// _hasIndeterminateDimensions = true;
} else {
try {
_dimensions[i] = MathUtil.parseAsInteger(dimStrings[i]);
_dimensions[i] = MathUtil.parseAsInt(dimStrings[i]);
} catch (Exception e) {
throw new XmlValueOutOfRangeException();
}
Expand Down Expand Up @@ -293,7 +293,7 @@ public static SOAPArrayType newSoap12Array(QName itemType, String arraySize) {
// _hasIndeterminateDimensions = true;
} else {
try {
dimensions[i] = MathUtil.parseAsInteger(dimStrings[i]);
dimensions[i] = MathUtil.parseAsInt(dimStrings[i]);
} catch (Exception e) {
throw new XmlValueOutOfRangeException();
}
Expand Down
Loading
Loading