diff options
39 files changed, 534 insertions, 288 deletions
diff --git a/client-compiler/src/com/vaadin/sass/linker/SassLinker.java b/client-compiler/src/com/vaadin/sass/linker/SassLinker.java index 05d9e311a6..3183f4bfe7 100644 --- a/client-compiler/src/com/vaadin/sass/linker/SassLinker.java +++ b/client-compiler/src/com/vaadin/sass/linker/SassLinker.java @@ -132,7 +132,7 @@ public class SassLinker extends AbstractLinker { if (!fileInfo.isMixin()) { scss.compile(); InputStream is = new ByteArrayInputStream(scss - .toString().getBytes()); + .printState().getBytes()); toReturn.add(this.emitInputStream(logger, is, fileInfo.getOriginalCssPath())); diff --git a/server/src/com/vaadin/server/VaadinServlet.java b/server/src/com/vaadin/server/VaadinServlet.java index baf97d23d9..3da264e0e7 100644 --- a/server/src/com/vaadin/server/VaadinServlet.java +++ b/server/src/com/vaadin/server/VaadinServlet.java @@ -852,7 +852,7 @@ public class VaadinServlet extends HttpServlet implements Constants { // cache it response.setHeader("Cache-Control", "no-cache"); final String mimetype = getService().getMimeType(filename); - writeResponse(response, mimetype, scss.toString()); + writeResponse(response, mimetype, scss.printState()); return true; } diff --git a/theme-compiler/src/com/vaadin/buildhelpers/CompileTheme.java b/theme-compiler/src/com/vaadin/buildhelpers/CompileTheme.java index dece1691f0..11e3b91800 100644 --- a/theme-compiler/src/com/vaadin/buildhelpers/CompileTheme.java +++ b/theme-compiler/src/com/vaadin/buildhelpers/CompileTheme.java @@ -103,11 +103,11 @@ public class CompileTheme { scss.compile(); BufferedWriter out = new BufferedWriter(new FileWriter(stylesCssName)); out.write(cssHeader.toString()); - out.write(scss.toString().replace("@version@", version)); + out.write(scss.printState().replace("@version@", version)); out.close(); System.out.println("Compiled CSS to " + stylesCssName + " (" - + scss.toString().length() + " bytes)"); + + scss.printState().length() + " bytes)"); createSprites(themeFolder, themeName); File oldCss = new File(stylesCssName); diff --git a/theme-compiler/src/com/vaadin/sass/SassCompiler.java b/theme-compiler/src/com/vaadin/sass/SassCompiler.java index 6a83425ca1..b554ce2b01 100644 --- a/theme-compiler/src/com/vaadin/sass/SassCompiler.java +++ b/theme-compiler/src/com/vaadin/sass/SassCompiler.java @@ -56,9 +56,9 @@ public class SassCompiler { scss.compile(); if (output == null) { - System.out.println(scss.toString()); + System.out.println(scss.printState()); } else { - writeFile(output, scss.toString()); + writeFile(output, scss.printState()); } } diff --git a/theme-compiler/src/com/vaadin/sass/internal/ScssStylesheet.java b/theme-compiler/src/com/vaadin/sass/internal/ScssStylesheet.java index 63f59cdfab..42325fde29 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/ScssStylesheet.java +++ b/theme-compiler/src/com/vaadin/sass/internal/ScssStylesheet.java @@ -81,7 +81,7 @@ public class ScssStylesheet extends Node { /** * Main entry point for the SASS compiler. Takes in a file and builds up a * ScssStylesheet tree out of it. Calling compile() on it will transform - * SASS into CSS. Calling toString() will print out the SCSS/CSS. + * SASS into CSS. Calling printState() will print out the SCSS/CSS. * * @param identifier * The file path. If null then null is returned. @@ -97,8 +97,8 @@ public class ScssStylesheet extends Node { /** * Main entry point for the SASS compiler. Takes in a file and an optional * parent style sheet, then builds up a ScssStylesheet tree out of it. - * Calling compile() on it will transform SASS into CSS. Calling toString() - * will print out the SCSS/CSS. + * Calling compile() on it will transform SASS into CSS. Calling + * printState() will print out the SCSS/CSS. * * @param identifier * The file path. If null then null is returned. @@ -119,7 +119,7 @@ public class ScssStylesheet extends Node { * Main entry point for the SASS compiler. Takes in a file, an optional * parent stylesheet, and document and error handlers. Then builds up a * ScssStylesheet tree out of it. Calling compile() on it will transform - * SASS into CSS. Calling toString() will print out the SCSS/CSS. + * SASS into CSS. Calling printState() will print out the SCSS/CSS. * * @param identifier * The file path. If null then null is returned. @@ -279,27 +279,13 @@ public class ScssStylesheet extends Node { * types will implement themselves. */ @Override + public String printState() { + return buildString(PRINT_STRATEGY); + } + + @Override public String toString() { - StringBuilder string = new StringBuilder(""); - String delimeter = "\n\n"; - // add charset declaration, if it is not default "ASCII". - if (!"ASCII".equals(getCharset())) { - string.append("@charset \"").append(getCharset()).append("\";") - .append(delimeter); - } - if (children.size() > 0) { - string.append(children.get(0).toString()); - } - if (children.size() > 1) { - for (int i = 1; i < children.size(); i++) { - String childString = children.get(i).toString(); - if (childString != null) { - string.append(delimeter).append(childString); - } - } - } - String output = string.toString(); - return output; + return "Stylesheet node [" + buildString(TO_STRING_STRATEGY) + "]"; } public void addChild(int index, VariableNode node) { @@ -460,6 +446,29 @@ public class ScssStylesheet extends Node { this.charset = charset; } + private String buildString(BuildStringStrategy strategy) { + StringBuilder string = new StringBuilder(""); + String delimeter = "\n\n"; + // add charset declaration, if it is not default "ASCII". + if (!"ASCII".equals(getCharset())) { + string.append("@charset \"").append(getCharset()).append("\";") + .append(delimeter); + } + if (children.size() > 0) { + string.append(strategy.build(children.get(0))); + } + if (children.size() > 1) { + for (int i = 1; i < children.size(); i++) { + String childString = strategy.build(children.get(i)); + if (childString != null) { + string.append(delimeter).append(childString); + } + } + } + String output = string.toString(); + return output; + } + static { String logFile = System.getProperty("java.util.logging.config.file"); if (logFile == null) { @@ -474,4 +483,5 @@ public class ScssStylesheet extends Node { } } } + } diff --git a/theme-compiler/src/com/vaadin/sass/internal/parser/LexicalUnitImpl.java b/theme-compiler/src/com/vaadin/sass/internal/parser/LexicalUnitImpl.java index 09fcdee7c0..97314c6e8c 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/parser/LexicalUnitImpl.java +++ b/theme-compiler/src/com/vaadin/sass/internal/parser/LexicalUnitImpl.java @@ -40,6 +40,8 @@ import com.vaadin.sass.internal.parser.function.FloorFunctionGenerator; import com.vaadin.sass.internal.parser.function.LightenFunctionGenerator; import com.vaadin.sass.internal.parser.function.RoundFunctionGenerator; import com.vaadin.sass.internal.parser.function.SCSSFunctionGenerator; +import com.vaadin.sass.internal.tree.Node; +import com.vaadin.sass.internal.tree.Node.BuildStringStrategy; import com.vaadin.sass.internal.util.DeepCopy; /** @@ -253,127 +255,33 @@ public class LexicalUnitImpl implements LexicalUnit, SCSSLexicalUnit, return params; } + /** + * Prints out the current state of the node tree. Will return SCSS before + * compile and CSS after. + * + * Result value could be null. + * + * @since 7.2 + * @return State as a string + */ + public String printState() { + return buildString(Node.PRINT_STRATEGY); + } + @Override public String toString() { - short type = getLexicalUnitType(); - String text = null; - switch (type) { - case SCSS_VARIABLE: - text = "$" + s; - break; - case SCSS_NULL: - text = ""; - break; - case LexicalUnit.SAC_OPERATOR_COMMA: - text = ","; - break; - case LexicalUnit.SAC_OPERATOR_PLUS: - text = "+"; - break; - case LexicalUnit.SAC_OPERATOR_MINUS: - text = "-"; - break; - case LexicalUnit.SAC_OPERATOR_MULTIPLY: - text = "*"; - break; - case LexicalUnit.SAC_OPERATOR_SLASH: - text = "/"; - break; - case LexicalUnit.SAC_OPERATOR_MOD: - text = "%"; - break; - case LexicalUnit.SAC_OPERATOR_EXP: - text = "^"; - break; - case LexicalUnit.SAC_OPERATOR_LT: - text = "<"; - break; - case LexicalUnit.SAC_OPERATOR_GT: - text = ">"; - break; - case LexicalUnit.SAC_OPERATOR_LE: - text = "<="; - break; - case LexicalUnit.SAC_OPERATOR_GE: - text = "=>"; - break; - case LexicalUnit.SAC_OPERATOR_TILDE: - text = "~"; - break; - case LexicalUnit.SAC_INHERIT: - text = "inherit"; - break; - case LexicalUnit.SAC_INTEGER: - text = Integer.toString(getIntegerValue(), 10); - break; - case LexicalUnit.SAC_REAL: - text = getFloatOrInteger(); - break; - case LexicalUnit.SAC_EM: - case SCSSLexicalUnit.SAC_LEM: - case SCSSLexicalUnit.SAC_REM: - case LexicalUnit.SAC_EX: - case LexicalUnit.SAC_PIXEL: - case LexicalUnit.SAC_INCH: - case LexicalUnit.SAC_CENTIMETER: - case LexicalUnit.SAC_MILLIMETER: - case LexicalUnit.SAC_POINT: - case LexicalUnit.SAC_PICA: - case LexicalUnit.SAC_PERCENTAGE: - case LexicalUnit.SAC_DEGREE: - case LexicalUnit.SAC_GRADIAN: - case LexicalUnit.SAC_RADIAN: - case LexicalUnit.SAC_MILLISECOND: - case LexicalUnit.SAC_SECOND: - case LexicalUnit.SAC_HERTZ: - case LexicalUnit.SAC_KILOHERTZ: - case LexicalUnit.SAC_DIMENSION: - text = getFloatOrInteger() + getDimensionUnitText(); - break; - case LexicalUnit.SAC_URI: - text = "url(" + getStringValue() + ")"; - break; - case LexicalUnit.SAC_RGBCOLOR: - case LexicalUnit.SAC_COUNTER_FUNCTION: - case LexicalUnit.SAC_COUNTERS_FUNCTION: - case LexicalUnit.SAC_RECT_FUNCTION: - case LexicalUnit.SAC_FUNCTION: - String funcName = getFunctionName(); - text = serializeFunction(funcName); - break; - case LexicalUnit.SAC_IDENT: - text = getStringValue(); - break; - case LexicalUnit.SAC_STRING_VALUE: - // @@SEEME. not exact - text = "\"" + getStringValue() + "\""; - break; - case LexicalUnit.SAC_ATTR: - text = "attr(" + getStringValue() + ")"; - break; - case LexicalUnit.SAC_UNICODERANGE: - text = "@@TODO"; - break; - case LexicalUnit.SAC_SUB_EXPRESSION: - text = getSubValues().toString(); - break; - default: - text = "@unknown"; - break; - } - if (getNextLexicalUnit() != null) { - if (getNextLexicalUnit().getLexicalUnitType() == SAC_OPERATOR_COMMA) { - return text + getNextLexicalUnit(); - } - return text + ' ' + getNextLexicalUnit(); + String result = simpleAsString(); + if (result == null) { + return "Lexical unit node [" + buildString(Node.TO_STRING_STRATEGY) + + "]"; } else { - return text; + return result; } } // A helper method for sass interpolation public String unquotedString() { - String result = toString(); + String result = printState(); if (result.length() >= 2 && ((result.charAt(0) == '"' && result .charAt(result.length() - 1) == '"') || (result @@ -389,7 +297,7 @@ public class LexicalUnitImpl implements LexicalUnit, SCSSLexicalUnit, if (denominator.getLexicalUnitType() != SAC_INTEGER && denominator.getLexicalUnitType() != SAC_REAL && getLexicalUnitType() != denominator.getLexicalUnitType()) { - throw new IncompatibleUnitsException(toString()); + throw new IncompatibleUnitsException(printState()); } setFloatValue(getFloatValue() / denominator.getFloatValue()); if (getLexicalUnitType() == denominator.getLexicalUnitType()) { @@ -426,7 +334,7 @@ public class LexicalUnitImpl implements LexicalUnit, SCSSLexicalUnit, && another.getLexicalUnitType() != SAC_INTEGER && another.getLexicalUnitType() != SAC_REAL && getLexicalUnitType() != another.getLexicalUnitType()) { - throw new IncompatibleUnitsException(toString()); + throw new IncompatibleUnitsException(printState()); } if (another.getLexicalUnitType() != SAC_INTEGER && another.getLexicalUnitType() != SAC_REAL) { @@ -438,7 +346,7 @@ public class LexicalUnitImpl implements LexicalUnit, SCSSLexicalUnit, @Override public LexicalUnitImpl modulo(LexicalUnitImpl another) { if (getLexicalUnitType() != another.getLexicalUnitType()) { - throw new IncompatibleUnitsException(toString()); + throw new IncompatibleUnitsException(printState()); } setIntegerValue(getIntegerValue() % another.getIntegerValue()); setNextLexicalUnit(another.getNextLexicalUnit()); @@ -748,6 +656,15 @@ public class LexicalUnitImpl implements LexicalUnit, SCSSLexicalUnit, } } + public String getValueAsString() { + Object value = getValue(); + if (value == null) { + return null; + } else { + return value.toString(); + } + } + public void setFunctionName(String functionName) { fname = functionName; } @@ -770,11 +687,7 @@ public class LexicalUnitImpl implements LexicalUnit, SCSSLexicalUnit, } - private String serializeFunction(String funcName) { - return getSerializer(funcName).printState(this); - } - - private static SCSSFunctionGenerator getSerializer(String funcName) { + private static SCSSFunctionGenerator getGenerator(String funcName) { SCSSFunctionGenerator serializer = SERIALIZERS.get(funcName); if (serializer == null) { return DEFAULT_SERIALIZER; @@ -804,7 +717,8 @@ public class LexicalUnitImpl implements LexicalUnit, SCSSLexicalUnit, } @Override - public String printState(LexicalUnitImpl function) { + public String printState(LexicalUnitImpl function, + BuildStringStrategy strategy) { StringBuilder builder = new StringBuilder(); LexicalUnitImpl firstParam = function.getParameters(); float value = firstParam.getFloatValue(); @@ -826,7 +740,7 @@ public class LexicalUnitImpl implements LexicalUnit, SCSSLexicalUnit, firstParam.setStringValue(builder.append('%').toString()); - return firstParam.toString(); + return strategy.build(firstParam); } } @@ -835,6 +749,137 @@ public class LexicalUnitImpl implements LexicalUnit, SCSSLexicalUnit, private static final SCSSFunctionGenerator DEFAULT_SERIALIZER = new DefaultFunctionGenerator(); + private String simpleAsString() { + short type = getLexicalUnitType(); + String text = null; + switch (type) { + case SCSS_VARIABLE: + text = "$" + s; + break; + case SCSS_NULL: + text = ""; + break; + case LexicalUnit.SAC_OPERATOR_COMMA: + text = ","; + break; + case LexicalUnit.SAC_OPERATOR_PLUS: + text = "+"; + break; + case LexicalUnit.SAC_OPERATOR_MINUS: + text = "-"; + break; + case LexicalUnit.SAC_OPERATOR_MULTIPLY: + text = "*"; + break; + case LexicalUnit.SAC_OPERATOR_SLASH: + text = "/"; + break; + case LexicalUnit.SAC_OPERATOR_MOD: + text = "%"; + break; + case LexicalUnit.SAC_OPERATOR_EXP: + text = "^"; + break; + case LexicalUnit.SAC_OPERATOR_LT: + text = "<"; + break; + case LexicalUnit.SAC_OPERATOR_GT: + text = ">"; + break; + case LexicalUnit.SAC_OPERATOR_LE: + text = "<="; + break; + case LexicalUnit.SAC_OPERATOR_GE: + text = "=>"; + break; + case LexicalUnit.SAC_OPERATOR_TILDE: + text = "~"; + break; + case LexicalUnit.SAC_INHERIT: + text = "inherit"; + break; + case LexicalUnit.SAC_INTEGER: + text = Integer.toString(getIntegerValue(), 10); + break; + case LexicalUnit.SAC_REAL: + text = getFloatOrInteger(); + break; + case LexicalUnit.SAC_EM: + case SCSSLexicalUnit.SAC_LEM: + case SCSSLexicalUnit.SAC_REM: + case LexicalUnit.SAC_EX: + case LexicalUnit.SAC_PIXEL: + case LexicalUnit.SAC_INCH: + case LexicalUnit.SAC_CENTIMETER: + case LexicalUnit.SAC_MILLIMETER: + case LexicalUnit.SAC_POINT: + case LexicalUnit.SAC_PICA: + case LexicalUnit.SAC_PERCENTAGE: + case LexicalUnit.SAC_DEGREE: + case LexicalUnit.SAC_GRADIAN: + case LexicalUnit.SAC_RADIAN: + case LexicalUnit.SAC_MILLISECOND: + case LexicalUnit.SAC_SECOND: + case LexicalUnit.SAC_HERTZ: + case LexicalUnit.SAC_KILOHERTZ: + case LexicalUnit.SAC_DIMENSION: + text = getFloatOrInteger() + getDimensionUnitText(); + break; + } + return text; + } + + private String buildString(BuildStringStrategy strategy) { + short type = getLexicalUnitType(); + String text = simpleAsString(); + if (text == null) { + switch (type) { + case LexicalUnit.SAC_URI: + text = "url(" + getStringValue() + ")"; + break; + case LexicalUnit.SAC_RGBCOLOR: + case LexicalUnit.SAC_COUNTER_FUNCTION: + case LexicalUnit.SAC_COUNTERS_FUNCTION: + case LexicalUnit.SAC_RECT_FUNCTION: + case LexicalUnit.SAC_FUNCTION: + text = buildFunctionString(strategy); + break; + case LexicalUnit.SAC_IDENT: + text = getStringValue(); + break; + case LexicalUnit.SAC_STRING_VALUE: + // @@SEEME. not exact + text = "\"" + getStringValue() + "\""; + break; + case LexicalUnit.SAC_ATTR: + text = "attr(" + getStringValue() + ")"; + break; + case LexicalUnit.SAC_UNICODERANGE: + text = "@@TODO"; + break; + case LexicalUnit.SAC_SUB_EXPRESSION: + text = strategy.build(getSubValues()); + break; + default: + text = "@unknown"; + break; + } + } + if (getNextLexicalUnit() != null) { + if (getNextLexicalUnit().getLexicalUnitType() == SAC_OPERATOR_COMMA) { + return text + strategy.build(getNextLexicalUnit()); + } + return text + ' ' + strategy.build(getNextLexicalUnit()); + } else { + return text; + } + } + + private String buildFunctionString(BuildStringStrategy strategy) { + SCSSFunctionGenerator generator = getGenerator(getFunctionName()); + return generator.printState(this, strategy); + } + static { for (SCSSFunctionGenerator serializer : initSerializers()) { SERIALIZERS.put(serializer.getFunctionName(), serializer); diff --git a/theme-compiler/src/com/vaadin/sass/internal/parser/function/AbsFunctionGenerator.java b/theme-compiler/src/com/vaadin/sass/internal/parser/function/AbsFunctionGenerator.java index 7f88649e84..7eeae85eba 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/parser/function/AbsFunctionGenerator.java +++ b/theme-compiler/src/com/vaadin/sass/internal/parser/function/AbsFunctionGenerator.java @@ -16,6 +16,7 @@ package com.vaadin.sass.internal.parser.function; import com.vaadin.sass.internal.parser.LexicalUnitImpl; +import com.vaadin.sass.internal.tree.Node.BuildStringStrategy; /** * @@ -30,10 +31,11 @@ public class AbsFunctionGenerator implements SCSSFunctionGenerator { } @Override - public String printState(LexicalUnitImpl function) { + public String printState(LexicalUnitImpl function, + BuildStringStrategy strategy) { LexicalUnitImpl firstParam = function.getParameters(); firstParam.setFloatValue(Math.abs(firstParam.getFloatValue())); - return firstParam.toString(); + return strategy.build(firstParam); } } diff --git a/theme-compiler/src/com/vaadin/sass/internal/parser/function/CeilFunctionGenerator.java b/theme-compiler/src/com/vaadin/sass/internal/parser/function/CeilFunctionGenerator.java index 7d19734fe7..23a6bc5edd 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/parser/function/CeilFunctionGenerator.java +++ b/theme-compiler/src/com/vaadin/sass/internal/parser/function/CeilFunctionGenerator.java @@ -16,6 +16,7 @@ package com.vaadin.sass.internal.parser.function; import com.vaadin.sass.internal.parser.LexicalUnitImpl; +import com.vaadin.sass.internal.tree.Node.BuildStringStrategy; /** * @@ -30,10 +31,11 @@ public class CeilFunctionGenerator implements SCSSFunctionGenerator { } @Override - public String printState(LexicalUnitImpl function) { + public String printState(LexicalUnitImpl function, + BuildStringStrategy strategy) { LexicalUnitImpl firstParam = function.getParameters(); firstParam.setFloatValue((float) Math.ceil(firstParam.getFloatValue())); - return firstParam.toString(); + return strategy.build(firstParam); } } diff --git a/theme-compiler/src/com/vaadin/sass/internal/parser/function/DarkenFunctionGenerator.java b/theme-compiler/src/com/vaadin/sass/internal/parser/function/DarkenFunctionGenerator.java index 2fbf0b2427..70481be84c 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/parser/function/DarkenFunctionGenerator.java +++ b/theme-compiler/src/com/vaadin/sass/internal/parser/function/DarkenFunctionGenerator.java @@ -16,6 +16,7 @@ package com.vaadin.sass.internal.parser.function; import com.vaadin.sass.internal.parser.LexicalUnitImpl; +import com.vaadin.sass.internal.tree.Node.BuildStringStrategy; import com.vaadin.sass.internal.util.ColorUtil; /** @@ -31,8 +32,9 @@ public class DarkenFunctionGenerator implements SCSSFunctionGenerator { } @Override - public String printState(LexicalUnitImpl function) { + public String printState(LexicalUnitImpl function, + BuildStringStrategy strategy) { LexicalUnitImpl dark = ColorUtil.darken(function); - return dark.toString(); + return strategy.build(dark); } } diff --git a/theme-compiler/src/com/vaadin/sass/internal/parser/function/DefaultFunctionGenerator.java b/theme-compiler/src/com/vaadin/sass/internal/parser/function/DefaultFunctionGenerator.java index d972a5bee8..8ab9b3fb14 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/parser/function/DefaultFunctionGenerator.java +++ b/theme-compiler/src/com/vaadin/sass/internal/parser/function/DefaultFunctionGenerator.java @@ -16,6 +16,7 @@ package com.vaadin.sass.internal.parser.function; import com.vaadin.sass.internal.parser.LexicalUnitImpl; +import com.vaadin.sass.internal.tree.Node.BuildStringStrategy; /** * @@ -30,16 +31,18 @@ public class DefaultFunctionGenerator implements SCSSFunctionGenerator { } @Override - public String printState(LexicalUnitImpl function) { + public String printState(LexicalUnitImpl function, + BuildStringStrategy strategy) { StringBuilder builder = new StringBuilder(function.getFunctionName()); - return builder.append('(').append(printParameters(function)) + return builder.append('(').append(printParameters(function, strategy)) .append(')').toString(); } - private String printParameters(LexicalUnitImpl function) { + private String printParameters(LexicalUnitImpl function, + BuildStringStrategy strategy) { if (function.getParameters() == null) { return null; } - return function.getParameters().toString(); + return strategy.build(function.getParameters()); } } diff --git a/theme-compiler/src/com/vaadin/sass/internal/parser/function/FloorFunctionGenerator.java b/theme-compiler/src/com/vaadin/sass/internal/parser/function/FloorFunctionGenerator.java index 57a0dcbf56..dc30f06ef0 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/parser/function/FloorFunctionGenerator.java +++ b/theme-compiler/src/com/vaadin/sass/internal/parser/function/FloorFunctionGenerator.java @@ -16,6 +16,7 @@ package com.vaadin.sass.internal.parser.function; import com.vaadin.sass.internal.parser.LexicalUnitImpl; +import com.vaadin.sass.internal.tree.Node.BuildStringStrategy; /** * @@ -30,11 +31,12 @@ public class FloorFunctionGenerator implements SCSSFunctionGenerator { } @Override - public String printState(LexicalUnitImpl function) { + public String printState(LexicalUnitImpl function, + BuildStringStrategy strategy) { LexicalUnitImpl firstParam = function.getParameters(); firstParam .setFloatValue((float) Math.floor(firstParam.getFloatValue())); - return firstParam.toString(); + return strategy.build(firstParam); } } diff --git a/theme-compiler/src/com/vaadin/sass/internal/parser/function/LightenFunctionGenerator.java b/theme-compiler/src/com/vaadin/sass/internal/parser/function/LightenFunctionGenerator.java index b911593457..c78e63e104 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/parser/function/LightenFunctionGenerator.java +++ b/theme-compiler/src/com/vaadin/sass/internal/parser/function/LightenFunctionGenerator.java @@ -16,6 +16,7 @@ package com.vaadin.sass.internal.parser.function; import com.vaadin.sass.internal.parser.LexicalUnitImpl; +import com.vaadin.sass.internal.tree.Node.BuildStringStrategy; import com.vaadin.sass.internal.util.ColorUtil; /** @@ -31,8 +32,9 @@ public class LightenFunctionGenerator implements SCSSFunctionGenerator { } @Override - public String printState(LexicalUnitImpl function) { - return ColorUtil.lighten(function).toString(); + public String printState(LexicalUnitImpl function, + BuildStringStrategy strategy) { + return strategy.build(ColorUtil.lighten(function)); } } diff --git a/theme-compiler/src/com/vaadin/sass/internal/parser/function/RoundFunctionGenerator.java b/theme-compiler/src/com/vaadin/sass/internal/parser/function/RoundFunctionGenerator.java index b8e8194f21..3eacba3b38 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/parser/function/RoundFunctionGenerator.java +++ b/theme-compiler/src/com/vaadin/sass/internal/parser/function/RoundFunctionGenerator.java @@ -16,6 +16,7 @@ package com.vaadin.sass.internal.parser.function; import com.vaadin.sass.internal.parser.LexicalUnitImpl; +import com.vaadin.sass.internal.tree.Node.BuildStringStrategy; /** * @@ -30,10 +31,11 @@ public class RoundFunctionGenerator implements SCSSFunctionGenerator { } @Override - public String printState(LexicalUnitImpl function) { + public String printState(LexicalUnitImpl function, + BuildStringStrategy strategy) { LexicalUnitImpl firstParam = function.getParameters(); firstParam.setFloatValue(Math.round(firstParam.getFloatValue())); - return firstParam.toString(); + return strategy.build(firstParam); } } diff --git a/theme-compiler/src/com/vaadin/sass/internal/parser/function/SCSSFunctionGenerator.java b/theme-compiler/src/com/vaadin/sass/internal/parser/function/SCSSFunctionGenerator.java index 4d34b59578..112fc1e395 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/parser/function/SCSSFunctionGenerator.java +++ b/theme-compiler/src/com/vaadin/sass/internal/parser/function/SCSSFunctionGenerator.java @@ -16,6 +16,7 @@ package com.vaadin.sass.internal.parser.function; import com.vaadin.sass.internal.parser.LexicalUnitImpl; +import com.vaadin.sass.internal.tree.Node.BuildStringStrategy; /** * Generator class is used to handle SCSS functions. Generator is applied to the @@ -48,7 +49,9 @@ public interface SCSSFunctionGenerator { * @since 7.2 * @param function * Function lexical unit to print its state + * @param strategy + * Strategy to build string from nodes * @return String state representation of the function */ - String printState(LexicalUnitImpl function); + String printState(LexicalUnitImpl function, BuildStringStrategy strategy); } diff --git a/theme-compiler/src/com/vaadin/sass/internal/tree/BlockNode.java b/theme-compiler/src/com/vaadin/sass/internal/tree/BlockNode.java index 8db522ea1a..5e7674e3b2 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/tree/BlockNode.java +++ b/theme-compiler/src/com/vaadin/sass/internal/tree/BlockNode.java @@ -41,33 +41,18 @@ public class BlockNode extends Node implements IVariableNode { this.selectorList = selectorList; } - public String toString(boolean indent) { - StringBuilder string = new StringBuilder(); - int i = 0; - for (final String s : selectorList) { - string.append(s); - if (i != selectorList.size() - 1) { - string.append(", "); - } - i++; - } - string.append(" {\n"); - for (Node child : children) { - if (indent) { - string.append("\t"); - } - string.append("\t" + child.toString() + "\n"); - } - if (indent) { - string.append("\t"); - } - string.append("}"); - return string.toString(); + public String buildString(boolean indent) { + return buildString(indent, PRINT_STRATEGY); + } + + @Override + public String printState() { + return buildString(false); } @Override public String toString() { - return toString(false); + return "BlockNode [" + buildString(true, TO_STRING_STRATEGY) + "]"; } @Override @@ -116,4 +101,28 @@ public class BlockNode extends Node implements IVariableNode { } } + private String buildString(boolean indent, BuildStringStrategy strategy) { + StringBuilder string = new StringBuilder(); + int i = 0; + for (final String s : selectorList) { + string.append(s); + if (i != selectorList.size() - 1) { + string.append(", "); + } + i++; + } + string.append(" {\n"); + for (Node child : children) { + if (indent) { + string.append("\t"); + } + string.append("\t" + strategy.build(child) + "\n"); + } + if (indent) { + string.append("\t"); + } + string.append("}"); + return string.toString(); + } + } diff --git a/theme-compiler/src/com/vaadin/sass/internal/tree/CommentNode.java b/theme-compiler/src/com/vaadin/sass/internal/tree/CommentNode.java index 70947d6022..968d0f7798 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/tree/CommentNode.java +++ b/theme-compiler/src/com/vaadin/sass/internal/tree/CommentNode.java @@ -32,11 +32,16 @@ public class CommentNode extends Node { } @Override - public String toString() { + public String printState() { return comment; } @Override + public String toString() { + return "Comment node [" + comment + "]"; + } + + @Override public void traverse() { // Not used in CommentNode } diff --git a/theme-compiler/src/com/vaadin/sass/internal/tree/ExtendNode.java b/theme-compiler/src/com/vaadin/sass/internal/tree/ExtendNode.java index dcabd2ff01..7614f7c606 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/tree/ExtendNode.java +++ b/theme-compiler/src/com/vaadin/sass/internal/tree/ExtendNode.java @@ -41,6 +41,11 @@ public class ExtendNode extends Node implements IVariableNode { } + @Override + public String toString() { + return "Extend node [" + getListAsString() + "]"; + } + public String getListAsString() { StringBuilder b = new StringBuilder(); for (final String s : list) { diff --git a/theme-compiler/src/com/vaadin/sass/internal/tree/FontFaceNode.java b/theme-compiler/src/com/vaadin/sass/internal/tree/FontFaceNode.java index 8986691984..b953b1af11 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/tree/FontFaceNode.java +++ b/theme-compiler/src/com/vaadin/sass/internal/tree/FontFaceNode.java @@ -18,21 +18,30 @@ package com.vaadin.sass.internal.tree; public class FontFaceNode extends Node { @Override + public String printState() { + return buildString(PRINT_STRATEGY); + } + + @Override public String toString() { + return "FontFace node [" + buildString(TO_STRING_STRATEGY) + "]"; + } + + @Override + public void traverse() { + // Not in use for FontFaceNode + } + + private String buildString(BuildStringStrategy strategy) { StringBuilder builder = new StringBuilder(); builder.append("@font-face {\n"); for (final Node child : children) { - builder.append("\t" + child.toString() + "\n"); + builder.append("\t" + strategy.build(child) + "\n"); } builder.append("}"); return builder.toString(); } - @Override - public void traverse() { - // Not in use for FontFaceNode - } - } diff --git a/theme-compiler/src/com/vaadin/sass/internal/tree/FunctionNode.java b/theme-compiler/src/com/vaadin/sass/internal/tree/FunctionNode.java index ced4671051..2d1001f47f 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/tree/FunctionNode.java +++ b/theme-compiler/src/com/vaadin/sass/internal/tree/FunctionNode.java @@ -50,7 +50,7 @@ public class FunctionNode extends Node implements IVariableNode { for (final VariableNode node : variables) { if (StringUtil.containsVariable(args, node.getName())) { args = StringUtil.replaceVariable(args, node.getName(), node - .getExpr().toString()); + .getExpr().printState()); } } } @@ -59,4 +59,5 @@ public class FunctionNode extends Node implements IVariableNode { public void traverse() { replaceVariables(ScssStylesheet.getVariables()); } + } diff --git a/theme-compiler/src/com/vaadin/sass/internal/tree/ImportNode.java b/theme-compiler/src/com/vaadin/sass/internal/tree/ImportNode.java index 6dc95db5c2..e112752138 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/tree/ImportNode.java +++ b/theme-compiler/src/com/vaadin/sass/internal/tree/ImportNode.java @@ -43,7 +43,7 @@ public class ImportNode extends Node { } @Override - public String toString() { + public String printState() { StringBuilder builder = new StringBuilder("@import "); if (isURL) { builder.append("url(").append(uri).append(")"); @@ -59,6 +59,11 @@ public class ImportNode extends Node { return builder.toString(); } + @Override + public String toString() { + return "Import node [" + printState() + "]"; + } + public String getUri() { return uri; } @@ -76,4 +81,5 @@ public class ImportNode extends Node { // nested imports ImportNodeHandler.traverse(getParentNode()); } + } diff --git a/theme-compiler/src/com/vaadin/sass/internal/tree/KeyframeSelectorNode.java b/theme-compiler/src/com/vaadin/sass/internal/tree/KeyframeSelectorNode.java index 085cd46d31..e89b9b2494 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/tree/KeyframeSelectorNode.java +++ b/theme-compiler/src/com/vaadin/sass/internal/tree/KeyframeSelectorNode.java @@ -24,14 +24,14 @@ public class KeyframeSelectorNode extends Node { } @Override + public String printState() { + return buildString(PRINT_STRATEGY); + } + + @Override public String toString() { - StringBuilder string = new StringBuilder(); - string.append(selector).append(" {\n"); - for (Node child : children) { - string.append("\t\t").append(child.toString()).append("\n"); - } - string.append("\t}"); - return string.toString(); + return "Key frame selector node [" + buildString(TO_STRING_STRATEGY) + + "]"; } @Override @@ -39,4 +39,13 @@ public class KeyframeSelectorNode extends Node { } + public String buildString(BuildStringStrategy strategy) { + StringBuilder string = new StringBuilder(); + string.append(selector).append(" {\n"); + for (Node child : children) { + string.append("\t\t").append(strategy.build(child)).append("\n"); + } + string.append("\t}"); + return string.toString(); + } } diff --git a/theme-compiler/src/com/vaadin/sass/internal/tree/KeyframesNode.java b/theme-compiler/src/com/vaadin/sass/internal/tree/KeyframesNode.java index b5e7491639..28b1b0d42f 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/tree/KeyframesNode.java +++ b/theme-compiler/src/com/vaadin/sass/internal/tree/KeyframesNode.java @@ -31,15 +31,13 @@ public class KeyframesNode extends Node implements IVariableNode { } @Override + public String printState() { + return buildString(PRINT_STRATEGY); + } + + @Override public String toString() { - StringBuilder string = new StringBuilder(); - string.append(keyframeName).append(" ").append(animationName) - .append(" {\n"); - for (Node child : children) { - string.append("\t\t").append(child.toString()).append("\n"); - } - string.append("\t}"); - return string.toString(); + return "Key frames node [" + buildString(TO_STRING_STRATEGY) + "]"; } @Override @@ -61,4 +59,14 @@ public class KeyframesNode extends Node implements IVariableNode { } } + private String buildString(BuildStringStrategy strategy) { + StringBuilder string = new StringBuilder(); + string.append(keyframeName).append(" ").append(animationName) + .append(" {\n"); + for (Node child : children) { + string.append("\t\t").append(strategy.build(child)).append("\n"); + } + string.append("\t}"); + return string.toString(); + } } diff --git a/theme-compiler/src/com/vaadin/sass/internal/tree/ListContainsNode.java b/theme-compiler/src/com/vaadin/sass/internal/tree/ListContainsNode.java index d2701647ee..54ec7550d7 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/tree/ListContainsNode.java +++ b/theme-compiler/src/com/vaadin/sass/internal/tree/ListContainsNode.java @@ -39,6 +39,5 @@ public class ListContainsNode extends ListModifyNode { VariableNode node = new VariableNode(variable.substring(1), LexicalUnitImpl.createString(contains), false); return node; - } } diff --git a/theme-compiler/src/com/vaadin/sass/internal/tree/ListModifyNode.java b/theme-compiler/src/com/vaadin/sass/internal/tree/ListModifyNode.java index 0c2327c90e..9da810d37b 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/tree/ListModifyNode.java +++ b/theme-compiler/src/com/vaadin/sass/internal/tree/ListModifyNode.java @@ -32,6 +32,12 @@ public abstract class ListModifyNode extends Node implements IVariableNode { return variable; } + @Override + public String toString() { + return "List append node [var = " + variable + " , list =" + list + + ", separator =" + separator + ", modify =" + modify + "]"; + } + public VariableNode getModifiedList() { final ArrayList<String> newList = new ArrayList<String>(list); modifyList(newList); @@ -95,10 +101,10 @@ public abstract class ListModifyNode extends Node implements IVariableNode { if (var.getName().equals(listVar.substring(1))) { String[] split = null; - if (var.getExpr().toString().contains(",")) { - split = var.getExpr().toString().split(","); + if (var.getExpr().printState().contains(",")) { + split = var.getExpr().printState().split(","); } else { - split = var.getExpr().toString().split(" "); + split = var.getExpr().printState().split(" "); } int i = list.indexOf(listVar); for (final String s : split) { diff --git a/theme-compiler/src/com/vaadin/sass/internal/tree/MediaNode.java b/theme-compiler/src/com/vaadin/sass/internal/tree/MediaNode.java index 34d8ccbf7d..c5494cb665 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/tree/MediaNode.java +++ b/theme-compiler/src/com/vaadin/sass/internal/tree/MediaNode.java @@ -37,7 +37,21 @@ public class MediaNode extends Node { } @Override + public String printState() { + return buildString(PRINT_STRATEGY, true); + } + + @Override public String toString() { + return buildString(TO_STRING_STRATEGY, true); + } + + @Override + public void traverse() { + + } + + private String buildString(BuildStringStrategy strategy, boolean indent) { StringBuilder builder = new StringBuilder("@media "); if (media != null) { for (int i = 0; i < media.getLength(); i++) { @@ -49,20 +63,20 @@ public class MediaNode extends Node { } builder.append(" {\n"); for (Node child : children) { + builder.append('\t'); if (child instanceof BlockNode) { - builder.append("\t" + ((BlockNode) child).toString(true) + "\n"); + if (PRINT_STRATEGY.equals(strategy)) { + builder.append(((BlockNode) child).buildString(indent)); + } else { + builder.append(strategy.build(child)); + + } } else { - builder.append("\t" + child.toString() + "\n"); + builder.append(strategy.build(child)); } - + builder.append('\n'); } builder.append("}"); return builder.toString(); } - - @Override - public void traverse() { - - } - } diff --git a/theme-compiler/src/com/vaadin/sass/internal/tree/MicrosoftRuleNode.java b/theme-compiler/src/com/vaadin/sass/internal/tree/MicrosoftRuleNode.java index 3938188a72..18946d7279 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/tree/MicrosoftRuleNode.java +++ b/theme-compiler/src/com/vaadin/sass/internal/tree/MicrosoftRuleNode.java @@ -35,17 +35,22 @@ public class MicrosoftRuleNode extends Node implements IVariableNode { for (final VariableNode var : variables) { if (StringUtil.containsVariable(value, var.getName())) { value = StringUtil.replaceVariable(value, var.getName(), var - .getExpr().toString()); + .getExpr().printState()); } } } @Override - public String toString() { + public String printState() { return name + ": " + value + ";"; } @Override + public String toString() { + return "MicrosoftRule node [" + printState() + "]"; + } + + @Override public void traverse() { replaceVariables(ScssStylesheet.getVariables()); } diff --git a/theme-compiler/src/com/vaadin/sass/internal/tree/MixinNode.java b/theme-compiler/src/com/vaadin/sass/internal/tree/MixinNode.java index c5ffb6d73d..23d65e1660 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/tree/MixinNode.java +++ b/theme-compiler/src/com/vaadin/sass/internal/tree/MixinNode.java @@ -45,10 +45,15 @@ public class MixinNode extends Node implements IVariableNode { } @Override - public String toString() { + public String printState() { return "name: " + name + " args: " + arglist; } + @Override + public String toString() { + return "Mixin node [" + printState() + "]"; + } + public String getName() { return name; } @@ -88,11 +93,11 @@ public class MixinNode extends Node implements IVariableNode { if (name.startsWith("$")) { if (name.equals("$" + var.getName())) { - name = var.getExpr().toString(); + name = var.getExpr().printState(); } } else if (name.startsWith("#{") && name.endsWith("}")) { if (name.equals("#{$" + var.getName() + "}")) { - name = var.getExpr().toString(); + name = var.getExpr().printState(); } } } diff --git a/theme-compiler/src/com/vaadin/sass/internal/tree/NestPropertiesNode.java b/theme-compiler/src/com/vaadin/sass/internal/tree/NestPropertiesNode.java index fc50cfda61..fb35e13175 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/tree/NestPropertiesNode.java +++ b/theme-compiler/src/com/vaadin/sass/internal/tree/NestPropertiesNode.java @@ -40,6 +40,11 @@ public class NestPropertiesNode extends Node implements IVariableNode { this.name = name; } + @Override + public String toString() { + return "Nest properties node [ name = " + name + " ]"; + } + public Collection<RuleNode> unNesting() { List<RuleNode> result = new ArrayList<RuleNode>(); for (Node child : children) { @@ -71,4 +76,9 @@ public class NestPropertiesNode extends Node implements IVariableNode { NestedNodeHandler.traverse(this); } + @Override + public String printState() { + return null; + } + } diff --git a/theme-compiler/src/com/vaadin/sass/internal/tree/Node.java b/theme-compiler/src/com/vaadin/sass/internal/tree/Node.java index 98b701d5a9..ea01655571 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/tree/Node.java +++ b/theme-compiler/src/com/vaadin/sass/internal/tree/Node.java @@ -20,7 +20,14 @@ import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; +import com.vaadin.sass.internal.parser.LexicalUnitImpl; + public abstract class Node implements Serializable { + + public static BuildStringStrategy PRINT_STRATEGY = new PrintStrategy(); + + public static BuildStringStrategy TO_STRING_STRATEGY = new ToStringStrategy(); + private static final long serialVersionUID = 5914711715839294816L; protected ArrayList<Node> children; @@ -108,11 +115,6 @@ public abstract class Node implements Serializable { return !children.isEmpty(); } - @Override - public String toString() { - return ""; - } - /** * Method for manipulating the data contained within the {@link Node}. * @@ -123,6 +125,19 @@ public abstract class Node implements Serializable { */ public abstract void traverse(); + /** + * Prints out the current state of the node tree. Will return SCSS before + * compile and CSS after. + * + * Result value could be null. + * + * @since 7.2 + * @return State as a string + */ + public String printState() { + return null; + } + public Node getParentNode() { return parentNode; } @@ -131,4 +146,39 @@ public abstract class Node implements Serializable { this.parentNode = parentNode; } + public static interface BuildStringStrategy { + + String build(Node node); + + String build(LexicalUnitImpl unit); + } + + public static class PrintStrategy implements BuildStringStrategy { + + @Override + public String build(Node node) { + return node.printState(); + } + + @Override + public String build(LexicalUnitImpl unit) { + return unit.printState(); + } + + } + + public static class ToStringStrategy implements BuildStringStrategy { + + @Override + public String build(Node node) { + return node.toString(); + } + + @Override + public String build(LexicalUnitImpl unit) { + return unit.toString(); + } + + } + } diff --git a/theme-compiler/src/com/vaadin/sass/internal/tree/RuleNode.java b/theme-compiler/src/com/vaadin/sass/internal/tree/RuleNode.java index a8fa87eb0a..e9886e6871 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/tree/RuleNode.java +++ b/theme-compiler/src/com/vaadin/sass/internal/tree/RuleNode.java @@ -57,19 +57,13 @@ public class RuleNode extends Node implements IVariableNode { } @Override - public String toString() { - String stringValue = value.toString() - + (important ? " !important" : ""); - if (!"".equals(stringValue.trim())) { - stringValue = variable + ": " + stringValue + ";"; - } else { - stringValue = ""; - } + public String printState() { + return buildString(PRINT_STRATEGY); + } - if (comment != null) { - stringValue += comment; - } - return stringValue; + @Override + public String toString() { + return "Rule node [" + buildString(TO_STRING_STRATEGY) + "]"; } public boolean isImportant() { @@ -104,12 +98,12 @@ public class RuleNode extends Node implements IVariableNode { if (value.getParameters() != null) { if (StringUtil.containsVariable(value.getParameters() - .toString(), node.getName())) { + .printState(), node.getName())) { LexicalUnitImpl param = value.getParameters(); while (param != null) { if (param.getLexicalUnitType() == LexicalUnitImpl.SCSS_VARIABLE - && param.getValue().toString() - .equals(node.getName())) { + && param.getValueAsString().equals( + node.getName())) { param.replaceValue(node.getExpr()); } param = param.getNextLexicalUnit(); @@ -120,11 +114,9 @@ public class RuleNode extends Node implements IVariableNode { && value.getStringValue().contains(interpolation)) { LexicalUnitImpl current = value; while (current != null) { - if (current.getValue().toString().contains(interpolation)) { + if (current.getValueAsString().contains(interpolation)) { - current.setStringValue(current - .getValue() - .toString() + current.setStringValue(current.getValueAsString() .replaceAll(Pattern.quote(interpolation), node.getExpr().unquotedString())); } @@ -134,7 +126,7 @@ public class RuleNode extends Node implements IVariableNode { LexicalUnitImpl current = value; while (current != null) { if (current.getLexicalUnitType() == LexicalUnitImpl.SCSS_VARIABLE - && current.getValue().toString() + && current.getValueAsString() .equals(node.getName())) { current.replaceValue(node.getExpr()); @@ -163,4 +155,21 @@ public class RuleNode extends Node implements IVariableNode { replaceVariables(ScssStylesheet.getVariables()); } } + + private String buildString(BuildStringStrategy strategy) { + String stringValue = strategy.build(value) + + (important ? " !important" : ""); + StringBuilder builder = new StringBuilder(); + if (!"".equals(stringValue.trim())) { + builder.append(variable); + builder.append(": "); + builder.append(stringValue); + builder.append(';'); + } + + if (comment != null) { + builder.append(comment); + } + return builder.toString(); + } } diff --git a/theme-compiler/src/com/vaadin/sass/internal/tree/SimpleNode.java b/theme-compiler/src/com/vaadin/sass/internal/tree/SimpleNode.java index 796f4d8d1d..91d9767b89 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/tree/SimpleNode.java +++ b/theme-compiler/src/com/vaadin/sass/internal/tree/SimpleNode.java @@ -38,16 +38,21 @@ public class SimpleNode extends Node implements IVariableNode { } @Override - public String toString() { + public String printState() { return text; } @Override + public String toString() { + return printState(); + } + + @Override public void replaceVariables(ArrayList<VariableNode> variables) { for (final VariableNode node : variables) { if (StringUtil.containsVariable(text, node.getName())) { text = StringUtil.replaceVariable(text, node.getName(), node - .getExpr().toString()); + .getExpr().printState()); } } } diff --git a/theme-compiler/src/com/vaadin/sass/internal/tree/VariableNode.java b/theme-compiler/src/com/vaadin/sass/internal/tree/VariableNode.java index f2499d72ab..6884ae6f36 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/tree/VariableNode.java +++ b/theme-compiler/src/com/vaadin/sass/internal/tree/VariableNode.java @@ -59,10 +59,13 @@ public class VariableNode extends Node implements IVariableNode { } @Override + public String printState() { + return buildString(PRINT_STRATEGY); + } + + @Override public String toString() { - StringBuilder builder = new StringBuilder("$"); - builder.append(name).append(": ").append(expr); - return builder.toString(); + return "Variable node [" + buildString(TO_STRING_STRATEGY) + "]"; } public void setGuarded(boolean guarded) { @@ -74,11 +77,11 @@ public class VariableNode extends Node implements IVariableNode { for (final VariableNode node : variables) { if (!equals(node)) { - if (StringUtil - .containsVariable(expr.toString(), node.getName())) { + if (StringUtil.containsVariable(expr.printState(), + node.getName())) { if (expr.getParameters() != null && StringUtil.containsVariable(expr.getParameters() - .toString(), node.getName())) { + .printState(), node.getName())) { replaceValues(expr.getParameters(), node); } else if (expr.getLexicalUnitType() == LexicalUnitImpl.SCSS_VARIABLE) { replaceValues(expr, node); @@ -92,7 +95,7 @@ public class VariableNode extends Node implements IVariableNode { while (unit != null) { if (unit.getLexicalUnitType() == LexicalUnitImpl.SCSS_VARIABLE - && unit.getValue().toString().equals(node.getName())) { + && unit.getValueAsString().equals(node.getName())) { LexicalUnitImpl.replaceValues(unit, node.getExpr()); } @@ -119,4 +122,10 @@ public class VariableNode extends Node implements IVariableNode { } VariableNodeHandler.traverse(this); } + + private String buildString(BuildStringStrategy strategy) { + StringBuilder builder = new StringBuilder("$"); + builder.append(name).append(": ").append(strategy.build(expr)); + return builder.toString(); + } } diff --git a/theme-compiler/src/com/vaadin/sass/internal/tree/controldirective/EachDefNode.java b/theme-compiler/src/com/vaadin/sass/internal/tree/controldirective/EachDefNode.java index fe1775f26b..88dbf2c25b 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/tree/controldirective/EachDefNode.java +++ b/theme-compiler/src/com/vaadin/sass/internal/tree/controldirective/EachDefNode.java @@ -80,7 +80,7 @@ public class EachDefNode extends Node implements IVariableNode { while (current != null) { if (current.getValue() != null && current.getLexicalUnitType() != LexicalUnitImpl.SAC_OPERATOR_COMMA) { - list.add(current.getValue().toString()); + list.add(current.getValueAsString()); } current = current.getNextLexicalUnit(); } diff --git a/theme-compiler/src/com/vaadin/sass/internal/tree/controldirective/IfElseDefNode.java b/theme-compiler/src/com/vaadin/sass/internal/tree/controldirective/IfElseDefNode.java index 8449a9d891..8c7fa86596 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/tree/controldirective/IfElseDefNode.java +++ b/theme-compiler/src/com/vaadin/sass/internal/tree/controldirective/IfElseDefNode.java @@ -24,13 +24,13 @@ import com.vaadin.sass.internal.visitor.IfElseNodeHandler; public class IfElseDefNode extends Node { @Override + public String printState() { + return buildString(PRINT_STRATEGY); + } + + @Override public String toString() { - StringBuilder b = new StringBuilder(); - for (final Node child : getChildren()) { - b.append(child.toString()); - b.append("\n"); - } - return b.toString(); + return "IfElseDef node [" + buildString(TO_STRING_STRATEGY) + "]"; } @Override @@ -48,4 +48,13 @@ public class IfElseDefNode extends Node { } } + private String buildString(BuildStringStrategy strategy) { + StringBuilder b = new StringBuilder(); + for (final Node child : getChildren()) { + b.append(strategy.build(child)); + b.append("\n"); + } + return b.toString(); + } + } diff --git a/theme-compiler/src/com/vaadin/sass/internal/tree/controldirective/IfNode.java b/theme-compiler/src/com/vaadin/sass/internal/tree/controldirective/IfNode.java index af795a58c3..6c98927110 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/tree/controldirective/IfNode.java +++ b/theme-compiler/src/com/vaadin/sass/internal/tree/controldirective/IfNode.java @@ -41,7 +41,7 @@ public class IfNode extends Node implements IfElseNode, IVariableNode { @Override public String toString() { - return "@if" + expression; + return "@if " + expression; } @Override @@ -49,7 +49,7 @@ public class IfNode extends Node implements IfElseNode, IVariableNode { for (final VariableNode node : variables) { if (StringUtil.containsVariable(expression, node.getName())) { expression = StringUtil.replaceVariable(expression, - node.getName(), node.getExpr().toString()); + node.getName(), node.getExpr().printState()); } } } diff --git a/theme-compiler/tests/src/com/vaadin/sass/AbstractTestBase.java b/theme-compiler/tests/src/com/vaadin/sass/AbstractTestBase.java index 01187b4502..ff92b636ed 100644 --- a/theme-compiler/tests/src/com/vaadin/sass/AbstractTestBase.java +++ b/theme-compiler/tests/src/com/vaadin/sass/AbstractTestBase.java @@ -73,7 +73,7 @@ public abstract class AbstractTestBase { originalScss = getFileContent(file); originalScss = originalScss.replaceAll(CR, ""); ScssStylesheet sheet = getStyleSheet(file); - parsedScss = sheet.toString(); + parsedScss = sheet.printState(); parsedScss = parsedScss.replace(CR, ""); Assert.assertEquals("Original CSS and parsed CSS do not match", originalScss, parsedScss); @@ -84,7 +84,7 @@ public abstract class AbstractTestBase { comparisonCss = comparisonCss.replaceAll(CR, ""); ScssStylesheet sheet = getStyleSheet(scss); sheet.compile(); - parsedScss = sheet.toString(); + parsedScss = sheet.printState(); parsedScss = parsedScss.replaceAll(CR, ""); Assert.assertEquals("Original CSS and parsed CSS do not match", comparisonCss, parsedScss); diff --git a/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/AbstractDirectoryScanningSassTests.java b/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/AbstractDirectoryScanningSassTests.java index 6a5f8db73d..b9b80a7588 100644 --- a/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/AbstractDirectoryScanningSassTests.java +++ b/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/AbstractDirectoryScanningSassTests.java @@ -104,7 +104,7 @@ public abstract class AbstractDirectoryScanningSassTests { scssFile.getCanonicalPath(), null, documentHandler, errorHandler); scssStylesheet.compile(); - String parsedCss = scssStylesheet.toString(); + String parsedCss = scssStylesheet.printState(); if (getCssFile(scssFile) != null) { String referenceCss = IOUtils.toString(new FileInputStream( diff --git a/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/CompassImports.java b/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/CompassImports.java index 4e87eb2197..02415dbe15 100644 --- a/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/CompassImports.java +++ b/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/CompassImports.java @@ -74,7 +74,7 @@ public class CompassImports extends AbstractTestBase { sheet.addResolver(new FilesystemResolver(additionalPath)); sheet.compile(); - parsedScss = sheet.toString(); + parsedScss = sheet.printState(); parsedScss = parsedScss.replaceAll(CR, ""); Assert.assertEquals("Original CSS and parsed CSS do not match", comparisonCss, parsedScss); diff --git a/theme-compiler/tests/src/com/vaadin/sass/tree/ImportNodeTest.java b/theme-compiler/tests/src/com/vaadin/sass/tree/ImportNodeTest.java index 68c886b407..a7cf442966 100644 --- a/theme-compiler/tests/src/com/vaadin/sass/tree/ImportNodeTest.java +++ b/theme-compiler/tests/src/com/vaadin/sass/tree/ImportNodeTest.java @@ -56,22 +56,22 @@ public class ImportNodeTest { } @Test - public void testToStringWhenIsURL() { + public void testSerializeWhenIsURL() { ImportNode node = new ImportNode("test", null, true); - Assert.assertEquals("@import url(test);", node.toString()); + Assert.assertEquals("@import url(test);", node.printState()); } @Test - public void testToStringWhenIsNotURL() { + public void testSerializeWhenIsNotURL() { ImportNode node = new ImportNode("test", null, false); - Assert.assertEquals("@import \"test\";", node.toString()); + Assert.assertEquals("@import \"test\";", node.printState()); } @Test - public void testToStringWithMediaQueries() { + public void testSerializeWithMediaQueries() { SACMediaListImpl ml = new SACMediaListImpl(); ml.add("screen"); ImportNode node = new ImportNode("test", ml, true); - Assert.assertEquals("@import url(test) screen;", node.toString()); + Assert.assertEquals("@import url(test) screen;", node.printState()); } } |