Example usage for java.text ParsePosition getErrorIndex

List of usage examples for java.text ParsePosition getErrorIndex

Introduction

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

Prototype

public int getErrorIndex() 

Source Link

Document

Retrieve the index at which an error occurred, or -1 if the error index has not been set.

Usage

From source file:org.apache.ws.security.util.XmlSchemaDateFormat.java

private void handleParseError(ParsePosition parse_pos, String error_reason) throws ParseException {
    throw new ParseException("INVALID_XSD_DATETIME: " + error_reason, parse_pos.getErrorIndex());
}

From source file:org.eclipse.riena.ui.ridgets.validation.AbstractValidDate.java

private boolean isDate(final String value, final String pattern, final boolean strict) {
    if (value == null || pattern == null || pattern.length() <= 0) {

        return false;
    }/*from   ww  w  .ja  v a2  s  .  co m*/
    if (strict && value.length() != pattern.length()) {
        return false;
    }
    final SimpleDateFormat formatter = new SimpleDateFormat(pattern);
    formatter.setLenient(false);

    final ParsePosition pos = new ParsePosition(0);
    formatter.parse(value, pos);
    if (pos.getErrorIndex() != -1) {
        return false;
    }
    if (strict) {
        if (pos.getIndex() < value.length()) {
            return false;
        }
    }
    return true;
}

From source file:org.jboss.dashboard.ui.formatters.FactoryURL.java

public FactoryURL(String value) throws ParseException {
    ParsePosition pPos = new ParsePosition(0);
    Object[] o = msgf.parse(value, pPos);
    if (o == null)
        throw new ParseException("Cannot parse " + value + ". Error at position " + pPos.getErrorIndex(),
                pPos.getErrorIndex());//from  w  w  w . ja  v a2s . c o  m

    beanName = StringEscapeUtils.UNESCAPE_HTML4.translate((String) o[0]);
    fieldName = StringEscapeUtils.UNESCAPE_HTML4.translate((String) o[1]);
}

From source file:org.jboss.dashboard.ui.resources.ResourceName.java

public static ResourceName getInstance(String resName) {
    ResourceName resource = new ResourceName();
    try {/*from w  ww  . java2s.  c  o m*/
        //Workspace
        ParsePosition pPos = new ParsePosition(0);
        String resourceName = useBase64Names ? new String(Base64.decode(resName)) : resName;
        Object[] o = msgf.parse(resourceName, pPos);
        if (o == null)
            throw new ParseException(
                    "Cannot parse " + resourceName + ". Error at position " + pPos.getErrorIndex(),
                    pPos.getErrorIndex());
        //log.debug("Parsing result: " + Arrays.asList(o));

        //log.debug("Workspace name determined from resource name is " + o[0]);
        resource.workspaceId = (String) o[0];
        resource.workspaceId = "".equals(resource.workspaceId) ? null : resource.workspaceId;

        //Section
        //log.debug("Section determined from resource name is " + o[1]);
        String sectionId = (String) o[1];
        if (!"".equals(sectionId))
            resource.sectionId = new Long(sectionId);

        //Panel
        //log.debug("Panel determined from resource name is " + o[2]);
        String panelId = (String) o[2];
        if (!"".equals(panelId))
            resource.panelId = new Long(panelId);

        //Category
        //log.debug("Category determined from resource name is " + o[3]);
        resource.category = (String) o[3];
        if ("".equals(resource.category))
            throw new ParseException("Cannot find non-empty category name", 3);

        //CategoryId
        //log.debug("CategoryId determined from resource name is " + o[4]);
        resource.categoryId = (String) o[4];
        resource.categoryId = "".equals(resource.categoryId) ? null : resource.categoryId;

        //ResourceId
        //log.debug("Resource determined from resource name is " + o[5]);
        resource.resourceId = (String) o[5];
        if ("".equals(resource.resourceId))
            resource.resourceId = null;

        StringBuffer sb = new StringBuffer();
        sb.append(SEPARATOR);
        sb.append(RenderUtils.noNull(resource.workspaceId));
        sb.append(SEPARATOR);
        sb.append(RenderUtils.noNull(resource.sectionId));
        sb.append(SEPARATOR);
        sb.append(RenderUtils.noNull(resource.panelId));
        sb.append(SEPARATOR);
        sb.append(RenderUtils.noNull(resource.category));
        sb.append(SEPARATOR);
        sb.append(RenderUtils.noNull(resource.categoryId));
        sb.append(SEPARATOR);
        sb.append(RenderUtils.noNull(resource.resourceId));
        resource.resName = useBase64Names ? Base64.encode(sb.toString().getBytes()) : sb.toString();

        /*if (resource.categoryId != null) {
        sb=new StringBuffer();
        sb.append(SEPARATOR);
        sb.append(SEPARATOR);
        sb.append(SEPARATOR);
        sb.append(SEPARATOR);
        sb.append(RenderUtils.noNull(resource.category));
        sb.append(SEPARATOR);
        sb.append(RenderUtils.noNull(resource.categoryId));
        sb.append(SEPARATOR);
        sb.append(RenderUtils.noNull(resource.resourceId));
                
        resource.portableResourceName = useBase64Names ? Base64.encode(sb.toString().getBytes()) : sb.toString();
        }
        else{
        resource.portableResourceName = resource.resName;
        log.debug("Resource "+resource.resName+" is not portable.");
        } */
        resource.portableResourceName = resource.resName;

        resource.resourceClass = Class.forName("org.jboss.dashboard.ui.resources."
                + resource.category.substring(0, 1).toUpperCase() + resource.category.substring(1));

    } catch (Exception e) {
        log.debug("Error processing resource name. ", e);
        resource = null;
    }
    return resource;
}

