Java Code Examples for java.text.ParseException#getLocalizedMessage()

The following examples show how to use java.text.ParseException#getLocalizedMessage() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: sis   File: AbstractParser.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates the object from a string and log the warnings if any.
 * This method is for implementation of {@code createFromWKT(String)} method is SIS factories only.
 *
 * @param  text  coordinate system encoded in Well-Known Text format (version 1 or 2).
 * @return the result of parsing the given text.
 * @throws FactoryException if the object creation failed.
 *
 * @see org.apache.sis.referencing.factory.GeodeticObjectFactory#createFromWKT(String)
 * @see org.apache.sis.referencing.operation.transform.DefaultMathTransformFactory#createFromWKT(String)
 */
@Override
public final Object createFromWKT(final String text) throws FactoryException {
    final Object value;
    try {
        value = parseObject(text, new ParsePosition(0));
    } catch (ParseException exception) {
        final Throwable cause = exception.getCause();
        if (cause instanceof FactoryException) {
            throw (FactoryException) cause;
        }
        throw new FactoryException(exception.getLocalizedMessage(), exception);
    }
    final Warnings warnings = getAndClearWarnings(value);
    if (warnings != null) {
        log(new LogRecord(Level.WARNING, warnings.toString()));
    }
    return value;
}
 
