private Long safelyParse()

in src/main/java/org/apache/sling/xss/impl/LongValidationRule.java [53:106]


    private Long safelyParse(String context, String input) throws ValidationException {
        // do not allow empty Strings such as "   " - so trim to ensure
        // isEmpty catches "    "
        if (input != null) input = input.trim();

        if (StringUtilities.isEmpty(input)) {
            if (allowNull) {
                return null;
            }
            throw new ValidationException(
                    context + ": Input number required",
                    "Input number required: context=" + context + ", input=" + input,
                    context);
        }

        // canonicalize
        String canonical = encoder.canonicalize(input);

        if (minValue > maxValue) {
            throw new ValidationException(
                    context + ": Invalid number input: context",
                    "Validation parameter error for number: maxValue ( " + maxValue
                            + ") must be greater than minValue ( " + minValue + ") for " + context,
                    context);
        }

        // validate min and max
        try {
            long i = Long.parseLong(canonical);
            if (i < minValue) {
                throw new ValidationException(
                        "Invalid number input must be between " + minValue + " and " + maxValue + ": context="
                                + context,
                        "Invalid number input must be between " + minValue + " and " + maxValue + ": context=" + context
                                + ", input=" + input,
                        context);
            }
            if (i > maxValue) {
                throw new ValidationException(
                        "Invalid number input must be between " + minValue + " and " + maxValue + ": context="
                                + context,
                        "Invalid number input must be between " + minValue + " and " + maxValue + ": context=" + context
                                + ", input=" + input,
                        context);
            }
            return i;
        } catch (NumberFormatException e) {
            throw new ValidationException(
                    context + ": Invalid number input",
                    "Invalid number input format: context=" + context + ", input=" + input,
                    e,
                    context);
        }
    }