コード例 #1
0
  protected List<SyndCategory> getCategories(
      Document doc, Map<String, Object> params, XWikiContext context) throws XWikiException {
    String mapping = (String) params.get(FIELD_CATEGORIES);
    if (mapping == null) {
      return getDefaultCategories(doc, params, context);
    }

    List<Object> categories;
    if (isVelocityCode(mapping)) {
      categories = parseList(mapping, doc, context);
    } else {
      categories = getListValue(mapping, doc, context);
    }

    List<SyndCategory> result = new ArrayList<SyndCategory>();
    for (Object category : categories) {
      if (category instanceof SyndCategory) {
        result.add((SyndCategory) category);
      } else if (category != null) {
        SyndCategory scat = new SyndCategoryImpl();
        scat.setName(category.toString());
        result.add(scat);
      }
    }
    return result;
  }
コード例 #2
0
  /**
   * A helper function to print out the contents of an entry to log.trace
   *
   * @param entry
   */
  public static void printEntry(SyndEntry entry) {
    if (!log.isTraceEnabled()) {
      return;
    }

    StringBuffer pr =
        new StringBuffer(
            "URI: "
                + entry.getUri()
                + "\n"
                + "Title: "
                + entry.getTitle()
                + "\n"
                + "\n"
                + "Date: "
                + entry.getPublishedDate()
                + "\n"
                + "Modified: "
                + entry.getUpdatedDate()
                + "\n");

    pr.append("Creators: \n");

    for (SyndPerson author : FeedHelper.getAuthors(entry)) {
      pr.append("  - " + author.getName() + "\n");
    }

    pr.append("Links: \n");
    for (SyndLink link : FeedHelper.getLinks(entry)) {
      pr.append("  - " + link.getTitle() + ": " + link.getHref() + "\n");
    }

    SyndContent description = entry.getDescription();
    if (description != null) {
      pr.append("\nDescription(" + description.getType() + "): " + description.getValue());
    }

    pr.append("Contents: \n");
    for (SyndContent content : FeedHelper.getContents(entry)) {
      pr.append(" Type: " + content.getType());
      pr.append(" Body: " + content.getValue());
      try {
        pr.append(
            " Body (Plain text): "
                + PlainTextExtractor.getPlainText(content.getType(), content.getValue()));
      } catch (ParserException e) {
        pr.append("Failed to parse content");
      }
    }

    pr.append("Categories: \n");
    for (SyndCategory category : FeedHelper.getCategories(entry)) {
      pr.append(category.getName() + "(" + category.getTaxonomyUri() + ")");
    }
    log.trace(pr.toString());
  }
コード例 #3
0
ファイル: SyndFeedFactory.java プロジェクト: GUEDDES/gnizr
 public static SyndFeed create(
     List<Bookmark> bookmarks,
     String author,
     String title,
     String link,
     Date pubDate,
     String feedUri) {
   logger.debug("SyndFeedFactory create method called");
   logger.debug("bookmarks=" + bookmarks);
   logger.debug("author=" + author);
   logger.debug("title=" + title);
   logger.debug("link=" + link);
   logger.debug("pubDate=" + pubDate);
   logger.debug("feedUri=" + feedUri);
   SyndFeed syndFeed = new SyndFeedImpl();
   syndFeed.setAuthor(author);
   syndFeed.setTitle(title);
   syndFeed.setUri(feedUri);
   syndFeed.setLink(link);
   syndFeed.setPublishedDate(pubDate);
   List<SyndEntry> entries = new ArrayList<SyndEntry>();
   for (Bookmark bmark : bookmarks) {
     SyndEntry entry = new SyndEntryImpl();
     entry.setTitle(bmark.getTitle());
     entry.setAuthor(bmark.getUser().getUsername());
     entry.setLink(bmark.getLink().getUrl());
     entry.setUri("urn:bookmark:" + bmark.getId());
     entry.setUpdatedDate(bmark.getLastUpdated());
     List<String> tags = bmark.getTagList();
     List<SyndCategory> cats = new ArrayList<SyndCategory>();
     for (String aTag : tags) {
       SyndCategory aCat = new SyndCategoryImpl();
       aCat.setName(aTag);
       cats.add(aCat);
     }
     if (cats.isEmpty() == false) {
       entry.setCategories(cats);
     }
     SyndContent cnt = new SyndContentImpl();
     cnt.setType("text/html");
     cnt.setValue(bmark.getNotes());
     entry.setDescription(cnt);
     entries.add(entry);
   }
   syndFeed.setEntries(entries);
   syndFeed.setEncoding("UTF-8");
   logger.debug("done initializing syndFeed object");
   return syndFeed;
 }