Example 2
Source Project: sis   File: LocalizedParseExceptionTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Tests the {@link LocalizedParseException} constructor using the default string.
 * This method also tests {@link Exceptions#getLocalizedMessage(Throwable, Locale)}
 * as a side-effect.
 */
@Test
public void testAutomaticMessage() {
    final ParsePosition pos = new ParsePosition(0);
    pos.setErrorIndex(5);
    final ParseException e = new LocalizedParseException(
            Locale.CANADA, Angle.class, "Some text to parse", pos);
    String message = e.getLocalizedMessage();
    assertTrue(message, message.contains("Some text to parse"));
    assertTrue(message, message.contains("can not be parsed"));
    assertTrue(message, message.contains("Angle"));

    assertEquals(message, Exceptions.getLocalizedMessage(e, Locale.CANADA));
    message = Exceptions.getLocalizedMessage(e, Locale.FRANCE);
    assertTrue(message, message.contains("Some text to parse"));
    assertTrue(message, message.contains("n’est pas reconnu"));
    assertTrue(message, message.contains("Angle"));
}
 
Example 3
Source Project: cuba   File: DoubleMinValidator.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void accept(T value) throws ValidationException {
    // consider null value is valid
    if (value == null) {
        return;
    }

    NumberConstraint constraint = null;

    if (value instanceof Number) {
        constraint = getNumberConstraint((Number) value);
    } else if (value instanceof String) {
        try {
            Datatype datatype = datatypeRegistry.getNN(Double.class);
            Locale locale = userSessionSource.getUserSession().getLocale();
            Double num = (Double) datatype.parse((String) value, locale);
            if (num == null) {
                fireValidationException(value);
            }
            constraint = getNumberConstraint(num);
        } catch (ParseException e) {
            throw new ValidationException(e.getLocalizedMessage());
        }
    }

    if (constraint == null
            || value instanceof BigDecimal
            || value instanceof Float) {
        throw new IllegalArgumentException("DoubleMinValidator doesn't support following type: '" + value.getClass() + "'");
    }

    if (!constraint.isDoubleMin(min, inclusive)) {
        fireValidationException(value);
    }
}
 
Example 4
Source Project: cuba   File: DecimalMinValidator.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void accept(T value) throws ValidationException {
    // consider null value is valid
    if (value == null) {
        return;
    }

    NumberConstraint constraint = null;

    if (value instanceof Number) {
        constraint = getNumberConstraint((Number) value);
    } else if (value instanceof String) {
        try {
            Datatype datatype = datatypeRegistry.getNN(BigDecimal.class);
            Locale locale = userSessionSource.getUserSession().getLocale();
            BigDecimal bigDecimal = (BigDecimal) datatype.parse((String) value, locale);
            if (bigDecimal == null) {
                fireValidationException(value);
            }
            constraint = getNumberConstraint(bigDecimal);
        } catch (ParseException e) {
            throw new ValidationException(e.getLocalizedMessage());
        }
    }

    if (constraint == null
            || value instanceof Double
            || value instanceof Float) {
        throw new IllegalArgumentException("DecimalMinValidator doesn't support following type: '" + value.getClass() + "'");
    }

    if (!constraint.isDecimalMin(min, inclusive)) {
        fireValidationException(value);
    }
}
 
Example 5
Source Project: cuba   File: DecimalMaxValidator.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void accept(T value) throws ValidationException {
    // consider null value is valid
    if (value == null) {
        return;
    }

    NumberConstraint constraint = null;

    if (value instanceof Number) {
        constraint = getNumberConstraint((Number) value);
    } else if (value instanceof String) {
        try {
            Datatype datatype = Datatypes.getNN(BigDecimal.class);
            Locale locale = userSessionSource.getUserSession().getLocale();
            BigDecimal bigDecimal = (BigDecimal) datatype.parse((String) value, locale);
            if (bigDecimal == null) {
                fireValidationException(value);
            }
            constraint = getNumberConstraint(bigDecimal);
        } catch (ParseException e) {
            throw new ValidationException(e.getLocalizedMessage());
        }
    }

    if (constraint == null
            || value instanceof Double
            || value instanceof Float) {
        throw new IllegalArgumentException("DecimalMaxValidator doesn't support following type: '" + value.getClass() + "'");
    }

    if (!constraint.isDecimalMax(max, inclusive)) {
        fireValidationException(value);
    }
}
 
Example 6
Source Project: cuba   File: DoubleMaxValidator.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void accept(T value) throws ValidationException {
    // consider null value is valid
    if (value == null) {
        return;
    }

    NumberConstraint constraint = null;

    if (value instanceof Number) {
        constraint = getNumberConstraint((Number) value);
    } else if (value instanceof String) {
        try {
            Datatype datatype = datatypeRegistry.getNN(Double.class);
            Locale locale = userSessionSource.getUserSession().getLocale();
            Double num = (Double) datatype.parse((String) value, locale);
            if (num == null) {
                fireValidationException(value);
            }
            constraint = getNumberConstraint(num);
        } catch (ParseException e) {
            throw new ValidationException(e.getLocalizedMessage());
        }
    }

    if (constraint == null
            || value instanceof BigDecimal
            || value instanceof Float) {
        throw new IllegalArgumentException("DoubleMaxValidator doesn't support following type: '" + value.getClass() + "'");
    }

    if (!constraint.isDoubleMax(max, inclusive)) {
        fireValidationException(value);
    }
}
 
Example 7
Source Project: cuba   File: DigitsValidator.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void accept(T value) throws ValidationException {
    // consider null value is valid
    if (value == null) {
        return;
    }

    NumberConstraint constraint = null;

    if (value instanceof Number) {
        constraint = getNumberConstraint((Number) value);
    } else if (value instanceof String) {
        try {
            Datatype datatype = Datatypes.getNN(BigDecimal.class);
            Locale locale = userSessionSource.getUserSession().getLocale();
            BigDecimal bigDecimal = (BigDecimal) datatype.parse((String) value, locale);
            if (bigDecimal == null) {
                fireValidationException(value);
            }
            constraint = getNumberConstraint(bigDecimal);
        } catch (ParseException e) {
            throw new ValidationException(e.getLocalizedMessage());
        }
    }

    if (constraint == null
            || value instanceof Double
            || value instanceof Float) {
        throw new IllegalArgumentException("DigitsValidator doesn't support following type: '" + value.getClass() + "'");
    }

    if (!constraint.isDigits(integer, fraction)) {
        fireValidationException(value);
    }
}
 
Example 8
Source Project: che   File: ThreadPullLauncher.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void scheduleCron(Runnable runnable, String cron) {
  if (cron == null || cron.isEmpty()) {
    throw new ConfigurationException("Cron parameter can't be null");
  }
  try {
    CronExpression expression = new CronExpression(cron);
    service.schedule(runnable, expression);
    LOG.debug("Schedule method {} with cron  {} schedule", runnable, cron);
  } catch (ParseException e) {
    LOG.error(e.getLocalizedMessage(), e);
    throw new ConfigurationException(e.getLocalizedMessage());
  }
}
 
Example 9
@Override
public int compare(IValue v1, IValue v2) {
    try {
        Date d1 = dateFormat.parse(v1.toString());
        Date d2 = dateFormat.parse(v2.toString());
        return d1.compareTo(d2);
    } catch (ParseException ex) {
        throw new ChaseException("Unable to parse float value " + ex.getLocalizedMessage());
    }
}
 
Example 10
Source Project: sis   File: LocalizedParseExceptionTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests the {@link LocalizedParseException} constructor using a given resource key
 * and the text that we failed to parse.
 */
@Test
public void testResourceKeyForText() {
    final ParseException e = new LocalizedParseException(
            Locale.CANADA, Errors.Keys.NodeHasNoParent_1, new Object[] {"text"}, 5);
    String message = e.getLocalizedMessage();
    assertTrue(message, message.contains("Node “text” has no parent."));
}
 
Example 11
Source Project: birt   File: JSEditor.java    License: Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Validates the contents of this editor.
 */
public void doValidate( )
{
	Image image = null;
	String message = null;

	if ( scriptValidator == null )
	{
		return;
	}

	try
	{
		scriptValidator.validate( true, true );
		image = ReportPlatformUIImages.getImage( IReportGraphicConstants.ICON_SCRIPT_NOERROR );
		message = Messages.getString( "JSEditor.Validate.NoError" ); //$NON-NLS-1$
	}
	catch ( ParseException e )
	{
		image = ReportPlatformUIImages.getImage( IReportGraphicConstants.ICON_SCRIPT_ERROR );
		message = e.getLocalizedMessage( );
	}
	finally
	{
		setValidateIcon( image, message );
		setFocus( );
	}
}
 
Example 12
Source Project: XACML   File: IpAddressValidator.java    License: MIT License 5 votes vote down vote up
@Override
public void validate(Object value) throws InvalidValueException {
	if (value instanceof String) {
		try {
			IPAddress.newInstance((String) value);
		} catch (ParseException e) {
			throw new InvalidValueException(e.getLocalizedMessage());
		}
	} else
		throw new InvalidValueException("Unrecognized IP Address");
}
 
Example 13
Source Project: XACML   File: DateTimeValidator.java    License: MIT License 5 votes vote down vote up
@Override
public void validate(Object value) throws InvalidValueException {
	if (value instanceof String) {
		try {
			ISO8601DateTime.fromISO8601DateTimeString((String) value);
		} catch (ParseException e) {
			throw new InvalidValueException(e.getLocalizedMessage());
		}
	} else
		throw new InvalidValueException("Unrecognized DateTime");
}
 
Example 14
Source Project: XACML   File: RFC822NameValidator.java    License: MIT License 5 votes vote down vote up
@Override
public void validate(Object value) throws InvalidValueException {
	if (value instanceof String) {
		try {
			RFC822Name.newInstance((String) value);
		} catch (ParseException e) {
			throw new InvalidValueException(e.getLocalizedMessage());
		}
	} else
		throw new InvalidValueException("Unrecognized RFC822 Name");
}
 
Example 15
Source Project: XACML   File: TimeValidator.java    License: MIT License 5 votes vote down vote up
@Override
public void validate(Object value) throws InvalidValueException {
	if (value instanceof String) {
		try {
			ISO8601Time.fromISO8601TimeString((String) value);
		} catch (ParseException e) {
			throw new InvalidValueException(e.getLocalizedMessage());
		}
	} else
		throw new InvalidValueException("Unrecognized Time");
}
 
Example 16
Source Project: XACML   File: DNSNameValidator.java    License: MIT License 5 votes vote down vote up
@Override
public void validate(Object value) throws InvalidValueException {
	if (value instanceof String) {
		try {
			RFC2396DomainName.newInstance((String) value);
		} catch (ParseException e) {
			throw new InvalidValueException(e.getLocalizedMessage());
		}
	} else
		throw new InvalidValueException("Unrecognized DNS Name");
}
 
Example 17
Source Project: XACML   File: YearMonthDurationValidator.java    License: MIT License 5 votes vote down vote up
@Override
public void validate(Object value) throws InvalidValueException {
	if (value instanceof String) {
		try {
			XPathYearMonthDuration.newInstance((String) value);
		} catch (ParseException e) {
			throw new InvalidValueException(e.getLocalizedMessage());
		}
	} else
		throw new InvalidValueException("Unrecognized YearMonthDuration");
}
 
Example 18
Source Project: XACML   File: DayTimeDurationValidator.java    License: MIT License 5 votes vote down vote up
@Override
public void validate(Object value) throws InvalidValueException {
	if (value instanceof String) {
		try {
			XPathDayTimeDuration.newInstance((String) value);
		} catch (ParseException e) {
			throw new InvalidValueException(e.getLocalizedMessage());
		}
	} else
		throw new InvalidValueException("Unrecognized DayTimeDuration");
}
 
Example 19
Source Project: XACML   File: DateValidator.java    License: MIT License 5 votes vote down vote up
@Override
public void validate(Object value) throws InvalidValueException {
	if (value instanceof String) {
		try {
			ISO8601Date.fromISO8601DateString((String) value);
		} catch (ParseException e) {
			throw new InvalidValueException(e.getLocalizedMessage());
		}
	} else
		throw new InvalidValueException("Unrecognized Date");
}
 
Example 20
Source Project: birt   File: ScriptValidator.java    License: Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Validates the current script, and selects the error if the specified falg
 * is <code>true</code>.
 * 
 * @param isFunctionBody
 *            <code>true</code> if a function body is validated,
 *            <code>false</code> otherwise.
 * @param isErrorSelected
 *            <code>true</code> if error will be selected after
 *            validating, <code>false</code> otherwise.
 * @throws ParseException
 *             if an syntax error is found.
 */
public void validate( boolean isFunctionBody, boolean isErrorSelected )
		throws ParseException
{
	if ( scriptViewer == null )
	{
		return;
	}

	clearAnnotations( );

	StyledText textField = scriptViewer.getTextWidget( );

	if ( textField == null || !textField.isEnabled( ) )
	{
		return;
	}

	String functionTag = "function(){"; //$NON-NLS-1$
	IDocument document = scriptViewer.getDocument( );
	String text = document == null ? null : scriptViewer.getDocument( )
			.get( );

	String script = text;

	if ( isFunctionBody )
	{
		script = functionTag + script + "\n}"; //$NON-NLS-1$
	}

	try
	{
		validateScript( script );
	}
	catch ( ParseException e )
	{
		int offset = e.getErrorOffset( );

		if ( isFunctionBody )
		{
			offset -= functionTag.length( );
			while ( offset >= text.length( ) )
			{
				offset--;
			}
		}

		String errorMessage = e.getLocalizedMessage( );
		Position position = getErrorPosition( text, offset );

		if ( position != null )
		{
			IAnnotationModel annotationModel = scriptViewer.getAnnotationModel( );

			if ( annotationModel != null )
			{
				annotationModel.addAnnotation( new Annotation( IReportGraphicConstants.ANNOTATION_ERROR,
						true,
						errorMessage ),
						position );
			}
			if ( isErrorSelected )
			{
				if ( scriptViewer instanceof SourceViewer )
				{
					( (SourceViewer) scriptViewer ).setSelection( new TextSelection( position.getOffset( ),
							position.getLength( ) ) );
				}
				scriptViewer.revealRange( position.getOffset( ),
						position.getLength( ) );
			}
		}
		throw new ParseException( e.getLocalizedMessage( ), position.offset );
	}
}