From source file:org.jbpm.formModeler.service.bb.mvc.components.FactoryURL.java

public static FactoryURL getURL(String value) throws ParseException {
    ParsePosition pPos = new ParsePosition(0);
    Object[] o = msgf.parse(value, pPos);
    if (o == null)
        throw new ParseException("Cannot parse " + value + ". Error at position " + pPos.getErrorIndex(),
                pPos.getErrorIndex());/*ww  w . java 2s  .  c o m*/
    String componentName = StringEscapeUtils.unescapeHtml4((String) o[0]);
    String propertyName = StringEscapeUtils.unescapeHtml4((String) o[1]);
    return new FactoryURL(componentName, propertyName);
}

From source file:org.kisoonlineapp.jsf.CalendarConverterInternal.java

@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
    final String trimmedValue = StringUtils.trimToNull(value);
    if (StringUtils.isBlank(trimmedValue)) {
        return null;
    }/*from   w  w  w  .  ja v a2 s.  c  om*/

    final SimpleDateFormat sdf = createSimpleDateFormat(context, pattern);
    sdf.setLenient(false);
    final ParsePosition pos = new ParsePosition(0);
    final Date date = sdf.parse(trimmedValue, pos);

    if (pos.getErrorIndex() >= 0 || pos.getIndex() != trimmedValue.length()) {
        throw new ConverterException("Cannot parse " + trimmedValue);
    }

    final Calendar cal = kisoOnlineApp.getNow();
    cal.setTime(date);
    return cal;
}

From source file:org.kuali.rice.core.impl.datetime.DateTimeServiceImpl.java

protected Date parse(String dateString, String pattern) throws ParseException {
    if (!StringUtils.isBlank(dateString)) {
        DateFormat dateFormat = new SimpleDateFormat(pattern);
        dateFormat.setLenient(false);//  w  w w  . j a  v  a  2s . c o  m
        ParsePosition parsePosition = new ParsePosition(0);
        Date testDate = dateFormat.parse(dateString, parsePosition);

        // Ensure that the entire date String can be parsed by the current format.
        if (testDate == null) {
            throw new ParseException("The date that you provided is invalid.", parsePosition.getErrorIndex());
        } else if (parsePosition.getIndex() != dateString.length()) {
            throw new ParseException("The date that you provided is invalid.", parsePosition.getIndex());
        }

        // Ensure that the date's year lies between 1000 and 9999, to help prevent database-related date errors.
        Calendar testCalendar = Calendar.getInstance();
        testCalendar.setLenient(false);
        testCalendar.setTime(testDate);
        if (testCalendar.get(Calendar.YEAR) < 1000 || testCalendar.get(Calendar.YEAR) > 9999) {
            throw new ParseException("The date that you provided is not between the years 1000 and 9999.", -1);
        }

        if (testCalendar.get(Calendar.YEAR) == 1970 && !pattern.contains("y".toLowerCase())) {
            Calendar curCalendar = Calendar.getInstance();
            curCalendar.setTime(new java.util.Date());
            testCalendar.set(Calendar.YEAR, curCalendar.get(Calendar.YEAR));
            testDate = testCalendar.getTime();
        }

        return testDate;
    }
    return null;
}