コード例 #4
0
 protected void _testCategories(List cats, String prefix) throws Exception {
   Set s1 = new HashSet();
   Set s2 = new HashSet();
   for (int i = 0; i < cats.size(); i++) {
     SyndCategory cat = (SyndCategory) cats.get(i);
     s1.add(cat.getTaxonomyUri() + " " + cat.getName());
     s2.add(
         getPrefix()
             + "."
             + prefix
             + ".category["
             + i
             + "]^domain"
             + " "
             + getPrefix()
             + "."
             + prefix
             + ".category["
             + i
             + "]");
   }
   assertTrue(s1.equals(s2));
 }
コード例 #5
0
 protected void testCategories(final List<SyndCategory> cats, final String prefix)
     throws Exception {
   final Set<String> s1 = new HashSet<String>();
   final Set<String> s2 = new HashSet<String>();
   for (int i = 0; i < cats.size(); i++) {
     final SyndCategory cat = cats.get(i);
     s1.add(cat.getTaxonomyUri() + " " + cat.getName());
     s2.add(
         getPrefix()
             + "."
             + prefix
             + ".category["
             + i
             + "]^domain"
             + " "
             + getPrefix()
             + "."
             + prefix
             + ".category["
             + i
             + "]");
   }
   assertTrue(s1.equals(s2));
 }
