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

The following examples show how to use java.text.ParseException#getLocalizedMessage() . 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 File: LocalizedParseExceptionTest.java    From sis with 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 2
Source File: AbstractParser.java    From sis with 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 3
Source File: IpAddressValidator.java    From XACML with 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 4
Source File: DateValidator.java    From XACML with 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 5
Source File: DayTimeDurationValidator.java    From XACML with 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 6
Source File: YearMonthDurationValidator.java    From XACML with 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 7
Source File: DNSNameValidator.java    From XACML with 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 8
Source File: TimeValidator.java    From XACML with 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 9
Source File: RFC822NameValidator.java    From XACML with 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 10
Source File: DateTimeValidator.java    From XACML with 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 11
Source File: DoubleMinValidator.java    From cuba with 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 12
Source File: JSEditor.java    From birt with 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 13
Source File: LocalizedParseExceptionTest.java    From sis with 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 14
Source File: DateComparator.java    From Llunatic with GNU General Public License v3.0 5 votes vote down vote up
@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 15
Source File: ThreadPullLauncher.java    From che with 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 16
Source File: DigitsValidator.java    From cuba with 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 17
Source File: DoubleMaxValidator.java    From cuba with 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 18
Source File: DecimalMaxValidator.java    From cuba with 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 19
Source File: DecimalMinValidator.java    From cuba with 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 20
Source File: ScriptValidator.java    From birt with 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 );
	}
}