/*
 * $Id: SilverchairHtmlLinkExtractor.java,v 1.2 2014-05-07 14:14:43 thib_gc 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.silverchair;

import java.io.IOException;
import java.net.URL;
import java.util.regex.*;

import org.lockss.extractor.*;
import org.lockss.plugin.ArchivalUnit;
import org.lockss.util.Logger;

public class SilverchairHtmlLinkExtractor extends GoslingHtmlLinkExtractor {

  private static final Logger logger = Logger.getLogger(SilverchairHtmlLinkExtractor.class);
  
  protected static final Pattern PATTERN_ARTICLE =
      Pattern.compile("/article\\.aspx\\?(articleid=[^&]+)$",
                      Pattern.CASE_INSENSITIVE);
  
  protected static final Pattern PATTERN_CITATION =
      Pattern.compile("/downloadCitation\\.aspx\\?(format=[^&]+)?$",
                      Pattern.CASE_INSENSITIVE);
  
  protected static final Pattern PATTERN_JQUERY =
      Pattern.compile("ajax\\.googleapis\\.com/ajax/libs/jquery/([^/]+)/jquery\\.min\\.js$",
                      Pattern.CASE_INSENSITIVE);
  
  @Override
  protected String extractLinkFromTag(StringBuffer link,
                                      ArchivalUnit au,
                                      Callback cb)
      throws IOException {
    char ch = link.charAt(0);
    if ((ch == 'a' || ch == 'A') && beginsWithTag(link, ATAG)) {
      String href = getAttributeValue(HREF, link);
      if (href == null) {
        href = "";
      }
      Matcher hrefMat = null;

      hrefMat = PATTERN_CITATION.matcher(href);
      if (hrefMat.find()) {
        logger.debug3("Found target citation URL");
        // Derive citation format; can be null
        String formatPair = hrefMat.group(1);
        Matcher srcUrlMat = PATTERN_ARTICLE.matcher(srcUrl);
        if (srcUrlMat.find()) {
          // Derive article ID
          String articleIdPair = srcUrlMat.group(1);
          // Generate correct citation URL
          String url = null;
          if (formatPair == null) {
            url = String.format("/downloadCitation.aspx?%s", articleIdPair);
          }
          else {
            url = String.format("/downloadCitation.aspx?%s&%s", formatPair, articleIdPair);
          }
          logger.debug3(String.format("Generated %s", url));
          if (baseUrl == null) { baseUrl = new URL(srcUrl); } // Copycat of parseLink()
          emit(cb, resolveUri(baseUrl, url));
        }
        return super.extractLinkFromTag(link, au, cb);
      }
      
      hrefMat = PATTERN_JQUERY.matcher(href);
      if (hrefMat.find()) {
        String jqueryVersion = hrefMat.group(1);
        // Generate local JQuery URL
        String url = String.format("/JS/plugins/jquery-%s.min.js", jqueryVersion);
        logger.debug3(String.format("Generated %s", url));
        if (baseUrl == null) { baseUrl = new URL(srcUrl); } // Copycat of parseLink()
        emit(cb, resolveUri(baseUrl, url));
        return super.extractLinkFromTag(link, au, cb);
      }
    }

    return super.extractLinkFromTag(link, au, cb);
  }

}

@@ -42,17 +42,17 @@
 ava,v 1.
-2
+3
  2014-05
@@ -62,12 +62,12 @@
  14:
-14:4
+38:2
 3 th
@@ -2596,16 +2596,17 @@
 rAt(0);%0A
+%0A
     if (
@@ -3788,30 +3788,247 @@
 %0A      %0A    
-  href
+%7D%0A%0A    else if ((ch == 's' %7C%7C ch == 'S') && beginsWithTag(link, SCRIPTTAG)) %7B%0A      String src = getAttributeValue(SRC, link);%0A      if (src == null) %7B%0A        src = %22%22;%0A      %7D%0A      Matcher srcMat = null;%0A      %0A      src
 Mat = PATTER
@@ -4036,36 +4036,35 @@
 _JQUERY.matcher(
-href
+src
 );%0A      if (hre
@@ -4052,36 +4052,35 @@
 src);%0A      if (
-href
+src
 Mat.find()) %7B%0A  
@@ -4069,32 +4069,82 @@
 rcMat.find()) %7B%0A
+        logger.debug3(%22Found target JQuery URL%22);%0A
         String j
@@ -4158,20 +4158,19 @@
 rsion = 
-href
+src
 Mat.grou
@@ -4550,24 +4550,29 @@
    %7D%0A    %7D%0A%0A
+    %0A
     return s

Separate citation URLs from JQuery URLs.