You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VariableNode.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright 2000-2013 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.sass.internal.tree;
  17. import java.util.ArrayList;
  18. import com.vaadin.sass.internal.ScssStylesheet;
  19. import com.vaadin.sass.internal.expression.ArithmeticExpressionEvaluator;
  20. import com.vaadin.sass.internal.parser.LexicalUnitImpl;
  21. import com.vaadin.sass.internal.util.StringUtil;
  22. import com.vaadin.sass.internal.visitor.VariableNodeHandler;
  23. public class VariableNode extends Node implements IVariableNode {
  24. private static final long serialVersionUID = 7003372557547748734L;
  25. private String name;
  26. private LexicalUnitImpl expr;
  27. private boolean guarded;
  28. public VariableNode(String name, LexicalUnitImpl expr, boolean guarded) {
  29. super();
  30. this.name = name;
  31. this.expr = expr;
  32. this.guarded = guarded;
  33. }
  34. public LexicalUnitImpl getExpr() {
  35. return expr;
  36. }
  37. public void setExpr(LexicalUnitImpl expr) {
  38. this.expr = expr;
  39. }
  40. public void setName(String name) {
  41. this.name = name;
  42. }
  43. public String getName() {
  44. return name;
  45. }
  46. public boolean isGuarded() {
  47. return guarded;
  48. }
  49. @Override
  50. public String toString() {
  51. StringBuilder builder = new StringBuilder("$");
  52. builder.append(name).append(": ").append(expr);
  53. return builder.toString();
  54. }
  55. public void setGuarded(boolean guarded) {
  56. this.guarded = guarded;
  57. }
  58. @Override
  59. public void replaceVariables(ArrayList<VariableNode> variables) {
  60. for (final VariableNode node : variables) {
  61. if (!equals(node)) {
  62. if (StringUtil
  63. .containsVariable(expr.toString(), node.getName())) {
  64. if (expr.getParameters() != null
  65. && StringUtil.containsVariable(expr.getParameters()
  66. .toString(), node.getName())) {
  67. replaceValues(expr.getParameters(), node);
  68. } else if (expr.getLexicalUnitType() == LexicalUnitImpl.SCSS_VARIABLE) {
  69. replaceValues(expr, node);
  70. }
  71. }
  72. }
  73. }
  74. }
  75. private void replaceValues(LexicalUnitImpl unit, VariableNode node) {
  76. while (unit != null) {
  77. if (unit.getLexicalUnitType() == LexicalUnitImpl.SCSS_VARIABLE
  78. && unit.getValue().toString().equals(node.getName())) {
  79. LexicalUnitImpl.replaceValues(unit, node.getExpr());
  80. }
  81. unit = unit.getNextLexicalUnit();
  82. }
  83. }
  84. @Override
  85. public void traverse() {
  86. /*
  87. * "replaceVariables(ScssStylesheet.getVariables());" seems duplicated
  88. * and can be extracted out of if, but it is not.
  89. * containsArithmeticalOperator must be called before replaceVariables.
  90. * Because for the "/" operator, it needs to see if its predecessor or
  91. * successor is a Variable or not, to determine it is an arithmetic
  92. * operator.
  93. */
  94. if (ArithmeticExpressionEvaluator.get().containsArithmeticalOperator(
  95. expr)) {
  96. replaceVariables(ScssStylesheet.getVariables());
  97. expr = ArithmeticExpressionEvaluator.get().evaluate(expr);
  98. } else {
  99. replaceVariables(ScssStylesheet.getVariables());
  100. }
  101. VariableNodeHandler.traverse(this);
  102. }
  103. }