/** * Method updateElement. * * @param counter * @param shouldExist * @param name * @param parent * @return Element */ protected Element updateElement(Counter counter, Element parent, String name, boolean shouldExist) { Element element = parent.getChild(name, parent.getNamespace()); if (element != null && shouldExist) { counter.increaseCount(); } if (element == null && shouldExist) { element = factory.element(name, parent.getNamespace()); insertAtPreferredLocation(parent, element, counter); counter.increaseCount(); } if (!shouldExist && element != null) { int index = parent.indexOf(element); if (index > 0) { Content previous = parent.getContent(index - 1); if (previous instanceof Text) { Text txt = (Text)previous; if (txt.getTextTrim().length() == 0) { parent.removeContent(txt); } } } parent.removeContent(element); } return element; }
/** * Method updateElement * * @param counter * @param shouldExist * @param name * @param parent */ protected Element updateElement( Counter counter, Element parent, String name, boolean shouldExist ) { Element element = parent.getChild( name, parent.getNamespace() ); if ( element != null && shouldExist ) { counter.increaseCount(); } if ( element == null && shouldExist ) { element = factory.element( name, parent.getNamespace() ); insertAtPreferredLocation( parent, element, counter ); counter.increaseCount(); } if ( !shouldExist && element != null ) { int index = parent.indexOf( element ); if ( index > 0 ) { Content previous = parent.getContent( index - 1 ); if ( previous instanceof Text ) { Text txt = (Text) previous; if ( txt.getTextTrim().length() == 0 ) { parent.removeContent( txt ); } } } parent.removeContent( element ); } return element; }
public void testCollectMacros() { Element root = new Element("root"); root.addContent(new Text("$MACro1$ some text $macro2$ other text $MACRo3$")); root.addContent(new Text("$macro4$ some text")); root.addContent(new Text("some text$macro5$")); root.addContent(new Text("file:$mac_ro6$")); root.addContent(new Text("jar://$macr.o7$ ")); root.addContent(new Text("$mac-ro8$ ")); root.addContent(new Text("$$$ ")); root.addContent(new Text("$c:\\a\\b\\c$ ")); root.addContent(new Text("$Revision 1.23$")); root.addContent(new Text("file://$root$/some/path/just$file$name.txt")); final Set<String> macros = PathMacrosCollector.getMacroNames(root, null, new PathMacrosImpl()); UsefulTestCase.assertSameElements(macros, "MACro1", "macro4", "mac_ro6", "macr.o7", "mac-ro8", "root"); }
@NotNull static String getTextValue(@NotNull Element element, @NotNull String defaultText) { List<Content> content = element.getContent(); int size = content.size(); StringBuilder builder = null; for (int i = 0; i < size; i++) { Content child = content.get(i); if (child instanceof Text) { String value = child.getValue(); if (builder == null && i == (size - 1)) { return value; } if (builder == null) { builder = new StringBuilder(); } builder.append(value); } } return builder == null ? defaultText : builder.toString(); }
@Nullable private Object serializeItem(@Nullable Object value, Object context, @NotNull SerializationFilter filter) { if (value == null) { LOG.warn("Collection " + myAccessor + " contains 'null' object"); return null; } Binding binding = XmlSerializerImpl.getBinding(value.getClass()); if (binding == null) { Element serializedItem = new Element(annotation == null ? Constants.OPTION : annotation.elementTag()); String attributeName = annotation == null ? Constants.VALUE : annotation.elementValueAttribute(); String serialized = XmlSerializerImpl.convertToString(value); if (attributeName.isEmpty()) { if (!serialized.isEmpty()) { serializedItem.addContent(new Text(serialized)); } } else { serializedItem.setAttribute(attributeName, serialized); } return serializedItem; } else { return binding.serialize(value, context, filter); } }
@Nullable @Override public Object serialize(@NotNull Object o, @Nullable Object context, @NotNull SerializationFilter filter) { Object value = myAccessor.read(o); Element serialized = new Element(myName); if (value == null) { return serialized; } if (myBinding == null) { serialized.addContent(new Text(XmlSerializerImpl.convertToString(value))); } else { Object node = myBinding.serialize(value, serialized, filter); if (node != null && node != serialized) { JDOMUtil.addContent(serialized, node); } } return serialized; }
@Test public void testIsEmptyLine() { assertEquals(false, textWrapperCreator.isBlankLineOrLines(new Text("\n sortpom\n "))); assertEquals(false, textWrapperCreator.isBlankLineOrLines(new Text("sortpom"))); assertEquals(true, textWrapperCreator.isBlankLineOrLines(new Text("\n "))); assertEquals(true, textWrapperCreator.isBlankLineOrLines(new Text(" \n "))); assertEquals(true, textWrapperCreator.isBlankLineOrLines(new Text(" \n\n "))); assertEquals(true, textWrapperCreator.isBlankLineOrLines(new Text("\n\n"))); assertEquals(true, textWrapperCreator.isBlankLineOrLines(new Text(" \r "))); assertEquals(true, textWrapperCreator.isBlankLineOrLines(new Text(" \r\r "))); assertEquals(true, textWrapperCreator.isBlankLineOrLines(new Text("\r\r"))); assertEquals(true, textWrapperCreator.isBlankLineOrLines(new Text(" \r\n "))); assertEquals(true, textWrapperCreator.isBlankLineOrLines(new Text(" \r\n\r\n "))); assertEquals(true, textWrapperCreator.isBlankLineOrLines(new Text("\r\n\r\n"))); assertEquals(false, textWrapperCreator.isBlankLineOrLines(new Text(" "))); }
private static int getHash(int hash, Element element) { hash = sumHash(hash, element.getName()); for (Object object : element.getAttributes()) { Attribute attribute = (Attribute) object; hash = sumHash(hash, attribute.getName()); hash = sumHash(hash, attribute.getValue()); } for (Object o : element.getContent()) { if (o instanceof Element) { hash = getHash(hash, (Element) o); } else if (o instanceof Text) { String text = ((Text) o).getText(); if (!isNullOrWhitespace(text)) { hash = sumHash(hash, text); } } } return hash; }
/** * Get the best content value for a JDOM object. For elements, the content text is returned. * For attributes, the attribute value is returned. For namespaces, the URI is returned. Etc... * @param jdomObject JDOM object such as Element, Attribute, Text, Namespace, Comment, ProcessingInstruction, String * @return Content value for the specified JDOM object * @since 4.2 */ public static String getContentValue( Object jdomObject ) { if(jdomObject == null) { return null; } else if(jdomObject instanceof String) { return (String)jdomObject; } else if(jdomObject instanceof Element) { return ((Element)jdomObject).getText(); } else if(jdomObject instanceof Attribute) { return ((Attribute)jdomObject).getValue(); } else if(jdomObject instanceof Text) { return ((Text)jdomObject).getValue(); } else if(jdomObject instanceof Namespace) { return ((Namespace)jdomObject).getURI(); } else if(jdomObject instanceof Comment) { return ((Comment)jdomObject).getText(); } else if(jdomObject instanceof ProcessingInstruction) { return ((ProcessingInstruction)jdomObject).getData(); } // Default return jdomObject.toString(); }
@Override public Element getXMLElement() { Element result = new Element("localInvocation"); if ((directory != null) && !directory.isEmpty()){ Element directoryElement = new Element("directory"); directoryElement.addContent(new Text(directory)); result.addContent(directoryElement); } if ((shellPrefix != null) && !shellPrefix.isEmpty()) { Element shellPrefixElement = new Element("shellPrefix"); shellPrefixElement.addContent(new Text(shellPrefix)); result.addContent(shellPrefixElement); } if ((linkCommand != null) && !linkCommand.isEmpty()) { Element linkCommandElement = new Element("linkCommand"); linkCommandElement.addContent(new Text(linkCommand)); result.addContent(linkCommandElement); } if (isRetrieveData()) { Element retrieveDataElement = new Element("retrieveData"); result.addContent(retrieveDataElement); } return result; }
private XMLNode createTreeNode(Content content) { XMLNode node = new XMLNode(content); if (content instanceof Parent) { Parent parent = (Parent) content; for (Object child : parent.getContent()) { if (child instanceof Element) node.add(createTreeNode((Content) child)); else if (textSizeLimit != 0 && child instanceof Text) { Text text = (Text) child; if (!text.getTextNormalize().isEmpty()) node.add(createTreeNode(text)); } } } return node; }
/** * @param from * Extract text recursively from this element and its children. * @param insertNewlineBefore * Insert newlines before these children. * @return Concatenated text. */ private static String getText(Element from, List<String> insertNewlineBefore) { StringBuilder sb = new StringBuilder(); for (Object child : from.getContent()) { if (child instanceof Element) { String childAsText = getText((Element) child, insertNewlineBefore).trim(); if (!childAsText.isEmpty()) { if (insertNewlineBefore.contains(((Element) child).getName())) { sb.append('\n'); } else { sb.append(' '); } sb.append(childAsText); } } else if (child instanceof Text) { String cont = ((Text) child).getText().trim(); if (!cont.isEmpty()) { sb.append(' '); sb.append(cont); } } } return sb.toString().trim(); }
public void testCollectMacros() { Element root = new Element("root"); root.addContent(new Text("$MACro1$ some text $macro2$ other text $MACRo3$")); root.addContent(new Text("$macro4$ some text")); root.addContent(new Text("some text$macro5$")); root.addContent(new Text("file:$mac_ro6$")); root.addContent(new Text("jar://$macr.o7$ ")); root.addContent(new Text("$mac-ro8$ ")); root.addContent(new Text("$$$ ")); root.addContent(new Text("$c:\\a\\b\\c$ ")); root.addContent(new Text("$Revision 1.23$")); final Set<String> macros = PathMacrosCollector.getMacroNames(root, null, new PathMacrosImpl()); assertEquals(5, macros.size()); assertTrue(macros.contains("MACro1")); assertTrue(macros.contains("macro4")); assertTrue(macros.contains("mac_ro6")); assertTrue(macros.contains("macr.o7")); assertTrue(macros.contains("mac-ro8")); }
@Override public Object serialize(Object o, Object context, SerializationFilter filter) { Element targetElement = new Element(myTagName); Object value = accessor.read(o); if (!StringUtil.isEmpty(myNameAttribute)) { targetElement.setAttribute(myNameAttribute, myName); } if (value == null) return targetElement; Object node = myBinding.serialize(value, targetElement, filter); if (node instanceof Text) { Text text = (Text)node; targetElement.setAttribute(myValueAttribute, text.getText()); } else { if (targetElement != node) { JDOMUtil.addContent(targetElement, node); } } return targetElement; }
private static void updateSecurityDomain(ConfigContext context, boolean enable) { List<Element> profiles = ConfigSupport.findProfileElements(context.getDocument(), NS_DOMAINS); for (Element profile : profiles) { Element security = profile.getChild("subsystem", NS_SECURITY); if (security != null) { Element domains = security.getChild("security-domains", NS_SECURITY); ConfigSupport.assertExists(domains, "Did not find the <security-domains> element"); Element domain = ConfigSupport.findElementWithAttributeValue(domains, "security-domain", "name", "hawtio-domain", NS_SECURITY); if (enable && domain == null) { URL resource = WildFlyCamelConfigPlugin.class.getResource("/hawtio-security-domain.xml"); domains.addContent(new Text(" ")); domains.addContent(ConfigSupport.loadElementFrom(resource)); domains.addContent(new Text("\n ")); } if (!enable && domain != null) { domain.getParentElement().removeContent(domain); } } } }
public HeaderJDOM(Submitter submitter) { Element source = new Element(SOURCE).setText(MACPAF_APPROVED_SYSTEM_ID); source.addContent(new Element(VERSION).setText(MACPAF_VERSION_NUMBER)); source.addContent(new Element(NAME).setText(MACPAF_NAME_OF_PRODUCT)); AddressJDOM addr = new AddressJDOM(MACPAF_BUSINESS_ADDR1, MACPAF_BUSINESS_ADDR2, MACPAF_BUSINESS_CITY, MACPAF_BUSINESS_STATE, MACPAF_BUSINESS_ZIP, MACPAF_BUSINESS_COUNTRY, MACPAF_BUSINESS_PHONE); List list = new ArrayList(); list.add(new Text(MACPAF_NAME_OF_BUSINESS)); list.addAll(addr.getElements()); log.debug("headerjdom list:" + list); source.addContent( (Content)new Element(CORPORATION).setContent(list)); element.addContent(source); element.addContent(new Element(DATE).setText(SIMPLE_DATE_FORMAT.format(new Date()))); element.addContent(new Element(SUBMITTER).setAttribute(REF, submitter.getId())); element.addContent( (Content)new Element(GEDCOM).addContent(new Element(VERSION).setText(GEDCOM_VERSION_55)). addContent(new Element(FORM).setText(FORM_LINEAGE_LINKED))); element.addContent(new Element(CHARACTER).setText(ANSEL)); element.addContent(new Element(LANGUAGE).setText(LANG_ENGLISH)); }
public void testCollectMacros() { Element root = new Element("root"); root.addContent(new Text("$MACro1$ some text $macro2$ other text $MACRo3$")); root.addContent(new Text("$macro4$ some text")); root.addContent(new Text("some text$macro5$")); root.addContent(new Text("file:$mac_ro6$")); root.addContent(new Text("jar://$macr.o7$ ")); root.addContent(new Text("$mac-ro8$ ")); root.addContent(new Text("$$$ ")); root.addContent(new Text("$c:\\a\\b\\c$ ")); root.addContent(new Text("$Revision 1.23$")); final Set<String> macros = PathMacrosService.getInstance().getMacroNames(root, null, new PathMacrosImpl()); assertEquals(5, macros.size()); assertTrue(macros.contains("MACro1")); assertTrue(macros.contains("macro4")); assertTrue(macros.contains("mac_ro6")); assertTrue(macros.contains("macr.o7")); assertTrue(macros.contains("mac-ro8")); }
@Nullable private Object serializeItem(@Nullable Object value, Object context, @Nonnull SerializationFilter filter) { if (value == null) { LOG.warn("Collection " + myAccessor + " contains 'null' object"); return null; } Binding binding = XmlSerializerImpl.getBinding(value.getClass()); if (binding == null) { Element serializedItem = new Element(annotation == null ? Constants.OPTION : annotation.elementTag()); String attributeName = annotation == null ? Constants.VALUE : annotation.elementValueAttribute(); String serialized = XmlSerializerImpl.convertToString(value); if (attributeName.isEmpty()) { if (!serialized.isEmpty()) { serializedItem.addContent(new Text(serialized)); } } else { serializedItem.setAttribute(attributeName, serialized); } return serializedItem; } else { return binding.serialize(value, context, filter); } }
@Nullable @Override public Object serialize(@Nonnull Object o, @Nullable Object context, @Nonnull SerializationFilter filter) { Object value = myAccessor.read(o); Element serialized = new Element(myName); if (value == null) { return serialized; } if (myBinding == null) { serialized.addContent(new Text(XmlSerializerImpl.convertToString(value))); } else { Object node = myBinding.serialize(value, serialized, filter); if (node != null && node != serialized) { JDOMUtil.addContent(serialized, node); } } return serialized; }
private void init() throws IOException { Document request = MyUtils.toDocument(wsdlDoc .generateRequest(parentOperation)); setDocumentation(); String expression = "//*[local-name()='" + name + "'][1]/text()"; try { XPath xp = XPath.newInstance(expression); Text t = (Text) xp.selectSingleNode(request); String value = t.getText(); if (value.equals("?")) { defaultValue = ""; } else if (value.startsWith("cid:")) { binary = true; defaultValue = ""; } else { defaultValue = value; } } catch (JDOMException e) { throw new IOException("Could not read the generated SOAP using " + expression, e); } }
private void setValue() { try { SAXBuilder parser = new SAXBuilder(); Document jdomDoc = parser.build(new ByteArrayInputStream(response .getBytes())); String expression = "//*[local-name()='" + name + "'][1]/text()"; XPath xp = XPath.newInstance(expression); Text t = (Text) xp.selectSingleNode(jdomDoc); if (t != null) value = t.getText(); } catch (JDOMException | IOException e) { logger.error("Could not set value for output " + name, e); } }
private Element generateElement (String parentName, Span span, List childSpans, THashMap tree) { Element parentElt = new Element (parentName); if (childSpans == null || childSpans.isEmpty ()) { parentElt.setContent (new Text (span.getText ())); } else { List childElts = new ArrayList (childSpans.size()); int start = span.getStartIdx (); int current = 0; for (int i = 0; i < childSpans.size(); i++) { LabeledSpan childSpan = (LabeledSpan) childSpans.get (i); Label childLabel = childSpan.getLabel(); int childStart = childSpan.getStartIdx () - start; if (childStart > current) { childElts.add (new Text (span.getText().substring (current, childStart))); } if (childLabel == backgroundTag) { childElts.add (new Text (childSpan.getText())); } else { String name = childLabel.getEntry ().toString(); List grandchildren = (List) tree.get (childSpan); childElts.add (generateElement (name, childSpan, grandchildren, tree)); } current = childSpan.getEndIdx () - start; } if (current < span.getEndIdx ()) childElts.add (new Text (span.getText().substring (current))); parentElt.addContent (childElts); } return parentElt; }
@Override public boolean skipPathMacros(Text element) { for (PathMacroFilter filter : myFilters) { if (filter.skipPathMacros(element)) return true; } return false; }
@Override public boolean recursePathMacros(Text element) { for (PathMacroFilter filter : myFilters) { if (filter.recursePathMacros(element)) return true; } return false; }
Wrapper<Content> createWrapper(Text text) { if (isSingleNewLine(text)) { return SingleNewlineInTextWrapper.INSTANCE; } else if (isBlankLineOrLines(text)) { return new UnsortedWrapper<>(new NewlineText()); } return new UnsortedWrapper<>(text); }
protected String getElementText(Element elem,String xpath) throws JDOMException { Text text = ((Text)XPath.selectSingleNode(elem, xpath+"/text()")); if(null == text) { return ""; } return text.getText(); }
public String getTextStringValue(Object obj) { if ( obj instanceof Text ) { return ((Text)obj).getText(); } if ( obj instanceof CDATA ) { return ((CDATA)obj).getText(); } return ""; }
public String getElementStringValue(Object obj) { Element elem = (Element) obj; StringBuffer buf = new StringBuffer(); List content = elem.getContent(); Iterator contentIter = content.iterator(); Object each = null; while ( contentIter.hasNext() ) { each = contentIter.next(); if ( each instanceof Text ) { buf.append( ((Text)each).getText() ); } else if ( each instanceof CDATA ) { buf.append( ((CDATA)each).getText() ); } else if ( each instanceof Element ) { buf.append( getElementStringValue( each ) ); } } return buf.toString(); }
public String translateNamespacePrefixToUri(String prefix, Object context) { Element element = null; if ( context instanceof Element ) { element = (Element) context; } else if ( context instanceof Text ) { element = (Element)((Text)context).getParent(); } else if ( context instanceof Attribute ) { element = ((Attribute)context).getParent(); } else if ( context instanceof XPathNamespace ) { element = ((XPathNamespace)context).getJDOMElement(); } else if ( context instanceof Comment ) { element = (Element)((Comment)context).getParent(); } else if ( context instanceof ProcessingInstruction ) { element = (Element)((ProcessingInstruction)context).getParent(); } if ( element != null ) { Namespace namespace = element.getNamespace( prefix ); if ( namespace != null ) { return namespace.getURI(); } } return null; }
public void testJaxen148() throws JaxenException, JDOMException, IOException { String xml = "<xml-document><nodes><node>" + "\ntest\n" + "</node></nodes></xml-document>"; SAXBuilder builder = new SAXBuilder(); Document document = builder.build( new InputSource( new StringReader(xml) ) ); JDOMXPath x = new JDOMXPath("/xml-document/nodes/node/text()"); Text t = (Text) x.selectSingleNode(document); assertEquals( "\ntest\n" , t.getText() ); }
private Element generateElement (String parentName, Span span, List childSpans, HashMap tree) { Element parentElt = new Element (parentName); if (childSpans == null || childSpans.isEmpty ()) { parentElt.setContent (new Text (span.getText ())); } else { List childElts = new ArrayList (childSpans.size()); int start = span.getStartIdx (); int current = 0; for (int i = 0; i < childSpans.size(); i++) { LabeledSpan childSpan = (LabeledSpan) childSpans.get (i); Label childLabel = childSpan.getLabel(); int childStart = childSpan.getStartIdx () - start; if (childStart > current) { childElts.add (new Text (span.getText().substring (current, childStart))); } if (childLabel == backgroundTag) { childElts.add (new Text (childSpan.getText())); } else { String name = childLabel.getEntry ().toString(); List grandchildren = (List) tree.get (childSpan); childElts.add (generateElement (name, childSpan, grandchildren, tree)); } current = childSpan.getEndIdx () - start; } if (current < span.getEndIdx ()) childElts.add (new Text (span.getText().substring (current))); parentElt.addContent (childElts); } return parentElt; }
/** * {@inheritDoc} */ public Content encodeXML(final String name, Namespace ns) { Element element = new Element(name, ns); //Element element = new Element(name, Namespace.getNamespace("llrp",LLRPConstants.LLRPNAMESPACE)); element.setContent(new Text(toString())); return element; }