Example usage for java.text ParseException ParseException

List of usage examples for java.text ParseException ParseException

Introduction

In this page you can find the example usage for java.text ParseException ParseException.

Prototype

public ParseException(String s, int errorOffset) 

Source Link

Document

Constructs a ParseException with the specified detail message and offset.

Usage

From source file:org.deidentifier.arx.DataHandle.java

/**
 * Returns a date/time value from the specified cell.
 *
 * @param row The cell's row index//ww w.  j  a  v  a  2  s . c o m
 * @param col The cell's column index
 * @return the date
 * @throws ParseException the parse exception
 */
public Date getDate(int row, int col) throws ParseException {
    String value = getValue(row, col);
    DataType<?> type = getDataType(getAttributeName(col));
    if (type instanceof ARXDate) {
        return ((ARXDate) type).parse(value);
    } else {
        throw new ParseException("Invalid datatype: " + type.getClass().getSimpleName(), col);
    }
}

From source file:org.wiztools.commons.DateUtil.java

/**
 * Tries to match common patterns by which date is mentioned, and returns a Date object.
 * @param dateStr The date in common pattern.
 * @return The java.util.Date object constructed by parsing the input string.
 * @throws ParseException Thrown when not able to parse the date string.
 *//*  w  w  w .j  a  v  a 2 s  . c  o  m*/
public static Date getDateFromString(final String dateStr) throws ParseException{
    SimpleDateFormat sdf = null;
    if(p1.matcher(dateStr).matches()){
        sdf = sdf_p1;
    }
    else if(p2.matcher(dateStr).matches()){
        sdf = sdf_p2;
    }
    else if(p3.matcher(dateStr).matches()){
        sdf = sdf_p3;
    }
    else if(p4.matcher(dateStr).matches()){
        sdf = sdf_p4;
    }
    else if(p5.matcher(dateStr).matches()){
        sdf = sdf_p5;
    }
    else if(p6.matcher(dateStr).matches()){
        sdf = sdf_p6;
    }
    else if(p7.matcher(dateStr).matches()){
        sdf = sdf_p7;
    }

    if(sdf != null){
        return sdf.parse(dateStr);
    }

    throw new ParseException(dateStr + " does not match any defined pattern!", 0);
}

From source file:com.haulmont.chile.core.datatypes.impl.AdaptiveNumberDatatype.java

protected void checkLongRange(String value, Number result) throws ParseException {
    if (result instanceof Double
            && (result.doubleValue() > Long.MAX_VALUE || result.doubleValue() < Long.MIN_VALUE))
        throw new ParseException(String.format("Long range exceeded: \"%s\"", value), 0);
}

From source file:org.jbpm.formModeler.core.processing.fieldHandlers.NumericFieldHandler.java

public Object getTheValue(Field field, String[] paramValue, String desiredClassName) throws Exception {
    if (paramValue == null || paramValue.length == 0)
        return null;

    if (desiredClassName.equals("byte")) {
        if (StringUtils.isEmpty(paramValue[0]))
            return new Byte((byte) 0);
        else/*ww w  .  j av a  2 s .com*/
            return Byte.decode(paramValue[0]);
    } else if (desiredClassName.equals("short")) {
        if (StringUtils.isEmpty(paramValue[0]))
            return new Short((short) 0);
        else
            return Short.decode(paramValue[0]);
    } else if (desiredClassName.equals("int")) {
        if (StringUtils.isEmpty(paramValue[0]))
            return new Integer(0);
        else
            return Integer.decode(paramValue[0]);
    } else if (desiredClassName.equals("long")) {
        if (StringUtils.isEmpty(paramValue[0]))
            return new Long(0L);
        else
            return Long.decode(paramValue[0]);
    } else if (desiredClassName.equals(Byte.class.getName())) {
        if (StringUtils.isEmpty(paramValue[0]))
            throw new EmptyNumberException();
        return Byte.decode(paramValue[0]);
    } else if (desiredClassName.equals(Short.class.getName())) {
        if (StringUtils.isEmpty(paramValue[0]))
            throw new EmptyNumberException();
        return Short.decode(paramValue[0]);

    } else if (desiredClassName.equals(Integer.class.getName())) {
        if (StringUtils.isEmpty(paramValue[0]))
            throw new EmptyNumberException();
        return Integer.decode(paramValue[0]);

    } else if (desiredClassName.equals(Long.class.getName())) {
        if (StringUtils.isEmpty(paramValue[0]))
            throw new EmptyNumberException();
        return Long.decode(paramValue[0]);

    } else if (desiredClassName.equals(Double.class.getName()) || desiredClassName.equals("double")
            || desiredClassName.equals(Float.class.getName()) || desiredClassName.equals("float")
            || desiredClassName.equals(BigDecimal.class.getName())) {

        if (StringUtils.isEmpty(paramValue[0]))
            throw new EmptyNumberException();

        DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(new Locale(LocaleManager.currentLang()));
        if (desiredClassName.equals(BigDecimal.class.getName()))
            df.setParseBigDecimal(true);
        String pattern = getFieldPattern(field);
        if (pattern != null && !"".equals(pattern)) {
            df.applyPattern(pattern);
        } else {
            df.applyPattern("###.##");
        }
        ParsePosition pp = new ParsePosition(0);
        Number num = df.parse(paramValue[0], pp);
        if (paramValue[0].length() != pp.getIndex() || num == null) {
            log.debug("Error on parsing value");
            throw new ParseException("Error parsing value", pp.getIndex());
        }

        if (desiredClassName.equals(BigDecimal.class.getName())) {
            return num;
        } else if (desiredClassName.equals(Float.class.getName()) || desiredClassName.equals("float")) {
            return new Float(num.floatValue());
        } else if (desiredClassName.equals(Double.class.getName()) || desiredClassName.equals("double")) {
            return new Double(num.doubleValue());
        }
    } else if (desiredClassName.equals(BigInteger.class.getName())) {
        if (StringUtils.isEmpty(paramValue[0]))
            throw new EmptyNumberException();
        return new BigInteger(paramValue[0]);

    }
    throw new IllegalArgumentException("Invalid class for NumericFieldHandler: " + desiredClassName);
}

