Search in sources :

Example 1 with StringValueConversionException

use of org.apache.wicket.util.string.StringValueConversionException in project wicket by apache.

the class WicketLinkTagHandler method onComponentTag.

@Override
protected final MarkupElement onComponentTag(ComponentTag tag) throws ParseException {
    // parser through Application.newMarkupParser().
    if ((autolinking == true) && (analyzeAutolinkCondition(tag) == true)) {
        // Mark it as autolink enabled
        tag.enableAutolink(true);
        // Just a dummy name. The ComponentTag will not be forwarded.
        tag.setId(AUTOLINK_ID + getRequestUniqueId());
        tag.setAutoComponentTag(true);
        tag.setModified(true);
        return tag;
    }
    // current autolink status.
    if (tag instanceof WicketTag) {
        final WicketTag wtag = (WicketTag) tag;
        if (wtag.isLinkTag()) {
            // Beginning of the region
            if (tag.isOpen() || tag.isOpenClose()) {
                if (tag.isOpen()) {
                    if (autolinkStatus == null) {
                        autolinkStatus = new ArrayListStack<>();
                    }
                    // remember the current setting to be reset after the
                    // region
                    autolinkStatus.push(autolinking);
                }
                // html allows to represent true in different ways
                final String autolink = tag.getAttributes().getString("autolink");
                try {
                    autolinking = Strings.isEmpty(autolink) || Strings.isTrue(autolink);
                } catch (StringValueConversionException e) {
                    throw new WicketRuntimeException("Invalid autolink attribute value \"" + autolink + "\"");
                }
            } else if (tag.isClose()) {
                // restore the autolink setting from before the region
                autolinking = autolinkStatus.pop();
            }
            return wtag;
        }
    }
    return tag;
}
Also used : WicketTag(org.apache.wicket.markup.WicketTag) StringValueConversionException(org.apache.wicket.util.string.StringValueConversionException) WicketRuntimeException(org.apache.wicket.WicketRuntimeException)

Example 2 with StringValueConversionException

use of org.apache.wicket.util.string.StringValueConversionException in project wicket by apache.

the class Duration method valueOf.

/**
 * Converts the given <code>String</code> to a new <code>Duration</code> object. The string can
 * take the form of a floating point number followed by a number of milliseconds, seconds,
 * minutes, hours or days. For example "6 hours" or "3.4 days". Parsing is case-insensitive.
 *
 * @param string
 *            a <code>String</code> to parse
 * @param locale
 *            the <code>Locale</code> used for parsing
 * @return the <code>Duration</code> value of the given <code>String</code>
 * @throws StringValueConversionException
 */
public static Duration valueOf(final String string, final Locale locale) throws StringValueConversionException {
    final Matcher matcher = pattern.matcher(string);
    if (matcher.matches()) {
        final double value = StringValue.valueOf(matcher.group(1), locale).toDouble();
        final String units = matcher.group(3);
        if (units.equalsIgnoreCase("millisecond")) {
            return milliseconds(value);
        } else if (units.equalsIgnoreCase("second")) {
            return seconds(value);
        } else if (units.equalsIgnoreCase("minute")) {
            return minutes(value);
        } else if (units.equalsIgnoreCase("hour")) {
            return hours(value);
        } else if (units.equalsIgnoreCase("day")) {
            return days(value);
        } else {
            throw new StringValueConversionException("Unrecognized units: " + string);
        }
    } else {
        throw new StringValueConversionException("Unable to parse duration: " + string);
    }
}
Also used : Matcher(java.util.regex.Matcher) StringValueConversionException(org.apache.wicket.util.string.StringValueConversionException)

Example 3 with StringValueConversionException

use of org.apache.wicket.util.string.StringValueConversionException in project wicket by apache.

the class Bytes method valueOf.

/**
 * Converts a string to a number of bytes. Strings consist of a floating point value followed by
 * K, M, G or T for kilobytes, megabytes, gigabytes or terabytes, respectively. The
 * abbreviations KB, MB, GB and TB are also accepted. Matching is case insensitive.
 *
 * @param string
 *            The string to convert
 * @param locale
 *            The Locale to be used for transformation
 * @return The Bytes value for the string
 * @throws StringValueConversionException
 */
public static Bytes valueOf(final String string, final Locale locale) throws StringValueConversionException {
    final Matcher matcher = valuePattern.matcher(string);
    // Valid input?
    if (matcher.matches()) {
        try {
            // Get double precision value
            final double value = NumberFormat.getNumberInstance(locale).parse(matcher.group(1)).doubleValue();
            // Get units specified
            final String units = matcher.group(3);
            if (units.equalsIgnoreCase("")) {
                return bytes(value);
            } else if (units.equalsIgnoreCase("K")) {
                return kilobytes(value);
            } else if (units.equalsIgnoreCase("M")) {
                return megabytes(value);
            } else if (units.equalsIgnoreCase("G")) {
                return gigabytes(value);
            } else if (units.equalsIgnoreCase("T")) {
                return terabytes(value);
            } else {
                throw new StringValueConversionException("Units not recognized: " + string);
            }
        } catch (ParseException e) {
            throw new StringValueConversionException("Unable to parse numeric part: " + string, e);
        }
    } else {
        throw new StringValueConversionException("Unable to parse bytes: " + string);
    }
}
Also used : Matcher(java.util.regex.Matcher) StringValueConversionException(org.apache.wicket.util.string.StringValueConversionException) ParseException(java.text.ParseException)

Aggregations

StringValueConversionException (org.apache.wicket.util.string.StringValueConversionException)3 Matcher (java.util.regex.Matcher)2 ParseException (java.text.ParseException)1 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)1 WicketTag (org.apache.wicket.markup.WicketTag)1