/*
 * $Id: OnixBooksZipSourceArticleIteratorFactory.java,v 1.2 2014-01-14 19:05:44 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.clockss.onixbooks;

import java.util.Iterator;
import java.util.regex.Pattern;

import org.lockss.daemon.PluginException;
import org.lockss.extractor.*;
import org.lockss.plugin.*;
import org.lockss.util.Logger;

public class OnixBooksZipSourceArticleIteratorFactory implements ArticleIteratorFactory, ArticleMetadataExtractorFactory {

  protected static Logger log = Logger.getLogger(OnixBooksZipSourceArticleIteratorFactory.class);

  // ROOT_TEMPLATE doesn't need to be defined as sub-tree is entire tree under base/year
  //Zip file is a flat directory that has both content and xml file in it
  private static final String PATTERN_TEMPLATE = 
      "\"%s%d/[^/]+\\.zip!/(.*)\\.xml$\",base_url,year";

  public static final Pattern XML_PATTERN = Pattern.compile("/(.*)\\.xml$", Pattern.CASE_INSENSITIVE);
  public static final String XML_REPLACEMENT = "/$1.xml";

  //
  // The source content structure looks like this:
  // <root_location>/<year>/<BigZipBall>.zip
  //     where theunderlying structure is one big directory containing
  //         Stanford to CLOCKSS/<name>.pdf - the book in pdf format
  //         Stanford to CLOCKSS/<name>.jpg - the cover for the book in <name>.pdf
  //  as well as one other <blah>.xml file which are ONOIX for books
  //  metadata for the accompanying files 
  //      //    correlation between the name of the XML file and the name of the pdf files it contains
  //    BUT the name of the format=15 productIdentifier is the isbn13 which is the <name> for both pdf
  //
  @Override
  public Iterator<ArticleFiles> createArticleIterator(ArchivalUnit au, MetadataTarget target) throws PluginException {
    SubTreeArticleIteratorBuilder builder = new SubTreeArticleIteratorBuilder(au);
    
    // no need to limit to ROOT_TEMPLATE
    SubTreeArticleIterator.Spec theSpec = builder.newSpec();
    theSpec.setTarget(target);
    theSpec.setPatternTemplate(PATTERN_TEMPLATE, Pattern.CASE_INSENSITIVE);
    /* this is necessary to be able to see what's inside the zip file */
    theSpec.setVisitArchiveMembers(true);
    builder.setSpec(theSpec);
    
    // NOTE - full_text_cu is set automatically to the url used for the articlefiles
    // ultimately the metadata extractor needs to set the entire facet map 

    // set up XML to be an aspect that will trigger an ArticleFiles to feed the metadata extractor
    builder.addAspect(XML_PATTERN,
                      XML_REPLACEMENT,
                      ArticleFiles.ROLE_ARTICLE_METADATA);

    return builder.getSubTreeArticleIterator();
  }
  
  @Override
  public ArticleMetadataExtractor createArticleMetadataExtractor(MetadataTarget target)
    throws PluginException {
    return new BaseArticleMetadataExtractor(ArticleFiles.ROLE_ARTICLE_METADATA);
  }
}

@@ -58,37 +58,45 @@
 v 1.
-2
+3
  2014-0
-1-1
+2-2
 4 19:0
-5:44 thib_gc
+1:04 alexandraohlson
  Exp
@@ -2135,16 +2135,183 @@
 LATE = %0A
+      // match any xml file within the zip UNLESS it exists below a %22__MACOSX/%22 directory%0A      // these directories are artifacts of the zip getting created on a MAC%0A
       %22%5C
@@ -2330,19 +2330,32 @@
 %5C.zip!/(
+?!.*__MACOSX/)
 .*
-)
 %5C%5C.xml$%5C
@@ -2356,16 +2356,17 @@
 .xml$%5C%22,
+ 
 base_url
@@ -2366,16 +2366,17 @@
 ase_url,
+ 
 year%22;%0A%0A

Exclude spurious files generated by mac as part of zip file creation - the .xml files aren't really xml files.