List of usage examples for org.dom4j Element nodeIterator
Iterator<Node> nodeIterator();
From source file:de.tudarmstadt.ukp.dkpro.wsd.senseval.reader.Semeval2AWReader.java
License:Apache License
@SuppressWarnings("unchecked") @Override/* w w w . ja va2s .co m*/ public void getNext(JCas jCas) throws IOException, CollectionException { int offset = 0; String s = ""; Element text = textIterator.next(); for (Iterator<Element> sentenceIterator = text.elementIterator(SENTENCE_ELEMENT_NAME); sentenceIterator .hasNext();) { Map<String, WSDItem> wsdItems = new HashMap<String, WSDItem>(); Map<String, LexicalItemConstituent> lics = new HashMap<String, LexicalItemConstituent>(); Map<String, String> sats = new HashMap<String, String>(); Element sentence = sentenceIterator.next(); Sentence sentenceAnnotation = new Sentence(jCas); sentenceAnnotation.setBegin(offset); // Loop over all nodes to get the document text in order for (Iterator<Node> nodeIterator = sentence.nodeIterator(); nodeIterator.hasNext();) { Node node = nodeIterator.next(); String nodeText = node.getText().replace('\n', ' '); String nodeName = node.getName(); if (nodeName == null) { offset += nodeText.length(); s += nodeText; continue; } // If the node is a satellite, create a LexicalItemConstituent if (nodeName.equals(SATELLITE_ELEMENT_NAME)) { String id = ((Element) node).attributeValue(ID_ATTRIBUTE_NAME); lics.put(id, newLexicalItemConstituent(jCas, id, LIC_TYPE_SATELLITE, offset, nodeText.length())); } // If the node is a head, create a LexicalItemConstituent and a WSDItem else if (nodeName.equals(HEAD_ELEMENT_NAME)) { Element head = (Element) node; String id = head.attributeValue(ID_ATTRIBUTE_NAME); String satellites = head.attributeValue(SATELLITES_ATTRIBUTE_NAME); lics.put(id, newLexicalItemConstituent(jCas, id, LIC_TYPE_HEAD, offset, nodeText.length())); wsdItems.put(id, newWsdItem(jCas, id, LIC_TYPE_HEAD, offset, nodeText.length(), head.attributeValue(POS_ATTRIBUTE_NAME), head.attributeValue(LEMMA_ATTRIBUTE_NAME))); if (satellites != null) sats.put(id, satellites); } // If the node is any other element, something is wrong else if (node.getNodeTypeName().equals("Entity") == false) { throw new CollectionException("unknown_element", new Object[] { node.getName() }); } offset += nodeText.length(); s += nodeText; } // Add a sentence annotation sentenceAnnotation.setEnd(offset); sentenceAnnotation.addToIndexes(); populateLexicalItemConstituents(jCas, wsdItems, lics, sats); } jCas.setDocumentText(s); try { setDocumentMetadata(jCas, text.attributeValue(ID_ATTRIBUTE_NAME)); } catch (URISyntaxException e) { throw new IOException(e); } textCount++; }
From source file:de.tudarmstadt.ukp.dkpro.wsd.senseval.reader.Senseval2AWReader.java
License:Apache License
@SuppressWarnings("unchecked") @Override/*from w w w .ja v a 2 s . co m*/ public void getNext(JCas jCas) throws IOException, CollectionException { int offset = 0; String s = ""; Element text = textIterator.next(); Map<String, WSDItem> wsdItems = new HashMap<String, WSDItem>(); Map<String, LexicalItemConstituent> lics = new HashMap<String, LexicalItemConstituent>(); Map<String, String> sats = new HashMap<String, String>(); // Loop over all nodes to get the document text in order for (Iterator<Node> nodeIterator = text.nodeIterator(); nodeIterator.hasNext();) { Node node = nodeIterator.next(); String nodeText = node.getText().replace('\n', ' '); String nodeName = node.getName(); if (nodeName == null) { offset += nodeText.length(); s += nodeText; continue; } // If the node is a satellite, create a LexicalItemConstituent if (nodeName.equals(SATELLITE_ELEMENT_NAME)) { String id = ((Element) node).attributeValue(ID_ATTRIBUTE_NAME); LexicalItemConstituent lic = newLexicalItemConstituent(jCas, id, LIC_TYPE_SATELLITE, offset, nodeText.length()); lics.put(id, lic); } // If the node is a head, create a LexicalItemConstituent and a WSDItem else if (nodeName.equals(HEAD_ELEMENT_NAME)) { Element head = (Element) node; String id = head.attributeValue(ID_ATTRIBUTE_NAME); String satellites = head.attributeValue(SATELLITES_ATTRIBUTE_NAME); lics.put(id, newLexicalItemConstituent(jCas, id, LIC_TYPE_HEAD, offset, nodeText.length())); WSDItem wsdItem = newWsdItem(jCas, id, LIC_TYPE_HEAD, offset, nodeText.length(), null, nodeText); wsdItems.put(id, wsdItem); if (satellites != null) { sats.put(id, satellites); } } // If the node is any other element, something is wrong else if (node.getNodeTypeName().equals("Entity") == false) { throw new CollectionException("unknown_element", new Object[] { node.getName() }); } offset += nodeText.length(); s += nodeText; } populateLexicalItemConstituents(jCas, wsdItems, lics, sats); jCas.setDocumentText(s); try { setDocumentMetadata(jCas, text.attributeValue(ID_ATTRIBUTE_NAME)); } catch (URISyntaxException e) { throw new IOException(e); } textCount++; }
From source file:de.tudarmstadt.ukp.dkpro.wsd.senseval.reader.Senseval2LSReader.java
License:Apache License
@SuppressWarnings("unchecked") @Override//w w w .ja v a 2 s . co m public void getNext(JCas jCas) throws IOException, CollectionException { // If there are no more <instance>s in this <lexelt>, get the next // <lexelt> if (instanceIterator.hasNext() == false) { lexelt = lexeltIterator.next(); lexeltPOS = getLexeltPOS(lexelt.attributeValue(ITEM_ATTRIBUTE_NAME)); lexeltLemma = getLexeltLemma(lexelt.attributeValue(ITEM_ATTRIBUTE_NAME)); textCount++; instanceIterator = lexelt.elementIterator(INSTANCE_ELEMENT_NAME); } Element instance = instanceIterator.next(); Element context = instance.element(CONTEXT_ELEMENT_NAME); int offset = 0; String s = ""; Map<String, WSDItem> wsdItems = new HashMap<String, WSDItem>(); Map<String, LexicalItemConstituent> lics = new HashMap<String, LexicalItemConstituent>(); Map<String, String> sats = new HashMap<String, String>(); // Loop over all nodes to get the document text in order for (Iterator<Node> nodeIterator = context.nodeIterator(); nodeIterator.hasNext();) { Node node = nodeIterator.next(); String nodeText = node.getText().replace('\n', ' '); String nodeName = node.getName(); if (nodeName == null) { offset += nodeText.length(); s += nodeText; continue; } // If the node is a satellite, create a LexicalItemConstituent if (nodeName.equals(SATELLITE_ELEMENT_NAME)) { String id = ((Element) node).attributeValue(ID_ATTRIBUTE_NAME); lics.put(id, newLexicalItemConstituent(jCas, id, LIC_TYPE_SATELLITE, offset, nodeText.length())); } // If the node is a head, create a LexicalItemConstituent and a // WSDItem else if (nodeName.equals(HEAD_ELEMENT_NAME)) { String id = instance.attributeValue(ID_ATTRIBUTE_NAME); String satellites = ((Element) node).attributeValue(SATELLITES_ATTRIBUTE_NAME); lics.put(id, newLexicalItemConstituent(jCas, id, LIC_TYPE_HEAD, offset, nodeText.length())); wsdItems.put(id, newWsdItem(jCas, id, LIC_TYPE_HEAD, offset, nodeText.length(), lexeltPOS, lexeltLemma)); if (satellites != null) { sats.put(id, satellites); } } // If the node is any other element, something is wrong else if (node.getNodeTypeName().equals("Entity") == false) { throw new CollectionException("unknown_element", new Object[] { node.getName() }); } offset += nodeText.length(); s += nodeText; } populateLexicalItemConstituents(jCas, wsdItems, lics, sats); jCas.setDocumentText(s); try { setDocumentMetadata(jCas, instance.attributeValue(ID_ATTRIBUTE_NAME)); } catch (URISyntaxException e) { throw new IOException(e); } }
From source file:edu.scripps.fl.pubchem.app.summary.NodesStage.java
License:Apache License
@Override public void innerProcess(Object obj) throws StageException { Document document = (Document) obj; try {/*from w w w .j a v a 2s . com*/ Element setElem = (Element) document.selectSingleNode("/eSummaryResult/DocumentSummarySet"); Iterator<Node> iter = setElem.nodeIterator(); for (; iter.hasNext();) { Node node = iter.next(); if ("DocumentSummary".equals(node.getName())) // eUtils version 2.0 emit(node); } } catch (Exception ex) { throw new StageException(this, ex); } }
From source file:io.mashin.oep.hpdl.XMLReadUtils.java
License:Open Source License
public static HashMap<String, Point> graphicalInfoFrom(Document document) { HashMap<String, Point> graphicalInfoMap = new HashMap<String, Point>(); try {// w w w . j a v a2 s . com SAXReader reader = new SAXReader(); Pattern p = Pattern.compile("\\s*<workflow>.*</workflow>\\s*", Pattern.DOTALL); @SuppressWarnings("unchecked") Iterator<Node> iter = document.nodeIterator(); while (iter.hasNext()) { Node xmlNode = iter.next(); if (xmlNode.getNodeType() == Node.COMMENT_NODE) { String graphicalInfo = xmlNode.getText(); if (p.matcher(graphicalInfo).find()) { Element graphicalElement = reader.read(new StringReader(graphicalInfo)).getRootElement(); @SuppressWarnings("unchecked") Iterator<Node> gIter = graphicalElement.nodeIterator(); while (gIter.hasNext()) { Node gNode = gIter.next(); if (gNode.getName() != null && gNode.getName().equals("node")) { graphicalInfoMap.put(gNode.valueOf("@name"), new Point(Integer.parseInt(gNode.valueOf("@x")), Integer.parseInt(gNode.valueOf("@y")))); } } break; } } } } catch (DocumentException ex) { ex.printStackTrace(); } return graphicalInfoMap; }
From source file:io.mashin.oep.hpdl.XMLReadUtils.java
License:Open Source License
public static List<Node> nodesList(Element rootElement) { List<Node> list = new ArrayList<Node>(); @SuppressWarnings("unchecked") Iterator<Node> iter = rootElement.nodeIterator(); while (iter.hasNext()) { Node node = iter.next();/*from ww w . jav a 2s .c om*/ if (node.getNodeType() == Node.ELEMENT_NODE && node.getName() != null && !node.getName().isEmpty()) { switch (node.getName()) { case "start": case "end": case "decision": case "fork": case "join": case "kill": case "action": list.add(node); } } } return list; }
From source file:org.apache.maven.archetype.common.DefaultPomManager.java
License:Apache License
public void addModule(File pom, String artifactId) throws IOException, XmlPullParserException, DocumentException, InvalidPackaging { boolean found = false; StringWriter writer = new StringWriter(); Reader fileReader = null;//w w w . java2 s.com try { fileReader = ReaderFactory.newXmlReader(pom); SAXReader reader = new SAXReader(); Document document = reader.read(fileReader); Element project = document.getRootElement(); String packaging = null; Element packagingElement = project.element("packaging"); if (packagingElement != null) { packaging = packagingElement.getStringValue(); } if (!"pom".equals(packaging)) { throw new InvalidPackaging( "Unable to add module to the current project as it is not of packaging type 'pom'"); } Element modules = project.element("modules"); if (modules == null) { modules = project.addText(" ").addElement("modules"); modules.setText("\n "); project.addText("\n"); } // TODO: change to while loop for (@SuppressWarnings("unchecked") Iterator<Element> i = modules.elementIterator("module"); i.hasNext() && !found;) { Element module = i.next(); if (module.getText().equals(artifactId)) { found = true; } } if (!found) { Node lastTextNode = null; for (@SuppressWarnings("unchecked") Iterator<Node> i = modules.nodeIterator(); i.hasNext();) { Node node = i.next(); if (node.getNodeType() == Node.ELEMENT_NODE) { lastTextNode = null; } else if (node.getNodeType() == Node.TEXT_NODE) { lastTextNode = node; } } if (lastTextNode != null) { modules.remove(lastTextNode); } modules.addText("\n "); modules.addElement("module").setText(artifactId); modules.addText("\n "); XMLWriter xmlWriter = new XMLWriter(writer); xmlWriter.write(document); FileUtils.fileWrite(pom.getAbsolutePath(), writer.toString()); } // end if } finally { IOUtil.close(fileReader); } }
From source file:org.apache.maven.archetype.old.DefaultOldArchetype.java
License:Apache License
static boolean addModuleToParentPom(String artifactId, Reader fileReader, Writer fileWriter) throws DocumentException, IOException, ArchetypeTemplateProcessingException { SAXReader reader = new SAXReader(); Document document = reader.read(fileReader); Element project = document.getRootElement(); String packaging = null;//from w w w . j a v a 2 s. com Element packagingElement = project.element("packaging"); if (packagingElement != null) { packaging = packagingElement.getStringValue(); } if (!"pom".equals(packaging)) { throw new ArchetypeTemplateProcessingException( "Unable to add module to the current project as it is not of packaging type 'pom'"); } Element modules = project.element("modules"); if (modules == null) { modules = project.addText(" ").addElement("modules"); modules.setText("\n "); project.addText("\n"); } boolean found = false; for (Iterator<?> i = modules.elementIterator("module"); i.hasNext() && !found;) { Element module = (Element) i.next(); if (module.getText().equals(artifactId)) { found = true; } } if (!found) { Node lastTextNode = null; for (Iterator<?> i = modules.nodeIterator(); i.hasNext();) { Node node = (Node) i.next(); if (node.getNodeType() == Node.ELEMENT_NODE) { lastTextNode = null; } else if (node.getNodeType() == Node.TEXT_NODE) { lastTextNode = node; } } if (lastTextNode != null) { modules.remove(lastTextNode); } modules.addText("\n "); modules.addElement("module").setText(artifactId); modules.addText("\n "); XMLWriter writer = new XMLWriter(fileWriter); writer.write(document); } return !found; }
From source file:org.b5chat.crossfire.core.property.XMLProperties.java
License:Open Source License
/** * Sets a property to an array of values. Multiple values matching the same property * is mapped to an XML file as multiple elements containing each value. * For example, using the name "foo.bar.prop", and the value string array containing * {"some value", "other value", "last value"} would produce the following XML: * <pre>/* w w w. j a v a 2s.c om*/ * <foo> * <bar> * <prop>some value</prop> * <prop>other value</prop> * <prop>last value</prop> * </bar> * </foo> * </pre> * * @param name the name of the property. * @param values the values for the property (can be empty but not null). */ public void setProperties(String name, List<String> values) { String[] propName = parsePropertyName(name); // Search for this property by traversing down the XML heirarchy, // stopping one short. Element element = document.getRootElement(); for (int i = 0; i < propName.length - 1; i++) { // If we don't find this part of the property in the XML heirarchy // we add it as a new node if (element.element(propName[i]) == null) { element.addElement(propName[i]); } element = element.element(propName[i]); } String childName = propName[propName.length - 1]; // We found matching property, clear all children. List<Element> toRemove = new ArrayList<Element>(); @SuppressWarnings("unchecked") Iterator<Element> iter = element.elementIterator(childName); while (iter.hasNext()) { toRemove.add(iter.next()); } for (iter = toRemove.iterator(); iter.hasNext();) { element.remove(iter.next()); } // Add the new children. for (String value : values) { Element childElement = element.addElement(childName); if (value.startsWith("<![CDATA[")) { @SuppressWarnings("unchecked") Iterator<Node> it = childElement.nodeIterator(); while (it.hasNext()) { Node node = it.next(); if (node instanceof CDATA) { childElement.remove(node); break; } } childElement.addCDATA(value.substring(9, value.length() - 3)); } else { childElement.setText(StringEscapeUtils.escapeXml(value)); } } saveProperties(); // Generate event. Map<String, Object> params = new HashMap<String, Object>(); params.put("value", values); PropertyEventDispatcher.dispatchEvent(name, PropertyEventDispatcher.EventType.xml_property_set, params); }
From source file:org.b5chat.crossfire.core.property.XMLProperties.java
License:Open Source License
/** * Sets the value of the specified property. If the property doesn't * currently exist, it will be automatically created. * * @param name the name of the property to set. * @param value the new value for the property. *///from ww w . ja v a 2s . c om public synchronized void setProperty(String name, String value) { if (!StringEscapeUtils.escapeXml(name).equals(name)) { throw new IllegalArgumentException("Property name cannot contain XML entities."); } if (name == null) { return; } if (value == null) { value = ""; } // Set cache correctly with prop name and value. propertyCache.put(name, value); String[] propName = parsePropertyName(name); // Search for this property by traversing down the XML heirarchy. Element element = document.getRootElement(); for (String aPropName : propName) { // If we don't find this part of the property in the XML heirarchy // we add it as a new node if (element.element(aPropName) == null) { element.addElement(aPropName); } element = element.element(aPropName); } // Set the value of the property in this node. if (value.startsWith("<![CDATA[")) { @SuppressWarnings("unchecked") Iterator<Node> it = element.nodeIterator(); while (it.hasNext()) { Node node = it.next(); if (node instanceof CDATA) { element.remove(node); break; } } element.addCDATA(value.substring(9, value.length() - 3)); } else { element.setText(value); } // Write the XML properties to disk saveProperties(); // Generate event. Map<String, Object> params = new HashMap<String, Object>(); params.put("value", value); PropertyEventDispatcher.dispatchEvent(name, PropertyEventDispatcher.EventType.xml_property_set, params); }