コード例 #6
0
ファイル: RSSImporterImpl.java プロジェクト: fregaham/KiWi
  //	@Transactional
  //	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
  public void importEntry(
      final SyndFeed feed,
      final SyndEntry entry,
      final Set<KiWiUriResource> types,
      final Set<ContentItem> tags,
      User user,
      final Collection<ContentItem> output) {
    if (user == null && entry.getAuthor() != null && !"".equals(entry.getAuthor())) {
      if (userService.userExists(entry.getAuthor())) {
        user = userService.getUserByLogin(entry.getAuthor());
      } else {

        //				user = userService.createUser(entry.getAuthor());
        /* In my opinion, it is not ok to create a user entity
         * without asking the person if he/she wants to be
         * created and persisted in the KiWi dataset.
         * Thus I'm changing the user to 'anonymous',
         * if he/she is'nt registered with the same nick that
         * is given in the rss entry.
         */
        user = userService.getUserByLogin("anonymous");
        kiwiEntityManager.persist(user);
      }
    }

    log.debug("feed entry: #0 (#1)", entry.getTitle(), entry.getUri());

    // create a new content item and copy all data from the feed entry
    ContentItem item;
    if (entry.getLink() != null) {
      item = contentItemService.createExternContentItem(entry.getLink());
    } else if (entry.getUri() != null) {
      try {
        // try parsing URI; if it is not valid,
        URI uri = new URI(entry.getUri());
        item = contentItemService.createExternContentItem(entry.getUri());
      } catch (URISyntaxException e) {
        item = contentItemService.createExternContentItem(feed.getLink() + "#" + entry.getUri());
      }
    } else {
      item = contentItemService.createContentItem();
    }
    contentItemService.updateTitle(item, entry.getTitle());

    if (feed.getLanguage() != null) item.setLanguage(new Locale(feed.getLanguage()));

    if (entry.getPublishedDate() != null) {
      item.setCreated(entry.getPublishedDate());
      item.setModified(entry.getPublishedDate());
    }

    if (entry.getUpdatedDate() != null) {
      if (entry.getPublishedDate() == null) {
        item.setCreated(entry.getUpdatedDate());
      }
      item.setModified(entry.getUpdatedDate());
    }

    item.setAuthor(user);

    // read feed content and set it as item's text content
    List<SyndContent> contents = entry.getContents();
    if (contents.size() == 1) {
      log.debug("using RSS content section provided by item");
      contentItemService.updateTextContentItem(item, "<p>" + contents.get(0).getValue() + "</p>");
    } else if (contents.size() > 1) {
      log.warn("feed entry contained more than one content section");
      contentItemService.updateTextContentItem(item, "<p>" + contents.get(0).getValue() + "</p>");
    } else if (contents.size() == 0) {
      if (entry.getDescription() != null && entry.getDescription().getValue() != null) {
        log.debug("using RSS description as no content section was available");
        contentItemService.updateTextContentItem(
            item, "<p>" + entry.getDescription().getValue() + "</p>");
      }
    }

    // save before tagging
    contentItemService.saveContentItem(item);

    // read feed categories and use them as tags
    for (SyndCategory cat : (List<SyndCategory>) entry.getCategories()) {
      ContentItem _cat;
      if (!taggingService.hasTag(item, cat.getName())) {
        if (cat.getTaxonomyUri() != null) {
          _cat = contentItemService.getContentItemByUri(cat.getTaxonomyUri());
          if (_cat == null) {
            _cat = contentItemService.createExternContentItem(cat.getTaxonomyUri());
            contentItemService.updateTitle(_cat, cat.getName());
            _cat.setAuthor(user);
            contentItemService.saveContentItem(_cat);
          }
          taggingService.createTagging(cat.getName(), item, _cat, user);
        } else {
          _cat = contentItemService.getContentItemByTitle(cat.getName());
          if (_cat == null) {
            _cat = contentItemService.createContentItem();
            contentItemService.updateTitle(_cat, cat.getName());
            _cat.setAuthor(user);
            contentItemService.saveContentItem(_cat);
          }
          taggingService.createTagging(cat.getName(), item, _cat, user);
        }
      }
    }
    // scan for Twitter-style hash tags in title (e.g. #kiwiknows, see KIWI-622)
    Matcher m_hashtag = p_hashtag.matcher(entry.getTitle());
    while (m_hashtag.find()) {
      String tag_label = m_hashtag.group(1);
      if (!taggingService.hasTag(item, tag_label)) {
        ContentItem tag = contentItemService.getContentItemByTitle(tag_label);
        if (tag == null) {
          tag = contentItemService.createContentItem();
          contentItemService.updateTitle(tag, tag_label);
          tag.setAuthor(user);
          contentItemService.saveContentItem(tag);
        }
        taggingService.createTagging(tag_label, item, tag, user);
      }
    }

    // check for geo information
    GeoRSSModule geoRSSModule = GeoRSSUtils.getGeoRSS(entry);
    if (geoRSSModule != null && geoRSSModule.getPosition() != null) {
      POI poi = kiwiEntityManager.createFacade(item, POI.class);
      poi.setLatitude(geoRSSModule.getPosition().getLatitude());
      poi.setLongitude(geoRSSModule.getPosition().getLongitude());
      kiwiEntityManager.persist(poi);
    }

    // check for media information
    MediaEntryModule mediaModule = (MediaEntryModule) entry.getModule(MediaModule.URI);
    if (mediaModule != null) {
      MediaContent[] media = mediaModule.getMediaContents();
      if (media.length > 0) {
        MediaContent m = media[0];
        if (m.getReference() instanceof UrlReference) {
          URL url = ((UrlReference) m.getReference()).getUrl();

          String type = m.getType();
          String name = url.getFile();
          if (name.lastIndexOf("/") > 0) {
            name = name.substring(name.lastIndexOf("/") + 1);
          }

          log.debug("importing media data from URL #0", url.toString());

          try {
            InputStream is = url.openStream();

            ByteArrayOutputStream bout = new ByteArrayOutputStream();

            int c;
            while ((c = is.read()) != -1) {
              bout.write(c);
            }

            byte[] data = bout.toByteArray();

            contentItemService.updateMediaContentItem(item, data, type, name);

            is.close();
            bout.close();
          } catch (IOException ex) {
            log.error("error importing media content from RSS stream");
          }
        } else {
          log.info("RSS importer can only import media with URL references");
        }
      } else {
        log.warn("media module found without content");
      }

      Category[] cats = mediaModule.getMetadata().getCategories();
      for (Category cat : cats) {
        ContentItem _cat;

        String label = cat.getLabel() != null ? cat.getLabel() : cat.getValue();

        if (!taggingService.hasTag(item, label)) {
          if (cat.getScheme() != null) {
            _cat = contentItemService.getContentItemByUri(cat.getScheme() + cat.getValue());
            if (_cat == null) {
              _cat = contentItemService.createExternContentItem(cat.getScheme() + cat.getValue());
              contentItemService.updateTitle(_cat, label);
              _cat.setAuthor(user);
              contentItemService.saveContentItem(_cat);
            }
            taggingService.createTagging(label, item, _cat, user);
          } else {
            _cat = contentItemService.getContentItemByTitle(label);
            if (_cat == null) {
              _cat = contentItemService.createContentItem();
              contentItemService.updateTitle(_cat, label);
              _cat.setAuthor(user);
              contentItemService.saveContentItem(_cat);
            }
            taggingService.createTagging(label, item, _cat, user);
          }
        }
      }
    }

    // add parameter categories as tags
    for (ContentItem tag : tags) {
      if (!taggingService.hasTag(item, tag.getTitle())) {
        taggingService.createTagging(tag.getTitle(), item, tag, user);
      }
    }

    // add parameter types as types
    for (KiWiUriResource type : types) {
      item.addType(type);
    }

    // add kiwi:FeedPost type
    item.addType(tripleStore.createUriResource(Constants.NS_KIWI_CORE + "FeedPost"));

    /* the flush is necessary, because CIs or tags will
     * otherwise be created multiple times when they
     * appear more than once in one RSS feed */
    entityManager.flush();
    log.debug("imported content item '#0' with URI '#1'", item.getTitle(), item.getResource());
  }
