/*
 * $Id: RSC2014HtmlHashFilterFactory.java,v 1.6 2014-12-22 22:58:01 etenbrink Exp $
 */

/*

Copyright (c) 2000-2014 Board of Trustees of Leland Stanford Jr. University,
all rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
STANFORD UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of Stanford University shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from Stanford University.

*/

package org.lockss.plugin.royalsocietyofchemistry;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Vector;

import org.htmlparser.Attribute;
import org.htmlparser.NodeFilter;
import org.htmlparser.Tag;
import org.htmlparser.filters.OrFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.visitors.NodeVisitor;
import org.lockss.daemon.PluginException;
import org.lockss.filter.FilterUtil;
import org.lockss.filter.WhiteSpaceFilter;
import org.lockss.filter.html.*;
import org.lockss.plugin.*;
import org.lockss.util.Logger;
import org.lockss.util.ReaderInputStream;

public class RSC2014HtmlHashFilterFactory implements FilterFactory {
  
  private static final Logger log = Logger.getLogger(RSC2014HtmlHashFilterFactory.class);
  
  // Transform to remove attributes from html and other tags
  // some tag attributes changed based on IP or other status
  // some attributes changed over time, either arbitrarily or sequentially
  protected static HtmlTransform xform = new HtmlTransform() {
    @Override
    public NodeList transform(NodeList nodeList) throws IOException {
      try {
        nodeList.visitAllNodesWith(new NodeVisitor() {
          @Override
          public void visitTag(Tag tag) {
            String tagName = tag.getTagName().toLowerCase();
            try {
              if ("html".equals(tagName) ||
                  "span".equals(tagName) ||
                  "div".equals(tagName) ||
                  "h1".equals(tagName) ||
                  "h2".equals(tagName) ||
                  "h3".equals(tagName) ||
                  "a".equals(tagName)) {
                Attribute a = tag.getAttributeEx(tagName);
                Vector<Attribute> v = new Vector<Attribute>();
                v.add(a);
                if (tag.isEmptyXmlTag()) {
                  Attribute end = tag.getAttributeEx("/");
                  v.add(end);
                }
                tag.setAttributesEx(v);
              }
            }
            catch (Exception exc) {
              log.debug2("Internal error (visitor)", exc); // Ignore this tag and move on
            }
            // Always
            super.visitTag(tag);
          }
        });
      }
      catch (ParserException pe) {
        log.debug2("Internal error (parser)", pe); // Bail
      }
      return nodeList;
    }
  };
  
  public InputStream createFilteredInputStream(ArchivalUnit au, InputStream in,
                                               String encoding)
      throws PluginException {
    NodeFilter[] filters = new NodeFilter[] {
        // Contains the current year.
        HtmlNodeFilters.tagWithAttributeRegex("div", "class", "footer"),
        // remove header for minor changes in nav & breadcrumbs
        HtmlNodeFilters.tagWithAttribute("div", "class", "header"),
        // Changeable scripts
        HtmlNodeFilters.tag("script"),
        // remove head for metadata changes and JS/CSS version number
        HtmlNodeFilters.tag("head"),
    };
    
    InputStream filtered =  new HtmlFilterInputStream(in, encoding,
        new HtmlCompoundTransform(HtmlNodeFilterTransform.exclude(new OrFilter(filters)), xform));
    Reader filteredReader = FilterUtil.getReader(filtered, encoding);
    return new ReaderInputStream(new WhiteSpaceFilter(filteredReader));
  }
  
}

@@ -46,29 +46,29 @@
 v 1.
-6
+7
  201
-4-12-22 22:58:01
+5-02-02 23:35:34
  ete
@@ -3863,16 +3863,28 @@
   %7D;%0A  %0A
+  @Override%0A
   public
@@ -4511,24 +4511,204 @@
 ag(%22head%22),%0A
+        // %3Cdiv id=%22top%22 class=%22navigation%22  access links intermittent http://xlink.rsc.org/?doi=c3dt52391h%0A        HtmlNodeFilters.tagWithAttribute(%22div%22, %22class%22, %22navigation%22),%0A
     %7D;%0A    %0A

Add new html hash filter for nav block