CSSManager.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. // TODO implement
  45. // <Property, <CSSValue, RuleValue>>
  46. HashMap<String, CSSValueValue> cssDeclarations = new HashMap<>();
  47. for (CSSRule r : rules) {
  48. int ruleValue = r.ConditionsMetBy(ca);
  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) || ruleValue >= cssDeclarations.get(property).getRuleValue())
  54. cssDeclarations.put(property, new CSSValueValue(value, ruleValue));
  55. }
  56. }
  57. String result = "";
  58. for (String key : cssDeclarations.keySet()) {
  59. result = result.concat(key).concat(": ").concat(cssDeclarations.get(key).getCssValue()).concat("; ");
  60. }
  61. return result.trim();
  62. }
  63. // TODO comment
  64. private static void updateCSSAble() {
  65. for (CSSable ca : cssAbles)
  66. ca.updateCSS();
  67. }
  68. // TODO comment
  69. private static CSSRule extractRule(String s) {
  70. String[] sArray = s.trim().split("\\{");
  71. return new CSSRule(extractSelectors(sArray[0]), parseCss(sArray[1]));
  72. }
  73. // TODO comment
  74. private static HashSet<CSSSelector> extractSelectors(String s) {
  75. HashSet<CSSSelector> selectors = new HashSet<>();
  76. String[] sArray = s.trim().split("\\,");
  77. for (String selecteor : sArray) {
  78. selectors.add(extractSelector(selecteor));
  79. }
  80. return selectors;
  81. }
  82. // TODO comment
  83. private static CSSSelector extractSelector(String s) {
  84. HashSet<String> classes = new HashSet<String>();
  85. String[] sArray = s.trim().split("\\.");
  86. for (int i = 1; i < sArray.length; i++) {
  87. classes.add(sArray[i]);
  88. }
  89. return new CSSSelector(sArray[0], classes);
  90. }
  91. // TODO comment
  92. private static HashSet<CSSDeclaration> parseCss(String s) {
  93. HashSet<CSSDeclaration> declarations = new HashSet<CSSDeclaration>();
  94. String[] sArray = s.trim().split("\\;");
  95. for (int i = 0; i < sArray.length; i++) {
  96. CSSDeclaration cssDeclaration = parseCssStatement(sArray[i]);
  97. String property = cssDeclaration.getProperty();
  98. for (CSSDeclaration cd : declarations) {
  99. if (property.equals(cd.getProperty())) {
  100. declarations.remove(cd);
  101. break;
  102. }
  103. }
  104. declarations.add(cssDeclaration);
  105. }
  106. return declarations;
  107. }
  108. // TODO comment
  109. private static CSSDeclaration parseCssStatement(String s) {
  110. String[] sArray = s.trim().split("\\:");
  111. return new CSSDeclaration(sArray[0], sArray[1]);
  112. }
  113. }