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/XmlCalendar.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 org.apache.xmlbeans.impl.util.SuppressForbidden;

import java.math.BigDecimal;
Expand Down Expand Up @@ -273,7 +274,7 @@ public static int getDefaultYear()
{
String yearstring = SystemProperties.getProperty("user.defaultyear");
if (yearstring != null)
defaultYear = Integer.parseInt(yearstring);
defaultYear = MathUtil.parseAsInteger(yearstring);
else
defaultYear = DEFAULT_DEFAULT_YEAR;
}
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.inst2xsd.util.TypeSystemHolder;
import org.apache.xmlbeans.impl.tool.CommandLine;
import org.apache.xmlbeans.impl.util.MathUtil;
import org.apache.xmlbeans.impl.xb.xsdschema.SchemaDocument;

import java.io.File;
Expand Down Expand Up @@ -149,7 +150,7 @@ else if (enumerations.equals("never"))
{
try
{
int intVal = Integer.parseInt(enumerations);
int intVal = MathUtil.parseAsInteger(enumerations);
inst2XsdOptions.setUseEnumerations(intVal);
}
catch (NumberFormatException e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.apache.xmlbeans.*;
import org.apache.xmlbeans.impl.common.QNameHelper;
import org.apache.xmlbeans.impl.store.Locale;
import org.apache.xmlbeans.impl.util.MathUtil;
import org.apache.xmlbeans.impl.xpath.XPathFactory;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Node;
Expand Down Expand Up @@ -328,7 +329,7 @@ public SchemaType typeForSignature(String signature) {

for (int i = parts.size() - 1; i >= 0; i -= 1) {
String part = parts.get(i);
if (part.length() < 1) {
if (part.isEmpty()) {
throw new IllegalArgumentException();
}
int offset = (part.length() >= 2 && part.charAt(1) == '=') ? 2 : 1;
Expand Down Expand Up @@ -449,12 +450,7 @@ public SchemaType typeForSignature(String signature) {
if (curType == null) {
throw new IllegalArgumentException();
} else {
int index;
try {
index = Integer.parseInt(part.substring(offset));
} catch (Exception e) {
throw new IllegalArgumentException();
}
int index = MathUtil.parseAsInteger(part.substring(offset));

if (curType.getSimpleVariety() != SchemaType.UNION) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.impl.util.MathUtil;

import java.util.Set;
import java.util.HashSet;
Expand Down Expand Up @@ -92,7 +93,7 @@ public static void main(String[] args)
if (indentStr == null)
indent = DEFAULT_INDENT;
else
indent = Integer.parseInt(indentStr);
indent = MathUtil.parseAsInteger(indentStr);

File[] files = cl.getFiles();

Expand Down
68 changes: 68 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 @@ -87,4 +87,72 @@ public static BigInteger parseAsBigInteger(String s) {
}
return new BigInteger(s);
}

/**
* @param s string to parse
* @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) {
if (s == null) {
throw new NullPointerException("Cannot parse null as Float");
}
if (s.length() > MAX_NUMBER_LENGTH) {
throw new IllegalArgumentException("Number has more than " + MAX_NUMBER_LENGTH + " characters");
}
return Float.parseFloat(s);
}

/**
* @param s string to parse
* @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) {
if (s == null) {
throw new NullPointerException("Cannot parse null as Double");
}
if (s.length() > MAX_NUMBER_LENGTH) {
throw new IllegalArgumentException("Number has more than " + MAX_NUMBER_LENGTH + " characters");
}
return Double.parseDouble(s);
}

/**
* @param s string to parse
* @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) {
if (s == null) {
throw new NullPointerException("Cannot parse null as Long");
}
if (s.length() > MAX_NUMBER_LENGTH) {
throw new IllegalArgumentException("Number has more than " + MAX_NUMBER_LENGTH + " characters");
}
return Long.parseLong(s);
}

/**
* @param s string to parse
* @return valid Integer
* @throws NumberFormatException if parse fails
* @throws IllegalArgumentException if string is too long
* @throws NullPointerException if string is null
*/
public static Integer parseAsInteger(String s) {
if (s == null) {
throw new NullPointerException("Cannot parse null as Integer");
}
if (s.length() > MAX_NUMBER_LENGTH) {
throw new IllegalArgumentException("Number has more than " + MAX_NUMBER_LENGTH + " characters");
}
return Integer.parseInt(s);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public static float lexFloat(CharSequence cs, boolean strict)
throw new NumberFormatException("Invalid char '" + ch + "' in float.");
}
}
return Float.parseFloat(v);
return MathUtil.parseAsFloat(v);
}

public static float lexFloat(CharSequence cs, Collection<XmlError> errors) {
Expand Down Expand Up @@ -186,7 +186,7 @@ public static double lexDouble(CharSequence cs, boolean strict)
throw new NumberFormatException("Invalid char '" + ch + "' in double.");
}
}
return Double.parseDouble(v);
return MathUtil.parseAsDouble(v);
}

public static double lexDouble(CharSequence cs, Collection<XmlError> errors) {
Expand Down Expand Up @@ -331,7 +331,7 @@ public static long lexLong(CharSequence cs)
rejectInvalidNumber(cs);
rejectSignAfterPlus(cs);
final String v = cs.toString();
return Long.parseLong(trimInitialPlus(v));
return MathUtil.parseAsLong(trimInitialPlus(v));
}

// trimInitialPlus drops a single leading '+', then Long.parseLong /
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import org.apache.xmlbeans.impl.store.Cur;
import org.apache.xmlbeans.impl.util.MathUtil;
import org.apache.xmlbeans.impl.xpath.saxon.SaxonXPath;
import org.apache.xmlbeans.impl.xpath.saxon.SaxonXQuery;
import org.apache.xmlbeans.impl.xpath.xmlbeans.XmlbeansXPath;
Expand Down Expand Up @@ -135,7 +136,7 @@ public static Path getCompiledPathSaxon(String pathExpr, String currentVar, Map<
}


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

return new SaxonXPath(pathExpr.substring(offset), currentVar, namespaces);
Expand Down Expand Up @@ -176,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 = Integer.parseInt(boundary.getOrDefault(XPath._NS_BOUNDARY, "0"));
boundaryVal = MathUtil.parseAsInteger(boundary.getOrDefault(XPath._NS_BOUNDARY, "0"));
}

return new SaxonXQuery(queryExpr, currentVar, boundaryVal, options);
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/apache/xmlbeans/soap/SOAPArrayType.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.apache.xmlbeans.impl.common.PrefixResolver;
import org.apache.xmlbeans.impl.common.QNameHelper;
import org.apache.xmlbeans.impl.common.XmlWhitespace;
import org.apache.xmlbeans.impl.util.MathUtil;
import org.apache.xmlbeans.impl.values.XmlValueOutOfRangeException;

import javax.xml.namespace.QName;
Expand Down Expand Up @@ -66,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 : Integer.parseInt(dimString);
return ("*".equals(dimString) || dimString.isEmpty()) ? -1 : MathUtil.parseAsInteger(dimString);
} catch (Exception e) {
throw new XmlValueOutOfRangeException("Malformed integer in SOAP array index");
}
Expand Down Expand Up @@ -126,7 +127,7 @@ public SOAPArrayType(QName name, String dimensions) {
// _hasIndeterminateDimensions = true;
} else {
try {
_dimensions[i] = Integer.parseInt(dimStrings[i]);
_dimensions[i] = MathUtil.parseAsInteger(dimStrings[i]);
} catch (Exception e) {
throw new XmlValueOutOfRangeException();
}
Expand Down Expand Up @@ -292,7 +293,7 @@ public static SOAPArrayType newSoap12Array(QName itemType, String arraySize) {
// _hasIndeterminateDimensions = true;
} else {
try {
dimensions[i] = Integer.parseInt(dimStrings[i]);
dimensions[i] = MathUtil.parseAsInteger(dimStrings[i]);
} catch (Exception e) {
throw new XmlValueOutOfRangeException();
}
Expand Down
Loading