/*
 * $Id: RSC2014HtmlHashFilterFactory.java,v 1.2 2014-07-01 18:23: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 {
  
  Logger log = Logger.getLogger(RSC2014HtmlHashFilterFactory.class);
  
  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"),
    };
    
    // HTML transform to html attributes that are moving around
    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)) {
                  Attribute a = tag.getAttributeEx("html");
                  Vector<Attribute> v = new Vector<Attribute>();
                  v.add(a);
                  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;
      }
    };
    
    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));
  }
  
}

@@ -42,17 +42,17 @@
 ava,v 1.
-2
+3
  2014-07
@@ -56,19 +56,19 @@
 -07-
-01 18:23:01
+31 22:31:25
  ete
@@ -2921,14 +2921,9 @@
  // 
-HTML t
+T
 rans
@@ -2934,46 +2934,190 @@
  to 
-html attributes that are moving around
+remove attributes from html and other tags%0A    // some tag attributes changed based on IP or other status%0A    // some attributes changed over time, either arbitrarily or sequentially
 %0A   
@@ -3511,16 +3511,238 @@
 tagName)
+ %7C%7C%0A                    %22span%22.equals(tagName) %7C%7C%0A                    %22div%22.equals(tagName) %7C%7C%0A                    %22h1%22.equals(tagName) %7C%7C%0A                    %22h2%22.equals(tagName) %7C%7C%0A                    %22a%22.equals(tagName)
 ) %7B%0A    
@@ -3788,22 +3788,23 @@
 ibuteEx(
-%22html%22
+tagName
 );%0A     

Update to remove attributes from additional tags