org.w3c.dom.CharacterData Java Examples

The following examples show how to use org.w3c.dom.CharacterData. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ItemHelperBase.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * Concatenate nodes for xpath
 * @param itemXml
 * @param xpath
 * @return
 */
protected String makeItemNodeText(Item itemXml, String xpath)
{
  //String text = "";
  List nodes = itemXml.selectNodes(xpath);
  Iterator iter = nodes.iterator();
  
  StringBuilder textbuf = new StringBuilder();   
  while (iter.hasNext())
  {
    Node node = (Node) iter.next();
    Node child = node.getFirstChild();
    if ( (child != null) && child instanceof CharacterData)
    {
      CharacterData cdi = (CharacterData) child;
      textbuf.append(" " + cdi.getData());
    }
  }
  String text = textbuf.toString();
  return text;
}
 
Example #2
Source File: ItemHelperBase.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * Concatenate nodes for xpath
 * @param itemXml
 * @param xpath
 * @return
 */
protected String makeItemNodeText(Item itemXml, String xpath)
{
  //String text = "";
  List nodes = itemXml.selectNodes(xpath);
  Iterator iter = nodes.iterator();
  
  StringBuilder textbuf = new StringBuilder();   
  while (iter.hasNext())
  {
    Node node = (Node) iter.next();
    Node child = node.getFirstChild();
    if ( (child != null) && child instanceof CharacterData)
    {
      CharacterData cdi = (CharacterData) child;
      textbuf.append(" " + cdi.getData());
    }
  }
  String text = textbuf.toString();
  return text;
}
 
Example #3
Source File: PrincipalPropertySearchReport.java    From cosmo with Apache License 2.0 6 votes vote down vote up
private boolean matchText(Element parent,
                          String match) {
    NodeList children = parent.getChildNodes();
    for (int i=0; i<children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            Element e = (Element) child;
            if (! matchText(e, match)) {
                return false;
            }
        } else if (child.getNodeType() == Node.TEXT_NODE ||
                   child.getNodeType() == Node.CDATA_SECTION_NODE) {
            String data = ((CharacterData) child).getData();
            if (! matchText(data, match)) {
                return false;
            }
        } // else we skip the node
    }
    return true;
}
 
Example #4
Source File: SOAPDocumentImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public void registerChildNodes(Node parentNode, boolean deep) {
    if (parentNode.getUserData(SAAJ_NODE) == null) {
        if (parentNode instanceof Element) {
            ElementFactory.createElement(this, (Element) parentNode);
        } else if (parentNode instanceof CharacterData) {
            switch (parentNode.getNodeType()) {
                case CDATA_SECTION_NODE:
                    new CDATAImpl(this, (CharacterData) parentNode);
                    break;
                case COMMENT_NODE:
                    new SOAPCommentImpl(this, (CharacterData) parentNode);
                    break;
                case TEXT_NODE:
                    new SOAPTextImpl(this, (CharacterData) parentNode);
                    break;
            }
        }
    }
    if (deep) {
        NodeList nodeList = parentNode.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node nextChild = nodeList.item(i);
            registerChildNodes(nextChild, true);
        }
    }
}
 
Example #5
Source File: TemplateRegistration.java    From azure-notificationhubs-android with Apache License 2.0 6 votes vote down vote up
@Override
protected void loadCustomXmlData(Element payloadNode) {
	NodeList bodyTemplateElements = payloadNode.getElementsByTagName("BodyTemplate");
	if (bodyTemplateElements.getLength() > 0) {
		NodeList bodyNodes = bodyTemplateElements.item(0).getChildNodes();
		for (int i = 0; i < bodyNodes.getLength(); i++) {
			if (bodyNodes.item(i) instanceof CharacterData) {
				CharacterData data = (CharacterData) bodyNodes.item(i);
				mBodyTemplate = data.getData();
				break;
			}
		}
	}

	setName(getNodeValue(payloadNode, "TemplateName"));
}
 
