下面列出了java.text.ParseException#getLocalizedMessage ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
/**
* 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"));
}
@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);
}
}
@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);
}
}
@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);
}
}
@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);
}
}
@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);
}
}
@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());
}
}
@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());
}
}
/**
* 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."));
}
/**
* 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( );
}
}
@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");
}
@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");
}
@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");
}
@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");
}
@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");
}
@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");
}
@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");
}
@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");
}
/**
* 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 );
}
}