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;
/**
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();
}
}
+
+ 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);
+ }
+ }
}
--- /dev/null
+/*
+ * 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();
+ }
+
+}
--- /dev/null
+/*
+ * 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();
+ }
+
+}
--- /dev/null
+/*
+ * 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();
+ }
+}
--- /dev/null
+/*
+ * 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();
+ }
+}
--- /dev/null
+/*
+ * 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();
+ }
+
+}
--- /dev/null
+/*
+ * 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();
+ }
+
+}
--- /dev/null
+/*
+ * 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();
+ }
+
+}
--- /dev/null
+/*
+ * 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);
+}