private void init() {
    Map<String, Element> containers = contentContainerFactory.list();
    for (String containerName : containers.keySet()) {
      List<String> containerStyles = new ArrayList<String>();
      styles.put(containerName, containerStyles);
      Element element = containers.get(containerName);
      List<JavaScriptRegExp> matchers = new ArrayList<JavaScriptRegExp>();
      if (element.hasAttribute("id")) {
        matchers.add(
            JavaScriptRegExp.create(
                "^\\s*#" + element.getAttribute("id") + "\\s+>?\\s*([^\\s,]+)"));
      }
      String[] cssClasseNames = element.getClassName().trim().split("\\s+");
      for (String cssClassName : cssClasseNames) {
        matchers.add(JavaScriptRegExp.create("^\\s*." + cssClassName + "\\s+>?\\s*([^\\s,]+)"));
      }

      JsArray<StyleSheet> styleSheets = StyleSheet.getAll();
      for (int i = 0; i < styleSheets.length(); i++) {
        StyleSheet styleSheet = styleSheets.get(i);
        if (styleSheet == null) continue;

        // In Chrome, if stylesheet originates from a different domain,
        // ss.cssRules simply won't exist. I believe the same is true for IE, but
        // I haven't tested it.
        //
        // In Firefox, if stylesheet originates from a different domain, trying
        // to access ss.cssRules will throw a SecurityError. Hence, we must use
        // try/catch to detect this condition in Firefox.
        JsArray<CSSStyleRule> cssRules;
        try {
          cssRules = styleSheet.cssRules();
        } catch (JavaScriptException e) {
          cssRules = null;
        }
        if (cssRules == null) continue;
        for (int j = 0; j < cssRules.length(); j++) {
          CSSStyleRule rule = cssRules.get(j);
          if (rule == null) continue;
          if (rule.selectorText() == null) continue;
          String[] selectors = rule.selectorText().split(",");
          for (String selector : selectors) {
            for (JavaScriptRegExp matcher : matchers) {
              JsArrayString matches = matcher.match(selector);
              if (matches != null
                  && matches.length() == 2
                  && !containerStyles.contains(matches.get(1))) {
                containerStyles.add(matches.get(1));
              }
            }
          }
        }
      }
    }
  }
Exemplo n.º 2
0
 /** Collect locations from template */
 private void scanForLocations(Element elem) {
   if (elem.hasAttribute("location")) {
     final String location = elem.getAttribute("location");
     locationToElement.put(location, elem);
     elem.setInnerHTML("");
   } else {
     final int len = DOM.getChildCount(elem);
     for (int i = 0; i < len; i++) {
       scanForLocations(DOM.getChild(elem, i));
     }
   }
 }
Exemplo n.º 3
0
  public String getAttribute(String key) {
    NodeList<Element> elements = rootElement.getElementsByTagName("attributes");
    assert elements.getLength() > 0;
    Element attributesNode = elements.getItem(0);

    NodeList<Node> childNodes = attributesNode.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
      Element node = (Element) childNodes.getItem(i);
      if (node.hasAttribute("name") && node.getAttribute("name").equals(key)) {
        return node.getAttribute("value");
      }
    }

    throw new RuntimeException("TaggedEntity does not have attribute '" + key + "'");
  }
Exemplo n.º 4
0
  public boolean hasAttribute(String key) {
    NodeList<Element> elements = rootElement.getElementsByTagName("attributes");
    assert elements.getLength() > 0;
    Element attributesNode = elements.getItem(0);

    NodeList<Node> childNodes = attributesNode.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
      Element node = (Element) childNodes.getItem(i);
      if (node.hasAttribute("name") && node.getAttribute("name").equals(key)) {
        return true;
      }
    }

    return false;
  }
Exemplo n.º 5
0
  /**
   * Adds a new key-value attribute pair or changes an existing one.
   *
   * @param key
   * @param value
   */
  public void setAttribute(String key, String value) {
    NodeList<Element> elements = rootElement.getElementsByTagName("attributes");
    assert elements.getLength() > 0;
    Element attributesNode = elements.getItem(0);

    NodeList<Node> childNodes = attributesNode.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
      Element node = (Element) childNodes.getItem(i);
      if (node.hasAttribute("name") && node.getAttribute("name").equals(key)) {
        node.setAttribute("value", value);
        return;
      }
    }

    /* if you get here, the rootElement wasn't found */
    Element element = JSFunctions.createPlainElement("attribute");
    element.setAttribute("name", key);
    element.setAttribute("value", value);
    attributesNode.insertAfter(element, attributesNode.getLastChild());
  }