package org.lockss.plugin.springer;

import org.lockss.daemon.PluginException;
import org.lockss.extractor.ArticleMetadataExtractor;
import org.lockss.extractor.ArticleMetadataExtractorFactory;
import org.lockss.extractor.BaseArticleMetadataExtractor;
import org.lockss.extractor.MetadataTarget;
import org.lockss.plugin.ArchivalUnit;
import org.lockss.plugin.ArticleFiles;
import org.lockss.plugin.ArticleIteratorFactory;
import org.lockss.plugin.SubTreeArticleIteratorBuilder;
import org.lockss.plugin.clockss.SourceXmlArticleIteratorFactory;
import org.lockss.plugin.clockss.SourceZipXmlArticleIteratorFactory;
import org.lockss.util.Logger;

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

public class SpringerJatsSourceZipXmlArticleIteratorFactory extends SourceXmlArticleIteratorFactory {

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

    protected static final String ALL_XML_PATTERN_TEMPLATE = "\"^%s[^/]+/(.*)\\.xml.Meta$\",base_url";

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

    @Override
    public Iterator<ArticleFiles> createArticleIterator(ArchivalUnit au,
                                                        MetadataTarget target)
            throws PluginException {
        SubTreeArticleIteratorBuilder builder = new SubTreeArticleIteratorBuilder(au);

        // no need to limit to ROOT_TEMPLATE
        builder.setSpec(builder.newSpec()
                .setTarget(target)
                .setPatternTemplate(getIncludePatternTemplate(), Pattern.CASE_INSENSITIVE)
                .setExcludeSubTreePattern(getExcludeSubTreePattern())
                .setVisitArchiveMembers(getIsArchive()));

        builder.addAspect(XML_PATTERN,
                XML_REPLACEMENT,
                ArticleFiles.ROLE_ARTICLE_METADATA);
        
        builder.addAspect(PDF_REPLACEMENT,
                ArticleFiles.ROLE_FULL_TEXT_PDF);

        builder.setFullTextFromRoles(ArticleFiles.ROLE_FULL_TEXT_PDF,
                ArticleFiles.ROLE_ARTICLE_METADATA); // though if it comes to this it won't emit

        return builder.getSubTreeArticleIterator();
    }

    protected boolean getIsArchive() {
        return false;
    }

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

}

@@ -988,24 +988,26 @@
 +/(.*)%5C%5C.xml
+%5C%5C
 .Meta$%5C%22,bas
@@ -1011,24 +1011,198 @@
 base_url%22;%0A%0A
+    protected static final Pattern NESTED_ARCHIVE_PATTERN =%0A            Pattern.compile(%22.*/.+%5C%5C.(zip%7Ctar%7Cgz%7Ctgz%7Ctar%5C%5C.gz)$%22,%0A                    Pattern.CASE_INSENSITIVE);%0A%0A
     public s
@@ -1263,16 +1263,18 @@
 *)%5C%5C.xml
+%5C%5C
 .Meta$%22,
@@ -2507,24 +2507,383 @@
 r();%0A    %7D%0A%0A
+    protected Pattern getExcludeSubTreePattern() %7B%0A        log.debug3(%22Fei: NESTED_ARCHIVE_PATTERN = %22 + NESTED_ARCHIVE_PATTERN);%0A        return NESTED_ARCHIVE_PATTERN;%0A    %7D%0A%0A    protected String getIncludePatternTemplate() %7B%0A        log.debug3(%22Fei: ALL_XML_PATTERN_TEMPLATE = %22 + ALL_XML_PATTERN_TEMPLATE);%0A        return ALL_XML_PATTERN_TEMPLATE;%0A    %7D%0A%0A%0A
     protecte

make change to all other related pattern to match springer setup