/*******************************************************************************
 * Copyright 2012 Crazywater
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package de.knufficast.logic.model;

import java.io.IOException;
import java.io.StringReader;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.xmlpull.v1.XmlPullParserException;

import de.knufficast.logic.XmlParser;
import de.knufficast.util.SetUtil;

/**
 * A class to post-process feeds after downloading them. For now only used to
 * whitelist the HTML tags used in the description.
 * 
 * @author crazywater
 * 
 */
public class FeedPostProcessor extends XmlParser {
  private XMLEpisode episode;
  private StringBuilder stringBuilder;
  
  private Set<String> whitelist = SetUtil.hash("p", "a", "li", "ol", "ul",
      "span", "div", "i", "u", "b");

  public void process(List<XMLFeed> feeds) {
    for (XMLFeed feed : feeds) {
      for (XMLEpisode episode : feed.getEpisodes()) {
        this.episode = episode;
        processEpisode();
      }
    }
  }

  private void processEpisode() {
    try {
      String content = episode.getContent();
      if (!"".equals(content)) {
        stringBuilder = new StringBuilder();
        parseFrom(new StringReader(content));
        episode.setContent(stringBuilder.toString());
      }
    } catch (XmlPullParserException e) {
      episode.setContent("");
    } catch (IOException e) {
      episode.setContent("");
    }
  }

  @Override
  protected void openTag(String tag, Map<String, String> attributes) {
    if (whitelist.contains(tag)) {
      stringBuilder.append("<" + tag);
      for (Map.Entry<String, String> entry : attributes.entrySet()) {
        stringBuilder.append(" " + entry.getKey() + "=" + "'"
            + entry.getValue() + "'");
      }
      stringBuilder.append(">");
    } else if ("".equals(episode.getImgUrl()) && "img".equals(tag)) {
      if (attributes.containsKey("src")) {
        episode.setImgUrl(attributes.get("src"));
      }
    }
  }

  @Override
  protected void closeTag(String tag) {
    if (whitelist.contains(tag)) {
      stringBuilder.append("</");
      stringBuilder.append(tag);
      stringBuilder.append(">");
    }
  }

  @Override
  protected void tagText(String text) {
    String tag = getCurrentTag();
    if (whitelist.contains(tag)) {
      stringBuilder.append(text);
    }
  }
}

@@ -1258,79 +1258,20 @@
 ate 
-XMLEpisode episode;%0A  private StringBuilder stringBuilder;%0A  %0A  private
+static final
  Set
@@ -1319,16 +1319,22 @@
 %22, %22li%22,
+%0A     
  %22ol%22, %22
@@ -1337,22 +1337,16 @@
 %22, %22ul%22,
-%0A     
  %22span%22,
@@ -1366,19 +1366,137 @@
 %22u%22, %22b%22
-);%0A
+, %22strong%22, %22em%22, %22blockquote%22,%0A      %22center%22);%0A%0A  private XMLEpisode episode;%0A  private StringBuilder stringBuilder;%0A  
 %0A  publi

Add some more tags to the whitelist