Example #6
Source File: QueuePoller.java    From swift-k with Apache License 2.0 6 votes vote down vote up
/**
 * getDataFromElement - Make XML parsing a bit easier 
 * @param e XML Element
 * @return XML data as a String
 */
public static String getDataFromElement(Element e) {
	try {
		Node child = e.getFirstChild();
		if (child instanceof CharacterData) {
			CharacterData cd = (CharacterData) child;
			return cd.getData();
		}
	}
	
	catch (Exception ex) {
		logger.debug("Error in getDataFromElement");
		logger.debug(ex.getMessage());
		logger.debug(ex.getStackTrace());
	}
	return "";
}
 
Example #7
Source File: XmlElement.java    From allure2 with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
public XmlElement(final Element element) {
    this.name = element.getNodeName();
    final NamedNodeMap attributes = element.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        final Node item = attributes.item(i);
        this.attributes.put(item.getNodeName(), item.getNodeValue());
    }

    final StringBuilder textValue = new StringBuilder();
    final NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        final Node node = children.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            this.children.add(new XmlElement((Element) node));
        }
        if (node.getNodeType() == Node.TEXT_NODE) {
            textValue.append(node.getNodeValue());
        }
        if (node.getNodeType() == Node.CDATA_SECTION_NODE) {
            textValue.append(((CharacterData) node).getData());
        }
    }
    this.value = textValue.toString();
}
 
Example #8
Source File: NodeImpl.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public final void setNodeValue(String nodeValue) throws DOMException {
    switch (getNodeType()) {
        case CDATA_SECTION_NODE:
        case COMMENT_NODE:
        case TEXT_NODE:
            ((CharacterData) this).setData(nodeValue);
            return;

        case PROCESSING_INSTRUCTION_NODE:
            ((ProcessingInstruction) this).setData(nodeValue);
            return;

        case ATTRIBUTE_NODE:
            ((Attr) this).setValue(nodeValue);
            return;

        case ELEMENT_NODE:
        case ENTITY_REFERENCE_NODE:
        case ENTITY_NODE:
        case DOCUMENT_NODE:
        case DOCUMENT_TYPE_NODE:
        case DOCUMENT_FRAGMENT_NODE:
        case NOTATION_NODE:
            return; // do nothing!

        default:
            throw new DOMException(DOMException.NOT_SUPPORTED_ERR,
                    "Unsupported node type " + getNodeType());
    }
}
 
Example #9
Source File: HttpGetAndroidWrapper.java    From gsn with GNU General Public License v3.0 5 votes vote down vote up
public static String getCharacterDataFromElement(Element e) 
{
	    Node child = e.getFirstChild();
	    if (child instanceof CharacterData) {
	       CharacterData cd = (CharacterData) child;
	       return cd.getData();
	    }
	    return "?";
 }
 
Example #10
Source File: FrontierSiliconRadioApiResult.java    From openhab1-addons with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * convert the value of a given XML element to a string for further processing
 * 
 * @param e
 *            XML Element
 * @return the elements value converted to string
 */
private static String getCharacterDataFromElement(Element e) {
    final Node child = e.getFirstChild();
    if (child instanceof CharacterData) {
        final CharacterData cd = (CharacterData) child;
        return cd.getData();
    }
    return "";
}
 
Example #11
Source File: FrontierSiliconRadioApiResult.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * convert the value of a given XML element to a string for further processing
 *
 * @param e
 *            XML Element
 * @return the elements value converted to string
 */
private static String getCharacterDataFromElement(Element e) {
    final Node child = e.getFirstChild();
    if (child instanceof CharacterData) {
        final CharacterData cd = (CharacterData) child;
        return cd.getData();
    }
    return "";
}
 
