From: denisanisimov Date: Tue, 28 Jan 2014 14:22:22 +0000 (+0200) Subject: Function compilation is refactored (#13253). X-Git-Tag: 7.2.0.beta1~161 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=40e07d0172532ebf5750b8c2aab5ec892e42cdab;p=vaadin-framework.git Function compilation is refactored (#13253). Change-Id: Ic3c5d6822d90703d7f02de3d1a3d15fe4366af07 --- 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 af1165adac..96f841de0b 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/parser/LexicalUnitImpl.java +++ b/theme-compiler/src/com/vaadin/sass/internal/parser/LexicalUnitImpl.java @@ -24,11 +24,22 @@ package com.vaadin.sass.internal.parser; import java.io.Serializable; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; import org.w3c.css.sac.LexicalUnit; import com.vaadin.sass.internal.expression.exception.IncompatibleUnitsException; -import com.vaadin.sass.internal.util.ColorUtil; +import com.vaadin.sass.internal.parser.function.AbsFunctionGenerator; +import com.vaadin.sass.internal.parser.function.CeilFunctionGenerator; +import com.vaadin.sass.internal.parser.function.DarkenFunctionGenerator; +import com.vaadin.sass.internal.parser.function.DefaultFunctionGenerator; +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.util.DeepCopy; /** @@ -325,30 +336,7 @@ public class LexicalUnitImpl implements LexicalUnit, SCSSLexicalUnit, case LexicalUnit.SAC_RECT_FUNCTION: case LexicalUnit.SAC_FUNCTION: String funcName = getFunctionName(); - LexicalUnitImpl firstParam = getParameters(); - if ("round".equals(funcName)) { - firstParam - .setFloatValue(Math.round(firstParam.getFloatValue())); - text = firstParam.toString(); - } else if ("ceil".equals(funcName)) { - firstParam.setFloatValue((float) Math.ceil(firstParam - .getFloatValue())); - text = firstParam.toString(); - } else if ("floor".equals(funcName)) { - firstParam.setFloatValue((float) Math.floor(firstParam - .getFloatValue())); - text = firstParam.toString(); - } else if ("abs".equals(funcName)) { - firstParam.setFloatValue(Math.abs(firstParam.getFloatValue())); - text = firstParam.toString(); - } else if ("darken".equals(funcName)) { - LexicalUnitImpl dark = ColorUtil.darken(this); - text = dark.toString(); - } else if ("lighten".equals(funcName)) { - text = ColorUtil.lighten(this).toString(); - } else { - text = getFunctionName() + "(" + getParameters() + ")"; - } + text = serializeFunction(funcName); break; case LexicalUnit.SAC_IDENT: text = getStringValue(); @@ -778,4 +766,38 @@ public class LexicalUnitImpl implements LexicalUnit, SCSSLexicalUnit, } } + + private String serializeFunction(String funcName) { + return getSerializer(funcName).printState(this); + } + + private static SCSSFunctionGenerator getSerializer(String funcName) { + SCSSFunctionGenerator serializer = SERIALIZERS.get(funcName); + if (serializer == null) { + return DEFAULT_SERIALIZER; + } else { + return serializer; + } + } + + private static List initSerializers() { + List list = new LinkedList(); + list.add(new AbsFunctionGenerator()); + list.add(new CeilFunctionGenerator()); + list.add(new DarkenFunctionGenerator()); + list.add(new FloorFunctionGenerator()); + list.add(new LightenFunctionGenerator()); + list.add(new RoundFunctionGenerator()); + return list; + } + + private static final Map SERIALIZERS = new HashMap(); + + private static final SCSSFunctionGenerator DEFAULT_SERIALIZER = new DefaultFunctionGenerator(); + + 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 new file mode 100644 index 0000000000..7f88649e84 --- /dev/null +++ b/theme-compiler/src/com/vaadin/sass/internal/parser/function/AbsFunctionGenerator.java @@ -0,0 +1,39 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.sass.internal.parser.function; + +import com.vaadin.sass.internal.parser.LexicalUnitImpl; + +/** + * + * @since 7.2 + * @author Vaadin Ltd + */ +public class AbsFunctionGenerator implements SCSSFunctionGenerator { + + @Override + public String getFunctionName() { + return "abs"; + } + + @Override + public String printState(LexicalUnitImpl function) { + LexicalUnitImpl firstParam = function.getParameters(); + firstParam.setFloatValue(Math.abs(firstParam.getFloatValue())); + return firstParam.toString(); + } + +} 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 new file mode 100644 index 0000000000..7d19734fe7 --- /dev/null +++ b/theme-compiler/src/com/vaadin/sass/internal/parser/function/CeilFunctionGenerator.java @@ -0,0 +1,39 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.sass.internal.parser.function; + +import com.vaadin.sass.internal.parser.LexicalUnitImpl; + +/** + * + * @since 7.2 + * @author Vaadin Ltd + */ +public class CeilFunctionGenerator implements SCSSFunctionGenerator { + + @Override + public String getFunctionName() { + return "ceil"; + } + + @Override + public String printState(LexicalUnitImpl function) { + LexicalUnitImpl firstParam = function.getParameters(); + firstParam.setFloatValue((float) Math.ceil(firstParam.getFloatValue())); + return firstParam.toString(); + } + +} 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 new file mode 100644 index 0000000000..2fbf0b2427 --- /dev/null +++ b/theme-compiler/src/com/vaadin/sass/internal/parser/function/DarkenFunctionGenerator.java @@ -0,0 +1,38 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.sass.internal.parser.function; + +import com.vaadin.sass.internal.parser.LexicalUnitImpl; +import com.vaadin.sass.internal.util.ColorUtil; + +/** + * + * @since 7.2 + * @author Vaadin Ltd + */ +public class DarkenFunctionGenerator implements SCSSFunctionGenerator { + + @Override + public String getFunctionName() { + return "darken"; + } + + @Override + public String printState(LexicalUnitImpl function) { + LexicalUnitImpl dark = ColorUtil.darken(function); + return dark.toString(); + } +} 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 new file mode 100644 index 0000000000..d972a5bee8 --- /dev/null +++ b/theme-compiler/src/com/vaadin/sass/internal/parser/function/DefaultFunctionGenerator.java @@ -0,0 +1,45 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.sass.internal.parser.function; + +import com.vaadin.sass.internal.parser.LexicalUnitImpl; + +/** + * + * @since 7.2 + * @author Vaadin Ltd + */ +public class DefaultFunctionGenerator implements SCSSFunctionGenerator { + + @Override + public String getFunctionName() { + return null; + } + + @Override + public String printState(LexicalUnitImpl function) { + StringBuilder builder = new StringBuilder(function.getFunctionName()); + return builder.append('(').append(printParameters(function)) + .append(')').toString(); + } + + private String printParameters(LexicalUnitImpl function) { + if (function.getParameters() == null) { + return null; + } + return function.getParameters().toString(); + } +} 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 new file mode 100644 index 0000000000..57a0dcbf56 --- /dev/null +++ b/theme-compiler/src/com/vaadin/sass/internal/parser/function/FloorFunctionGenerator.java @@ -0,0 +1,40 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.sass.internal.parser.function; + +import com.vaadin.sass.internal.parser.LexicalUnitImpl; + +/** + * + * @since 7.2 + * @author Vaadin Ltd + */ +public class FloorFunctionGenerator implements SCSSFunctionGenerator { + + @Override + public String getFunctionName() { + return "floor"; + } + + @Override + public String printState(LexicalUnitImpl function) { + LexicalUnitImpl firstParam = function.getParameters(); + firstParam + .setFloatValue((float) Math.floor(firstParam.getFloatValue())); + return firstParam.toString(); + } + +} 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 new file mode 100644 index 0000000000..b911593457 --- /dev/null +++ b/theme-compiler/src/com/vaadin/sass/internal/parser/function/LightenFunctionGenerator.java @@ -0,0 +1,38 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.sass.internal.parser.function; + +import com.vaadin.sass.internal.parser.LexicalUnitImpl; +import com.vaadin.sass.internal.util.ColorUtil; + +/** + * + * @since 7.2 + * @author Vaadin Ltd + */ +public class LightenFunctionGenerator implements SCSSFunctionGenerator { + + @Override + public String getFunctionName() { + return "lighten"; + } + + @Override + public String printState(LexicalUnitImpl function) { + return ColorUtil.lighten(function).toString(); + } + +} 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 new file mode 100644 index 0000000000..b8e8194f21 --- /dev/null +++ b/theme-compiler/src/com/vaadin/sass/internal/parser/function/RoundFunctionGenerator.java @@ -0,0 +1,39 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.sass.internal.parser.function; + +import com.vaadin.sass.internal.parser.LexicalUnitImpl; + +/** + * + * @since 7.2 + * @author Vaadin Ltd + */ +public class RoundFunctionGenerator implements SCSSFunctionGenerator { + + @Override + public String getFunctionName() { + return "round"; + } + + @Override + public String printState(LexicalUnitImpl function) { + LexicalUnitImpl firstParam = function.getParameters(); + firstParam.setFloatValue(Math.round(firstParam.getFloatValue())); + return firstParam.toString(); + } + +} 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 new file mode 100644 index 0000000000..4d34b59578 --- /dev/null +++ b/theme-compiler/src/com/vaadin/sass/internal/parser/function/SCSSFunctionGenerator.java @@ -0,0 +1,54 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.sass.internal.parser.function; + +import com.vaadin.sass.internal.parser.LexicalUnitImpl; + +/** + * Generator class is used to handle SCSS functions. Generator is applied to the + * function lexical unit if its method {@link #getFunctionName()} returns name + * of the function. + * + * If there are no dedicated generator for the function then default generator + * is used. + * + * @since 7.2 + * @author Vaadin Ltd + */ +public interface SCSSFunctionGenerator { + + /** + * Returns function name handled by this generator. Default generator + * returns null and is used if there is no dedicated generator + * for given function. + * + * @since 7.2 + * @return + */ + String getFunctionName(); + + /** + * Prints out the current state of the function. State is SCSS content of + * the function before compilation and compiled CSS content after + * compilation. + * + * @since 7.2 + * @param function + * Function lexical unit to print its state + * @return String state representation of the function + */ + String printState(LexicalUnitImpl function); +}