List of usage examples for org.jdom2 Element getText
public String getText()
From source file:com.rometools.modules.georss.GMLParser.java
License:Apache License
private static PositionList parsePosList(final Element element) { final String coordinates = element.getText(); final String[] coord = Strings.trimToEmpty(coordinates).split(" "); final PositionList posList = new PositionList(); for (int i = 0; i < coord.length; i += 2) { posList.add(Double.parseDouble(coord[i]), Double.parseDouble(coord[i + 1])); }//www . j av a 2 s. c om return posList; }
From source file:com.rometools.modules.georss.GMLParser.java
License:Apache License
static Module parseGML(final Element element) { GeoRSSModule geoRSSModule = null;// ww w.j a va2 s.c o m final Element pointElement = element.getChild("Point", GeoRSSModule.GML_NS); final Element lineStringElement = element.getChild("LineString", GeoRSSModule.GML_NS); final Element polygonElement = element.getChild("Polygon", GeoRSSModule.GML_NS); final Element envelopeElement = element.getChild("Envelope", GeoRSSModule.GML_NS); if (pointElement != null) { final Element posElement = pointElement.getChild("pos", GeoRSSModule.GML_NS); if (posElement != null) { geoRSSModule = new GMLModuleImpl(); final String coordinates = posElement.getText(); final String[] coord = Strings.trimToEmpty(coordinates).split(" "); final Position pos = new Position(Double.parseDouble(coord[0]), Double.parseDouble(coord[1])); geoRSSModule.setGeometry(new Point(pos)); } } else if (lineStringElement != null) { final Element posListElement = lineStringElement.getChild("posList", GeoRSSModule.GML_NS); if (posListElement != null) { geoRSSModule = new GMLModuleImpl(); geoRSSModule.setGeometry(new LineString(parsePosList(posListElement))); } } else if (polygonElement != null) { Polygon poly = null; // The external ring final Element exteriorElement = polygonElement.getChild("exterior", GeoRSSModule.GML_NS); if (exteriorElement != null) { final Element linearRingElement = exteriorElement.getChild("LinearRing", GeoRSSModule.GML_NS); if (linearRingElement != null) { final Element posListElement = linearRingElement.getChild("posList", GeoRSSModule.GML_NS); if (posListElement != null) { if (poly == null) { poly = new Polygon(); } poly.setExterior(new LinearRing(parsePosList(posListElement))); } } } // The internal rings (holes) final List<Element> interiorElementList = polygonElement.getChildren("interior", GeoRSSModule.GML_NS); final Iterator<Element> it = interiorElementList.iterator(); while (it.hasNext()) { final Element interiorElement = it.next(); if (interiorElement != null) { final Element linearRingElement = interiorElement.getChild("LinearRing", GeoRSSModule.GML_NS); if (linearRingElement != null) { final Element posListElement = linearRingElement.getChild("posList", GeoRSSModule.GML_NS); if (posListElement != null) { if (poly == null) { poly = new Polygon(); } poly.getInterior().add(new LinearRing(parsePosList(posListElement))); } } } } if (poly != null) { geoRSSModule = new GMLModuleImpl(); geoRSSModule.setGeometry(poly); } } else if (envelopeElement != null) { final Element lowerElement = envelopeElement.getChild("lowerCorner", GeoRSSModule.GML_NS); final Element upperElement = envelopeElement.getChild("upperCorner", GeoRSSModule.GML_NS); if (lowerElement != null && upperElement != null) { geoRSSModule = new GMLModuleImpl(); final String lowerCoordinates = lowerElement.getText(); final String[] lowerCoord = Strings.trimToEmpty(lowerCoordinates).split(" "); final String upperCoordinates = upperElement.getText(); final String[] upperCoord = Strings.trimToEmpty(upperCoordinates).split(" "); final Envelope envelope = new Envelope(Double.parseDouble(lowerCoord[0]), Double.parseDouble(lowerCoord[1]), Double.parseDouble(upperCoord[0]), Double.parseDouble(upperCoord[1])); geoRSSModule.setGeometry(envelope); } } return geoRSSModule; }
From source file:com.rometools.modules.georss.SimpleParser.java
License:Apache License
private static PositionList parsePosList(final Element element) { PositionList posList = null;/*from w w w .j a v a2 s. c om*/ final String coordinates = Strings.trimToNull(element.getText()); if (coordinates != null) { posList = new PositionList(); final String[] coord = coordinates.split(" "); for (int i = 0; i < coord.length; i += 2) { final double latitude = Double.parseDouble(coord[i]); final double longitude = Double.parseDouble(coord[i + 1]); posList.add(latitude, longitude); } } return posList; }
From source file:com.rometools.modules.georss.SimpleParser.java
License:Apache License
static Module parseSimple(final Element element) { final Element pointElement = element.getChild("point", GeoRSSModule.SIMPLE_NS); final Element lineElement = element.getChild("line", GeoRSSModule.SIMPLE_NS); final Element polygonElement = element.getChild("polygon", GeoRSSModule.SIMPLE_NS); final Element boxElement = element.getChild("box", GeoRSSModule.SIMPLE_NS); final Element whereElement = element.getChild("where", GeoRSSModule.SIMPLE_NS); GeoRSSModule geoRSSModule = null;/* w ww . j a va2 s . c om*/ if (pointElement != null) { final String coordinates = Strings.trimToNull(pointElement.getText()); if (coordinates != null) { final String[] coord = coordinates.split(" "); if (coord.length == 2) { final Double latitude = Doubles.parse(coord[0]); final Double longitude = Doubles.parse(coord[1]); if (latitude != null && longitude != null) { final Position pos = new Position(latitude, longitude); final Point point = new Point(pos); geoRSSModule = new SimpleModuleImpl(); geoRSSModule.setGeometry(point); } } } } else if (lineElement != null) { final PositionList posList = parsePosList(lineElement); if (posList != null) { final LineString lineString = new LineString(posList); geoRSSModule = new SimpleModuleImpl(); geoRSSModule.setGeometry(lineString); } } else if (polygonElement != null) { final PositionList posList = parsePosList(polygonElement); if (posList != null) { final LinearRing linearRing = new LinearRing(posList); final Polygon poly = new Polygon(); poly.setExterior(linearRing); geoRSSModule = new SimpleModuleImpl(); geoRSSModule.setGeometry(poly); } } else if (boxElement != null) { final String coordinates = Strings.trimToNull(boxElement.getText()); if (coordinates != null) { final String[] coord = coordinates.split(" "); final double bottom = Double.parseDouble(coord[0]); final double left = Double.parseDouble(coord[1]); final double top = Double.parseDouble(coord[2]); final double right = Double.parseDouble(coord[3]); final Envelope envelope = new Envelope(bottom, left, top, right); geoRSSModule = new SimpleModuleImpl(); geoRSSModule.setGeometry(envelope); } } else if (whereElement != null) { geoRSSModule = (GeoRSSModule) GMLParser.parseGML(whereElement); } return geoRSSModule; }
From source file:com.rometools.modules.georss.W3CGeoParser.java
License:Apache License
static Module parseW3C(final Element element) { GeoRSSModule geoRSSModule = null;//from w ww .j a v a2s. c o m // do we have an optional "Point" element ? Element pointElement = element.getChild("Point", GeoRSSModule.W3CGEO_NS); // we don't have an optional "Point" element if (pointElement == null) { pointElement = element; } final Element lat = pointElement.getChild("lat", GeoRSSModule.W3CGEO_NS); Element lng = pointElement.getChild("long", GeoRSSModule.W3CGEO_NS); if (lng == null) { lng = pointElement.getChild("lon", GeoRSSModule.W3CGEO_NS); } if (lat != null && lng != null) { geoRSSModule = new W3CGeoModuleImpl(); final String latTxt = Strings.trimToNull(lat.getText()); final String lngTxt = Strings.trimToNull(lng.getText()); if (latTxt != null && lngTxt != null) { final double latitude = Double.parseDouble(lat.getText()); final double longitude = Double.parseDouble(lng.getText()); final Position position = new Position(latitude, longitude); final Point point = new Point(position); geoRSSModule.setGeometry(point); } } return geoRSSModule; }
From source file:com.rometools.modules.itunes.io.ITunesParser.java
License:Open Source License
@Override public com.rometools.rome.feed.module.Module parse(final Element element, final Locale locale) { AbstractITunesObject module = null;/* w w w . jav a 2s . c o m*/ if (element.getName().equals("channel")) { final FeedInformationImpl feedInfo = new FeedInformationImpl(); module = feedInfo; // Now I am going to get the channel specific tags final Element owner = element.getChild("owner", ns); if (owner != null) { final Element name = owner.getChild("name", ns); if (name != null) { feedInfo.setOwnerName(name.getValue().trim()); } final Element email = owner.getChild("email", ns); if (email != null) { feedInfo.setOwnerEmailAddress(email.getValue().trim()); } } final Element image = element.getChild("image", ns); if (image != null && image.getAttributeValue("href") != null) { try { final URL imageURL = new URL(image.getAttributeValue("href").trim()); feedInfo.setImage(imageURL); } catch (final MalformedURLException e) { LOG.debug("Malformed URL Exception reading itunes:image tag: {}", image.getAttributeValue("href")); } } final List<Element> categories = element.getChildren("category", ns); for (final Element element2 : categories) { final Element category = element2; if (category != null && category.getAttribute("text") != null) { final Category cat = new Category(); cat.setName(category.getAttribute("text").getValue().trim()); final Element subcategory = category.getChild("category", ns); if (subcategory != null && subcategory.getAttribute("text") != null) { final Subcategory subcat = new Subcategory(); subcat.setName(subcategory.getAttribute("text").getValue().trim()); cat.setSubcategory(subcat); } feedInfo.getCategories().add(cat); } } } else if (element.getName().equals("item")) { final EntryInformationImpl entryInfo = new EntryInformationImpl(); module = entryInfo; // Now I am going to get the item specific tags final Element duration = element.getChild("duration", ns); if (duration != null && duration.getValue() != null) { final Duration dur = new Duration(duration.getValue().trim()); entryInfo.setDuration(dur); } } if (module != null) { // All these are common to both Channel and Item final Element author = element.getChild("author", ns); if (author != null && author.getText() != null) { module.setAuthor(author.getText()); } final Element block = element.getChild("block", ns); if (block != null) { module.setBlock(true); } final Element explicit = element.getChild("explicit", ns); if (explicit != null && explicit.getValue() != null && explicit.getValue().trim().equalsIgnoreCase("yes")) { module.setExplicit(true); } final Element keywords = element.getChild("keywords", ns); if (keywords != null) { final StringTokenizer tok = new StringTokenizer(getXmlInnerText(keywords).trim(), ","); final String[] keywordsArray = new String[tok.countTokens()]; for (int i = 0; tok.hasMoreTokens(); i++) { keywordsArray[i] = tok.nextToken(); } module.setKeywords(keywordsArray); } final Element subtitle = element.getChild("subtitle", ns); if (subtitle != null) { module.setSubtitle(subtitle.getTextTrim()); } final Element summary = element.getChild("summary", ns); if (summary != null) { module.setSummary(summary.getTextTrim()); } } return module; }
From source file:com.rometools.modules.mediarss.io.MediaModuleParser.java
License:Open Source License
private Metadata parseMetadata(final Element e) { final Metadata md = new Metadata(); // categories {/* w ww. j a v a 2 s . co m*/ final List<Element> categories = e.getChildren("category", getNS()); final ArrayList<Category> values = new ArrayList<Category>(); for (int i = 0; categories != null && i < categories.size(); i++) { try { final Element cat = categories.get(i); values.add(new Category(cat.getAttributeValue("scheme"), cat.getAttributeValue("label"), cat.getText())); } catch (final Exception ex) { LOG.warn("Exception parsing category tag.", ex); } } md.setCategories(values.toArray(new Category[values.size()])); } // copyright try { final Element copy = e.getChild("copyright", getNS()); if (copy != null) { md.setCopyright(copy.getText()); md.setCopyrightUrl( copy.getAttributeValue("url") != null ? new URI(copy.getAttributeValue("url")) : null); } } catch (final Exception ex) { LOG.warn("Exception parsing copyright tag.", ex); } // credits { final List<Element> credits = e.getChildren("credit", getNS()); final ArrayList<Credit> values = new ArrayList<Credit>(); for (int i = 0; credits != null && i < credits.size(); i++) { try { final Element cred = credits.get(i); values.add(new Credit(cred.getAttributeValue("scheme"), cred.getAttributeValue("role"), cred.getText())); md.setCredits(values.toArray(new Credit[values.size()])); } catch (final Exception ex) { LOG.warn("Exception parsing credit tag.", ex); } } } // description try { final Element description = e.getChild("description", getNS()); if (description != null) { md.setDescription(description.getText()); md.setDescriptionType(description.getAttributeValue("type")); } } catch (final Exception ex) { LOG.warn("Exception parsing description tag.", ex); } // hash try { final Element hash = e.getChild("hash", getNS()); if (hash != null) { md.setHash(new Hash(hash.getAttributeValue("algo"), hash.getText())); } } catch (final Exception ex) { LOG.warn("Exception parsing hash tag.", ex); } // keywords { final Element keywords = e.getChild("keywords", getNS()); if (keywords != null) { final StringTokenizer tok = new StringTokenizer(keywords.getText(), ","); final String[] value = new String[tok.countTokens()]; for (int i = 0; tok.hasMoreTokens(); i++) { value[i] = tok.nextToken().trim(); } md.setKeywords(value); } } // ratings { final ArrayList<Rating> values = new ArrayList<Rating>(); final List<Element> ratings = e.getChildren("rating", getNS()); for (final Element ratingElement : ratings) { try { final String ratingText = ratingElement.getText(); String ratingScheme = Strings.trimToNull(ratingElement.getAttributeValue("scheme")); if (ratingScheme == null) { ratingScheme = "urn:simple"; } final Rating rating = new Rating(ratingScheme, ratingText); values.add(rating); } catch (final Exception ex) { LOG.warn("Exception parsing rating tag.", ex); } } md.setRatings(values.toArray(new Rating[values.size()])); } // text { final List<Element> texts = e.getChildren("text", getNS()); final ArrayList<Text> values = new ArrayList<Text>(); for (int i = 0; texts != null && i < texts.size(); i++) { try { final Element text = texts.get(i); final Time start = text.getAttributeValue("start") == null ? null : new Time(text.getAttributeValue("start")); final Time end = text.getAttributeValue("end") == null ? null : new Time(text.getAttributeValue("end")); values.add(new Text(text.getAttributeValue("type"), text.getTextTrim(), start, end)); } catch (final Exception ex) { LOG.warn("Exception parsing text tag.", ex); } } md.setText(values.toArray(new Text[values.size()])); } // thumbnails { final ArrayList<Thumbnail> values = new ArrayList<Thumbnail>(); final List<Element> thumbnails = e.getChildren("thumbnail", getNS()); for (final Element thumb : thumbnails) { try { final String timeAttr = Strings.trimToNull(thumb.getAttributeValue("time")); Time time = null; if (timeAttr != null) { time = new Time(timeAttr); } final String widthAttr = thumb.getAttributeValue("width"); final Integer width = Integers.parse(widthAttr); final String heightAttr = thumb.getAttributeValue("height"); final Integer height = Integers.parse(heightAttr); final String url = thumb.getAttributeValue("url"); final URI uri = new URI(url); final Thumbnail thumbnail = new Thumbnail(uri, width, height, time); values.add(thumbnail); } catch (final Exception ex) { LOG.warn("Exception parsing thumbnail tag.", ex); } } md.setThumbnail(values.toArray(new Thumbnail[values.size()])); } // title { final Element title = e.getChild("title", getNS()); if (title != null) { md.setTitle(title.getText()); md.setTitleType(title.getAttributeValue("type")); } } // restrictions { final List<Element> restrictions = e.getChildren("restriction", getNS()); final ArrayList<Restriction> values = new ArrayList<Restriction>(); for (int i = 0; i < restrictions.size(); i++) { final Element r = restrictions.get(i); Restriction.Type type = null; if (r.getAttributeValue("type").equalsIgnoreCase("uri")) { type = Restriction.Type.URI; } else if (r.getAttributeValue("type").equalsIgnoreCase("country")) { type = Restriction.Type.COUNTRY; } Restriction.Relationship relationship = null; if (r.getAttributeValue("relationship").equalsIgnoreCase("allow")) { relationship = Restriction.Relationship.ALLOW; } else if (r.getAttributeValue("relationship").equalsIgnoreCase("deny")) { relationship = Restriction.Relationship.DENY; } final Restriction value = new Restriction(relationship, type, r.getTextTrim()); values.add(value); } md.setRestrictions(values.toArray(new Restriction[values.size()])); } // handle adult { final Element adult = e.getChild("adult", getNS()); if (adult != null && md.getRatings().length == 0) { final Rating[] r = new Rating[1]; if (adult.getTextTrim().equals("true")) { r[0] = new Rating("urn:simple", "adult"); } else { r[0] = new Rating("urn:simple", "nonadult"); } md.setRatings(r); } } return md; }
From source file:com.rometools.modules.opensearch.impl.OpenSearchModuleParser.java
License:Apache License
@Override public Module parse(final Element dcRoot, final Locale locale) { final URL baseURI = findBaseURI(dcRoot); boolean foundSomething = false; final OpenSearchModule osm = new OpenSearchModuleImpl(); Element e = dcRoot.getChild("totalResults", OS_NS); if (e != null) { try {// w w w .j a v a 2s.c o m osm.setTotalResults(Integer.parseInt(e.getText())); foundSomething = true; } catch (final NumberFormatException ex) { // Ignore setting the field and post a warning System.err .println("Warning: The element totalResults must be an integer value: " + ex.getMessage()); } } e = dcRoot.getChild("itemsPerPage", OS_NS); if (e != null) { try { osm.setItemsPerPage(Integer.parseInt(e.getText())); foundSomething = true; } catch (final NumberFormatException ex) { // Ignore setting the field and post a warning System.err .println("Warning: The element itemsPerPage must be an integer value: " + ex.getMessage()); } } e = dcRoot.getChild("startIndex", OS_NS); if (e != null) { try { osm.setStartIndex(Integer.parseInt(e.getText())); foundSomething = true; } catch (final NumberFormatException ex) { // Ignore setting the field and post a warning System.err.println("Warning: The element startIndex must be an integer value: " + ex.getMessage()); } } final List<Element> queries = dcRoot.getChildren("Query", OS_NS); if (queries != null && !queries.isEmpty()) { // Create the OSQuery list final List<OSQuery> osqList = new LinkedList<OSQuery>(); for (final Iterator<Element> iter = queries.iterator(); iter.hasNext();) { e = iter.next(); osqList.add(parseQuery(e)); } osm.setQueries(osqList); } e = dcRoot.getChild("link", OS_NS); if (e != null) { osm.setLink(parseLink(e, baseURI)); } return foundSomething ? osm : null; }
From source file:com.rometools.modules.photocast.io.Parser.java
License:Open Source License
@Override public Module parse(final Element element, final Locale locale) { if (element.getName().equals("channel") || element.getName().equals("feed")) { return new PhotocastModuleImpl(); } else if (element.getChild("metadata", Parser.NS) == null && element.getChild("image", Parser.NS) == null) { return null; }//from ww w.j av a 2 s .c o m final PhotocastModule pm = new PhotocastModuleImpl(); final List<Element> children = element.getChildren(); final Iterator<Element> it = children.iterator(); while (it.hasNext()) { final Element e = it.next(); if (!e.getNamespace().equals(Parser.NS)) { continue; } if (e.getName().equals("photoDate")) { try { pm.setPhotoDate(Parser.PHOTO_DATE_FORMAT.parse(e.getText())); } catch (final Exception ex) { LOG.warn("Unable to parse photoDate: " + e.getText(), ex); } } else if (e.getName().equals("cropDate")) { try { pm.setCropDate(Parser.CROP_DATE_FORMAT.parse(e.getText())); } catch (final Exception ex) { LOG.warn("Unable to parse cropDate: " + e.getText(), ex); } } else if (e.getName().equals("thumbnail")) { try { pm.setThumbnailUrl(new URL(e.getText())); } catch (final Exception ex) { LOG.warn("Unable to parse thumnail: " + e.getText(), ex); } } else if (e.getName().equals("image")) { try { pm.setImageUrl(new URL(e.getText())); } catch (final Exception ex) { LOG.warn("Unable to parse image: " + e.getText(), ex); } } else if (e.getName().equals("metadata")) { String comments = ""; PhotoDate photoDate = null; if (e.getChildText("PhotoDate") != null) { try { photoDate = new PhotoDate(Double.parseDouble(e.getChildText("PhotoDate"))); } catch (final Exception ex) { LOG.warn("Unable to parse PhotoDate: " + e.getText(), ex); } } if (e.getChildText("Comments") != null) { comments = e.getChildText("Comments"); } pm.setMetadata(new Metadata(photoDate, comments)); } } return pm; }
From source file:com.rometools.modules.slash.io.SlashModuleParser.java
License:Open Source License
@Override public Module parse(final Element element, final Locale locale) { final SlashImpl si = new SlashImpl(); Element tag = element.getChild("hit_parade", SlashModuleParser.NS); if (tag != null) { final StringTokenizer tok = new StringTokenizer(tag.getText(), ","); final Integer[] hp = new Integer[tok.countTokens()]; for (int i = 0; tok.hasMoreTokens(); i++) { hp[i] = new Integer(tok.nextToken()); }// www.j a v a 2s. com si.setHitParade(hp); } tag = null; tag = element.getChild("comments", SlashModuleParser.NS); if (tag != null) { si.setComments(new Integer(tag.getText())); } tag = null; tag = element.getChild("department", SlashModuleParser.NS); if (tag != null) { si.setDepartment(tag.getText().trim()); } tag = null; tag = element.getChild("section", SlashModuleParser.NS); if (tag != null) { si.setSection(tag.getText().trim()); } if (si.getHitParade() != null || si.getComments() != null || si.getDepartment() != null || si.getSection() != null) { return si; } return null; }