Ejemplo n.º 1
0
 /**
  * RSSAutoDiscover要素からタイトルなどを取得して、FeedSourceに設定する。
  *
  * @param element
  * @return
  */
 private FeedSource setFeedSource(Element element) {
   FeedSource entity = new FeedSource();
   String title = element.getAttribute(REL_ATTRIBUTE_TITLE);
   String feedUrl = element.getAttribute(REL_ATTRIBUTE_HREF);
   entity.setTitle(title);
   entity.setFeedUrl(feedUrl);
   return entity;
 }
Ejemplo n.º 2
0
  /**
   * 指定されたURLから、フィードのURLを検索する。
   *
   * @param url フィードURL検索先
   * @return 検索結果リスト。見つからなかった場合は空のArrayListを返却する。
   * @throws FeedParseException HTML解析中にエラーが発生した場合
   */
  public ArrayList<FeedSource> findFeedUrl(String strUrl) throws FeedParseException {
    ArrayList<FeedSource> feedList = new ArrayList<FeedSource>();
    HttpURLConnection connection = null;
    InputStream is = null;
    try {
      // 指定されたURLに接続する。
      connection = connectURL(strUrl);
      is = connection.getInputStream();

      String enc = connection.getContentEncoding();
      if (enc == null) {
        // content-encodingヘッダが設定されて無い場合はとりあえずutf-8を設定。
        enc = "utf-8";
      }

      // 接続したサイトのソースを取得し、パースする。
      DOMResult result = parseDOM(is, enc);

      // linkタグを取得する。
      Document doc = (Document) result.getNode();
      String docEnc = getDocumentEncoding(doc);

      if (docEnc != null && !enc.equals(docEnc)) {
        // Documentの文字コードが事前に設定したものと違う場合は、
        // 文字コードを変更し、再接続する。
        dispose(connection, is);
        connection = connectURL(strUrl);
        is = connection.getInputStream();

        result = parseDOM(is, docEnc);
        doc = (Document) result.getNode();
      }
      NodeList childs = doc.getElementsByTagName(TAG_NAME_LINK);

      for (int i = 0; i < childs.getLength(); i++) {
        Element element = (Element) childs.item(i);

        if (isRssAutoDiscovery(element)) {
          FeedSource entity = setFeedSource(element);
          FeedParseFacade facade = new FeedParseFacade();
          entity.setVersion(facade.getRssVersion(entity.getFeedUrl()));
          feedList.add(entity);
        }
      }

    } catch (Exception e) {
      throw new FeedParseException(e);
    } finally {
      // 後始末
      dispose(connection, is);
    }
    return feedList;
  }