private static String getTime(NodeList aChildren) { // If no event is provided, appointment is whole working day String timeData, time = "99:99-99:99"; Node firstElNode = aChildren.item(0); // If no event is provided, event is whole working day if (firstElNode.getNodeType() == Node.ELEMENT_NODE) { Element el = (Element) firstElNode; String tagName = el.getTagName(); if (tagName.equals("span") && el.getAttribute("class" ).equals("link")) { time = getTime(firstElNode.getChildNodes()); } else { time = "08:00-18:00"; } } else if (firstElNode.getNodeType() == Node.TEXT_NODE){ timeData = ((CharacterData) firstElNode).getData(); // Filter   alias time = timeData.substring(0, 5).concat(timeData.substring(6)); } return time; }
/** * Extract the textual content from a Node. * This is rather like the XPath value of a Node. * @param node The node to extract the text from * @return The textual value of the node */ public static String getText(Node node) { StringBuffer reply = new StringBuffer(); NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if ((child instanceof CharacterData && !(child instanceof Comment)) || child instanceof EntityReference) { reply.append(child.getNodeValue()); } else if (child.getNodeType() == Node.ELEMENT_NODE) { reply.append(getText(child)); } } return reply.toString(); }
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); } } }
@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(); }
/** * Retrieve the {@code CDATA} value from a given Node - noting that the {@code node}'s child nodes will be searched and the first {@code * CDATA} tag will be returned. * * @param node The node that will be searched. * @return The {@code CDATA} found as a {@link String} or {@code null} if a {@code CDATA} node was not found. */ protected String getChildCDataNodeValue(Node node) { String result = null; NodeList children = node.getChildNodes(); if (children != null) { for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child.getNodeType() == Node.CDATA_SECTION_NODE) { result = CharacterData.class.cast(child).getData(); if (result != null) { result = result.trim(); } break; } } } return result; }
/** * 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; }
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; }
/** * 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 ""; }
/** * Finds the values for the XPath {@code expression}. * * @throws XPathExpressionException if the {@code expression} is invalid */ @SuppressWarnings("unchecked") <V> List<V> valuesInternal(String expression, Converter<V> converter) throws XPathExpressionException { XPathExpression expr = XPathFactory.newInstance().newXPath().compile(expression); NodeList nodeList = (NodeList) expr.evaluate(source, XPathConstants.NODESET); if (nodeList.getLength() == 0) return Collections.emptyList(); List<V> values = new ArrayList<V>(nodeList.getLength()); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); String value = null; if (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.ATTRIBUTE_NODE) value = node.getNodeValue(); else if (node.getNodeType() == Node.CDATA_SECTION_NODE) value = ((CharacterData) node).getData(); // Convert value if (value != null) values.add(converter == null ? (V) value : converter.convert(value)); } return values; }
/** * Creates an CitationStyle instance out of the style string */ private static Optional<CitationStyle> createCitationStyleFromSource(final String source, final String filename) { if ((filename != null) && !filename.isEmpty() && (source != null) && !source.isEmpty()) { try { DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); InputSource is = new InputSource(); is.setCharacterStream(new StringReader(stripInvalidProlog(source))); Document doc = db.parse(is); NodeList nodes = doc.getElementsByTagName("info"); NodeList titleNode = ((Element) nodes.item(0)).getElementsByTagName("title"); String title = ((CharacterData) titleNode.item(0).getFirstChild()).getData(); return Optional.of(new CitationStyle(filename, title, source)); } catch (ParserConfigurationException | SAXException | IOException e) { LOGGER.error("Error while parsing source", e); } } return Optional.empty(); }
@Override public String getTextValue(Element valueEle) { if (valueEle != 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().trim(); } return null; }
private void findAndParseSelectKey(Node node, MappedStatementConfig config) { state.getConfig().getErrorContext().setActivity("parsing select key tags"); boolean foundSQLFirst = false; NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child.getNodeType() == Node.CDATA_SECTION_NODE || child.getNodeType() == Node.TEXT_NODE) { String data = ((CharacterData) child).getData(); if (data.trim().length() > 0) { foundSQLFirst = true; } } else if (child.getNodeType() == Node.ELEMENT_NODE && "selectKey".equals(child.getNodeName())) { Properties attributes = NodeletUtils.parseAttributes(child, state.getGlobalProps()); String keyPropName = attributes.getProperty("keyProperty"); String resultClassName = attributes.getProperty("resultClass"); String type = attributes.getProperty("type"); config.setSelectKeyStatement(new XMLSqlSource(state, child), resultClassName, keyPropName, foundSQLFirst, type); break; } } state.getConfig().getErrorContext().setMoreInfo(null); }
public static String parseTextExpression(Node textElement) { if (textElement instanceof CharacterData) { return ((CharacterData) textElement).getData().trim(); } else { return ""; } }
public static String getCharacterDataFromElement(Element e) { Node child = e.getFirstChild(); if (child instanceof CharacterData) { CharacterData cd = (CharacterData) child; return cd.getData(); } return ""; }
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; }
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))); } }
@Test public void testAppendData() throws Exception { CharacterData cd = createCharacterData("DOM"); cd.appendData("2"); assertEquals(cd.getData(), "DOM2"); }
@Test(dataProvider = "data-for-replace-neg") public void testReplaceDataNeg(String text, int offset, int count, String arg) throws Exception { CharacterData cd = createCharacterData(text); try { cd.replaceData(offset, count, arg); fail(DOMEXCEPTION_EXPECTED); } catch (DOMException e) { assertEquals(e.code, INDEX_SIZE_ERR); } }
@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); } }
@Test(dataProvider = "data-for-substring-neg") public void testSubstringDataNeg(String text, int offset, int count) throws Exception { CharacterData cd = createCharacterData(text); try { cd.substringData(offset, count); fail(DOMEXCEPTION_EXPECTED); } catch (DOMException e) { assertEquals(e.code, INDEX_SIZE_ERR); } }
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; }
private String getCharacterDataFromElement(Element e) { try { Node child = e.getFirstChild(); if (child instanceof CharacterData) { CharacterData cd = (CharacterData) child; return cd.getData(); } } catch (Exception ex) { ex.printStackTrace(); } return ""; }
/** * 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(); }
/** * Retrieve character data from an element if it exists * * @param e is the DOM Element to retrieve character data from * @return the character data as String [possibly empty String] */ private static String getCharacterDataFromElement (Element e) { Node child = e.getFirstChild(); if (child instanceof CharacterData) { CharacterData cd = (CharacterData) child; return cd.getData(); } return ""; }
private String getValueFromXML(String key) { Element line = (Element) meta.getElementsByTagName(key).item(0); if (line == null) return null; Node child = line.getFirstChild(); if (child instanceof CharacterData) return ((CharacterData) child).getData(); return null; }
public static String getCharacterDataFromElement(Element e) { Node child = e.getFirstChild(); if (child instanceof CharacterData) { CharacterData cd = (CharacterData) child; return cd.getData(); } return"" ; }
/** * 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(final Element valueElement) { Validate.notNull(valueElement, "Element must not be null"); final StringBuilder sb = new StringBuilder(); final NodeList nl = valueElement.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { final Node item = nl.item(i); if (item instanceof CharacterData && !(item instanceof Comment) || item instanceof EntityReference) { sb.append(item.getNodeValue()); } } return sb.toString(); }
public void characters (char ch [], int start, int length) throws SAXException { // we can't create populated entity ref nodes using // only public DOM APIs (they've got to be readonly // at creation time) if (currentEntity != null) return; Node lastChild = top.getLastChild (); // merge consecutive text or CDATA nodes if appropriate. if (lastChild instanceof Text) { if (consumer.isHidingCDATA () // consecutive Text content ... always merge || (!inCDATA && !(lastChild instanceof CDATASection)) // consecutive CDATASection content ... don't // merge between sections, only within them || (inCDATA && mergeCDATA && lastChild instanceof CDATASection) ) { CharacterData last = (CharacterData) lastChild; String value = new String (ch, start, length); last.appendData (value); return; } } if (inCDATA && !consumer.isHidingCDATA ()) { top.appendChild (createText (true, ch, start, length)); mergeCDATA = true; } else top.appendChild (createText (false, ch, start, length)); }
private String getCharacterDataFromElement(Element e) { Node child = e.getFirstChild(); if (child instanceof CharacterData) { CharacterData cd = (CharacterData) child; return cd.getData(); } return "?"; }