MozFlex extends AbstractCssProperty<MozFlex> implements StateChangeInformer<CssProperty> { @Override public MozFlex setCssValue(final String cssValue) { final String trimmedCssValue; if (cssValue == null) { throw new NullValueException( "null is an invalid value. The value format should be as for example \"0 1 auto\" or initial/inherit."); } else if ((trimmedCssValue = TagStringUtil .toLowerCase(StringUtil.strip(cssValue))).isEmpty()) { throw new NullValueException(cssValue + " is an invalid value. The value format should be as for example \"0 1 auto\" or initial/inherit."); } final String[] cssValueParts = StringUtil.splitBySpace(trimmedCssValue); if (cssValueParts.length > 1) { if (trimmedCssValue.contains(MozFlexBasis.INITIAL)) { throw new InvalidValueException( "The string 'initial' makes the given cssValue invalid, please remove 'initial' from it and try."); } if (trimmedCssValue.contains(MozFlexBasis.INHERIT)) { throw new InvalidValueException( "The string 'inherit' makes the given cssValue invalid, please remove 'inherit' from it and try."); } } else { if (PREDEFINED_CONSTANTS.contains(trimmedCssValue)) { if (mozFlexGrow != null) { mozFlexGrow.setAlreadyInUse(false); mozFlexGrow = null; } if (mozFlexShrink != null) { mozFlexShrink.setAlreadyInUse(false); mozFlexShrink = null; } if (mozFlexBasis != null) { mozFlexBasis.setAlreadyInUse(false); mozFlexBasis = null; } this.cssValue = trimmedCssValue; if (getStateChangeInformer() != null) { getStateChangeInformer().stateChanged(this); } return this; } } MozFlexGrow mozFlexGrow = null; MozFlexShrink mozFlexShrink = null; MozFlexBasis mozFlexBasis = null; for (final String eachPart : cssValueParts) { if (mozFlexGrow == null && MozFlexGrow.isValid(eachPart)) { if (this.mozFlexGrow == null) { mozFlexGrow = new MozFlexGrow(eachPart); mozFlexGrow.setStateChangeInformer(this); mozFlexGrow.setAlreadyInUse(true); } else { this.mozFlexGrow.setCssValue(eachPart); mozFlexGrow = this.mozFlexGrow; } } else if (mozFlexShrink == null && MozFlexShrink.isValid(eachPart)) { if (this.mozFlexGrow == null) { mozFlexShrink = new MozFlexShrink(eachPart); mozFlexShrink.setStateChangeInformer(this); mozFlexShrink.setAlreadyInUse(true); } else { this.mozFlexShrink.setCssValue(eachPart); mozFlexShrink = this.mozFlexShrink; } } else if (mozFlexBasis == null && MozFlexBasis.isValid(eachPart)) { if (this.mozFlexBasis == null) { mozFlexBasis = new MozFlexBasis(eachPart); mozFlexBasis.setStateChangeInformer(this); mozFlexBasis.setAlreadyInUse(true); } else { this.mozFlexBasis.setCssValue(eachPart); mozFlexBasis = this.mozFlexBasis; } } } final StringBuilder cssValueBuilder = new StringBuilder(); boolean invalid = true; if (mozFlexGrow == null) { if (this.mozFlexGrow == null) { mozFlexGrow = new MozFlexGrow(1); mozFlexGrow.setStateChangeInformer(this); mozFlexGrow.setAlreadyInUse(true); } else { this.mozFlexGrow.setValue(0); mozFlexGrow = this.mozFlexGrow; } } else { invalid = false; } cssValueBuilder.append(mozFlexGrow.getCssValue()).append(' '); if (mozFlexShrink == null) { if (this.mozFlexShrink == null) { mozFlexShrink = new MozFlexShrink(1); mozFlexShrink.setStateChangeInformer(this); mozFlexShrink.setAlreadyInUse(true); } else { this.mozFlexShrink.setValue(1); mozFlexShrink = this.mozFlexShrink; } } else { invalid = false; } cssValueBuilder.append(mozFlexShrink.getCssValue()).append(' '); if (mozFlexBasis == null) { if (cssValueParts.length == 3) { throw new InvalidValueException( "the given cssValue contains invalid flex-basis value."); } if (this.mozFlexBasis == null) { mozFlexBasis = new MozFlexBasis(0); mozFlexBasis.setStateChangeInformer(this); mozFlexBasis.setAlreadyInUse(true); } else { this.mozFlexBasis.setAsAuto(); mozFlexBasis = this.mozFlexBasis; } } else { invalid = false; } cssValueBuilder.append(mozFlexBasis.getCssValue()); if (invalid) { throw new InvalidValueException(cssValue + " is an invalid value. The value format should be as for example \"0 1 auto\" or initial/inherit."); } this.cssValue = cssValueBuilder.toString(); mozFlexGrow.setStateChangeInformer(this); mozFlexGrow.setAlreadyInUse(true); mozFlexShrink.setStateChangeInformer(this); mozFlexShrink.setAlreadyInUse(true); mozFlexBasis.setStateChangeInformer(this); mozFlexBasis.setAlreadyInUse(true); this.mozFlexGrow = mozFlexGrow; this.mozFlexShrink = mozFlexShrink; this.mozFlexBasis = mozFlexBasis; if (getStateChangeInformer() != null) { getStateChangeInformer().stateChanged(this); } return this; } MozFlex();  MozFlex(final String cssValue);  MozFlex(final MozFlex mozFlex); @Override String getCssName(); @Override String getCssValue(); @Override String toString(); @Override MozFlex setCssValue(final String cssValue); static boolean isValid(final String cssValue); void setAsInitial(); void setAsInherit(); void setAsAuto(); MozFlexBasis getMozFlexBasis(); MozFlexShrink getMozFlexShrink(); MozFlexGrow getMozFlexGrow(); MozFlex setMozFlexGrow(final MozFlexGrow mozFlexGrow); MozFlex setMozFlexShrink(final MozFlexShrink mozFlexShrink); MozFlex setMozFlexBasis(final MozFlexBasis mozFlexBasis); @Override void stateChanged(final CssProperty stateChangedObject); static final String INITIAL; static final String INHERIT; static final String AUTO; }
@Test(expected = InvalidValueException.class) public void testSetCssValueStringInvalid() { final MozFlex flex = new MozFlex(); try { flex.setCssValue("2 5 25"); } catch (InvalidValueException e) { try { flex.setCssValue("fddf"); } catch (InvalidValueException e1) { throw e1; } } }