Example #12
Source File: XmlConfigLoader.java    From extentreports-java with Apache License 2.0 5 votes vote down vote up
public ConfigStore getConfigStore() {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder;
    String value;

    try {
        documentBuilder = documentBuilderFactory.newDocumentBuilder();

        Document doc = documentBuilder.parse(stream);
        doc.getDocumentElement().normalize();

        NodeList nodeList = doc.getElementsByTagName("configuration").item(0).getChildNodes();

        for (int ix = 0; ix < nodeList.getLength(); ix++) {
            Node node = nodeList.item(ix);

            Element el = node.getNodeType() == Node.ELEMENT_NODE ? (Element) node : null;

            if (el != null) {
                value = el.getTextContent();

                value = el instanceof CharacterData ? ((CharacterData) el).getData() : value;
                store.addConfig(el.getNodeName(), value);
            }
        }

        return store;
    } catch (IOException | SAXException | ParserConfigurationException e) {
        LOG.log(Level.SEVERE, "Failed to load external configuration", e);
    }

    return null;
}
 
Example #13
Source File: XMLHelper.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
public static String getNodeValue(Node node) {
    if(node instanceof Comment){
        return null;
    }
    if(node instanceof CharacterData) {
        return ((CharacterData)node).getData();
    }
    if(node instanceof EntityReference) {
        return node.getNodeValue();
    }
    if(node instanceof Element) {
        return getTextValue((Element)node);
    }
    return node.getNodeValue();
}
 
Example #14
Source File: XMLHelper.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Extract the text value from the given DOM element, ignoring XML comments. <p>Appends all CharacterData nodes and
 * EntityReference nodes into a single String value, excluding Comment nodes.
 *
 * @see CharacterData
 * @see EntityReference
 * @see Comment
 */
public static String getTextValue(Element valueEle) {
    if(valueEle == null) throw new IllegalArgumentException("Element must not be null");
    StringBuilder sb = new StringBuilder();
    NodeList nl = valueEle.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node item = nl.item(i);
        if ((item instanceof CharacterData && !(item instanceof Comment)) || item instanceof EntityReference) {
            sb.append(item.getNodeValue());
        }else if(item instanceof Element) {
            sb.append(getTextValue((Element)item));
        }
    }
    return sb.toString();
}
 
Example #15
Source File: DomUtils.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Extracts the text value from the given DOM element, ignoring XML comments.
 * <p>Appends all CharacterData nodes and EntityReference nodes into a single
 * String value, excluding Comment nodes. Only exposes actual user-specified
 * text, no default values of any kind.
 * @see CharacterData
 * @see EntityReference
 * @see Comment
 */
public static String getTextValue(Element valueEle) {
	Assert.notNull(valueEle, "Element must not be null");
	StringBuilder sb = new StringBuilder();
	NodeList nl = valueEle.getChildNodes();
	for (int i = 0; i < nl.getLength(); i++) {
		Node item = nl.item(i);
		if ((item instanceof CharacterData && !(item instanceof Comment)) || item instanceof EntityReference) {
			sb.append(item.getNodeValue());
		}
	}
	return sb.toString();
}
 
Example #16
Source File: TopologyModuleParamsArray.java    From netphony-topology with Apache License 2.0 5 votes vote down vote up
/**
 * This method gets the value from an XML element.
 * @param e XML Element.
 * @return Value as string.
 */
private String getCharacterDataFromElement(Element e) {
	if (e == null)
	{
		return null;
	}
	Node child = e.getFirstChild();
	if (child instanceof CharacterData) {
		CharacterData cd = (CharacterData) child;
		return cd.getData();
	} else {
		return "?";
	}
}
 
Example #17
Source File: TEDUpdaterFloodlight.java    From netphony-topology with Apache License 2.0 5 votes vote down vote up
private String getCharacterDataFromElement(Element e) {
	Node child = e.getFirstChild();
	if (child instanceof CharacterData) {
		CharacterData cd = (CharacterData) child;
		return cd.getData();
	} else {
		return "?";
	}
}
 
