Exemple #1
0
  private static int importFromBookmarks(
      BookmarksDB db,
      final DigestURI baseURL,
      final InputStreamReader input,
      final String tag,
      final boolean importPublic) {

    int importCount = 0;

    Map<MultiProtocolURI, Properties> links = new HashMap<MultiProtocolURI, Properties>();
    String title;
    MultiProtocolURI url;
    Bookmark bm;
    final Set<String> tags = ListManager.string2set(tag); // this allow multiple default tags
    try {
      // load the links
      final ContentScraper scraper = new ContentScraper(baseURL);
      // OutputStream os = new htmlFilterOutputStream(null, scraper, null, false);
      final Writer writer = new TransformerWriter(null, null, scraper, null, false);
      FileUtils.copy(input, writer);
      writer.close();
      links = scraper.getAnchors();
    } catch (final IOException e) {
      Log.logWarning(
          "BOOKMARKS", "error during load of links: " + e.getClass() + " " + e.getMessage());
    }
    for (final Entry<MultiProtocolURI, Properties> link : links.entrySet()) {
      url = link.getKey();
      title = link.getValue().getProperty("name", "");
      Log.logInfo("BOOKMARKS", "links.get(url)");
      if ("".equals(title)) { // cannot be displayed
        title = url.toString();
      }
      bm = db.new Bookmark(url.toString());
      bm.setProperty(Bookmark.BOOKMARK_TITLE, title);
      bm.setTags(tags);
      bm.setPublic(importPublic);
      db.saveBookmark(bm);

      importCount++;
    }

    return importCount;
  }
Exemple #2
0
  private static int parseXMLimport(BookmarksDB db, final Node doc, final boolean importPublic) {
    int importCount = 0;
    if ("post".equals(doc.getNodeName())) {
      final NamedNodeMap attributes = doc.getAttributes();
      final String url = attributes.getNamedItem("href").getNodeValue();
      if ("".equals(url)) {
        return 0;
      }
      final Bookmark bm = db.new Bookmark(url);
      String tagsString = "";
      String title = "";
      String description = "";
      String time = "";
      if (attributes.getNamedItem("tag") != null) {
        tagsString = attributes.getNamedItem("tag").getNodeValue();
      }
      if (attributes.getNamedItem("description") != null) {
        title = attributes.getNamedItem("description").getNodeValue();
      }
      if (attributes.getNamedItem("extended") != null) {
        description = attributes.getNamedItem("extended").getNodeValue();
      }
      if (attributes.getNamedItem("time") != null) {
        time = attributes.getNamedItem("time").getNodeValue();
      }
      Set<String> tags = new HashSet<String>();

      if (title != null) {
        bm.setProperty(Bookmark.BOOKMARK_TITLE, title);
      }
      if (tagsString != null) {
        tags = ListManager.string2set(tagsString.replace(' ', ','));
      }
      bm.setTags(tags, true);
      if (time != null) {

        Date parsedDate = null;
        try {
          parsedDate = ISO8601Formatter.FORMATTER.parse(time);
        } catch (final ParseException e) {
          parsedDate = new Date();
        }
        bm.setTimeStamp(parsedDate.getTime());
      }
      if (description != null) {
        bm.setProperty(Bookmark.BOOKMARK_DESCRIPTION, description);
      }
      bm.setPublic(importPublic);
      db.saveBookmark(bm);

      importCount++;
    }
    final NodeList children = doc.getChildNodes();
    if (children != null) {
      for (int i = 0; i < children.getLength(); i++) {
        importCount += parseXMLimport(db, children.item(i), importPublic);
      }
    }

    return importCount;
  }