From source file:org.mongeez.reader.FormattedJavascriptChangeSetReader.java

private void parseFileHeader(Resource file, String line) throws IOException, ParseException {
    if (line == null || !FILE_HEADER_PATTERN.matcher(line).matches()) {
        throw new ParseException(file.getFile().getPath() + " did not begin with the expected comment:\n"
                + LINE_COMMENT + FILE_HEADER, -1);
    }//from   w w  w.j av a 2s.c  o m
}

From source file:org.azkfw.datasource.xml.XmlDatasourceFactory.java

private static XmlField readField(final int aCol, final XmlFieldEntity aField) throws ParseException {
    if (StringUtility.isEmpty(aField.name)) {
        throw new ParseException("Field name is empty.[row: 2; col: " + aCol + ";]", 2);
    }//  w  w w  . ja va 2s  . co m
    if (StringUtility.isEmpty(aField.type)) {
        throw new ParseException("Field type is empty.[row: 2; col: " + aCol + ";]", 2);
    }

    FieldType fieldType = FieldType.valueOfName(aField.type.trim());
    if (FieldType.Unknown == fieldType) {
        throw new ParseException("Undefined type.[type: " + aField.type + "; col: " + aCol + ";]", 2);
    }

    XmlField field = new XmlField();
    field.label = aField.label;
    field.name = aField.name;
    field.type = fieldType;

    return field;
}

From source file:gov.nih.nci.ncicb.tcga.dcc.qclive.common.TabDelimitedFileParser.java

/**
 * Loads the tab delimited file into a HashMap containing rows of data.  Row 0 represents the header of the tab delimited file.
 * Each ROW loaded contains the row number (0-based) and a String[] of the values for each row)
 *
 * @param file         the tab delimited file we want to load
 * @param commentToken if the line begins with this, it will be skipped.  may be null for no skipping.
 * @throws IOException when the exception can't be found.
 *///from ww w.j av a 2  s . c  om
public void loadTabDelimitedContent(final File file, final String commentToken,
        final boolean validateColumnsSize) throws IOException, ParseException {

    final Map<Integer, String[]> dataMap = new HashMap<Integer, String[]>();
    final Map<Integer, Integer> allDataLinesMapIncludingComments = new HashMap<Integer, Integer>();
    String dataLine;
    Integer rowCount = 0;
    Integer rowCountCommentEmptyLines = 0;

    FileReader fReader = new FileReader(file);
    @SuppressWarnings({ "IOResourceOpenedButNotSafelyClosed" })
    BufferedReader in = new BufferedReader(fReader);
    int columnCount = -1;
    try {
        while ((dataLine = in.readLine()) != null) {
            //count comment of empty lines also
            if (StringUtils.isEmpty(dataLine.trim()) || StringUtils.startsWith(dataLine, commentToken)) {
                rowCountCommentEmptyLines++;
            }
            // ignore blank lines!
            if (dataLine.trim().length() > 0 && (commentToken == null || !dataLine.startsWith(commentToken))) {
                final String[] rowCols = dataLine.split("\t", -1);
                //Initialize columnCount
                columnCount = (columnCount == -1) ? rowCols.length : columnCount;

                if (validateColumnsSize && (columnCount != rowCols.length)) {
                    throw new ParseException("Error at line no " + rowCount + ": Expected " + columnCount
                            + " columns but found " + rowCols.length + " columns.", rowCols.length);
                }
                dataMap.put(rowCount, rowCols);
                rowCount++;
            }

            //setup the map like this <currentDataLineNum,total no. of commentOrEmptyLines>
            allDataLinesMapIncludingComments.put(rowCount, rowCountCommentEmptyLines);

        }
        getTabDelimitedContent().setTabDelimitedContents(dataMap);
        setCommentAndEmptyLineMap(allDataLinesMapIncludingComments);
    } finally {
        IOUtils.closeQuietly(fReader);
        IOUtils.closeQuietly(in);

        fReader = null;
        in = null;
    }
}

