]> source.dussan.org Git - vaadin-framework.git/commitdiff
Function compilation is refactored (#13253).
authordenisanisimov <denis@vaadin.com>
Tue, 28 Jan 2014 14:22:22 +0000 (16:22 +0200)
committerMika Murtojärvi <mika@vaadin.com>
Wed, 12 Feb 2014 11:28:36 +0000 (11:28 +0000)
Change-Id: Ic3c5d6822d90703d7f02de3d1a3d15fe4366af07

theme-compiler/src/com/vaadin/sass/internal/parser/LexicalUnitImpl.java
theme-compiler/src/com/vaadin/sass/internal/parser/function/AbsFunctionGenerator.java [new file with mode: 0644]
theme-compiler/src/com/vaadin/sass/internal/parser/function/CeilFunctionGenerator.java [new file with mode: 0644]
theme-compiler/src/com/vaadin/sass/internal/parser/function/DarkenFunctionGenerator.java [new file with mode: 0644]
theme-compiler/src/com/vaadin/sass/internal/parser/function/DefaultFunctionGenerator.java [new file with mode: 0644]
theme-compiler/src/com/vaadin/sass/internal/parser/function/FloorFunctionGenerator.java [new file with mode: 0644]
theme-compiler/src/com/vaadin/sass/internal/parser/function/LightenFunctionGenerator.java [new file with mode: 0644]
theme-compiler/src/com/vaadin/sass/internal/parser/function/RoundFunctionGenerator.java [new file with mode: 0644]
theme-compiler/src/com/vaadin/sass/internal/parser/function/SCSSFunctionGenerator.java [new file with mode: 0644]

index af1165adac8665b61f387d1d9b3451cf70212136..96f841de0b0b371ce01cfc91b4914881b616a6db 100644 (file)
 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<SCSSFunctionGenerator> initSerializers() {
+        List<SCSSFunctionGenerator> list = new LinkedList<SCSSFunctionGenerator>();
+        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<String, SCSSFunctionGenerator> SERIALIZERS = new HashMap<String, SCSSFunctionGenerator>();
+
+    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 (file)
index 0000000..7f88649
--- /dev/null
@@ -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 (file)
index 0000000..7d19734
--- /dev/null
@@ -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 (file)
index 0000000..2fbf0b2
--- /dev/null
@@ -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 (file)
index 0000000..d972a5b
--- /dev/null
@@ -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 (file)
index 0000000..57a0dcb
--- /dev/null
@@ -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 (file)
index 0000000..b911593
--- /dev/null
@@ -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 (file)
index 0000000..b8e8194
--- /dev/null
@@ -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 (file)
index 0000000..4d34b59
--- /dev/null
@@ -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 <code>null</code> 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);
+}