Пример #1
0
  /**
   * @param cssValue
   * @return true if the given {@code cssValue} is valid.
   * @since 1.0.0
   * @author WFF
   */
  public static boolean isValid(final String cssValue) {

    if (cssValue == null) {
      return false;
    }

    final String trimmedCssValue = cssValue.trim();

    if (trimmedCssValue.isEmpty()) {
      return false;
    }

    final String trimmedCssValueLowerCase = TagStringUtil.toLowerCase(trimmedCssValue);

    if (INITIAL.equals(trimmedCssValueLowerCase)
        || INHERIT.equals(trimmedCssValueLowerCase)
        || TRANSPARENT.equals(trimmedCssValueLowerCase)) {
      return true;
    }

    if (CssColorName.isValid(trimmedCssValue)) {
      return true;
    }

    if (RgbCssValue.isValid(trimmedCssValue)) {
      return true;
    }
    if (RgbaCssValue.isValid(trimmedCssValue)) {
      return true;
    }
    if (HslCssValue.isValid(trimmedCssValue)) {
      return true;
    }
    if (HslaCssValue.isValid(trimmedCssValue)) {
      return true;
    }

    try {

      if (!trimmedCssValue.startsWith("#")) {
        return false;
      }

      final long value = Long.parseLong(cssValue.replaceFirst("[#]", ""), 16);

      return !(value > CommonConstants.FFFFFF_HEX_VALUE || value < 0);
    } catch (final NumberFormatException ex) {
    }

    return false;
  }
Пример #2
0
  /**
   * @param cssValue the value should be a color/color code/rgb, for example <code>#0000ff</code>.
   *     {@code null} is considered as an invalid value and it will throw {@code
   *     NullValueException}.And an empty string is also considered as an invalid value and it will
   *     throw {@code InvalidValueException}.
   * @since 1.0.0
   * @author WFF
   */
  @Override
  public BorderTopColor setCssValue(final String cssValue) {
    final String previousCssValue = this.cssValue;
    try {
      if (cssValue == null) {
        throw new NullValueException(
            "null is an invalid value. The value should be any color for example #0000ff, rgb(15, 25, 155) Or, initial/inherit/transparent.");
      } else if (!isValid(cssValue)) {
        throw new InvalidValueException(
            cssValue
                + " is an invalid value. The value should be any color for example #0000ff, rgb(15, 25, 155) Or, initial/inherit/transparent.");
      } else {
        this.cssValue = cssValue.trim();
        if (RgbCssValue.isValid(cssValue)) {
          if (rgbCssValue == null) {
            rgbCssValue = new RgbCssValue(cssValue);
          } else {
            rgbCssValue.setRgb(this.cssValue);
          }
          rgbCssValue.setStateChangeInformer(this);
        } else {
          makeRgbCssValueNull();
        }

        if (RgbaCssValue.isValid(cssValue)) {
          if (rgbaCssValue == null) {
            rgbaCssValue = new RgbaCssValue(cssValue);
          } else {
            rgbaCssValue.setRgba(this.cssValue);
          }
          rgbaCssValue.setStateChangeInformer(this);
        } else {
          makeRgbaCssValueNull();
        }

        if (HslCssValue.isValid(cssValue)) {
          if (hslCssValue == null) {
            hslCssValue = new HslCssValue(cssValue);
          } else {
            hslCssValue.setHsl(this.cssValue);
          }
          hslCssValue.setStateChangeInformer(this);
        } else {
          makeHslCssValueNull();
        }

        if (HslaCssValue.isValid(cssValue)) {
          if (hslaCssValue == null) {
            hslaCssValue = new HslaCssValue(cssValue);
          } else {
            hslaCssValue.setHsla(this.cssValue);
          }
          hslaCssValue.setStateChangeInformer(this);
        } else {
          makeHslaCssValueNull();
        }

        if (getStateChangeInformer() != null) {
          getStateChangeInformer().stateChanged(this);
        }
      }
    } catch (final InvalidValueException e) {
      this.cssValue = previousCssValue;
      throw e;
    }
    return this;
  }