List of usage examples for com.google.gwt.dom.client NodeList getItem
public T getItem(int index)
From source file:org.vectomatic.svg.edit.client.engine.SVGModel.java
License:Open Source License
/** * Completes the specified SVG subtree so that all the proper SVG elements have * an associated title and desc elements * @param root The SVG subtree to complete *//*from w w w . j a v a 2 s . c o m*/ public void createTitleDesc(SVGElement root) { // Perform a DFS of the tree to collect all the SVG elements // which can have a title and desc but do not yet have one List<SVGElement> incompleteElements = new ArrayList<SVGElement>(); Stack<SVGElement> stack = new Stack<SVGElement>(); stack.push(root); while (stack.size() > 0) { SVGElement element = stack.pop(); if (hasTitleDesc(element) && (DOMHelper.evaluateNodeXPath(element, TITLE_PROPERTY.getXPath(), SVGPrefixResolver.INSTANCE) == null || DOMHelper.evaluateNodeXPath(element, DESC_PROPERTY.getXPath(), SVGPrefixResolver.INSTANCE) == null)) { incompleteElements.add(element); } NodeList<Node> childNodes = element.getChildNodes(); for (int i = 0, length = childNodes.getLength(); i < length; i++) { Node child = childNodes.getItem(i); if (child.getNodeType() == Node.ELEMENT_NODE) { Element childElement = child.cast(); if (SVGConstants.SVG_NAMESPACE_URI.equals(DOMHelper.getNamespaceURI(childElement))) { stack.push((SVGElement) childElement.cast()); } } } } // Create the missing desc and titles SVGDocument document = root.getOwnerDocument().cast(); for (SVGElement element : incompleteElements) { SVGTitleElement title = DOMHelper.evaluateNodeXPath(element, TITLE_PROPERTY.getXPath(), SVGPrefixResolver.INSTANCE); if (title == null) { title = DOMHelper .createElementNS(document, SVGConstants.SVG_NAMESPACE_URI, SVGConstants.SVG_TITLE_TAG) .cast(); title.appendChild(document.createTextNode(generateName(element))); Node firstChild = element.getFirstChild(); if (firstChild == null) { element.appendChild(title); } else { element.insertBefore(title, firstChild); } } SVGDescElement desc = DOMHelper.evaluateNodeXPath(element, DESC_PROPERTY.getXPath(), SVGPrefixResolver.INSTANCE); if (desc == null) { desc = DOMHelper .createElementNS(document, SVGConstants.SVG_NAMESPACE_URI, SVGConstants.SVG_DESC_TAG) .cast(); desc.appendChild(document.createTextNode("")); element.insertAfter(desc, title); } } }
From source file:org.vectomatic.svg.edit.client.engine.SVGProcessor.java
License:Open Source License
public static int normalizeIds(OMSVGElement srcSvg) { docId++;// w ww. j a v a 2 s . com // Collect all the original element ids and replace them with a // normalized id int idIndex = 0; Map<String, Element> idToElement = new HashMap<String, Element>(); Map<String, String> idToNormalizedId = new HashMap<String, String>(); List<Element> queue = new ArrayList<Element>(); queue.add(srcSvg.getElement()); while (queue.size() > 0) { Element element = queue.remove(0); String id = element.getId(); if (id != null) { idToElement.put(id, element); String normalizedId = "d" + docId + "_" + idIndex++; idToNormalizedId.put(id, normalizedId); element.setId(normalizedId); } NodeList<Node> childNodes = element.getChildNodes(); for (int i = 0, length = childNodes.getLength(); i < length; i++) { Node childNode = childNodes.getItem(i); if (childNode.getNodeType() == Node.ELEMENT_NODE) { queue.add((Element) childNode.cast()); } } } // Change all the attributes which are URI references Set<String> attNames = new HashSet<String>(Arrays.asList(new String[] { "clip-path", "mask", "marker-start", "marker-mid", "marker-end", "fill", "stroke", "filter", "cursor", "style" })); queue.add(srcSvg.getElement()); IdRefTokenizer tokenizer = GWT.create(IdRefTokenizer.class); while (queue.size() > 0) { Element element = queue.remove(0); if (DOMHelper.hasAttributeNS(element, SVGConstants.XLINK_NAMESPACE_URI, SVGConstants.XLINK_HREF_ATTRIBUTE)) { String idRef = DOMHelper.getAttributeNS(element, SVGConstants.XLINK_NAMESPACE_URI, SVGConstants.XLINK_HREF_ATTRIBUTE).substring(1); String normalizeIdRef = idToNormalizedId.get(idRef); DOMHelper.setAttributeNS(element, SVGConstants.XLINK_NAMESPACE_URI, SVGConstants.XLINK_HREF_ATTRIBUTE, "#" + normalizeIdRef); } NamedNodeMap<Attr> attrs = DOMHelper.getAttributes(element); for (int i = 0, length = attrs.getLength(); i < length; i++) { Attr attr = attrs.item(i); if (attNames.contains(attr.getName())) { StringBuilder builder = new StringBuilder(); tokenizer.tokenize(attr.getValue()); IdRefTokenizer.IdRefToken token; while ((token = tokenizer.nextToken()) != null) { String value = token.getValue(); if (token.getKind() == IdRefTokenizer.IdRefToken.DATA) { builder.append(value); } else { value = idToNormalizedId.get(value); builder.append(value == null ? token.getValue() : value); } } attr.setValue(builder.toString()); } } NodeList<Node> childNodes = element.getChildNodes(); for (int i = 0, length = childNodes.getLength(); i < length; i++) { Node childNode = childNodes.getItem(i); if (childNode.getNodeType() == Node.ELEMENT_NODE) { queue.add((Element) childNode.cast()); } } } return docId; }
From source file:org.vectomatic.svg.samples.client.xpath.XPathSample.java
License:Open Source License
private void visit(Node node, Element parentSpan) { SpanElement span = doc.createSpanElement(); nodeToSpan.put(node, span);//from w w w . ja v a2s .com parentSpan.appendChild(span); NodeList<Node> childNodes = node.getChildNodes(); switch (node.getNodeType()) { case Node.ELEMENT_NODE: { Element element = node.<Element>cast(); span.addClassName(css.element()); // Populate the span with a start tag span.appendChild(createMarkup("<")); String tagName = element.getTagName(); int index = tagName.indexOf(":"); if (index != -1) { span.appendChild(doc.createTextNode(tagName.substring(0, index))); span.appendChild(createMarkup(":")); span.appendChild(doc.createTextNode(tagName.substring(index + 1))); } else { span.appendChild(doc.createTextNode(tagName)); } // Create the attribute nodes spans NamedNodeMap<Attr> attributes = DOMHelper.getAttributes(element); for (int i = 0, length = attributes.getLength(); i < length; i++) { span.appendChild(doc.createTextNode(" ")); SpanElement attrSpan = doc.createSpanElement(); attrSpan.addClassName(css.attribute()); span.appendChild(attrSpan); Attr attr = attributes.item(i); nodeToSpan.put(attr, attrSpan); String attrName = attr.getName(); index = attrName.indexOf(":"); if (index != -1) { attrSpan.appendChild(doc.createTextNode(attrName.substring(0, index))); attrSpan.appendChild(createMarkup(":")); attrSpan.appendChild(doc.createTextNode(attrName.substring(index + 1))); } else { attrSpan.appendChild(doc.createTextNode(attrName)); } attrSpan.appendChild(createMarkup("=\"")); attrSpan.appendChild(doc.createTextNode(attr.getValue())); attrSpan.appendChild(createMarkup("\"")); } span.appendChild(createMarkup(childNodes.getLength() > 0 ? ">" : "/>")); } break; case Node.TEXT_NODE: { // Populate span with text Text text = node.<Text>cast(); span.addClassName(css.text()); span.appendChild(doc.createTextNode(text.getData())); } break; } for (int i = 0, count = node.getChildCount(); i < count; i++) { visit(childNodes.getItem(i), span); } if (childNodes.getLength() > 0 && node.getNodeType() == Node.ELEMENT_NODE) { Element element = node.<Element>cast(); span.addClassName(css.element()); // Populate the span with a close tag span.appendChild(createMarkup("</")); String tagName = element.getTagName(); int index = tagName.indexOf(":"); if (index != -1) { span.appendChild(doc.createTextNode(tagName.substring(0, index))); span.appendChild(createMarkup(":")); span.appendChild(doc.createTextNode(tagName.substring(index + 1))); } else { span.appendChild(doc.createTextNode(tagName)); } span.appendChild(createMarkup(">")); } }
From source file:org.waveprotocol.wave.client.clipboard.Clipboard.java
License:Apache License
private String maybeGetAttributeFromContainer(Element srcContainer, String attribName) { NodeList<Element> elementsByClassName = DomHelper.getElementsByClassName(srcContainer, MAGIC_CLASSNAME); if (elementsByClassName != null && elementsByClassName.getLength() > 0) { return elementsByClassName.getItem(0).getAttribute(attribName); }/* ww w . j a va 2 s . c o m*/ return null; }
From source file:org.waveprotocol.wave.client.common.util.DomHelper.java
License:Apache License
/** * Gets a list of descendants of e that match the given class name. * * If the browser has the native method, that will be called. Otherwise, it * traverses descendents of the given element and returns the list of elements * with matching classname.// w w w . j av a 2 s . co m * * @param e * @param className */ public static NodeList<Element> getElementsByClassName(Element e, String className) { if (QuirksConstants.SUPPORTS_GET_ELEMENTS_BY_CLASSNAME) { return getElementsByClassNameNative(e, className); } else { NodeList<Element> all = e.getElementsByTagName("*"); if (all == null) { return null; } JsArray<Element> ret = JavaScriptObject.createArray().cast(); for (int i = 0; i < all.getLength(); ++i) { Element item = all.getItem(i); if (className.equals(item.getClassName())) { ret.push(item); } } return ret.cast(); } }
From source file:org.waveprotocol.wave.client.wavepanel.view.dom.DomUtil.java
License:Apache License
/** * Returns the specified child element of the given element. * /*www .j a v a 2s . c o m*/ * @param parent parent element * @param childTypeString string representaton of the child element * @param toExclude element to be excluded from the search * @param startFrom element to start search with * @param step step to change the child index */ private static Element findChildElementExclusive(Element parent, String childTypeString, Element toExclude, Element startFrom, int step) { if (parent != null) { NodeList<Node> children = parent.getChildNodes(); int begin; int end; if (step == +1) { begin = 0; end = children.getLength(); } else { begin = children.getLength() - 1; end = -1; } boolean startFound = false; for (int i = begin; i != end; i += step) { Node child = children.getItem(i); if (child instanceof Element) { Element e = (Element) child; if (doesElementHaveTypeString(e, childTypeString)) { if (e != toExclude) { if (startFrom != null && !startFound) { startFound = true; } else { return e; } } } } } } return null; }
From source file:org.xwiki.gwt.dom.client.Element.java
License:Open Source License
/** * Expands inner elements with meta data. * /*from www . jav a 2s .com*/ * @return this element */ public final Element expandInnerMetaData() { // Get all the inner elements with meta data. NodeList<com.google.gwt.dom.client.Element> elements = getElementsByTagName("*"); List<Element> elementsWithMetaData = new ArrayList<Element>(); for (int i = 0; i < elements.getLength(); i++) { Element element = (Element) elements.getItem(i); if (element.xHasAttribute(META_DATA_ATTR)) { elementsWithMetaData.add(element); } } // Expand meta data. Don't iterate the node list directly because it is live and meta data can contain elements. for (Element element : elementsWithMetaData) { // Remove the cached reference to the meta data document fragment because it might be shared by clone nodes. // We could have cloned the meta data document fragment but this is not reliable with some DOM nodes like // embedded objects. element.removeProperty(META_DATA_REF); element.expandMetaData(false); } return this; }
From source file:org.xwiki.gwt.wysiwyg.client.plugin.font.DynamicListBoxPicker.java
License:Open Source License
/** * @return the additional option group, which contains all the options that were added dynamically *///from w w w . j a v a 2 s . com private Element getAdditionalOptionGroup() { NodeList<Element> groups = getElement().getElementsByTagName("optgroup"); if (groups.getLength() > 0) { // The last group should be the additional group. return groups.getItem(groups.getLength() - 1); } else { Element additionalGroup = getElement().getOwnerDocument().createOptGroupElement(); additionalGroup.setAttribute("label", getAdditionalOptionGroupLabel()); getElement().appendChild(additionalGroup); return additionalGroup; } }
From source file:org.xwiki.gwt.wysiwyg.client.plugin.image.ImageMetaDataExtractor.java
License:Open Source License
/** * {@inheritDoc}/*from ww w . j a va 2 s. c om*/ * * @see InnerHTMLListener#onInnerHTMLChange(Element) */ public void onInnerHTMLChange(Element parent) { // look up all images in this subtree NodeList<com.google.gwt.dom.client.Element> imgs = parent.getElementsByTagName("img"); for (int i = 0; i < imgs.getLength(); i++) { Element img = (Element) imgs.getItem(i); processElement(img); } }
From source file:org.xwiki.gwt.wysiwyg.client.plugin.importer.IEPasteFilter.java
License:Open Source License
/** * {@inheritDoc}//from w ww. j av a 2s . c o m * * @see PasteFilter#filter(Document) */ @Override public void filter(Document document) { // We get the list of elements using a native DOM API instead of traversing the document because the DOM // document can be in an invalid state. NodeList<Element> descendants = document.getElementsByTagName("*"); List<Element> nonHTMLElements = new ArrayList<Element>(); for (int i = 0; i < descendants.getLength(); i++) { Element descendant = descendants.getItem(i); if (!StringUtils.isEmpty(descendant.getPropertyString("tagUrn"))) { nonHTMLElements.add(descendant); } } for (Element element : nonHTMLElements) { try { element.getParentNode().removeChild(element); } catch (Exception e) { // Skip this element. The DOM is in a bad state. } } super.filter(document); }