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.

SCSSDocumentHandlerImpl.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. package com.vaadin.sass.handler;
  2. import java.util.Collection;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Stack;
  6. import org.w3c.css.sac.CSSException;
  7. import org.w3c.css.sac.InputSource;
  8. import org.w3c.css.sac.LexicalUnit;
  9. import org.w3c.css.sac.SACMediaList;
  10. import org.w3c.css.sac.SelectorList;
  11. import com.vaadin.sass.ScssStylesheet;
  12. import com.vaadin.sass.tree.BlockNode;
  13. import com.vaadin.sass.tree.EachNode;
  14. import com.vaadin.sass.tree.ExtendNode;
  15. import com.vaadin.sass.tree.ForNode;
  16. import com.vaadin.sass.tree.IfNode;
  17. import com.vaadin.sass.tree.ImportNode;
  18. import com.vaadin.sass.tree.MediaNode;
  19. import com.vaadin.sass.tree.MixinDefNode;
  20. import com.vaadin.sass.tree.MixinNode;
  21. import com.vaadin.sass.tree.NestPropertiesNode;
  22. import com.vaadin.sass.tree.Node;
  23. import com.vaadin.sass.tree.RuleNode;
  24. import com.vaadin.sass.tree.VariableNode;
  25. import com.vaadin.sass.tree.WhileNode;
  26. public class SCSSDocumentHandlerImpl implements SCSSDocumentHandler {
  27. private final ScssStylesheet styleSheet;
  28. Stack<Node> nodeStack = new Stack<Node>();
  29. private Map<String, Stack<LexicalUnit>> variableMap;
  30. public SCSSDocumentHandlerImpl() {
  31. this(new ScssStylesheet());
  32. variableMap = new HashMap<String, Stack<LexicalUnit>>();
  33. }
  34. public SCSSDocumentHandlerImpl(ScssStylesheet styleSheet) {
  35. this.styleSheet = styleSheet;
  36. nodeStack.push(styleSheet);
  37. }
  38. public void addVariable(String varName, LexicalUnit value) {
  39. if (variableMap.get(varName) == null) {
  40. variableMap.put(varName, new Stack<LexicalUnit>());
  41. }
  42. Stack<LexicalUnit> valueStack = variableMap.get(varName);
  43. valueStack.push(value);
  44. }
  45. public void removeVaraible(String varName) {
  46. Stack<LexicalUnit> valueStack = variableMap.get(varName);
  47. if (valueStack != null && !valueStack.isEmpty()) {
  48. valueStack.pop();
  49. }
  50. }
  51. public LexicalUnit getVariable(String varName) {
  52. Stack<LexicalUnit> valueStack = variableMap.get(varName);
  53. if (valueStack != null && !valueStack.isEmpty()) {
  54. return valueStack.peek();
  55. }
  56. return null;
  57. }
  58. @Override
  59. public ScssStylesheet getStyleSheet() {
  60. return styleSheet;
  61. }
  62. @Override
  63. public void startDocument(InputSource source) throws CSSException {
  64. nodeStack.push(styleSheet);
  65. // System.out.println("startDocument(InputSource source): "
  66. // + source.getURI());
  67. }
  68. @Override
  69. public void endDocument(InputSource source) throws CSSException {
  70. // System.out.println("endDocument(InputSource source): "
  71. // + source.getURI());
  72. }
  73. @Override
  74. public void variable(String name, LexicalUnit value, boolean guarded) {
  75. VariableNode node = new VariableNode(name, value, guarded);
  76. nodeStack.peek().appendChild(node);
  77. }
  78. @Override
  79. public void debugDirective() {
  80. }
  81. @Override
  82. public ForNode forDirective(String var, String from, String to,
  83. boolean exclusive, String body) {
  84. ForNode node = new ForNode(var, from, to, exclusive, body);
  85. System.out.println(node);
  86. return node;
  87. }
  88. @Override
  89. public EachNode eachDirective(String var, String list, String body) {
  90. EachNode node = new EachNode(var, list, body);
  91. System.out.println(node);
  92. return node;
  93. }
  94. @Override
  95. public WhileNode whileDirective(String condition, String body) {
  96. WhileNode node = new WhileNode(condition, body);
  97. System.out.println(node);
  98. return node;
  99. }
  100. @Override
  101. public IfNode ifDirective() {
  102. return new IfNode();
  103. }
  104. @Override
  105. public void comment(String text) throws CSSException {
  106. System.out.println("comment(String text): " + text);
  107. }
  108. @Override
  109. public void ignorableAtRule(String atRule) throws CSSException {
  110. System.out.println("ignorableAtRule(String atRule): " + atRule);
  111. }
  112. @Override
  113. public void namespaceDeclaration(String prefix, String uri)
  114. throws CSSException {
  115. System.out.println("namespaceDeclaration(String prefix, String uri): "
  116. + prefix + ", " + uri);
  117. }
  118. @Override
  119. public void importStyle(String uri, SACMediaList media,
  120. String defaultNamespaceURI) throws CSSException {
  121. }
  122. @Override
  123. public void startMedia(SACMediaList media) throws CSSException {
  124. MediaNode node = new MediaNode(media);
  125. nodeStack.peek().appendChild(node);
  126. nodeStack.push(node);
  127. }
  128. @Override
  129. public void endMedia(SACMediaList media) throws CSSException {
  130. nodeStack.pop();
  131. }
  132. @Override
  133. public void startPage(String name, String pseudo_page) throws CSSException {
  134. System.out.println("startPage(String name, String pseudo_page): "
  135. + name + ", " + pseudo_page);
  136. }
  137. @Override
  138. public void endPage(String name, String pseudo_page) throws CSSException {
  139. System.out.println("endPage(String name, String pseudo_page): " + name
  140. + ", " + pseudo_page);
  141. }
  142. @Override
  143. public void startFontFace() throws CSSException {
  144. System.out.println("startFontFace()");
  145. }
  146. @Override
  147. public void endFontFace() throws CSSException {
  148. System.out.println("endFontFace()");
  149. }
  150. @Override
  151. public void startSelector(SelectorList selectors) throws CSSException {
  152. BlockNode node = new BlockNode(selectors);
  153. nodeStack.peek().appendChild(node);
  154. nodeStack.push(node);
  155. }
  156. @Override
  157. public void endSelector(SelectorList selectors) throws CSSException {
  158. nodeStack.pop();
  159. }
  160. @Override
  161. public void property(String name, LexicalUnit value, boolean important)
  162. throws CSSException {
  163. property(name, value, important, null);
  164. }
  165. public void property(String name, LexicalUnit value, boolean important,
  166. String comment) {
  167. RuleNode node = new RuleNode(name, value, important, comment);
  168. nodeStack.peek().appendChild(node);
  169. }
  170. @Override
  171. public void extendDirective(SelectorList list) {
  172. ExtendNode node = new ExtendNode(list);
  173. nodeStack.peek().appendChild(node);
  174. }
  175. @Override
  176. public MixinDefNode mixinDirective(String name, String args, String body) {
  177. MixinDefNode node = new MixinDefNode(name, args, body);
  178. return node;
  179. }
  180. @Override
  181. public void startNestedProperties(String name) {
  182. NestPropertiesNode node = new NestPropertiesNode(name);
  183. nodeStack.peek().appendChild(node);
  184. nodeStack.push(node);
  185. }
  186. @Override
  187. public void endNestedProperties(String name) {
  188. nodeStack.pop();
  189. }
  190. @Override
  191. public void startMixinDirective(String name, Collection<VariableNode> args) {
  192. MixinDefNode node = new MixinDefNode(name, args);
  193. nodeStack.peek().appendChild(node);
  194. nodeStack.push(node);
  195. }
  196. @Override
  197. public void endMixinDirective(String name, Collection<VariableNode> args) {
  198. nodeStack.pop();
  199. }
  200. @Override
  201. public void includeDirective(String name, Collection<LexicalUnit> args) {
  202. MixinNode node = new MixinNode(name, args);
  203. nodeStack.peek().appendChild(node);
  204. }
  205. @Override
  206. public void importStyle(String uri, SACMediaList media, boolean isURL) {
  207. ImportNode node = new ImportNode(uri, media, isURL);
  208. nodeStack.peek().appendChild(node);
  209. }
  210. }