您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ScssStylesheet.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package com.vaadin.sass;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import org.w3c.css.sac.CSSException;
  7. import com.vaadin.sass.handler.SCSSDocumentHandler;
  8. import com.vaadin.sass.handler.SCSSDocumentHandlerImpl;
  9. import com.vaadin.sass.parser.Parser;
  10. import com.vaadin.sass.tree.Node;
  11. import com.vaadin.sass.visitor.BlockVisitor;
  12. import com.vaadin.sass.visitor.ExtendVisitor;
  13. import com.vaadin.sass.visitor.ImportVisitor;
  14. import com.vaadin.sass.visitor.MixinVisitor;
  15. import com.vaadin.sass.visitor.NestPropertiesVisitor;
  16. import com.vaadin.sass.visitor.ParentSelectorVisitor;
  17. import com.vaadin.sass.visitor.VariableVisitor;
  18. import com.vaadin.sass.visitor.Visitor;
  19. public class ScssStylesheet extends Node {
  20. private static final long serialVersionUID = 3849790204404961608L;
  21. /**
  22. * Read in a file SCSS and parse it into a ScssStylesheet
  23. *
  24. * @param file
  25. * @throws IOException
  26. */
  27. public ScssStylesheet() {
  28. super();
  29. }
  30. /**
  31. * Main entry point for the SASS compiler. Takes in a file and builds upp a
  32. * ScssStylesheet tree out of it. Calling compile() on it will transform
  33. * SASS into CSS. Calling toString() will print out the SCSS/CSS.
  34. *
  35. * @param file
  36. * @return
  37. * @throws CSSException
  38. * @throws IOException
  39. */
  40. public static ScssStylesheet get(File file) throws CSSException,
  41. IOException {
  42. Parser parser = new Parser();
  43. SCSSDocumentHandler handler = new SCSSDocumentHandlerImpl();
  44. file = file.getCanonicalFile();
  45. handler.getStyleSheet().setFileName(file.getAbsoluteFile().getParent());
  46. parser.setDocumentHandler(handler);
  47. parser.parseStyleSheet(file.getAbsolutePath());
  48. return handler.getStyleSheet();
  49. }
  50. /**
  51. * Applies all the visitors and compiles SCSS into Css.
  52. *
  53. * @throws Exception
  54. */
  55. public void compile() throws Exception {
  56. List<Visitor> visitors = new ArrayList<Visitor>();
  57. visitors.add(new ImportVisitor());
  58. visitors.add(new MixinVisitor());
  59. visitors.add(new VariableVisitor());
  60. visitors.add(new ParentSelectorVisitor());
  61. visitors.add(new BlockVisitor());
  62. visitors.add(new NestPropertiesVisitor());
  63. visitors.add(new ExtendVisitor());
  64. for (Visitor visitor : visitors) {
  65. visitor.traverse(this);
  66. }
  67. }
  68. /**
  69. * Prints out the current state of the node tree. Will return SCSS before
  70. * compile and CSS after.
  71. *
  72. * For now this is an own method with it's own implementation that most node
  73. * types will implement themselves.
  74. */
  75. @Override
  76. public String toString() {
  77. StringBuilder string = new StringBuilder("");
  78. if (children.size() > 0) {
  79. string.append(children.get(0).toString());
  80. }
  81. String delimeter = "\n\n";
  82. if (children.size() > 1) {
  83. for (int i = 1; i < children.size(); i++) {
  84. String childString = children.get(i).toString();
  85. if (childString != null) {
  86. string.append(delimeter).append(childString);
  87. }
  88. }
  89. }
  90. String output = string.toString();
  91. return output;
  92. }
  93. }