Example #18
Source File: ElementImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
protected TextImpl convertToSoapText(CharacterData characterData) {
        final Node soapNode = getSoapDocument().findIfPresent(characterData);
        if (soapNode instanceof TextImpl) {
            return (TextImpl) soapNode;
        } else {
            TextImpl t = null;
            switch (characterData.getNodeType()) {
                case CDATA_SECTION_NODE:
                    t = new CDATAImpl(getSoapDocument(), characterData.getData());
                    break;
                case COMMENT_NODE:
                    t = new SOAPCommentImpl(getSoapDocument(), characterData.getData());
                    break;
                case TEXT_NODE:
                    t = new SOAPTextImpl(getSoapDocument(), characterData.getData());
                    break;
            }
            Node parent = getSoapDocument().find(characterData.getParentNode());
            if (parent != null) {
                parent.replaceChild(t, characterData);
            } // XXX else throw an exception?

            return t;

//            return replaceElementWithSOAPElement(
//                element,
//                (ElementImpl) createElement(NameImpl.copyElementName(element)));
        }
    }
 
Example #19
Source File: TurnitinReviewServiceImpl.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * This method was originally private, but is being made public for the
 * moment so we can run integration tests. TODO Revisit this decision.
 *
 * @param siteId
 * @throws SubmissionException
 * @throws TransientSubmissionException
 */
@SuppressWarnings("unchecked")
public void createClass(String siteId) throws SubmissionException, TransientSubmissionException {
	log.debug("Creating class for site: " + siteId);

	String cpw = defaultClassPassword;
	String ctl = siteId;
	String fcmd = "2";
	String fid = "2";
	String utp = "2"; // user type 2 = instructor
	String cid = siteId;

	Document document = null;

	Map params = TurnitinAPIUtil.packMap(turnitinConn.getBaseTIIOptions(), "cid", cid, "cpw", cpw, "ctl", ctl,
			"fcmd", fcmd, "fid", fid, "utp", utp);

	params.putAll(getInstructorInfo(siteId));

	document = turnitinConn.callTurnitinReturnDocument(params);

	Element root = document.getDocumentElement();
	String rcode = ((CharacterData) (root.getElementsByTagName("rcode").item(0).getFirstChild())).getData().trim();

	if (((CharacterData) (root.getElementsByTagName("rcode").item(0).getFirstChild())).getData().trim()
			.compareTo("20") == 0
			|| ((CharacterData) (root.getElementsByTagName("rcode").item(0).getFirstChild())).getData().trim()
					.compareTo("21") == 0) {
		log.debug("Create Class successful");
	} else {
		String rmessage = ((CharacterData) (root.getElementsByTagName("rmessage").item(0).getFirstChild())).getData().trim();
		throw new ContentReviewProviderException(getFormattedMessage("enrolment.class.creation.error.with.code", rmessage, rcode),
			createLastError(doc->createFormattedMessageXML(doc, "enrolment.class.creation.error.with.code", rmessage, rcode)));
	}
}
 
Example #20
Source File: XNode.java    From mybaties with Apache License 2.0 5 votes vote down vote up
private String getBodyData(Node child) {
  if (child.getNodeType() == Node.CDATA_SECTION_NODE
      || child.getNodeType() == Node.TEXT_NODE) {
    String data = ((CharacterData) child).getData();
    data = PropertyParser.parse(data, variables);
    return data;
  }
  return null;
}
 
Example #21
Source File: Xml.java    From totallylazy with Apache License 2.0 5 votes vote down vote up
public static String contents(Node node) throws Exception {
    if (node instanceof Attr) {
        return contents((Attr) node);
    }
    if (node instanceof CharacterData) {
        return contents((CharacterData) node);
    }
    if (node instanceof Element) {
        return contents((Element) node);
    }
    throw new UnsupportedOperationException("Unknown node type " + node.getClass());
}
 
