/*
 * $Id: SimpleHtmlMetaTagMetadataExtractor.java,v 1.9 2011-11-09 05:19:17 pgust Exp $
 */

/*

Copyright (c) 2000-2010 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.extractor;

import java.io.*;
import org.apache.commons.lang.StringEscapeUtils;

import org.lockss.util.*;
import org.lockss.plugin.*;

public class SimpleHtmlMetaTagMetadataExtractor
  extends SimpleFileMetadataExtractor {

  static Logger log = Logger.getLogger("SimpleHtmlMetaTagMetadataExtractor");

  public SimpleHtmlMetaTagMetadataExtractor() {
  }

  public ArticleMetadata extract(MetadataTarget target, CachedUrl cu)
      throws IOException {
    if (cu == null) {
      throw new IllegalArgumentException("extract() called with null CachedUrl");
    }
    ArticleMetadata ret = new ArticleMetadata();
    BufferedReader bReader =
      new BufferedReader(cu.openForReading());
    for (String line = bReader.readLine();
	 line != null;
	 line = bReader.readLine()) {
      int i = StringUtil.indexOfIgnoreCase(line, "<meta ");
      while (i >= 0) {
        // recognize end of tag character preceded by a double-quote,
        // separated by zero or more whitespace characters
        int j = i+1;
        while (true) {
          j = StringUtil.indexOfIgnoreCase(line, ">", j);
          if ((j < 0) || line.substring(i,j).trim().endsWith("\"")) {
            break;
          }
          j++;
        }
        if (j < 0) {
          // join next line with tag end
          String nextLine = bReader.readLine();
          if (nextLine == null) {
            break;
          }
          line += nextLine;
          continue;
        }
        String meta = line.substring(i, j+1);
        if (log.isDebug3()) log.debug3("meta: " + meta);
        addTag(meta, ret);
        i = StringUtil.indexOfIgnoreCase(line, "<meta ", j+1);
      }
    }
    IOUtil.safeClose(bReader);
    return ret;
  }
  private void addTag(String line, ArticleMetadata ret) {
    String nameFlag = "name=\"";
    int nameBegin = StringUtil.indexOfIgnoreCase(line, nameFlag);
    if (nameBegin <= 0) {
      if (log.isDebug3()) log.debug3(line + " : no " + nameFlag);
      return;
    }
    nameBegin += nameFlag.length();
    int nameEnd = line.indexOf('"', nameBegin + 1);
    if (nameEnd <= nameBegin) {
      log.debug2(line + " : " + nameFlag + " unterminated");
      return;
    }
    String name = line.substring(nameBegin, nameEnd);
    String contentFlag = "content=\"";
    int contentBegin = StringUtil.indexOfIgnoreCase(line, contentFlag);
    if (contentBegin <= 0) {
      if (log.isDebug3()) log.debug3(line + " : no " + contentFlag);
      return;
    }
    if (nameBegin <= contentBegin && nameEnd >= contentBegin) {
      log.debug2(line + " : " + contentFlag + " overlaps " + nameFlag);
      return;
    }
    contentBegin += contentFlag.length();
    int contentEnd = line.indexOf('"', contentBegin + 1);
    if (log.isDebug3()) {
      log.debug3(line + " name [" + nameBegin + "," + nameEnd + "] cont [" +
		 contentBegin + "," + contentEnd + "]");
    }
    if (contentEnd <= contentBegin) {
      log.debug2(line + " : " + contentFlag + " unterminated");
      return;
    }
    if (contentBegin <= (nameBegin - nameFlag.length())
	&& contentEnd >= (nameBegin - nameFlag.length())) {
      log.debug2(line + " : " + nameFlag + " overlaps " + contentFlag);
      return;
    }
      
    String content = line.substring(contentBegin, contentEnd);
    // filter out raw HTML tags embedded within content value
    // -- publishers sometimes get sloppy
    content = content.replaceAll("<[^>]+>", "");
    content = StringEscapeUtils.unescapeHtml(content);

    if (log.isDebug3()) log.debug3("Add: " + name + " = " + content);
    ret.putRaw(name, content);
  }
}

@@ -48,17 +48,18 @@
 ava,v 1.
-9
+10
  2011-11
@@ -67,15 +67,15 @@
 09 0
-5:19:17
+6:34:52
  pgu
@@ -2366,16 +2366,54 @@
 haracter
+ preceded by optional '/', %0A        //
  precede
@@ -2435,20 +2435,16 @@
 uote
-,%0A        //
+ that is
  sep
@@ -2466,16 +2466,28 @@
 or more 
+%0A        // 
 whitespa
@@ -2619,17 +2619,16 @@
  if 
-(
 (j %3C 0) 
 %7C%7C l
@@ -2627,30 +2627,162 @@
  0) 
-%7C%7C line.substring(i,j)
+break;%0A          String s = line.substring(i,j);%0A          if (s.endsWith(%22/%22)) %7B%0A            s = s.substring(0,s.length()-1);%0A          %7D%0A          if (s
 .tri

Update parsing meta tag to allow a '/' immediately before the closing '>'.