List of usage examples for com.google.gwt.dom.client NodeList getLength
public int getLength()
From source file:com.brazoft.foundation.gwt.client.component.ElementResolver.java
License:Apache License
public static Element getElementByTagName(Element parent, String tagName) { NodeList<Element> nodeList = parent.getElementsByTagName(tagName); if (nodeList.getLength() > 0) { return nodeList.getItem(0); }//from w w w . jav a 2s . c o m return null; }
From source file:com.brazoft.foundation.gwt.client.component.ElementResolver.java
License:Apache License
public static Element getChildByIdRec(Element parent, String id) { NodeIterable<Node> elements = new NodeIterable<Node>(parent.getChildNodes()); for (Node node : elements) { Element child = (Element) node; if (child != null && child.getId() != null) { if (child.getId().equals(id)) { return child; }/*from w ww . ja v a 2s. co m*/ NodeList<Node> childNodes = child.getChildNodes(); for (int cii = 0; cii < childNodes.getLength(); cii++) { Element child2 = (Element) childNodes.getItem(cii); Element childByIdRec = getChildByIdRec(child2, id); if (childByIdRec != null) { return childByIdRec; } } } } return null; }
From source file:com.calclab.emite.browser.client.DomAssist.java
License:Open Source License
/** * Remove all the childs of the given element * //from ww w . j a v a 2 s . c o m * @param element * the element */ public void clearElement(final Element element) { final NodeList<Node> childs = element.getChildNodes(); for (int index = 0, total = childs.getLength(); index < total; index++) { element.removeChild(childs.getItem(index)); } }
From source file:com.calclab.emite.browser.client.DomAssist.java
License:Open Source License
/** * Retrieves all the div on the page that matches the given class * /*from w ww. j a va 2s . c o m*/ * @param cssClass * the css class to match * @return a list of elements */ public ArrayList<Element> findElementsByClass(final String cssClass) { Log.debug("Finding elements div." + cssClass); final ArrayList<Element> elements = new ArrayList<Element>(); final Document document = Document.get(); final NodeList<Element> divs = document.getElementsByTagName("div"); for (int index = 0, total = divs.getLength(); index < total; index++) { final Element div = divs.getItem(index); final String[] classes = div.getClassName().split(" "); for (final String className : classes) { if (cssClass.equals(className)) { elements.add(div); } } } return elements; }
From source file:com.calclab.emite.browser.client.PageAssist.java
License:Open Source License
/** * Get the value of meta information writen in the html page. The meta * information is a html tag with name of meta usually placed inside the the * head section with two attributes: id and content. For example: * // ww w . java 2 s.c o m * <code><meta name="name" value="userName" /></code> * * @param id * the 'id' value of the desired meta tag * @return the value of the attribute 'value' or null if not found */ public static final String getMeta(final String id) { String value = null; Element element = null; final NodeList<Element> elements = Document.get().getElementsByTagName("meta"); if (elements != null) { for (int i = 0; i < elements.getLength() && element == null; i++) { final Element candidate = elements.getItem(i); if (id.equals(candidate.getAttribute("name"))) { element = candidate; } } } if (element == null) { element = DOM.getElementById(id); } if (element != null) { value = element.getPropertyString("content"); } return value; }
From source file:com.calclab.emite.browser.PageAssist.java
License:Open Source License
/** * Get the value of meta information written in the html page. The meta * information is a html tag with name of meta usually placed inside the the * head section with two attributes: id and content. For example: * /*www . j a v a 2 s . c o m*/ * <code><meta name="name" value="userName" /></code> * * @param id * the 'id' value of the desired meta tag * @param defaultValue * the default value to return if the meta is not found * @return the value of the attribute 'value' or null if not found */ @Nullable public static final String getMeta(final String id, @Nullable final String defaultValue) { checkNotNull(id); final NodeList<Element> elements = Document.get().getElementsByTagName("meta"); for (int i = 0; i < elements.getLength(); i++) { final Element candidate = elements.getItem(i); if (id.equals(candidate.getAttribute("name"))) return candidate.getPropertyString("content"); } final Element domElement = DOM.getElementById(id); if (domElement != null) return domElement.getPropertyString("content"); return defaultValue; }
From source file:com.centimia.gxt.sample.widgetdashboard.client.WidgetDashboard.java
License:Open Source License
public void onModuleLoad() { String styleSheetName = "resources/css/gxt-all.css"; if (LocaleInfo.getCurrentLocale().isRTL()) { styleSheetName = "resources/css/gxt-all-rtl.css"; }/* w w w . j a v a 2s . co m*/ // Find existing style sheets that need to be removed boolean styleSheetsFound = false; final HeadElement headElem = StyleSheetLoader.getHeadElement(); final List<Element> toRemove = new ArrayList<Element>(); NodeList<Node> children = headElem.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node node = children.getItem(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element elem = Element.as(node); if (elem.getTagName().equalsIgnoreCase("link") && elem.getPropertyString("rel").equalsIgnoreCase("stylesheet")) { styleSheetsFound = true; String href = elem.getPropertyString("href"); // If the correct style sheets are already loaded, then we should have // nothing to remove. if (!href.equals(styleSheetName) && href.indexOf("gxt") != -1) { toRemove.add(elem); } } } } // Return if we already have the correct style sheets if (styleSheetsFound && toRemove.size() != 0) { // Remove the old style sheets for (Element elem : toRemove) { headElem.removeChild(elem); } } ExampleServiceAsync service = (ExampleServiceAsync) GWT.create(ExampleService.class); ServiceDefTarget endpoint = (ServiceDefTarget) service; String moduleRelativeURL = SERVICE; endpoint.setServiceEntryPoint(moduleRelativeURL); Registry.register(SERVICE, service); FileServiceAsync fileservice = (FileServiceAsync) GWT.create(FileService.class); endpoint = (ServiceDefTarget) fileservice; moduleRelativeURL = FILE_SERVICE; endpoint.setServiceEntryPoint(moduleRelativeURL); Registry.register(FILE_SERVICE, fileservice); ExamplesModel model = new ExamplesModel(); for (int i = 0; i < model.getChildren().size(); i++) { Category cat = (Category) model.getChildren().get(i); for (int j = 0; j < cat.getChildren().size(); j++) { Entry entry = (Entry) cat.getChildren().get(j); examples.put(entry.getId(), entry); } } Registry.register(MODEL, model); viewport = new Viewport(); BorderLayout layout = new BorderLayout(); demoPanel = new ContentPanel(); demoPanel.setLayout(new BorderLayout()); viewport.setLayout(layout); createNorth(); createTree(); panel.setResizeTabs(true); panel.setMinTabWidth(115); panel.setAnimScroll(true); panel.setTabScroll(true); panel.setCloseContextMenu(true); replaceView("basicgrid"); demoPanel.add(panel, new BorderLayoutData(LayoutRegion.CENTER)); viewport.add(tree, new BorderLayoutData(LayoutRegion.WEST)); viewport.add(demoPanel, new BorderLayoutData(LayoutRegion.CENTER)); StyleSheetLoader.loadStyleSheet(styleSheetName, "ext-el-mask", this); }
From source file:com.cgxlib.xq.client.impl.research.SelectorEngineJS.java
License:Apache License
private static void getDescendantNodes(JsNodeArray matchingElms, String nextTagStr, Node prevRef) { NodeList<Element> children = getElementsByTagName(nextTagStr, prevRef); for (int k = 0, klen = children.getLength(); k < klen; k++) { Node child = children.getItem(k); if (child.getParentNode() == prevRef) { matchingElms.addNode(child); }/*from ww w . ja v a 2 s .co m*/ } }
From source file:com.cgxlib.xq.client.impl.research.SelectorEngineJS.java
License:Apache License
public NodeList<Element> select(String sel, Node ctx) { String selectors[] = sel.replace("\\s*(,)\\s*", "$1").split(","); boolean identical = false; JsNodeArray elm = JsNodeArray.create(); for (int a = 0, len = selectors.length; a < len; a++) { if (a > 0) { identical = false;/*from ww w . j a va 2 s .c o m*/ for (int b = 0, bl = a; b < bl; b++) { if (JsUtils.eq(selectors[a], selectors[b])) { identical = true; break; } } if (identical) { continue; } } String currentRule = selectors[a]; JsObjectArray<String> cssSelectors = selectorSplitRegExp.match(currentRule); JsNodeArray prevElem = JsNodeArray.create(ctx); for (int i = 0, slen = cssSelectors.length(); i < slen; i++) { JsNodeArray matchingElms = JsNodeArray.create(); String rule = cssSelectors.get(i); if (i > 0 && childOrSiblingRefRegExp.test(rule)) { JsObjectArray<String> childOrSiblingRef = childOrSiblingRefRegExp.exec(rule); if (JsUtils.truth(childOrSiblingRef)) { JsObjectArray<String> nextTag = new JsRegexp("^\\w+").exec(cssSelectors.get(i + 1)); JsRegexp nextRegExp = null; String nextTagStr = null; if (JsUtils.truth(nextTag)) { nextTagStr = nextTag.get(0); nextRegExp = new JsRegexp("(^|\\s)" + nextTagStr + "(\\s|$)", "i"); } for (int j = 0, jlen = prevElem.size(); j < jlen; j++) { Node prevRef = prevElem.getNode(j); String ref = childOrSiblingRef.get(0); if (JsUtils.eq(">", ref)) { getDescendantNodes(matchingElms, nextTagStr, prevRef); } else if (JsUtils.eq("+", ref)) { getSiblingNodes(matchingElms, nextTag, nextRegExp, prevRef); } else if (JsUtils.eq("~", ref)) { getGeneralSiblingNodes(matchingElms, nextTag, nextRegExp, prevRef); } } prevElem = matchingElms; clearAdded(prevElem); rule = cssSelectors.get(++i); if (new JsRegexp("^\\w+$").test(rule)) { continue; } setSkipTag(prevElem, true); } } JsObjectArray<String> cssSelector = cssSelectorRegExp.exec(rule); SplitRule splitRule = new SplitRule( !JsUtils.truth(cssSelector.get(1)) || JsUtils.eq(cssSelector.get(3), "*") ? "*" : cssSelector.get(1), !JsUtils.eq(cssSelector.get(3), "*") ? cssSelector.get(2) : null, cssSelector.get(4), cssSelector.get(6), cssSelector.get(10)); if (JsUtils.truth(splitRule.id)) { Element domelem = Document.get().getElementById(splitRule.id.substring(1)); if (JsUtils.truth(domelem)) { matchingElms = JsNodeArray.create(domelem); } prevElem = matchingElms; } else if (JsUtils.truth(splitRule.tag) && !isSkipped(prevElem)) { if (i == 0 && matchingElms.size() == 0 && prevElem.size() == 1) { prevElem = matchingElms = JsNodeArray .create(getElementsByTagName(splitRule.tag, prevElem.getNode(0))); } else { NodeList<Element> tagCollectionMatches; for (int l = 0, ll = prevElem.size(); l < ll; l++) { tagCollectionMatches = getElementsByTagName(splitRule.tag, prevElem.getNode(l)); for (int m = 0, mlen = tagCollectionMatches.getLength(); m < mlen; m++) { Node tagMatch = tagCollectionMatches.getItem(m); if (!isAdded(tagMatch)) { setAdded(tagMatch, true); matchingElms.addNode(tagMatch); } } } prevElem = matchingElms; clearAdded(prevElem); } if (matchingElms.size() == 0) { break; } setSkipTag(prevElem, false); if (JsUtils.truth(splitRule.allClasses)) { String[] allClasses = splitRule.allClasses.replaceFirst("^\\.", "").split("\\."); JsRegexp[] regExpClassNames = new JsRegexp[allClasses.length]; for (int n = 0, nl = allClasses.length; n < nl; n++) { regExpClassNames[n] = new JsRegexp("(^|\\s)" + allClasses[n] + "(\\s|$)"); } JsNodeArray matchingClassElms = JsNodeArray.create(); for (int o = 0, olen = prevElem.size(); o < olen; o++) { Element current = prevElem.getElement(o); String elmClass = current.getClassName(); boolean addElm = false; if (JsUtils.truth(elmClass) && !isAdded(current)) { for (int p = 0, pl = regExpClassNames.length; p < pl; p++) { addElm = regExpClassNames[p].test(elmClass); if (!addElm) { break; } } if (addElm) { setAdded(current, true); matchingClassElms.addNode(current); } } } clearAdded(prevElem); prevElem = matchingElms = matchingClassElms; } if (JsUtils.truth(splitRule.allAttr)) { JsObjectArray<String> allAttr = JsRegexp.match("\\[[^\\]]+\\]", "g", splitRule.allAttr); JsRegexp[] regExpAttributes = new JsRegexp[allAttr.length()]; String[] regExpAttributesStr = new String[allAttr.length()]; JsRegexp attributeMatchRegExp = new JsRegexp( "(\\w+)(\\^|\\$|\\*|\\||~)?=?[\"']?([\\w\u00C0-\uFFFF\\s\\-_\\.]+)?"); for (int q = 0, ql = allAttr.length(); q < ql; q++) { JsObjectArray<String> attributeMatch = attributeMatchRegExp.exec(allAttr.get(q)); String attributeValue = JsUtils.truth(attributeMatch.get(3)) ? attributeMatch.get(3).replaceAll("\\.", "\\.") : null; String attrVal = attrToRegExp(attributeValue, (JsUtils.or(attributeMatch.get(2), null))); regExpAttributes[q] = (JsUtils.truth(attrVal) ? new JsRegexp(attrVal) : null); regExpAttributesStr[q] = attributeMatch.get(1); } JsNodeArray matchingAttributeElms = JsNodeArray.create(); for (int r = 0, rlen = matchingElms.size(); r < rlen; r++) { Element current = matchingElms.getElement(r); boolean addElm = false; for (int s = 0, sl = regExpAttributes.length; s < sl; s++) { addElm = false; JsRegexp attributeRegexp = regExpAttributes[s]; String currentAttr = getAttr(current, regExpAttributesStr[s]); if (JsUtils.truth(currentAttr) && currentAttr.length() != 0) { if (attributeRegexp == null || attributeRegexp.test(currentAttr)) { addElm = true; } } if (!addElm) { break; } } if (addElm) { matchingAttributeElms.addNode(current); } } prevElem = matchingElms = matchingAttributeElms; } if (JsUtils.truth(splitRule.allPseudos)) { JsRegexp pseudoSplitRegExp = new JsRegexp(":(\\w[\\w\\-]*)(\\(([^\\)]+)\\))?"); JsObjectArray<String> allPseudos = JsRegexp.match("(:\\w+[\\w\\-]*)(\\([^\\)]+\\))?", "g", splitRule.allPseudos); for (int t = 0, tl = allPseudos.length(); t < tl; t++) { JsObjectArray<String> pseudo = pseudoSplitRegExp.match(allPseudos.get(t)); String pseudoClass = JsUtils.truth(pseudo.get(1)) ? pseudo.get(1).toLowerCase() : null; String pseudoValue = JsUtils.truth(pseudo.get(3)) ? pseudo.get(3) : null; matchingElms = getElementsByPseudo(matchingElms, pseudoClass, pseudoValue); clearAdded(matchingElms); } prevElem = matchingElms; } } } elm.pushAll(prevElem); } return JsUtils.unique(elm.<JsArray<Element>>cast()).cast(); }
From source file:com.cgxlib.xq.client.impl.SelectorEngine.java
License:Apache License
public NodeList<Element> filter(NodeList<Element> nodes, Predicate p) { JsNodeArray res = JsNodeArray.create(); for (int i = 0, l = nodes.getLength(), j = 0; i < l; i++) { Element e = nodes.getItem(i); if (p.f(e, i)) { res.addNode(e, j++);//from w w w . j a va2 s .c o m } } return res; }