CSSRule.java 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package de.tu_darmstadt.informatik.tk.scopviz.ui.css;
  2. import java.util.HashSet;
  3. import java.util.Iterator;
  4. /**
  5. * Stores a single Rule containing selectors and declarations. Offers a function
  6. * to check whether a CSSable matches one selector.
  7. *
  8. * @author Matthias Wilhelm
  9. */
  10. class CSSRule {
  11. /**
  12. * A Set to store all selectors for the this rule.
  13. */
  14. HashSet<CSSSelector> selectors = new HashSet<CSSSelector>();
  15. /**
  16. * A Set to store all declarations for the this rule.
  17. */
  18. HashSet<CSSDeclaration> declarations = new HashSet<CSSDeclaration>();
  19. /**
  20. * A String to store the declarations in a human readable form.
  21. */
  22. String css;
  23. /**
  24. *
  25. * @param selectors
  26. * @param declarations
  27. */
  28. CSSRule(HashSet<CSSSelector> selectors, HashSet<CSSDeclaration> declarations) {
  29. super();
  30. this.selectors = selectors;
  31. this.declarations = declarations;
  32. css = "";
  33. for (CSSDeclaration dc : declarations) {
  34. css = css.concat(dc.toString()).concat("; ");
  35. }
  36. css = css.trim();
  37. }
  38. /**
  39. * Checks whether a CSSable matches one selector.
  40. *
  41. * @param suspect
  42. * the CSSable to check
  43. * @return a positive integer if the condition is met. The more difficult
  44. * the rule was to meet, the greater the integer.
  45. */
  46. int ConditionsMetBy(CSSable suspect) {
  47. int result = 0;
  48. Iterator<CSSSelector> i = selectors.iterator();
  49. while (i.hasNext()) {
  50. CSSSelector condition = i.next();
  51. int r = -1;
  52. if (condition.ConditionsMetBy(suspect))
  53. r = condition.getValue();
  54. if (r > result)
  55. result = r;
  56. }
  57. return result;
  58. }
  59. /**
  60. *
  61. * @return all stored declarations
  62. */
  63. public HashSet<CSSDeclaration> getDeclarations() {
  64. return declarations;
  65. }
  66. @Override
  67. public String toString() {
  68. return selectors.toString().replace("[", "").replace("]", "") + " { " + css + " }";
  69. }
  70. }