CSSManager.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package de.tu_darmstadt.informatik.tk.scopviz.ui.css;
  2. import java.util.HashMap;
  3. import java.util.HashSet;
  4. import de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
  5. public class CSSManager {
  6. /**
  7. * Du zerstörst diesen REGEX und Matthias zerstört dich
  8. */
  9. // TODO comment
  10. private static final String CSS_MATCH_REGEX = "(\\s*([A-Za-z]+|[A-Za-z]*(\\.[A-Za-z_-]*)+\\s*\\,?)*\\s*\\{(\\s*[A-Za-z_-]+\\s*\\:\\s*[0-9A-Za-z\\(\\)\\_\\#\\'\\\"\\,\\s-]+\\s*\\;?)+\\s*\\})+\\s*";
  11. // TODO comment
  12. static HashSet<CSSRule> rules = new HashSet<CSSRule>();
  13. // TODO comment
  14. private static HashSet<CSSable> cssAbles = new HashSet<CSSable>();
  15. // TODO comment
  16. public static void addRule(String rule) {
  17. addRule(rule, true);
  18. }
  19. // TODO comment
  20. public static void addRule(String rule, boolean updateCSSable) {
  21. if (!rule.matches(CSS_MATCH_REGEX)) {
  22. Debug.out("rule << " + rule + " >> doesn't match regex");
  23. return;
  24. }
  25. String[] sArray = rule.trim().split("\\}");
  26. for (String s : sArray) {
  27. CSSRule newRule = extractRule(s);
  28. rules.add(newRule);
  29. Debug.out("<< " + newRule.toString() + " >> added.");
  30. }
  31. if (updateCSSable)
  32. updateCSSAble();
  33. }
  34. // TODO comment
  35. public static void addCSSAble(CSSable ca) {
  36. cssAbles.add(ca);
  37. }
  38. // TODO comment
  39. public static void removeCSSAble(CSSable ca) {
  40. cssAbles.remove(ca);
  41. }
  42. // TODO comment
  43. public static String getCSS(CSSable ca) {
  44. // <Property, <CSSValue, RuleValue>>
  45. HashMap<String, CSSValueValue> cssDeclarations = new HashMap<>();
  46. for (CSSRule r : rules) {
  47. int ruleValue = r.ConditionsMetBy(ca);
  48. if (ruleValue > 0) {
  49. HashSet<CSSDeclaration> declarations = r.getDeclarations();
  50. for (CSSDeclaration d : declarations) {
  51. String property = d.getProperty();
  52. String value = d.getValue();
  53. if (!cssDeclarations.containsKey(property)
  54. || ruleValue >= cssDeclarations.get(property).getRuleValue())
  55. cssDeclarations.put(property, new CSSValueValue(value, ruleValue));
  56. }
  57. }
  58. }
  59. String result = "";
  60. for (String key : cssDeclarations.keySet()) {
  61. result = result.concat(key).concat(": ").concat(cssDeclarations.get(key).getCssValue()).concat("; ");
  62. }
  63. return result.trim();
  64. }
  65. // TODO comment
  66. private static void updateCSSAble() {
  67. for (CSSable ca : cssAbles)
  68. ca.updateCSS();
  69. }
  70. // TODO comment
  71. private static CSSRule extractRule(String s) {
  72. String[] sArray = s.trim().split("\\{");
  73. return new CSSRule(extractSelectors(sArray[0]), parseCss(sArray[1]));
  74. }
  75. // TODO comment
  76. private static HashSet<CSSSelector> extractSelectors(String s) {
  77. HashSet<CSSSelector> selectors = new HashSet<>();
  78. String[] sArray = s.trim().split("\\,");
  79. for (String selecteor : sArray) {
  80. selectors.add(extractSelector(selecteor));
  81. }
  82. return selectors;
  83. }
  84. // TODO comment
  85. private static CSSSelector extractSelector(String s) {
  86. HashSet<String> classes = new HashSet<String>();
  87. String[] sArray = s.trim().split("\\.");
  88. for (int i = 1; i < sArray.length; i++) {
  89. classes.add(sArray[i]);
  90. }
  91. return new CSSSelector(sArray[0], classes);
  92. }
  93. // TODO comment
  94. private static HashSet<CSSDeclaration> parseCss(String s) {
  95. HashSet<CSSDeclaration> declarations = new HashSet<CSSDeclaration>();
  96. String[] sArray = s.trim().split("\\;");
  97. for (int i = 0; i < sArray.length; i++) {
  98. CSSDeclaration cssDeclaration = parseCssStatement(sArray[i]);
  99. String property = cssDeclaration.getProperty();
  100. for (CSSDeclaration cd : declarations) {
  101. if (property.equals(cd.getProperty())) {
  102. declarations.remove(cd);
  103. break;
  104. }
  105. }
  106. declarations.add(cssDeclaration);
  107. }
  108. return declarations;
  109. }
  110. // TODO comment
  111. private static CSSDeclaration parseCssStatement(String s) {
  112. String[] sArray = s.trim().split("\\:");
  113. return new CSSDeclaration(sArray[0], sArray[1]);
  114. }
  115. }