List of usage examples for org.dom4j Document clearContent
void clearContent();
Node
instances this branch may contain. From source file:org.lnicholls.galleon.apps.podcasting.Podcasting.java
License:Open Source License
public static Element getDocument(String location) { try {// ww w . j av a2 s . c om Document document = null; PersistentValue persistentValue = PersistentValueManager.loadPersistentValue(Podcasting.class.getName() + "." + location); String content = persistentValue == null ? null : persistentValue.getValue(); if (PersistentValueManager.isAged(persistentValue) && !location.equals("local")) { try { String page = Tools.getPage(new URL(location)); if (page != null && page.length() > 0) { content = page; } } catch (Exception ex) { Tools.logException(Podcasting.class, ex, "Could not cache document: " + location); } } else { if (location.equals("local")) content = ""; } if (content != null) { SAXReader saxReader = new SAXReader(); try { if (location.equals("local")) { document = saxReader.read(new File("d:/galleon/ipodder.opml")); } else { StringReader stringReader = new StringReader(content); document = saxReader.read(stringReader); if (PersistentValueManager.isAged(persistentValue)) { PersistentValueManager.savePersistentValue(Podcasting.class.getName() + "." + location, content, 60 * 60 * 6); } stringReader.close(); stringReader = null; } } catch (Throwable ex) { if (persistentValue != null) { StringReader stringReader = new StringReader(persistentValue.getValue()); document = saxReader.read(stringReader); stringReader.close(); stringReader = null; } } if (document != null) { Element root = document.getRootElement(); document.clearContent(); document = null; if (root.getName().equals("opml")) return root.element("body"); } } } catch (Exception ex) { Tools.logException(Podcasting.class, ex, "Could not download document: " + location); } return null; }
From source file:org.lnicholls.galleon.apps.podcasting.Podcasting.java
License:Open Source License
public static Podcast getPodcast(Podcast podcast) { PersistentValue persistentValue = PersistentValueManager .loadPersistentValue(Podcasting.class.getName() + "." + podcast.getPath()); String content = persistentValue == null ? null : persistentValue.getValue(); if (PersistentValueManager.isAged(persistentValue)) { try {// w w w . ja v a2 s . c om String page = Tools.getPage(new URL(podcast.getPath())); if (page != null && page.length() > 0) content = page; } catch (Exception ex) { Tools.logException(Podcasting.class, ex, "Could not cache listing: " + podcast.getPath()); } } if (content != null) { try { SAXReader saxReader = new SAXReader(); StringReader stringReader = new StringReader(content); // Document document = saxReader.read(new // File("d:/galleon/itunes2.rss.xml")); Document document = saxReader.read(stringReader); stringReader.close(); stringReader = null; Element root = document.getRootElement(); // check for errors if (root != null && root.getName().equals("rss")) { Element channel = root.element("channel"); if (channel != null) { String channelTitle = null; String channelLink = null; String channelDescription = null; String channelSubtitle = null; String channelImage1 = null; String channelImage2 = null; String channelExplicit = null; String channelBlock = null; String channelCategory = null; String channelKeywords = null; String channelSummary = null; String channelTtl = null; String channelAuthor = null; String value = null; if ((value = Tools.getAttribute(channel, "author")) != null) { channelAuthor = value; } if ((value = Tools.getAttribute(channel, "title")) != null) { channelTitle = value; } if ((value = Tools.getAttribute(channel, "link")) != null) { channelLink = value; } if ((value = Tools.getAttribute(channel, "description")) != null) { channelDescription = value; } if ((value = Tools.getAttribute(channel, "subtitle")) != null) { channelSubtitle = value; } for (Iterator i = channel.elementIterator("image"); i.hasNext();) { Element item = (Element) i.next(); if (item.element("url") != null) { if ((value = Tools.getAttribute(item, "url")) != null) { channelImage1 = value; // rss } } else if (item.element("image") != null) { item = item.element("image"); if ((value = Tools.getAttribute(item, "href")) != null) { channelImage1 = value; // itunes } } else if (item.attribute("href") != null) { channelImage2 = item.attributeValue("href"); // itunes } } if ((value = Tools.getAttribute(channel, "explicit")) != null) { channelExplicit = value; } if ((value = Tools.getAttribute(channel, "block")) != null) { channelBlock = value; } if ((value = Tools.getAttribute(channel, "keywords")) != null) { channelKeywords = value; } if ((value = Tools.getAttribute(channel, "summary")) != null) { channelSummary = value; } if ((value = Tools.getAttribute(channel, "ttl")) != null) { channelTtl = value; } Element categoryElement = channel.element("category"); if (categoryElement != null) { if ((value = categoryElement.attributeValue("text")) != null) { channelCategory = value; Element subCategory = categoryElement.element("category"); if (subCategory != null) { if ((value = subCategory.attributeValue("text")) != null) channelCategory = channelCategory + ", " + value; } } } if (channelAuthor != null && channelAuthor.length() > 0) podcast.setAuthor(Tools.cleanHTML(channelAuthor)); if (channelTitle != null && channelTitle.length() > 0) podcast.setTitle(Tools.cleanHTML(channelTitle)); if (channelLink != null && channelLink.length() > 0) podcast.setLink(channelLink); if (channelDescription != null && channelDescription.length() > 0) podcast.setDescription(Tools.trim(Tools.cleanHTML(channelDescription), 4096)); if (channelSubtitle != null && channelSubtitle.length() > 0) podcast.setSubtitle(Tools.trim(Tools.cleanHTML(channelSubtitle), 4096)); if (channelImage2 == null) { if (channelImage1 != null && (channelImage1.endsWith(".png") || channelImage1.endsWith(".jpg") || channelImage1 .endsWith(".gif"))) podcast.setImage(channelImage1); // rss } else { if (channelImage2.endsWith(".png") || channelImage2.endsWith(".jpg")) podcast.setImage(channelImage2); // itunes } if (channelBlock != null) podcast.setBlock(new Boolean(!channelBlock.toLowerCase().equals("no"))); else podcast.setBlock(Boolean.FALSE); if (channelCategory != null && channelCategory.length() > 0) podcast.setCategory(Tools.cleanHTML(channelCategory)); if (channelExplicit != null) podcast.setExplicit(new Boolean(!channelExplicit.toLowerCase().equals("no"))); else podcast.setExplicit(Boolean.FALSE); if (channelKeywords != null && channelKeywords.length() > 0) podcast.setKeywords(Tools.cleanHTML(channelKeywords)); if (channelSummary != null && channelSummary.length() > 0) podcast.setSummary(Tools.trim(Tools.cleanHTML(channelSummary), 4096)); if (channelTtl != null && channelTtl.length() > 0) { try { podcast.setTtl(new Integer(channelTtl)); } catch (Exception ex) { } } else podcast.setTtl(new Integer(0)); List tracks = podcast.getTracks(); if (tracks == null) tracks = new ArrayList(); for (Iterator i = channel.elementIterator("item"); i.hasNext();) { Element item = (Element) i.next(); String title = null; String description = null; String link = null; String pubDate = null; String guid = null; String category = null; String explicit = null; String author = null; String summary = null; String enclosureUrl = null; String enclosureLength = null; String enclosureType = null; String block = null; String duration = null; String keywords = null; String subtitle = null; if ((value = Tools.getAttribute(item, "title")) != null) { title = value; } if ((value = Tools.getAttribute(item, "description")) != null) { description = value; } if ((value = Tools.getAttribute(item, "link")) != null) { link = value; } if ((value = Tools.getAttribute(item, "pubDate")) != null) { pubDate = value; } if ((value = Tools.getAttribute(item, "guid")) != null) { guid = value; } if ((value = Tools.getAttribute(item, "category")) != null) { category = value; } if ((value = Tools.getAttribute(item, "explicit")) != null) { explicit = value; } for (Iterator j = item.elementIterator("author"); j.hasNext();) { Element authorItem = (Element) j.next(); author = authorItem.getTextTrim(); } if ((value = Tools.getAttribute(item, "summary")) != null) { summary = value; } Element enclosureElement = item.element("enclosure"); if (enclosureElement != null) { if ((value = enclosureElement.attributeValue("url")) != null) { enclosureUrl = value; } if ((value = enclosureElement.attributeValue("length")) != null) { enclosureLength = value; } if ((value = enclosureElement.attributeValue("type")) != null) { enclosureType = value; } } if ((value = Tools.getAttribute(item, "block")) != null) { block = value; } categoryElement = item.element("category"); if (categoryElement != null) { if ((value = categoryElement.attributeValue("text")) != null) { category = value; Element subCategory = categoryElement.element("category"); if (subCategory != null) { if ((value = subCategory.attributeValue("text")) != null) category = category + ", " + value; } } } if ((value = Tools.getAttribute(item, "duration")) != null) { duration = value; } if ((value = Tools.getAttribute(item, "keywords")) != null) { keywords = value; } if ((value = Tools.getAttribute(item, "subtitle")) != null) { subtitle = value; } if (enclosureUrl != null && enclosureUrl.length() > 0 && enclosureUrl.endsWith(".mp3")) { PodcastTrack podcastTrack = null; boolean existing = false; for (int k = 0; k < tracks.size(); k++) { PodcastTrack current = (PodcastTrack) tracks.get(k); if (current != null && current.getUrl() != null && enclosureUrl != null && current.getUrl().equals(enclosureUrl)) { existing = true; podcastTrack = current; break; } } if (podcastTrack == null) podcastTrack = new PodcastTrack(); if (title != null && title.length() > 0) podcastTrack.setTitle(Tools.cleanHTML(title)); if (description != null && description.length() > 0) podcastTrack.setDescription(Tools.trim(Tools.cleanHTML(description), 4096)); if (link != null && link.length() > 0) podcastTrack.setLink(link); if (pubDate != null && pubDate.length() > 0) { try { podcastTrack.setPublicationDate(new Date(pubDate)); } catch (Exception ex) { } } if (guid != null && guid.length() > 0) podcastTrack.setGuid(guid); if (category != null && category.length() > 0) podcastTrack.setCategory(Tools.cleanHTML(category)); if (explicit != null && explicit.length() > 0) podcastTrack.setExplicit(new Boolean(!explicit.toLowerCase().equals("no"))); else podcastTrack.setExplicit(Boolean.FALSE); if (author != null && author.length() > 0) podcastTrack.setAuthor(Tools.cleanHTML(author)); if (summary != null && summary.length() > 0) podcastTrack.setSummary(Tools.trim(Tools.cleanHTML(summary), 4096)); if (enclosureUrl != null && enclosureUrl.length() > 0) podcastTrack.setUrl(enclosureUrl); if (enclosureLength != null && enclosureLength.length() > 0) { try { podcastTrack.setSize(Long.parseLong(enclosureLength)); } catch (Exception ex) { } } if (enclosureType != null && enclosureType.length() > 0) podcastTrack.setMimeType(enclosureType); if (block != null && block.length() > 0) podcastTrack.setBlock(new Boolean(!block.toLowerCase().equals("no"))); else podcastTrack.setBlock(Boolean.FALSE); if (duration != null && duration.length() > 0) { try { SimpleDateFormat timeDateFormat = new SimpleDateFormat(); timeDateFormat.applyPattern("HH:mm:ss"); ParsePosition pos = new ParsePosition(0); Date date = timeDateFormat.parse(duration, pos); if (date == null) { timeDateFormat.applyPattern("mm:ss"); date = timeDateFormat.parse(duration, pos); } podcastTrack.setDuration(new Long(date.getTime())); } catch (Exception ex) { } } if (keywords != null && keywords.length() > 0) podcastTrack.setKeywords(Tools.cleanHTML(keywords)); if (subtitle != null && subtitle.length() > 0) podcastTrack.setSubtitle(Tools.trim(Tools.cleanHTML(subtitle), 4096)); if (podcastTrack.getMimeType() == null) podcastTrack.setMimeType(Mp3File.DEFAULT_MIME_TYPE); podcastTrack.setPodcast(podcast.getId()); if (!existing) tracks.add(podcastTrack); } } podcast.setTracks(tracks); } } else { ChannelBuilderIF builder = new ChannelBuilder(); ChannelIF channel = FeedParser .parse(builder, new ByteArrayInputStream((content.getBytes("UTF-8")))); if (channel != null) { podcast.setDescription(Tools.cleanHTML(channel.getDescription())); podcast.setDateUpdated(channel.getLastBuildDate()); podcast.setTtl(new Integer(channel.getTtl())); List items = getListing(channel); if (items != null && items.size() > 0) { ArrayList tracks = new ArrayList(); for (Iterator i = items.iterator(); i.hasNext(); /* Nothing */) { ItemIF item = (ItemIF) i.next(); String description = Tools.trim(item.getDescription(), 4096); tracks.add(new PodcastTrack(Tools.cleanHTML(item.getTitle()), null, null, Tools.trim( Tools.cleanHTML(item.getDescription()), 4096), null, null, null, null, Boolean.FALSE, Boolean.FALSE, null, item.getDate(), item.getEnclosure() .getLocation().toExternalForm(), "audio/mpeg", item.getEnclosure() .getLength(), 0, new Long(0), new Integer(0), 0, 0, podcast.getId(), new Integer(0), null)); } podcast.setTracks(tracks); } } builder.close(); builder = null; } document.clearContent(); document = null; if (PersistentValueManager.isAged(persistentValue)) { int ttl = podcast.getTtl().intValue(); if (ttl < 10) ttl = 60; else ttl = 60 * 6; PersistentValueManager.savePersistentValue(Podcasting.class.getName() + "." + podcast.getPath(), content, ttl * 60); } } catch (Exception ex) { Tools.logException(Podcasting.class, ex, "Could not download listing: " + podcast.getPath()); return null; } } return podcast; }
From source file:org.lnicholls.galleon.apps.traffic.Traffic.java
License:Open Source License
public static List getTrafficResults(String street, String city, String state, String zip, String radius) { List trafficResults = new ArrayList(); // http://api.local.yahoo.com/MapsService/V1/trafficData?appid=YahooDemo&street=701+First+Street&city=Sunnyvale&state=CA&include_map=1 String url = "http://api.local.yahoo.com/MapsService/V1/trafficData?appid=" + "galleonhme" + "&street=" + URLEncoder.encode(street) + "&city=" + URLEncoder.encode(city) + "&state=" + URLEncoder.encode(state) + "&radius=" + URLEncoder.encode(radius) + "&include_map=1&image_width=640&image_height=480"; log.info(url);/*from ww w . j av a 2 s .c om*/ PersistentValue persistentValue = PersistentValueManager.loadPersistentValue(Traffic.class.getName() + "." + url); String content = persistentValue == null ? null : persistentValue.getValue(); if (PersistentValueManager.isAged(persistentValue)) { try { String page = Tools.getPage(new URL(url)); if (page != null && page.length() > 0) content = page; } catch (Exception ex) { Tools.logException(Traffic.class, ex, "Could not cache traffic: " + url); } } if (content != null) { try { SAXReader saxReader = new SAXReader(); StringReader stringReader = new StringReader(content); // Document document = saxReader.read(new // File("d:/galleon/itunes2.rss.xml")); Document document = saxReader.read(stringReader); stringReader.close(); stringReader = null; Element root = document.getRootElement(); // check for errors if (root != null && root.getName().equals("ResultSet")) { for (Iterator i = root.elementIterator("Result"); i.hasNext();) { Element result = (Element) i.next(); String value = null; String type = null; String title = null; String description = null; String severity = null; String direction = null; String map = null; if ((value = Tools.getAttribute(result, "type")) != null) { type = value; } if ((value = Tools.getAttribute(result, "Title")) != null) { title = value; } if ((value = Tools.getAttribute(result, "Description")) != null) { description = Tools.cleanHTML(value); } if ((value = Tools.getAttribute(result, "Severity")) != null) { severity = Tools.cleanHTML(value); } if ((value = Tools.getAttribute(result, "Direction")) != null) { direction = Tools.cleanHTML(value); } if ((value = Tools.getAttribute(result, "ImageUrl")) != null) { map = Tools.cleanHTML(value); } if ((value = Tools.getAttribute(result, "EndDate")) != null) { try { long time = Long.parseLong(value) * 1000; Date date = new Date(time); log.info(date); if (date.after(new Date())) trafficResults .add(new Result(type, title, description, severity, direction, map)); } catch (Exception ex) { Tools.logException(Traffic.class, ex); } } else trafficResults.add(new Result(type, title, description, severity, direction, map)); } } document.clearContent(); document = null; if (PersistentValueManager.isAged(persistentValue)) { PersistentValueManager.savePersistentValue(Traffic.class.getName() + "." + url, content, 60); } } catch (Exception ex) { Tools.logException(Traffic.class, ex, "Could not download traffic: " + url); return null; } } return trafficResults; }
From source file:org.lnicholls.galleon.apps.videocasting.Videocasting.java
License:Open Source License
public static Element getDocument(String location) { try {//from w w w .j a v a2 s . c om Document document = null; PersistentValue persistentValue = PersistentValueManager .loadPersistentValue(Videocasting.class.getName() + "." + location); String content = persistentValue == null ? null : persistentValue.getValue(); // log.debug("content1="+content); if (PersistentValueManager.isAged(persistentValue) && !location.equals("local")) { try { String page = Tools.getPage(new URL(location)); if (page != null && page.length() > 0) { content = page; } } catch (Exception ex) { Tools.logException(Videocasting.class, ex, "Could not cache document: " + location); } } else { if (location.equals("local")) content = ""; } // log.debug("content2="+content); if (content != null) { SAXReader saxReader = new SAXReader(); try { if (location.equals("local")) { document = saxReader.read(new File("d:/galleon/ivideoder.opml")); } else { StringReader stringReader = new StringReader(content); document = saxReader.read(stringReader); if (PersistentValueManager.isAged(persistentValue)) { PersistentValueManager.savePersistentValue( Videocasting.class.getName() + "." + location, content, 60 * 60 * 6); } stringReader.close(); stringReader = null; } } catch (Throwable ex) { if (persistentValue != null) { StringReader stringReader = new StringReader(persistentValue.getValue()); document = saxReader.read(stringReader); stringReader.close(); stringReader = null; } } if (document != null) { Element root = document.getRootElement(); document.clearContent(); document = null; if (root.getName().equals("opml")) return root.element("body"); } } } catch (Exception ex) { Tools.logException(Videocasting.class, ex, "Could not download document: " + location); } return null; }
From source file:org.lnicholls.galleon.apps.videocasting.Videocasting.java
License:Open Source License
public static Videocast getVideocast(Videocast videocast) { PersistentValue persistentValue = PersistentValueManager .loadPersistentValue(Videocasting.class.getName() + "." + videocast.getPath()); String content = persistentValue == null ? null : persistentValue.getValue(); // log.debug("content3="+content); if (PersistentValueManager.isAged(persistentValue)) { try {/*from ww w .java 2s .c o m*/ String page = Tools.getPage(new URL(videocast.getPath())); if (page != null && page.length() > 0) content = page; } catch (Exception ex) { Tools.logException(Videocasting.class, ex, "Could not cache listing: " + videocast.getPath()); } } // log.debug("content4="+content); if (content != null) { try { SAXReader saxReader = new SAXReader(); StringReader stringReader = new StringReader(content); // Document document = saxReader.read(new // File("d:/galleon/itunes2.rss.xml")); Document document = saxReader.read(stringReader); stringReader.close(); stringReader = null; Element root = document.getRootElement(); // check for errors if (root != null && root.getName().equals("rss")) { Element channel = root.element("channel"); if (channel != null) { String channelTitle = null; String channelLink = null; String channelDescription = null; String channelSubtitle = null; String channelImage1 = null; String channelImage2 = null; String channelImage3 = null; String channelExplicit = null; String channelBlock = null; String channelCategory = null; String channelKeywords = null; String channelSummary = null; String channelTtl = null; String channelAuthor = null; String value = null; if ((value = Tools.getAttribute(channel, "author")) != null) { channelAuthor = value; } if ((value = Tools.getAttribute(channel, "title")) != null) { channelTitle = value; } if ((value = Tools.getAttribute(channel, "link")) != null) { channelLink = value; } if ((value = Tools.getAttribute(channel, "description")) != null) { channelDescription = value; } if ((value = Tools.getAttribute(channel, "subtitle")) != null) { channelSubtitle = value; } for (Iterator i = channel.elementIterator("image"); i.hasNext();) { Element item = (Element) i.next(); if (item.element("url") != null) { if ((value = Tools.getAttribute(item, "url")) != null) { channelImage1 = value; // rss } } else if (item.attribute("href") != null) { channelImage2 = item.attributeValue("href"); // itunes } } Element thumbnailElement = channel.element("thumbnail"); if (thumbnailElement != null) { if ((value = thumbnailElement.attributeValue("url")) != null) { channelImage3 = value; } } if ((value = Tools.getAttribute(channel, "explicit")) != null) { channelExplicit = value; } if ((value = Tools.getAttribute(channel, "block")) != null) { channelBlock = value; } if ((value = Tools.getAttribute(channel, "keywords")) != null) { channelKeywords = value; } if ((value = Tools.getAttribute(channel, "summary")) != null) { channelSummary = value; } if ((value = Tools.getAttribute(channel, "ttl")) != null) { channelTtl = value; } Element categoryElement = channel.element("category"); if (categoryElement != null) { if ((value = categoryElement.attributeValue("text")) != null) { channelCategory = value; Element subCategory = categoryElement.element("category"); if (subCategory != null) { if ((value = subCategory.attributeValue("text")) != null) channelCategory = channelCategory + ", " + value; } } } if (channelAuthor != null && channelAuthor.length() > 0) videocast.setAuthor(Tools.cleanHTML(channelAuthor)); if (channelTitle != null && channelTitle.length() > 0) videocast.setTitle(Tools.cleanHTML(channelTitle)); if (channelLink != null && channelLink.length() > 0) videocast.setLink(channelLink); if (channelDescription != null && channelDescription.length() > 0) videocast.setDescription(Tools.trim(Tools.cleanHTML(channelDescription), 4096)); if (channelSubtitle != null && channelSubtitle.length() > 0) videocast.setSubtitle(Tools.trim(Tools.cleanHTML(channelSubtitle), 4096)); if (channelImage2 == null) { if (channelImage1 != null && (channelImage1.endsWith(".png") || channelImage1.endsWith(".jpg") || channelImage1 .endsWith(".gif"))) videocast.setImage(channelImage1); // rss else videocast.setImage(channelImage3); } else { if (channelImage2.endsWith(".png") || channelImage2.endsWith(".jpg")) videocast.setImage(channelImage2); // itunes } if (channelBlock != null) videocast.setBlock(new Boolean(!channelBlock.toLowerCase().equals("no"))); else videocast.setBlock(Boolean.FALSE); if (channelCategory != null && channelCategory.length() > 0) videocast.setCategory(Tools.cleanHTML(channelCategory)); if (channelExplicit != null) videocast.setExplicit(new Boolean(!channelExplicit.toLowerCase().equals("no"))); else videocast.setExplicit(Boolean.FALSE); if (channelKeywords != null && channelKeywords.length() > 0) videocast.setKeywords(Tools.cleanHTML(channelKeywords)); if (channelSummary != null && channelSummary.length() > 0) videocast.setSummary(Tools.trim(channelSummary, 4096)); if (channelTtl != null && channelTtl.length() > 0) { try { videocast.setTtl(new Integer(channelTtl)); } catch (Exception ex) { } } else videocast.setTtl(new Integer(0)); List tracks = videocast.getTracks(); if (tracks == null) tracks = new ArrayList(); for (Iterator i = channel.elementIterator("item"); i.hasNext();) { Element item = (Element) i.next(); String title = null; String description = null; String link = null; String pubDate = null; String guid = null; String category = null; String explicit = null; String author = null; String summary = null; String enclosureUrl = null; String enclosureLength = null; String enclosureType = null; String block = null; String duration = null; String keywords = null; String subtitle = null; if ((value = Tools.getAttribute(item, "title")) != null) { title = value; } if ((value = Tools.getAttribute(item, "description")) != null) { description = value; } if ((value = Tools.getAttribute(item, "link")) != null) { link = value; } if ((value = Tools.getAttribute(item, "pubDate")) != null) { pubDate = value; } if ((value = Tools.getAttribute(item, "guid")) != null) { guid = value; } if ((value = Tools.getAttribute(item, "category")) != null) { category = value; } if ((value = Tools.getAttribute(item, "explicit")) != null) { explicit = value; } for (Iterator j = item.elementIterator("author"); j.hasNext();) { Element authorItem = (Element) j.next(); author = authorItem.getTextTrim(); } if ((value = Tools.getAttribute(item, "summary")) != null) { summary = value; } Element enclosureElement = item.element("enclosure"); if (enclosureElement != null) { if ((value = enclosureElement.attributeValue("url")) != null) { enclosureUrl = value; } if ((value = enclosureElement.attributeValue("length")) != null) { enclosureLength = value; } if ((value = enclosureElement.attributeValue("type")) != null) { enclosureType = value; } } if ((value = Tools.getAttribute(item, "block")) != null) { block = value; } categoryElement = item.element("category"); if (categoryElement != null) { if ((value = categoryElement.attributeValue("text")) != null) { category = value; Element subCategory = categoryElement.element("category"); if (subCategory != null) { if ((value = subCategory.attributeValue("text")) != null) category = category + ", " + value; } } } if ((value = Tools.getAttribute(item, "duration")) != null) { duration = value; } if ((value = Tools.getAttribute(item, "keywords")) != null) { keywords = value; } if ((value = Tools.getAttribute(item, "subtitle")) != null) { subtitle = value; } if (enclosureUrl != null && enclosureUrl.length() > 0) { VideocastTrack videocastTrack = null; boolean existing = false; for (int k = 0; k < tracks.size(); k++) { VideocastTrack current = (VideocastTrack) tracks.get(k); if (current != null && current.getUrl() != null && enclosureUrl != null && current.getUrl().equals(enclosureUrl)) { existing = true; videocastTrack = current; break; } } if (videocastTrack == null) videocastTrack = new VideocastTrack(); if (title != null && title.length() > 0 && (videocastTrack.getTitle() == null || videocastTrack.getTitle().trim().length() == 0)) videocastTrack.setTitle(Tools.cleanHTML(title)); if (description != null && description.length() > 0) videocastTrack.setDescription(Tools.trim(Tools.cleanHTML(description), 4096)); if (link != null && link.length() > 0) videocastTrack.setLink(link); if (pubDate != null && pubDate.length() > 0) { try { videocastTrack.setPublicationDate(new Date(pubDate)); } catch (Exception ex) { } } if (guid != null && guid.length() > 0) videocastTrack.setGuid(guid); if (category != null && category.length() > 0) videocastTrack.setCategory(Tools.cleanHTML(category)); if (explicit != null && explicit.length() > 0) videocastTrack.setExplicit(new Boolean(!explicit.toLowerCase().equals("no"))); else videocastTrack.setExplicit(Boolean.FALSE); if (author != null && author.length() > 0) videocastTrack.setAuthor(Tools.cleanHTML(author)); if (summary != null && summary.length() > 0) videocastTrack.setSummary(Tools.trim(Tools.cleanHTML(summary), 4096)); if (enclosureUrl != null && enclosureUrl.length() > 0) videocastTrack.setUrl(enclosureUrl); if (enclosureLength != null && enclosureLength.length() > 0) { try { videocastTrack.setSize(Long.parseLong(enclosureLength)); } catch (Exception ex) { } } if (enclosureType != null && enclosureType.length() > 0) videocastTrack.setMimeType(enclosureType); if (block != null && block.length() > 0) videocastTrack.setBlock(new Boolean(!block.toLowerCase().equals("no"))); else videocastTrack.setBlock(Boolean.FALSE); if (duration != null && duration.length() > 0) { try { SimpleDateFormat timeDateFormat = new SimpleDateFormat(); timeDateFormat.applyPattern("HH:mm:ss"); ParsePosition pos = new ParsePosition(0); Date date = timeDateFormat.parse(duration, pos); if (date == null) { timeDateFormat.applyPattern("mm:ss"); date = timeDateFormat.parse(duration, pos); } videocastTrack.setDuration(new Long(date.getTime())); } catch (Exception ex) { } } if (keywords != null && keywords.length() > 0) videocastTrack.setKeywords(Tools.cleanHTML(keywords)); if (subtitle != null && subtitle.length() > 0) videocastTrack.setSubtitle(Tools.trim(Tools.cleanHTML(subtitle), 4096)); if (videocastTrack.getMimeType() == null) videocastTrack.setMimeType(VideoFile.DEFAULT_MIME_TYPE); videocastTrack.setVideocast(videocast.getId()); if (!existing) tracks.add(videocastTrack); } } videocast.setTracks(tracks); } } else { ChannelBuilderIF builder = new ChannelBuilder(); ChannelIF channel = FeedParser .parse(builder, new ByteArrayInputStream((content.getBytes("UTF-8")))); if (channel != null) { videocast.setDescription(Tools.cleanHTML(channel.getDescription())); videocast.setDateUpdated(channel.getLastBuildDate()); videocast.setTtl(new Integer(channel.getTtl())); List items = getListing(channel); if (items != null && items.size() > 0) { ArrayList tracks = new ArrayList(); for (Iterator i = items.iterator(); i.hasNext(); /* Nothing */) { ItemIF item = (ItemIF) i.next(); String description = Tools.trim(item.getDescription(), 4096); tracks.add(new VideocastTrack(Tools.cleanHTML(item.getTitle()), null, null, Tools.trim( Tools.cleanHTML(item.getDescription()), 4096), null, null, null, null, Boolean.FALSE, Boolean.FALSE, null, item.getDate(), item.getEnclosure() .getLocation().toExternalForm(), "audio/mpeg", item.getEnclosure() .getLength(), 0, new Long(0), new Integer(0), 0, 0, videocast.getId(), new Integer(0), null)); } videocast.setTracks(tracks); } } builder.close(); builder = null; } document.clearContent(); document = null; if (PersistentValueManager.isAged(persistentValue)) { int ttl = videocast.getTtl().intValue(); if (ttl < 10) ttl = 60; else ttl = 60 * 6; PersistentValueManager.savePersistentValue( Videocasting.class.getName() + "." + videocast.getPath(), content, ttl * 60); } } catch (Exception ex) { Tools.logException(Videocasting.class, ex, "Could not download listing: " + videocast.getPath()); return null; } } return videocast; }
From source file:org.waarp.openr66.database.data.DbHostConfiguration.java
License:Open Source License
/** * Shortcut to add all paths element from source into set * @param source//ww w .j av a 2s . c om * @param path * @param set * @return True if ok */ private boolean updateSet(String source, String path, HashSet<String> set) { if (source != null && !source.isEmpty()) { Document document = null; StringReader reader = null; if (source != null && !source.isEmpty()) { try { reader = new StringReader(source); document = new SAXReader().read(reader); } catch (DocumentException e) { logger.error("Unable to read the XML Config " + path + " string: " + source, e); if (reader != null) { reader.close(); } return false; } if (document == null) { logger.error("Unable to read the XML Config " + path + " string: " + source); if (reader != null) { reader.close(); } return false; } @SuppressWarnings("unchecked") List<Element> list = document.selectNodes(path); for (Element element : list) { String sval = element.getText().trim(); if (sval.isEmpty()) { continue; } set.add(sval.trim()); } list.clear(); document.clearContent(); document = null; if (reader != null) { reader.close(); reader = null; } } } return true; }
From source file:org.waarp.openr66.database.data.DbHostConfiguration.java
License:Open Source License
/** * update Business with possible purge and new or added content, and updating in memory information * @param config/*from w ww . j a va 2s . c o m*/ * @param newbusiness * @param purged * @return True if updated */ public boolean updateBusiness(Configuration config, String newbusiness, boolean purged) { HashSet<String> set = new HashSet<String>(); if (!updateSet(newbusiness, XML_BUSINESS + "/" + XML_BUSINESSID, set)) { return false; } if (purged) { config.businessWhiteSet.clear(); } else { String business = getBusiness(); if (!updateSet(business, XML_BUSINESS + "/" + XML_BUSINESSID, set)) { return false; } } config.businessWhiteSet.addAll(set); if ((newbusiness != null && !newbusiness.isEmpty()) || purged) { Document document = DocumentHelper.createDocument(DocumentHelper.createElement(XML_BUSINESS)); Element root = document.getRootElement(); for (String sval : set) { root.addElement(XML_BUSINESSID).setText(sval); logger.info("Business Allow: " + sval); } setBusiness(root.asXML()); try { update(); } catch (WaarpDatabaseException e) { document.clearContent(); document = null; return false; } document.clearContent(); document = null; } set.clear(); set = null; return true; }
From source file:org.waarp.openr66.database.data.DbHostConfiguration.java
License:Open Source License
/** * Shortcut to add all paths element with key and value from source into map * @param source/*from www .j ava 2 s.co m*/ * @param path * @param keypath * @param valpath * @param split * @param map * @return True if ok */ private boolean updateMap(String source, String path, String keypath, String valpath, String split, HashMap<String, HashSet<String>> map) { if (source != null && !source.isEmpty()) { Document document = null; StringReader reader = null; if (source != null && !source.isEmpty()) { try { reader = new StringReader(source); document = new SAXReader().read(reader); } catch (DocumentException e) { logger.error("Unable to read the XML Config " + path + " string: " + source, e); if (reader != null) { reader.close(); } return false; } if (document == null) { logger.error("Unable to read the XML Config " + path + " string: " + source); if (reader != null) { reader.close(); } return false; } @SuppressWarnings("unchecked") List<Element> list = document.selectNodes(path); for (Element element : list) { Element nodeid = (Element) element.selectSingleNode(keypath); if (nodeid == null) { continue; } Element nodeset = (Element) element.selectSingleNode(valpath); if (nodeset == null) { continue; } String refHostId = nodeid.getText(); String aliasesid = nodeset.getText(); String[] aliasid = aliasesid.split(split); HashSet<String> set = null; if (map.containsKey(refHostId)) { set = map.get(refHostId); } else { set = new HashSet<String>(); } for (String namealias : aliasid) { set.add(namealias); } map.put(refHostId, set); } list.clear(); document.clearContent(); document = null; if (reader != null) { reader.close(); reader = null; } } } return true; }
From source file:org.waarp.openr66.database.data.DbHostConfiguration.java
License:Open Source License
/** * update Alias with possible purge and new or added content, and updating in memory information * @param config//from w w w.j ava 2 s . c om * @param newalias * @param purged * @return True if updated */ public boolean updateAlias(Configuration config, String newalias, boolean purged) { HashMap<String, HashSet<String>> map = new HashMap<String, HashSet<String>>(); if (!updateMap(newalias, XML_ALIASES + "/" + XML_ALIAS, XML_REALID, XML_ALIASID, " |\\|", map)) { return false; } if (purged) { config.aliases.clear(); } else { String alias = getAliases(); if (!updateMap(alias, XML_ALIASES + "/" + XML_ALIAS, XML_REALID, XML_ALIASID, " |\\|", map)) { return false; } } if ((newalias != null && !newalias.isEmpty()) || purged) { Document document = DocumentHelper.createDocument(DocumentHelper.createElement(XML_ALIASES)); Element root = document.getRootElement(); for (Entry<String, HashSet<String>> entry : map.entrySet()) { Element elt = root.addElement(XML_ALIAS); elt.addElement(XML_REALID).setText(entry.getKey()); String cumul = null; for (String namealias : entry.getValue()) { config.aliases.put(namealias, entry.getKey()); if (cumul == null) { cumul = namealias; } else { cumul += " " + namealias; } } elt.addElement(XML_ALIASID).setText(cumul); } setAliases(root.asXML()); try { update(); } catch (WaarpDatabaseException e) { document.clearContent(); document = null; return false; } document.clearContent(); document = null; } else { for (Entry<String, HashSet<String>> entry : map.entrySet()) { for (String namealias : entry.getValue()) { config.aliases.put(namealias, entry.getKey()); } } } map.clear(); map = null; return true; }
From source file:org.waarp.openr66.database.data.DbHostConfiguration.java
License:Open Source License
/** * update Roles with possible purge and new or added content, and updating in memory information * @param config/*from ww w. j a v a 2 s . co m*/ * @param newroles * @param purged * @return True if ok */ public boolean updateRoles(Configuration config, String newroles, boolean purged) { HashMap<String, HashSet<String>> map = new HashMap<String, HashSet<String>>(); if (!updateMap(newroles, XML_ROLES + "/" + XML_ROLE, XML_ROLEID, XML_ROLESET, " |\\|", map)) { return false; } if (purged) { config.roles.clear(); } else { String roles = getRoles(); if (!updateMap(roles, XML_ROLES + "/" + XML_ROLE, XML_ROLEID, XML_ROLESET, " |\\|", map)) { return false; } } if ((newroles != null && !newroles.isEmpty()) || purged) { Document document = DocumentHelper.createDocument(DocumentHelper.createElement(XML_ROLES)); Element root = document.getRootElement(); RoleDefault newrole = new RoleDefault(); for (Entry<String, HashSet<String>> entry : map.entrySet()) { Element elt = root.addElement(XML_ROLE); elt.addElement(XML_ROLEID).setText(entry.getKey()); String cumul = null; if (entry.getValue().contains(ROLE.NOACCESS.name())) { newrole.setRole(ROLE.NOACCESS); cumul = ROLE.NOACCESS.name(); } for (String namerole : entry.getValue()) { try { RoleDefault.ROLE roletype = RoleDefault.ROLE.valueOf(namerole.toUpperCase()); if (roletype != ROLE.NOACCESS) { newrole.addRole(roletype); if (cumul == null) { cumul = namerole.toUpperCase(); } else { cumul += " " + namerole.toUpperCase(); } } } catch (IllegalArgumentException e) { // ignore } } logger.info("New Role: " + entry.getKey() + ":" + newrole); config.roles.put(entry.getKey(), newrole); elt.addElement(XML_ROLESET).setText(cumul); } setRoles(root.asXML()); try { update(); } catch (WaarpDatabaseException e) { document.clearContent(); document = null; return false; } document.clearContent(); document = null; } else { RoleDefault newrole = new RoleDefault(); for (Entry<String, HashSet<String>> entry : map.entrySet()) { if (entry.getValue().contains(ROLE.NOACCESS.name())) { newrole.setRole(ROLE.NOACCESS); } for (String namerole : entry.getValue()) { try { RoleDefault.ROLE roletype = RoleDefault.ROLE.valueOf(namerole.toUpperCase()); if (roletype != ROLE.NOACCESS) { newrole.addRole(roletype); } } catch (IllegalArgumentException e) { // ignore } } logger.info("New Role: " + entry.getKey() + ":" + newrole); config.roles.put(entry.getKey(), newrole); } } map.clear(); map = null; return true; }