From source file:org.apache.solr.common.util.DateUtil.java

/**
 * Slightly modified from org.apache.commons.httpclient.util.DateUtil.parseDate
 * <p/>/*w w w .ja  v a  2  s .  c o  m*/
 * Parses the date value using the given date formats.
 *
 * @param dateValue   the date value to parse
 * @param dateFormats the date formats to use
 * @param startDate   During parsing, two digit years will be placed in the range
 *                    <code>startDate</code> to <code>startDate + 100 years</code>. This value may
 *                    be <code>null</code>. When <code>null</code> is given as a parameter, year
 *                    <code>2000</code> will be used.
 * @return the parsed date
 * @throws ParseException if none of the dataFormats could parse the dateValue
 */
public static Date parseDate(String dateValue, Collection<String> dateFormats, Date startDate)
        throws ParseException {

    if (dateValue == null) {
        throw new IllegalArgumentException("dateValue is null");
    }
    if (dateFormats == null) {
        dateFormats = DEFAULT_HTTP_CLIENT_PATTERNS;
    }
    if (startDate == null) {
        startDate = DEFAULT_TWO_DIGIT_YEAR_START;
    }
    // trim single quotes around date if present
    // see issue #5279
    if (dateValue.length() > 1 && dateValue.startsWith("'") && dateValue.endsWith("'")) {
        dateValue = dateValue.substring(1, dateValue.length() - 1);
    }

    SimpleDateFormat dateParser = null;
    Iterator formatIter = dateFormats.iterator();

    while (formatIter.hasNext()) {
        String format = (String) formatIter.next();
        if (dateParser == null) {
            dateParser = new SimpleDateFormat(format, Locale.US);
            dateParser.setTimeZone(GMT);
            dateParser.set2DigitYearStart(startDate);
        } else {
            dateParser.applyPattern(format);
        }
        try {
            return dateParser.parse(dateValue);
        } catch (ParseException pe) {
            // ignore this exception, we will try the next format
        }
    }

    // we were unable to parse the date
    throw new ParseException("Unable to parse the date " + dateValue, 0);
}

From source file:org.openhie.openempi.util.DateUtil.java

/**
 * This method converts a String to a date using the datePattern
 * /*w  ww  .  j  a  v a  2s .  com*/
 * @param strDate the date to convert (in format MM/dd/yyyy)
 * @return a date object
 * @throws ParseException when String doesn't match the expected format
 */
public static Date convertStringToDate(String strDate) throws ParseException {
    Date aDate = null;

    try {
        if (log.isDebugEnabled()) {
            log.debug("converting date with pattern: " + getDatePattern());
        }

        aDate = convertStringToDate(getDatePattern(), strDate);
    } catch (ParseException pe) {
        log.error("Could not convert '" + strDate + "' to a date, throwing exception");
        pe.printStackTrace();
        throw new ParseException(pe.getMessage(), pe.getErrorOffset());
    }

    return aDate;
}

From source file:com.cloudlbs.core.utils.DateUtil.java

/**
 * Slightly modified from// w  w w .ja  v  a2s  .  co m
 * org.apache.commons.httpclient.util.DateUtil.parseDate
 * <p/>
 * Parses the date value using the given date formats.
 * 
 * @param dateValue
 *            the date value to parse
 * @param dateFormats
 *            the date formats to use
 * @param startDate
 *            During parsing, two digit years will be placed in the range
 *            <code>startDate</code> to <code>startDate + 100 years</code>.
 *            This value may be <code>null</code>. When <code>null</code> is
 *            given as a parameter, year <code>2000</code> will be used.
 * @return the parsed date
 * @throws ParseException
 *             if none of the dataFormats could parse the dateValue
 */
public static Date parseDate(String dateValue, Collection<String> dateFormats, Date startDate)
        throws ParseException {

    if (dateValue == null) {
        throw new IllegalArgumentException("dateValue is null");
    }
    if (dateFormats == null) {
        dateFormats = DEFAULT_HTTP_CLIENT_PATTERNS;
    }
    if (startDate == null) {
        startDate = DEFAULT_TWO_DIGIT_YEAR_START;
    }
    // trim single quotes around date if present
    // see issue #5279
    if (dateValue.length() > 1 && dateValue.startsWith("'") && dateValue.endsWith("'")) {
        dateValue = dateValue.substring(1, dateValue.length() - 1);
    }

    SimpleDateFormat dateParser = null;
    Iterator<String> formatIter = dateFormats.iterator();

    while (formatIter.hasNext()) {
        String format = (String) formatIter.next();
        if (dateParser == null) {
            dateParser = new SimpleDateFormat(format, Locale.US);
            dateParser.setTimeZone(GMT);
            dateParser.set2DigitYearStart(startDate);
        } else {
            dateParser.applyPattern(format);
        }
        try {
            return dateParser.parse(dateValue);
        } catch (ParseException pe) {
            // ignore this exception, we will try the next format
        }
    }

    // we were unable to parse the date
    throw new ParseException("Unable to parse the date " + dateValue, 0);
}