From source file:org.lingcloud.molva.ocl.util.GenericTypeValidator.java

/**
 * Checks if the value can safely be converted to a byte primitive.
 * /*from   w w  w. j  a v  a  2 s  . com*/
 * @param value
 *            The value validation is being performed on.
 * @param locale
 *            The locale to use to parse the number (system default if null)
 * @return the converted Byte value.
 */
public static Byte formatByte(String value, Locale locale) {
    Byte result = null;

    if (value != null) {
        NumberFormat formatter = null;
        if (locale != null) {
            formatter = NumberFormat.getNumberInstance(locale);
        } else {
            formatter = NumberFormat.getNumberInstance(Locale.getDefault());
        }
        formatter.setParseIntegerOnly(true);
        ParsePosition pos = new ParsePosition(0);
        Number num = formatter.parse(value, pos);

        // If there was no error and we used the whole string
        if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) {
            if (num.doubleValue() >= Byte.MIN_VALUE && num.doubleValue() <= Byte.MAX_VALUE) {
                result = Byte.valueOf(num.byteValue());
            }
        }
    }

    return result;
}

From source file:org.lingcloud.molva.ocl.util.GenericTypeValidator.java

/**
 * Checks if the value can safely be converted to a short primitive.
 * /*from  w  ww.ja  va 2  s . co  m*/
 * @param value
 *            The value validation is being performed on.
 * @param locale
 *            The locale to use to parse the number (system default if
 *            null)vv
 * @return the converted Short value.
 */
public static Short formatShort(String value, Locale locale) {
    Short result = null;

    if (value != null) {
        NumberFormat formatter = null;
        if (locale != null) {
            formatter = NumberFormat.getNumberInstance(locale);
        } else {
            formatter = NumberFormat.getNumberInstance(Locale.getDefault());
        }
        formatter.setParseIntegerOnly(true);
        ParsePosition pos = new ParsePosition(0);
        Number num = formatter.parse(value, pos);

        // If there was no error and we used the whole string
        if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) {
            if (num.doubleValue() >= Short.MIN_VALUE && num.doubleValue() <= Short.MAX_VALUE) {
                result = Short.valueOf(num.shortValue());
            }
        }
    }

    return result;
}

From source file:org.lingcloud.molva.ocl.util.GenericTypeValidator.java

/**
 * Checks if the value can safely be converted to an int primitive.
 * //from w w  w. j a  v a  2s . com
 * @param value
 *            The value validation is being performed on.
 * @param locale
 *            The locale to use to parse the number (system default if null)
 * @return the converted Integer value.
 */
public static Integer formatInt(String value, Locale locale) {
    Integer result = null;

    if (value != null) {
        NumberFormat formatter = null;
        if (locale != null) {
            formatter = NumberFormat.getNumberInstance(locale);
        } else {
            formatter = NumberFormat.getNumberInstance(Locale.getDefault());
        }
        formatter.setParseIntegerOnly(true);
        ParsePosition pos = new ParsePosition(0);
        Number num = formatter.parse(value, pos);

        // If there was no error and we used the whole string
        if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) {
            if (num.doubleValue() >= Integer.MIN_VALUE && num.doubleValue() <= Integer.MAX_VALUE) {
                result = Integer.valueOf(num.intValue());
            }
        }
    }

    return result;
}