List of usage examples for org.dom4j Node getNodeTypeName
String getNodeTypeName();
From source file:com.cladonia.xml.editor.Editor.java
License:Open Source License
public Matcher search(String xpath, String search, boolean regExp, boolean matchCase, boolean matchword, boolean down, boolean wrap) { Vector nodes = null;//from w w w .jav a 2s . c om Vector ranges = new Vector(); try { if (xpath != null && xpath.trim().length() > 0) { nodes = document.search(xpath); } } catch (Exception e) { parent.setStatus("Invalid XPath \"" + xpath + "\"!"); return null; } if (nodes != null) { for (int i = 0; i < nodes.size(); i++) { Node node = (Node) nodes.elementAt(i); Range range = null; if (node instanceof XElement) { range = new Range((XElement) node); } else if (node instanceof XAttribute) { range = new Range((XAttribute) node); } else { parent.setStatus("Invalid Node Type \"" + node.getNodeTypeName() + "\"!"); return null; // range = new Range( (XElement)node.getParent()); } addRange(ranges, range); } } else { ranges.addElement(new Range(0, getEditor().getDocument().getLength())); } Matcher matcher = null; try { matcher = getEditor().search(ranges, search, regExp, matchCase, matchword, down, wrap); } catch (PatternSyntaxException ex) { parent.setStatus("Invalid Regular Expression \"" + search + "\"!"); return null; } if (matcher != null) { parent.setStatus("Search complete."); } else { parent.setStatus("String \"" + search + "\" not found!"); } return matcher; }
From source file:com.cladonia.xngreditor.actions.ToolsAddNodeAction.java
License:Open Source License
/** * add one of various types of node to the xpath - selected nodes * /*from w w w . j a va 2 s . c o m*/ * @param document * @param xpathPredicate * @param nodeType * @param namespace * @param name * @param value * @return */ public String addNode(ExchangerDocument document, String xpathPredicate, String nodeType, String namespace, String prefix, String name, String value) throws Exception { Vector nodeList = document.search(xpathPredicate); Vector attributeList = new Vector(); String warning = ""; if (nodeList.size() > 0) { try { for (int cnt = 0; cnt < nodeList.size(); ++cnt) { Node node = (Node) nodeList.get(cnt); if (node instanceof Element) { XElement e = (XElement) node; if (nodeType.equalsIgnoreCase("Element Node")) { QName qname = null; //resolve the namespace string if (!namespace.equalsIgnoreCase("none")) { Namespace newNs; try { newNs = new Namespace(prefix, namespace); qname = new QName(name, newNs); } catch (StringIndexOutOfBoundsException e1) { //cannot parse string MessageHandler.showError(parent, "Could not resolve Namespace:\n" + namespace, "Tools Add Node"); qname = new QName(name); } } else { qname = new QName(name); } XElement newNode = new XElement(qname); e.add(newNode); newNode.setValue(value); } else if (nodeType.equalsIgnoreCase("Attribute Node")) { QName qname = null; //resolve the namespace string if (!namespace.equalsIgnoreCase("none")) { Namespace newNs; try { newNs = new Namespace(namespace.substring(0, namespace.indexOf(":")), namespace.substring(namespace.indexOf(":") + 1, namespace.length())); qname = new QName(name, newNs); } catch (StringIndexOutOfBoundsException e1) { //cannot parse string MessageHandler.showError(parent, "Could not resolve Namespace:\n" + namespace, "Tools Add Node"); qname = new QName(name); } } else { qname = new QName(name); } XAttribute newNode = new XAttribute(qname, value); e.add(newNode); } else if (nodeType.equalsIgnoreCase("Text Node")) { FlyweightText newNode = new FlyweightText(value); e.add(newNode); } else if (nodeType.equalsIgnoreCase("CDATA Section Node")) { FlyweightCDATA newNode = new FlyweightCDATA(value); e.add(newNode); } else if (nodeType.equalsIgnoreCase("Processing Instruction Node")) { FlyweightProcessingInstruction newNode = new FlyweightProcessingInstruction(name, value); e.add(newNode); } else if (nodeType.equalsIgnoreCase("Comment Node")) { FlyweightComment newNode = new FlyweightComment(value); e.add(newNode); } } else if (node instanceof Document) { XDocument d = (XDocument) node; if (nodeType.equalsIgnoreCase("Processing Instruction Node")) { FlyweightProcessingInstruction newNode = new FlyweightProcessingInstruction(name, value); d.add(newNode); } else if (nodeType.equalsIgnoreCase("Comment Node")) { FlyweightComment newNode = new FlyweightComment(value); d.add(newNode); } else { //cant handle any others // can only handle elements MessageHandler.showError(parent, "Cannot add nodes to this xpath\n+" + "XPath: " + xpathPredicate + "refers to a" + node.getNodeTypeName(), "Tools Add Node"); //end for loop cnt = nodeList.size(); return (null); } } else { //can only handle elements MessageHandler.showError(parent, "Cannot add nodes to this xpath\n+" + "XPath: " + xpathPredicate + "refers to a" + node.getNodeTypeName(), "Tools Add Node"); //end for loop cnt = nodeList.size(); return (null); } } } catch (NullPointerException e) { MessageHandler.showError(parent, "XPath: " + xpathPredicate + "\nCannot be resolved", "Tools Add Node"); return (null); } catch (Exception e) { MessageHandler.showError(parent, "Error Adding Node", "Tools Add Node"); return (null); } document.update(); } else { MessageHandler.showError(parent, "No nodes could be found for:\n" + xpathPredicate, "Tools Add Node"); return (null); } return (document.getText()); }
From source file:com.cladonia.xngreditor.actions.ToolsConvertNodeAction.java
License:Open Source License
/** * add one of various types of node to the xpath - selected nodes * //from w w w .ja v a 2 s .co m * @param document * @param xpathPredicate * @param nodeType * @return */ public String convertNode(ExchangerDocument document, String xpathPredicate, String nodeType) { Vector nodeList = document.search(xpathPredicate); Vector attributeList = new Vector(); String warning = ""; if (nodeList.size() > 0) { try { for (int cnt = 0; cnt < nodeList.size(); ++cnt) { Node node = (Node) nodeList.get(cnt); if (node instanceof Element) { XElement e = (XElement) node; Element parentE = e.getParent(); if ((e.hasChildElements()) || (e.attributeCount() > 0)) { if ((e.hasChildElements())) { MessageHandler.showError(parent, "Cannont convert since node has child elements ", "Tools Convert Node Error"); return (null); } else if (e.attributeCount() > 0) { MessageHandler.showError("Cannont convert since node has attributes", "Tools Convert Node Error"); return (null); } cnt = nodeList.size(); } else { if (nodeType.equalsIgnoreCase("Element Node")) { MessageHandler.showError(parent, "Node is already an Element", "Tools Convert Node Error"); //end loop cnt = nodeList.size(); return (null); } else if (nodeType.equalsIgnoreCase("Attribute Node")) { //check if it has child elements QName qname = e.getQName(); //resolve the namespace string parentE.add(new XAttribute(qname, e.getValue())); parentE.remove(e); } else if (nodeType.equalsIgnoreCase("Text Node")) { FlyweightText newNode = new FlyweightText(e.getText()); parentE.add(newNode); parentE.remove(e); } else if (nodeType.equalsIgnoreCase("CDATA Section Node")) { FlyweightCDATA newNode = new FlyweightCDATA(e.getText()); parentE.add(newNode); parentE.remove(e); } else if (nodeType.equalsIgnoreCase("Processing Instruction Node")) { FlyweightProcessingInstruction newNode = new FlyweightProcessingInstruction( e.getText(), ""); parentE.add(newNode); parentE.remove(e); } else if (nodeType.equalsIgnoreCase("Comment Node")) { FlyweightComment newNode = new FlyweightComment(e.getText()); parentE.add(newNode); parentE.remove(e); } } } else if (node instanceof Attribute) { XAttribute e = (XAttribute) node; Element parentE = e.getParent(); if (nodeType.equalsIgnoreCase("Element Node")) { QName qname = e.getQName(); //resolve the namespace string XElement newE = new XElement(qname); parentE.add(newE); newE.setValue(e.getValue()); parentE.remove(e); } else if (nodeType.equalsIgnoreCase("Attribute Node")) { MessageHandler.showError(parent, "Node is already an Attribute", "Tools Convert Node Error"); //end loop cnt = nodeList.size(); return (null); } else if (nodeType.equalsIgnoreCase("Text Node")) { FlyweightText newNode = new FlyweightText(e.getText()); parentE.add(newNode); parentE.remove(e); } else if (nodeType.equalsIgnoreCase("CDATA Section Node")) { FlyweightCDATA newNode = new FlyweightCDATA(e.getText()); parentE.add(newNode); parentE.remove(e); } else if (nodeType.equalsIgnoreCase("Processing Instruction Node")) { FlyweightProcessingInstruction newNode = new FlyweightProcessingInstruction(e.getText(), ""); parentE.add(newNode); parentE.remove(e); } else if (nodeType.equalsIgnoreCase("Comment Node")) { FlyweightComment newNode = new FlyweightComment(e.getText()); parentE.add(newNode); parentE.remove(e); } } else { //can only handle elements MessageHandler .showError(parent, "Can only Convert Nodes to elements\n+" + "XPath: " + xpathPredicate + "refers to a" + node.getNodeTypeName(), "Tools Convert Node Error"); //end for loop cnt = nodeList.size(); } } } catch (NullPointerException e) { MessageHandler.showError(parent, "XPath: " + xpathPredicate + "\nCannot be resolved", "Tools Convert Node Error"); return (null); } catch (Exception e) { MessageHandler.showError(parent, "Error Adding Node", "Tools Convert Node Error"); return (null); } document.update(); } else { MessageHandler.showError(parent, "No nodes could be found for:\n" + xpathPredicate, "Tools Convert Node Error"); return (null); } return (document.getText()); }
From source file:de.tudarmstadt.ukp.dkpro.wsd.io.reader.WebCAGeXMLReader.java
License:Apache License
@SuppressWarnings("unchecked") private StringBuffer processText(JCas jCas, Element text) throws CollectionException { StringBuffer documentText = new StringBuffer(); int offset = 0; // 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(); // TODO: For now we ignore satellites. We should add support for // them./*from w w w . j a va 2s . c o m*/ if (nodeName == null || nodeName.equals(ELEMENT_SAT)) { offset += nodeText.length(); documentText.append(nodeText); continue; } // If the node is a head, create a LexicalItemConstituent and a // WSDItem else if (nodeName.equals(ELEMENT_HEAD)) { Element head = (Element) node; String headId = head.attributeValue(ATTR_ID); String lemma = head.attributeValue(ATTR_LEMMA); logger.trace("Reading instance " + headId); // Skip word forms without a POS String pos = head.attributeValue(ATTR_POS); if (pos == null) { logger.warn("No POS provided for " + headId + "; skipping"); continue; } try { pos = webCAGePosToPOS(pos).toString(); } catch (IllegalArgumentException e) { logger.warn("Unrecognized POS " + pos + " provided for " + headId + "; skipping"); continue; } // Create the necessary WSDItem and LexicalItemConstituent // annotations for this word form LexicalItemConstituent c = newLexicalItemConstituent(jCas, headId, ELEMENT_HEAD, offset, nodeText.length()); WSDItem w = newWsdItem(jCas, headId, offset, nodeText.length(), pos, lemma); w.setConstituents(new FSArray(jCas, 1)); w.setConstituents(0, c); // Get an array of lexical unit IDs (LUIDs). LUIDs are found // in the luids attribute and are separated with // # characters. String luids[] = head.attributeValue(ATTR_LUIDS).split("#"); FSArray senseArray = new FSArray(jCas, luids.length); for (int i = 0; i < luids.length; i++) { Sense sense = new Sense(jCas); sense.setId(luids[i].substring(1)); sense.setConfidence(1.0); sense.addToIndexes(); senseArray.set(i, sense); } WSDResult wsdResult = new WSDResult(jCas); wsdResult.setWsdItem(w); wsdResult.setSenses(senseArray); wsdResult.setSenseInventory(senseInventory); wsdResult.setDisambiguationMethod(DISAMBIGUATION_METHOD_NAME); wsdResult.addToIndexes(); } // 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(); documentText.append(nodeText); } return documentText; }
From source file:de.tudarmstadt.ukp.dkpro.wsd.senseval.reader.Semeval2AWReader.java
License:Apache License
@SuppressWarnings("unchecked") @Override/*from ww w.j av a 2 s. com*/ 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 ww w . ja va 2 s .com*/ 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//from w w w .j a va2 s .c o 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:dk.netarkivet.common.utils.XmlTree.java
License:Open Source License
/** * Initialise a node in an XML tree.//from w w w.ja va 2 s . c o m * * @param n The XML node for this node * @param parser The parser that can convert a leaf node to a value of type T. * @throws ArgumentNotValid on null argument, or if n is not of type element or document. */ private XmlTree(Node n, ValueParser<T> parser) { ArgumentNotValid.checkNotNull(n, "Node n"); ArgumentNotValid.checkNotNull(parser, "ValueParser<T> parser"); if (n.getNodeType() == Node.DOCUMENT_NODE) { root = (Document) n; element = null; } else if (n.getNodeType() == Node.ELEMENT_NODE) { element = (Element) n; root = null; } else { throw new ArgumentNotValid("Invalid XML node type '" + n.getNodeTypeName() + "'"); } this.parser = parser; }
From source file:org.apereo.portal.io.xml.SpELDataTemplatingStrategy.java
License:Apache License
@Override public Source processTemplates(Document data, String filename) { log.trace("Processing templates for document XML={}", data.asXML()); for (String xpath : XPATH_EXPRESSIONS) { @SuppressWarnings("unchecked") List<Node> nodes = data.selectNodes(xpath); for (Node n : nodes) { String inpt, otpt;/*from w w w . j ava2 s . c o m*/ switch (n.getNodeType()) { case org.w3c.dom.Node.ATTRIBUTE_NODE: Attribute a = (Attribute) n; inpt = a.getValue(); otpt = processText(inpt); if (otpt == null) { throw new RuntimeException("Invalid expression '" + inpt + "' in file " + filename); } if (!otpt.equals(inpt)) { a.setValue(otpt); } break; case org.w3c.dom.Node.TEXT_NODE: case org.w3c.dom.Node.CDATA_SECTION_NODE: inpt = n.getText(); otpt = processText(inpt); if (otpt == null) { throw new RuntimeException("Invalid expression '" + inpt + "' in file " + filename); } if (!otpt.equals(inpt)) { n.setText(otpt); } break; default: String msg = "Unsupported node type: " + n.getNodeTypeName(); throw new RuntimeException(msg); } } } final SAXSource rslt = new DocumentSource(data); rslt.setSystemId(filename); // must be set, else import chokes return rslt; }
From source file:org.jasig.portal.io.xml.SpELDataTemplatingStrategy.java
License:Apache License
@Override public Source processTemplates(Document data, String filename) { log.trace("Processing templates for document XML={}", data.asXML()); for (String xpath : XPATH_EXPRESSIONS) { @SuppressWarnings("unchecked") List<Node> nodes = data.selectNodes(xpath); for (Node n : nodes) { String inpt, otpt;// w w w . ja va 2 s . c o m switch (n.getNodeType()) { case org.w3c.dom.Node.ATTRIBUTE_NODE: Attribute a = (Attribute) n; inpt = a.getValue(); otpt = processText(inpt); if (otpt == null) { throw new RuntimeException("Invalid expression '" + inpt + "' in file " + filename); } if (!otpt.equals(inpt)) { a.setValue(otpt); } break; case org.w3c.dom.Node.TEXT_NODE: case org.w3c.dom.Node.CDATA_SECTION_NODE: inpt = n.getText(); otpt = processText(inpt); if (otpt == null) { throw new RuntimeException("Invalid expression '" + inpt + "' in file " + filename); } if (!otpt.equals(inpt)) { n.setText(otpt); } break; default: String msg = "Unsupported node type: " + n.getNodeTypeName(); throw new RuntimeException(msg); } } } final SAXSource rslt = new DocumentSource(data); rslt.setSystemId(filename); // must be set, else import chokes return rslt; }