List of usage examples for org.jdom2 Element getTextNormalize
public String getTextNormalize()
From source file:ca.nrc.cadc.xml.JsonOutputter.java
License:Open Source License
private void writeElement(Element e, PrintWriter w, int i, boolean listItem) throws IOException { boolean childListItem = listElementNames.contains(e.getName()); indent(w, i);//from w w w. ja v a 2s .c o m if (!listItem) { // write key w.print(QUOTE); if (StringUtil.hasText(e.getNamespacePrefix())) { w.print(e.getNamespacePrefix()); w.print(":"); } w.print(e.getName()); w.print(QUOTE); w.print(" : "); } // write value w.print("{"); boolean multiLine = true; boolean children = false; boolean attrs = writeAttributes(e, w, i + 1); if (childListItem) { // in badgerfish, this would be the name of child elements but prefer $ since [] // is the value of e and e is a list of children; $ is also consistent with how // we (and badgerfish) handle leaf values inside elements: { "$" : value } if (attrs) { w.print(","); } indent(w, i + 1); w.print(QUOTE); w.print("$"); w.print(QUOTE); w.print(" : "); w.print("["); children = writeChildElements(e, w, i + 2, childListItem, attrs); indent(w, i + 1); w.print("]"); } else children = writeChildElements(e, w, i + 1, childListItem, attrs); if (!children && !childListItem) // plain value { if (attrs) { w.print(","); indent(w, i + 1); } else multiLine = false; String sval = e.getTextNormalize(); w.print(QUOTE); w.print("$"); w.print(QUOTE); w.print(" : "); if (isBoolean(e.getName(), sval) || isNumeric(e.getName(), sval)) w.print(sval); else { w.print(QUOTE); w.print(sval); w.print(QUOTE); } } if (multiLine) indent(w, i); w.print("}"); }
From source file:com.abyala.decisiontree.SimpleDecisionTreeParser.java
License:Open Source License
private StringResultAttribute parseResultStringAttribute(final Element element, final String name, final Method method) throws DecisionTreeParserException { final StringResultAttribute.Builder builder = new StringResultAttribute.Builder(name, method); for (Element value : element.getChildren("value")) { final String text = value.getTextNormalize(); builder.addEnumValue(text);//from w w w. j a va 2 s . c o m if ("true".equals(value.getAttributeValue("default"))) { builder.setDefaultValue(text); } } return builder.build(); }
From source file:com.abyala.decisiontree.SimpleDecisionTreeParser.java
License:Open Source License
InputType parseStringInputType(final Element typeElement) throws DecisionTreeParserException { final String name = typeElement.getAttributeValue("name"); final StringInputType.Builder builder = new StringInputType.Builder(name); boolean hasDefaultValue = false; for (Element child : typeElement.getChildren("value")) { final boolean isDefault = "true".equals(child.getAttributeValue("default")); if (isDefault && hasDefaultValue) { throw new DecisionTreeParserException( "Input-type \"" + name + "\" may not have more than one default type."); }//from ww w . j a v a 2s .c om builder.addEnumValue(child.getTextNormalize(), isDefault); hasDefaultValue = isDefault; } return builder.build(); }
From source file:com.c4om.autoconf.ulysses.extra.svinchangesetgenerator.SVINChangesetGenerator.java
License:Apache License
/** * Recursive method that mixes two XML trees in the following way: * <ul>//from w w w . ja va 2 s . c o m * <li>If a child exists at the source leaf but not at destination, the * element is copied as a child to the destination leaf</li> * <li>If a child exists at both the source and the destination leafs and * the source child has children, this method is recursively called to mix * both children</li> * </ul> * Some important remarks: * <ul> * <li>Equality comparison is not made via common methods but via * {@link JDOMUtils#elementsEqualAtCNameAndAttributes(Element, Element)} * .</li> * <li>Results of this method are returned as changes to the * destinationLeaf.</li> * <li>An attribute may be appended to all the elements added to the * destination leaf by this method.</li> * <li>Elements of a concrete namespace can be ignored by this method, if desired.</li> * </ul> * * @param sourceLeaf * The source leaf to mix into the destination leaf. It remains * unchanged. * @param destinationLeaf * The destination leaf, where the source leaf will be mixed * into. Results will be returned as changes at this element, so * IT WILL NOT REMAIN UNCHANGED AFTER THIS METHOD (normally). You * should consider using {@link Element#clone()} if necessary. * @param metadataAttributeToAppend * an attribute to be appended to each element added by this * method to the destinationLeaf. If a node with descendants is * added, the attribute will be added only to the top-level * element (not to all the descendants). If null, no attribute * will be added. * @param ignoreNamespaceURIIfTextPresent any element whose namespace URI matches this one * will be ignored if the source element has text content. * If null, no element is ignored. */ private void mixTreesRecursive(Element sourceLeaf, Element destinationLeaf, Attribute metadataAttributeToAppend, String ignoreNamespaceURIIfTextPresent) { List<Content> sourceLeafContent = sourceLeaf.getContent(); //j is the index for "only-element" content for (int i = 0, j = 0; i < sourceLeafContent.size(); i++) { Content currentContent = sourceLeafContent.get(i); if (!(currentContent instanceof Element)) { continue; } Element currentSourceChild = (Element) currentContent; Element currentDestinationChild = searchElementEqualAtCNameAndAttributes(destinationLeaf.getChildren(), currentSourceChild); if (currentDestinationChild == null) { if (ignoreNamespaceURIIfTextPresent != null && !destinationLeaf.getTextNormalize().equals("") && ignoreNamespaceURIIfTextPresent.equals(currentSourceChild.getNamespaceURI())) { continue; } // There is not equivalent node at destination, so we copy the // whole currentSourceChild. Element elementToAdd = currentSourceChild.clone(); if (metadataAttributeToAppend != null) { elementToAdd.setAttribute(metadataAttributeToAppend.clone()); } destinationLeaf.addContent(j, elementToAdd); } else { // Element exists at destination. If it has children, we recurse // to fill them (they might be not completely filled). if (currentSourceChild.getChildren().size() > 0) { mixTreesRecursive(currentSourceChild, currentDestinationChild, metadataAttributeToAppend, ignoreNamespaceURIIfTextPresent); } } j++; } }
From source file:com.musala.cron.RefreshDbTask.java
License:Apache License
@Scheduled(fixedRate = 300000) @Transactional//from w w w . j a v a2 s . co m public void printMe() { logger.info("Cron task is started"); for (Site rssFeedSite : siteService.findAll()) { logger.info("--------------> Reading information for site: " + rssFeedSite.getRssLink()); SAXBuilder builder = new SAXBuilder(); Document doc = null; try { doc = builder.build(new URL(rssFeedSite.getRssLink())); if (rssFeedSite.getLastVisitDateTag() != null) { Element root = doc.getRootElement(); ElementFilter filter = new ElementFilter(rssFeedSite.getLastVisitDateTag()); String currentLastVisitedDate = null; for (Element c : root.getDescendants(filter)) { currentLastVisitedDate = c.getTextNormalize(); } if (currentLastVisitedDate != null && currentLastVisitedDate.equals(rssFeedSite.getLastVisitDate())) { logger.info("--------------> Rss {} is not changed", rssFeedSite.getRssLink()); } else { logger.info("--------------> Rss {} is changed", rssFeedSite.getRssLink()); rssFeedSite.setLastVisitDate(currentLastVisitedDate); subject.processRss(rssFeedSite); } } } catch (JDOMException | IOException e) { logger.warn("Error occurred during reading of rss", e); } } }
From source file:cz.muni.fi.mir.mathmlcanonicalization.modules.MrowNormalizer.java
License:Apache License
/** * Test if element is an operator representing an opening or closing * parenthesis according to properties//from w w w . ja va 2 s .c o m * * @param element element to test * @param propertyName name of property specifiyng opening or closing * parentheses * @return true if element is a parentheses according to propertyName */ private Boolean isParenthesis(final Element element, final String propertyName) { assert element != null && propertyName != null && isProperty(propertyName); if (!element.getName().equals(OPERATOR)) { return false; } return getPropertySet(propertyName).contains(element.getTextNormalize()); }
From source file:de.nava.informa.parsers.RSS_2_0_Parser.java
License:Open Source License
/** * @see de.nava.informa.core.ChannelParserIF#parse(de.nava.informa.core.ChannelBuilderIF, org.jdom2.Element) */// w w w.jav a 2s . c o m public ChannelIF parse(ChannelBuilderIF cBuilder, Element root) throws ParseException { if (cBuilder == null) { throw new RuntimeException("Without builder no channel can be created."); } Date dateParsed = new Date(); logger.debug("start parsing."); Namespace defNS = ParserUtils.getDefaultNS(root); if (defNS == null) { defNS = Namespace.NO_NAMESPACE; logger.info("No default namespace found."); } Namespace dcNS = ParserUtils.getNamespace(root, "dc"); // fall back to default name space if (dcNS == null) { dcNS = defNS; } // Content namespace Namespace contentNS = ParserUtils.getNamespace(root, "content"); // fall back to default name space if (contentNS == null) { contentNS = defNS; } ParserUtils.matchCaseOfChildren(root, "channel"); // Get the channel element (only one occurs) Element channel = root.getChild("channel", defNS); if (channel == null) { logger.warn("Channel element could not be retrieved from feed."); throw new ParseException("No channel element found in feed."); } // --- read in channel information ParserUtils.matchCaseOfChildren(channel, new String[] { "title", "description", "link", "language", "item", "image", "textinput", "copyright", "rating", "docs", "generator", "pubDate", "lastBuildDate", "category", "managingEditor", "webMaster", "cloud" }); // 1 title element ChannelIF chnl = cBuilder.createChannel(channel, channel.getChildTextTrim("title", defNS)); // set channel format chnl.setFormat(ChannelFormat.RSS_2_0); // 1 description element chnl.setDescription(channel.getChildTextTrim("description", defNS)); // 1 link element chnl.setSite(ParserUtils.getURL(channel.getChildTextTrim("link", defNS))); // 1 language element chnl.setLanguage(channel.getChildTextTrim("language", defNS)); // 1..n item elements List items = channel.getChildren("item", defNS); for (Object item1 : items) { Element item = (Element) item1; ParserUtils.matchCaseOfChildren(item, new String[] { "title", "link", "encoded", "description", "subject", "category", "pubDate", "date", "author", "creator", "comments", "guid", "source", "enclosure" }); // get title element Element elTitle = item.getChild("title", defNS); String strTitle = "<No Title>"; if (elTitle != null) { strTitle = elTitle.getTextTrim(); } if (logger.isDebugEnabled()) { logger.debug("Item element found (" + strTitle + ")."); } // get link element Element elLink = item.getChild("link", defNS); String strLink = ""; if (elLink != null) { strLink = elLink.getTextTrim(); } // get description element Element elDesc = item.getChild("encoded", contentNS); if (elDesc == null) { elDesc = item.getChild("description", defNS); } String strDesc = ""; if (elDesc != null) { strDesc = elDesc.getTextTrim(); } // generate new RSS item (link to article) ItemIF rssItem = cBuilder.createItem(item, chnl, strTitle, strDesc, ParserUtils.getURL(strLink)); // get subject element Element elSubject = item.getChild("subject", defNS); if (elSubject == null) { // fallback mechanism: get dc:subject element elSubject = item.getChild("subject", dcNS); } if (elSubject != null) { rssItem.setSubject(elSubject.getTextTrim()); } // get category list // get list of <category> elements List listCategory = item.getChildren("category", defNS); if (listCategory.size() < 1) { // fallback mechanism: get dc:category element listCategory = item.getChildren("category", dcNS); } if (listCategory.size() > 0) { RecursiveHashtable<String> catTable = new RecursiveHashtable<String>(); // for each category, parse hierarchy for (Object aListCategory : listCategory) { RecursiveHashtable<String> currTable = catTable; Element elCategory = (Element) aListCategory; // get contents of category element String[] titles = elCategory.getTextNormalize().split("/"); for (String title : titles) { // tokenize category string to extract out hierarchy if (!currTable.containsKey(title)) { // if token does not exist in current map, add it with child Hashtable currTable.put(title, new RecursiveHashtable<String>()); } // reset current Hashtable to child's Hashtable then iterate to next token currTable = currTable.get(title); } } ArrayList<CategoryIF> catList = new ArrayList<CategoryIF>(); // transform cat list & hierarchy into list of CategoryIF elements Enumeration<String> enumCategories = catTable.keys(); while (enumCategories.hasMoreElements()) { String key = enumCategories.nextElement(); // build category list: getCategoryList(parent, title, children) CategoryIF cat = getCategoryList(null, key, catTable.get(key)); catList.add(cat); } if (catList.size() > 0) { // if categories were actually created, then add list to item node rssItem.setCategories(catList); } } // get publication date Element elDate = item.getChild("pubDate", defNS); if (elDate == null) { // fallback mechanism: get dc:date element elDate = item.getChild("date", dcNS); } if (elDate != null) { rssItem.setDate(ParserUtils.getDate(elDate.getTextTrim())); } rssItem.setFound(dateParsed); // get Author element Element elAuthor = item.getChild("author", defNS); if (elAuthor == null) { // fallback mechanism: get dc:creator element elAuthor = item.getChild("creator", dcNS); } if (elAuthor != null) rssItem.setCreator(elAuthor.getTextTrim()); // get Comments element Element elComments = item.getChild("comments", defNS); String strComments = ""; if (elComments != null) { strComments = elComments.getTextTrim(); } rssItem.setComments(ParserUtils.getURL(strComments)); // get guid element Element elGuid = item.getChild("guid", defNS); if (elGuid != null) { String guidUrl = elGuid.getTextTrim(); if (guidUrl != null) { boolean permaLink = true; Attribute permaLinkAttribute = elGuid.getAttribute("isPermaLink", defNS); if (permaLinkAttribute != null) { String permaLinkStr = permaLinkAttribute.getValue(); if (permaLinkStr != null) { permaLink = Boolean.valueOf(permaLinkStr); } } ItemGuidIF itemGuid = cBuilder.createItemGuid(rssItem, guidUrl, permaLink); rssItem.setGuid(itemGuid); } } // get source element Element elSource = item.getChild("source", defNS); if (elSource != null) { String sourceName = elSource.getTextTrim(); Attribute sourceAttribute = elSource.getAttribute("url", defNS); if (sourceAttribute != null) { String sourceLocation = sourceAttribute.getValue().trim(); ItemSourceIF itemSource = cBuilder.createItemSource(rssItem, sourceName, sourceLocation, null); rssItem.setSource(itemSource); } } // get enclosure element Element elEnclosure = item.getChild("enclosure", defNS); if (elEnclosure != null) { URL location = null; String type = null; int length = -1; Attribute urlAttribute = elEnclosure.getAttribute("url", defNS); if (urlAttribute != null) { location = ParserUtils.getURL(urlAttribute.getValue().trim()); } Attribute typeAttribute = elEnclosure.getAttribute("type", defNS); if (typeAttribute != null) { type = typeAttribute.getValue().trim(); } Attribute lengthAttribute = elEnclosure.getAttribute("length", defNS); if (lengthAttribute != null) { try { length = Integer.parseInt(lengthAttribute.getValue().trim()); } catch (NumberFormatException e) { logger.warn(e); } } ItemEnclosureIF itemEnclosure = cBuilder.createItemEnclosure(rssItem, location, type, length); rssItem.setEnclosure(itemEnclosure); } } // 0..1 image element Element image = channel.getChild("image", defNS); if (image != null) { ParserUtils.matchCaseOfChildren(image, new String[] { "title", "url", "link", "width", "height", "description" }); ImageIF rssImage = cBuilder.createImage(image.getChildTextTrim("title", defNS), ParserUtils.getURL(image.getChildTextTrim("url", defNS)), ParserUtils.getURL(image.getChildTextTrim("link", defNS))); Element imgWidth = image.getChild("width", defNS); if (imgWidth != null) { try { rssImage.setWidth(Integer.parseInt(imgWidth.getTextTrim())); } catch (NumberFormatException e) { logger.warn("Error parsing width: " + e.getMessage()); } } Element imgHeight = image.getChild("height", defNS); if (imgHeight != null) { try { rssImage.setHeight(Integer.parseInt(imgHeight.getTextTrim())); } catch (NumberFormatException e) { logger.warn("Error parsing height: " + e.getMessage()); } } Element imgDescr = image.getChild("description", defNS); if (imgDescr != null) { rssImage.setDescription(imgDescr.getTextTrim()); } chnl.setImage(rssImage); } // 0..1 textinput element Element txtinp = channel.getChild("textinput", defNS); if (txtinp != null) { ParserUtils.matchCaseOfChildren(txtinp, new String[] { "title", "description", "name", "link" }); TextInputIF rssTextInput = cBuilder.createTextInput(txtinp.getChildTextTrim("title", defNS), txtinp.getChildTextTrim("description", defNS), txtinp.getChildTextTrim("name", defNS), ParserUtils.getURL(txtinp.getChildTextTrim("link", defNS))); chnl.setTextInput(rssTextInput); } // 0..1 copyright element Element copyright = channel.getChild("copyright", defNS); if (copyright != null) { chnl.setCopyright(copyright.getTextTrim()); } // 0..1 Rating element Element rating = channel.getChild("rating", defNS); if (rating != null) { chnl.setRating(rating.getTextTrim()); } // 0..1 Docs element Element docs = channel.getChild("docs", defNS); if (docs != null) { chnl.setDocs(docs.getTextTrim()); } // 0..1 Generator element Element generator = channel.getChild("generator", defNS); if (generator != null) { chnl.setGenerator(generator.getTextTrim()); } // 0..1 ttl element Element ttl = channel.getChild("ttl", defNS); if (ttl != null) { String ttlValue = ttl.getTextTrim(); try { chnl.setTtl(Integer.parseInt(ttlValue)); } catch (NumberFormatException e) { logger.warn("Invalid TTL format: '" + ttlValue + "'"); } } // 0..1 pubDate element Element pubDate = channel.getChild("pubDate", defNS); if (pubDate != null) { chnl.setPubDate(ParserUtils.getDate(pubDate.getTextTrim())); } // 0..1 lastBuildDate element Element lastBuildDate = channel.getChild("lastBuildDate", defNS); if (lastBuildDate != null) { chnl.setLastBuildDate(ParserUtils.getDate(lastBuildDate.getTextTrim())); } // get category list // get list of <category> elements List listCategory = channel.getChildren("category", defNS); if (listCategory.size() < 1) { // fallback mechanism: get dc:category element listCategory = channel.getChildren("category", dcNS); } if (listCategory.size() > 0) { RecursiveHashtable<String> catTable = new RecursiveHashtable<String>(); // for each category, parse hierarchy for (Object aListCategory : listCategory) { RecursiveHashtable<String> currTable = catTable; Element elCategory = (Element) aListCategory; // get contents of category element String[] titles = elCategory.getTextNormalize().split("/"); for (String title : titles) { // tokenize category string to extract out hierarchy if (!currTable.containsKey(title)) { // if token does not exist in current map, add it with child Hashtable currTable.put(title, new RecursiveHashtable<String>()); } // reset current Hashtable to child's Hashtable then iterate to next token currTable = currTable.get(title); } } ArrayList<CategoryIF> catList = new ArrayList<CategoryIF>(); // transform cat list & hierarchy into list of CategoryIF elements Enumeration<String> enumCategories = catTable.keys(); while (enumCategories.hasMoreElements()) { String key = enumCategories.nextElement(); // build category list: getCategoryList(parent, title, children) CategoryIF cat = getCategoryList(null, key, catTable.get(key)); catList.add(cat); } if (catList.size() > 0) { // if categories were actually created, then add list to item node chnl.setCategories(catList); } } // 0..1 managingEditor element Element managingEditor = channel.getChild("managingEditor", defNS); if (managingEditor != null) { chnl.setCreator(managingEditor.getTextTrim()); } // 0..1 webMaster element Element webMaster = channel.getChild("webMaster", defNS); if (webMaster != null) { chnl.setPublisher(webMaster.getTextTrim()); } // 0..1 cloud element Element cloud = channel.getChild("cloud", defNS); if (cloud != null) { String _port = cloud.getAttributeValue("port", defNS); int port = -1; if (_port != null) { try { port = Integer.parseInt(_port); } catch (NumberFormatException e) { logger.warn(e); } } chnl.setCloud(cBuilder.createCloud(cloud.getAttributeValue("domain", defNS), port, cloud.getAttributeValue("path", defNS), cloud.getAttributeValue("registerProcedure", defNS), cloud.getAttributeValue("protocol", defNS))); } chnl.setLastUpdated(dateParsed); // 0..1 skipHours element // 0..1 skipDays element return chnl; }
From source file:de.smartics.maven.alias.domain.AliasesProcessor.java
License:Apache License
private void appendApplyTos(final Element extensionElement, final AliasExtension.Builder builder) { final Element applyToElement = extensionElement.getChild("apply-to", nsAlias); if (applyToElement != null) { for (final Element groupElement : applyToElement.getChildren("group", nsAlias)) { final String group = groupElement.getTextNormalize(); builder.addGroup(group);/*from w w w .j a va 2 s.c o m*/ } for (final Element aliasElement : applyToElement.getChildren("alias", nsAlias)) { final String alias = aliasElement.getTextNormalize(); builder.addAlias(alias); } } }
From source file:de.smartics.maven.alias.domain.AliasesProcessor.java
License:Apache License
private Alias createAlias(final Element aliasElement) { final Alias.Builder builder = new Alias.Builder(); final Attribute env = aliasElement.getAttribute("env"); final Element command = aliasElement.getChild("command", nsAlias); final String normalizedCommandText = command.getTextNormalize(); builder.withName(aliasElement.getChildTextNormalize("name", nsAlias)).withCommand(normalizedCommandText); final String comment = readComment(aliasElement); builder.withComment(comment);/*www .j a v a 2s .c om*/ if (env != null) { builder.withEnv(env.getValue()); } final Attribute args = command.getAttribute("passArgs"); if (args != null && !Boolean.parseBoolean(args.getValue())) { builder.withPassArgs(false); } return builder.build(); }
From source file:de.smartics.maven.plugin.jboss.modules.parser.AbstractModuleClusionAdder.java
License:Apache License
public void addClusions(final Element matchElement) { final Element clusionsElement = matchElement.getChild(collectionElementId, NS); if (clusionsElement != null) { final List<Element> clusionElements = clusionsElement.getChildren(elementId, NS); for (final Element clusionElement : clusionElements) { final String name = clusionElement.getTextNormalize(); final ModuleClusion clusion = new ModuleClusion(name); add(clusion);/*from ww w .j ava2 s . co m*/ } } }