Example #22
Source File: DomUtils.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Extracts the text value from the given DOM element, ignoring XML comments.
 * <p>Appends all CharacterData nodes and EntityReference nodes into a single
 * String value, excluding Comment nodes. Only exposes actual user-specified
 * text, no default values of any kind.
 * @see CharacterData
 * @see EntityReference
 * @see Comment
 */
public static String getTextValue(Element valueEle) {
	Assert.notNull(valueEle, "Element must not be null");
	StringBuilder sb = new StringBuilder();
	NodeList nl = valueEle.getChildNodes();
	for (int i = 0; i < nl.getLength(); i++) {
		Node item = nl.item(i);
		if ((item instanceof CharacterData && !(item instanceof Comment)) || item instanceof EntityReference) {
			sb.append(item.getNodeValue());
		}
	}
	return sb.toString();
}
 
Example #23
Source File: WemoLinkDiscoveryService.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
public static String getCharacterDataFromElement(Element e) {
    Node child = e.getFirstChild();
    if (child instanceof CharacterData) {
        CharacterData cd = (CharacterData) child;
        return cd.getData();
    }
    return "?";
}
 
Example #24
Source File: AbstractCharacterDataTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testAppendData() throws Exception {
    CharacterData cd = createCharacterData("DOM");
    cd.appendData("2");
    assertEquals(cd.getData(), "DOM2");

}
 
Example #25
Source File: DomUtils.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Extracts the text value from the given DOM element, ignoring XML comments.
 * <p>Appends all CharacterData nodes and EntityReference nodes into a single
 * String value, excluding Comment nodes. Only exposes actual user-specified
 * text, no default values of any kind.
 * @see CharacterData
 * @see EntityReference
 * @see Comment
 */
public static String getTextValue(Element valueEle) {
	Assert.notNull(valueEle, "Element must not be null");
	StringBuilder sb = new StringBuilder();
	NodeList nl = valueEle.getChildNodes();
	for (int i = 0; i < nl.getLength(); i++) {
		Node item = nl.item(i);
		if ((item instanceof CharacterData && !(item instanceof Comment)) || item instanceof EntityReference) {
			sb.append(item.getNodeValue());
		}
	}
	return sb.toString();
}
 
Example #26
Source File: MetaDataObjectSerializer_indent.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
private boolean hasNewline(Node n) {
  if (n instanceof Comment) {
    return false;
  }
  CharacterData c = (CharacterData) n;
  return (-1) != c.getData().indexOf('\n');
}
 
Example #27
Source File: TopologyReaderXML.java    From netphony-topology with Apache License 2.0 5 votes vote down vote up
public static String getCharacterDataFromElement(Element e) {
	org.w3c.dom.Node child = e.getFirstChild();
	if (child instanceof CharacterData) {
		CharacterData cd = (CharacterData) child;
		return cd.getData();
	} else {
		return "?";
	}
}
 
Example #28
Source File: AbstractCharacterDataTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "data-for-insert-neg")
public void testInsertDataNeg(String text, int offset) throws Exception {
    CharacterData cd = createCharacterData(text);
    try {
        cd.insertData(offset, "TEST");
        fail(DOMEXCEPTION_EXPECTED);
    } catch (DOMException e) {
        assertEquals(e.code, INDEX_SIZE_ERR);
    }
}
 
Example #29
Source File: XmlNodeWrapper.java    From tangyuan2 with GNU General Public License v3.0 5 votes vote down vote up
private String getBodyData(Node child) {
	if (child.getNodeType() == Node.CDATA_SECTION_NODE || child.getNodeType() == Node.TEXT_NODE) {
		String data = ((CharacterData) child).getData();
		// TODO data = PropertyParser.parse(data, null);
		return data;
	}
	return null;
}
 
Example #30
Source File: WemoMakerHandler.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
public static String getCharacterDataFromElement(Element e) {
    Node child = e.getFirstChild();
    if (child instanceof CharacterData) {
        CharacterData cd = (CharacterData) child;
        return cd.getData();
    }
    return "?";
}