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; }
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 + "'"); }
public void setAttributes(HashMap<String, String> map) { NodeList<Element> elements = rootElement.getElementsByTagName("attributes"); assert elements.getLength() > 0; Element attributesNode = elements.getItem(0); attributesNode.removeAllChildren(); Node last = null; for (String key : map.keySet()) { Element element = JSFunctions.createTextElement("attribute", ""); element.setAttribute("name", key); element.setAttribute("value", map.get(key)); last = attributesNode.insertAfter(element, last); } }
/** * Returns a mutable map which is not bound to the TaggedEntity; any changes made in the map will * not be reflected in the tagName. * * @return mutable unbound map */ public HashMap<String, String> getAttributes() { NodeList<Element> elements = rootElement.getElementsByTagName("attributes"); assert elements.getLength() > 0; Element attributesNode = elements.getItem(0); HashMap<String, String> rvalue = new HashMap<>(); for (int i = 0; i < attributesNode.getChildCount(); i++) { final Node node = attributesNode.getChild(i); JsArray<Node> attributeList = getAttributes((Element) node); String name = attributeList.get(0).getNodeValue(); String value = attributeList.get(1).getNodeValue(); rvalue.put(name, value); } return rvalue; }
/** * 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()); }
public String getTag() { NodeList<Element> elements = rootElement.getElementsByTagName("tagname"); assert elements.getLength() > 0; Element tagname = elements.getItem(0); return tagname.getInnerText(); }
public String getText() { NodeList<Element> elements = rootElement.getElementsByTagName("entity"); assert elements.getLength() > 0; Element entity = elements.getItem(0); return entity.getInnerText(); }
public void setLemma(String text) { NodeList<Element> elements = rootElement.getElementsByTagName("lemma"); assert elements.getLength() > 0; assert text != null; elements.getItem(0).setInnerText(text); }
public String getLemma() { NodeList<Element> elements = rootElement.getElementsByTagName("lemma"); assert elements.getLength() > 0; Element lemma = elements.getItem(0); return lemma.getInnerText(); }