コード例 #7
0
  // Logs a new ATOM entry
  public static synchronized void addATOMEntry(
      String title, String link, String description, File atomFile, String context) {
    try {

      if (atomFile.exists()) {

        // System.out.println("ATOM file found!");
        /** Namespace URI for content:encoded elements */
        String CONTENT_NS = "http://www.w3.org/2005/Atom";

        /** Parses RSS or Atom to instantiate a SyndFeed. */
        SyndFeedInput input = new SyndFeedInput();

        /** Transforms SyndFeed to RSS or Atom XML. */
        SyndFeedOutput output = new SyndFeedOutput();

        // Load the feed, regardless of RSS or Atom type
        SyndFeed feed = input.build(new XmlReader(atomFile));

        // Set the output format of the feed
        feed.setFeedType("atom_1.0");

        List<SyndEntry> items = feed.getEntries();
        int numItems = items.size();
        if (numItems > 9) {
          items.remove(0);
          feed.setEntries(items);
        }

        SyndEntry newItem = new SyndEntryImpl();
        newItem.setTitle(title);
        newItem.setLink(link);
        newItem.setUri(link);
        SyndContent desc = new SyndContentImpl();
        desc.setType("text/html");
        desc.setValue(description);
        newItem.setDescription(desc);
        desc.setType("text/html");
        newItem.setPublishedDate(new java.util.Date());

        List<SyndCategory> categories = new ArrayList<SyndCategory>();
        if (CommonConfiguration.getProperty("htmlTitle", context) != null) {
          SyndCategory category2 = new SyndCategoryImpl();
          category2.setName(CommonConfiguration.getProperty("htmlTitle", context));
          categories.add(category2);
        }
        newItem.setCategories(categories);
        if (CommonConfiguration.getProperty("htmlAuthor", context) != null) {
          newItem.setAuthor(CommonConfiguration.getProperty("htmlAuthor", context));
        }
        items.add(newItem);
        feed.setEntries(items);

        feed.setPublishedDate(new java.util.Date());

        FileWriter writer = new FileWriter(atomFile);
        output.output(feed, writer);
        writer.toString();
      }
    } catch (IOException ioe) {
      System.out.println("ERROR: Could not find the ATOM file.");
      ioe.printStackTrace();
    } catch (Exception e) {
      System.out.println("Unknown exception trying to add an entry to the ATOM file.");
      e.printStackTrace();
    }
  }
コード例 #8
0
ファイル: RomeFeedFetcher.java プロジェクト: wanghl/Roller
  // build a SubscriptionEntry from Rome SyndEntry and SyndFeed
  private SubscriptionEntry buildEntry(SyndEntry romeEntry) {

    // if we don't have a permalink then we can't continue
    if (romeEntry.getLink() == null) {
      return null;
    }

    SubscriptionEntry newEntry = new SubscriptionEntry();

    newEntry.setTitle(romeEntry.getTitle());
    newEntry.setPermalink(romeEntry.getLink());

    // Play some games to get the author
    DCModule entrydc = (DCModule) romeEntry.getModule(DCModule.URI);
    if (romeEntry.getAuthor() != null) {
      newEntry.setAuthor(romeEntry.getAuthor());
    } else {
      newEntry.setAuthor(entrydc.getCreator()); // use <dc:creator>
    }

    // Play some games to get the updated date
    if (romeEntry.getUpdatedDate() != null) {
      newEntry.setUpdateTime(new Timestamp(romeEntry.getUpdatedDate().getTime()));
    }
    // TODO: should we set a default update time here?

    // And more games getting publish date
    if (romeEntry.getPublishedDate() != null) {
      newEntry.setPubTime(new Timestamp(romeEntry.getPublishedDate().getTime())); // use <pubDate>
    } else if (entrydc != null && entrydc.getDate() != null) {
      newEntry.setPubTime(new Timestamp(entrydc.getDate().getTime())); // use <dc:date>
    } else {
      newEntry.setPubTime(newEntry.getUpdateTime());
    }

    // get content and unescape if it is 'text/plain'
    if (romeEntry.getContents().size() > 0) {
      SyndContent content = (SyndContent) romeEntry.getContents().get(0);
      if (content != null && content.getType().equals("text/plain")) {
        newEntry.setText(StringEscapeUtils.unescapeHtml(content.getValue()));
      } else if (content != null) {
        newEntry.setText(content.getValue());
      }
    }

    // no content, try summary
    if (newEntry.getText() == null || newEntry.getText().trim().length() == 0) {
      if (romeEntry.getDescription() != null) {
        newEntry.setText(romeEntry.getDescription().getValue());
      }
    }

    // copy categories
    if (romeEntry.getCategories().size() > 0) {
      List list = new ArrayList();
      Iterator cats = romeEntry.getCategories().iterator();
      while (cats.hasNext()) {
        SyndCategory cat = (SyndCategory) cats.next();
        list.add(cat.getName());
      }
      newEntry.setCategoriesString(list);
    }

    return newEntry;
  }