// Path: c19books/src/main/java/uk/bl/dpt/parsers/ArchiveParser.java
// public class ArchiveParser {
// 	/**
// 	 * @param is an InputStream to parse the zip from
// 	 * @return the set of names in this zip file
// 	 * @throws IOException if the stream can't be read
// 	 */
// 	static public Set<String> getNames(InputStream is) throws IOException {
// 		Set<String> names = new HashSet<String>();
// 		ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is));
// 		ZipEntry entry;
// 		while ((entry = zis.getNextEntry()) != null) {
// 			if (!entry.isDirectory())
// 				names.add(entry.getName());
// 		}
// 		zis.close();
// 		return names;
// 	}
// 	/**
// 	 * @param is
// 	 * @return
// 	 * @throws IOException
// 	 * @throws ArchiveException
// 	 */
// 	static public Set<String> getApacheNames(InputStream is) throws IOException, ArchiveException {
// 		Set<String> names = new HashSet<String>();
// 		ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream(is);
// 		ArchiveEntry entry;
// 		while ((entry = ais.getNextEntry()) != null) {
// 			if (!entry.isDirectory())
// 				names.add(entry.getName());
// 		}
// 		ais.close();
// 		return names;
// 	}
// 	
// 	/**
// 	 * @param is
// 	 * @param name
// 	 * @return
// 	 * @throws IOException
// 	 */
// 	static public InputStream getElementStream(InputStream is, String name) throws IOException {
// 		ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is));
// 		ZipEntry entry;
// 		while ((entry = zis.getNextEntry()) != null) {
// 			if (entry.getName().equals(name)) {
// 				return zis;
// 			}
// 		}
// 		zis.close();
// 		return null;
// 	}
// }


import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import uk.bl.dpt.parsers.ArchiveParser;
    public void parseMets(String folderName, String metsName) throws DocumentException, IOException {
        SAXReader saxReader = new SAXReader();
        Document metsDoc    = saxReader.read(new InputStreamReader(new FileInputStream(folderName+metsName), "UTF-8"));
        Element rootElt     = metsDoc.getRootElement();
		rootElt.addNamespace("mets", "http://www.loc.gov/METS/");
		rootElt.addNamespace("xlink", "http://www.w3.org/1999/xlink");
		
		Element imgGrpElt    = (Element) rootElt.selectSingleNode("//mets:fileGrp[@ID='IMGGRP']");
		Node imgZipAtt  = imgGrpElt.selectSingleNode("//mets:file[@ID='ZIPJP2']/mets:FLocat/@xlink:href");
		List<Node> fileElements4Img = imgGrpElt.selectNodes("mets:file/mets:file/mets:FLocat/@xlink:href");
		Set<String> imgNameSetFromMets = new HashSet<String>();
		for(Node file: fileElements4Img){
			imgNameSetFromMets.add(formatImgMetsFilename(file.getText()));
		} 
		
		String imgZipFileName = folderName + imgZipAtt.getText() + ".dat";
		Set<String> imgFileNameSetInZip = ArchiveParser.getNames(new FileInputStream(imgZipFileName));
		
		compareImgSets(imgNameSetFromMets, imgFileNameSetInZip);
		
		
		Element altoGrpElt           = (Element) rootElt.selectSingleNode("//mets:fileGrp[@ID='ALTOGRP']");
		Node altoZipAtt              = altoGrpElt.selectSingleNode("//mets:file[@ID='ZIPALTO']/mets:FLocat/@xlink:href");
		List<Node> fileElements4Alto = altoGrpElt.selectNodes("mets:file/mets:file/mets:FLocat/@xlink:href");
		Set<String> altoNameSetFromMets = new HashSet<String>();
		for(Node file: fileElements4Alto){
			altoNameSetFromMets.add(formatAltoMetsFilename(file.getText()));
		} 
				
		String altoZipFileName = folderName + altoZipAtt.getText() + ".dat";
// Path: c19books/src/main/java/uk/bl/dpt/parsers/ArchiveParser.java
// public class ArchiveParser {
// 	/**
// 	 * @param is an InputStream to parse the zip from
// 	 * @return the set of names in this zip file
// 	 * @throws IOException if the stream can't be read
// 	 */
// 	static public Set<String> getNames(InputStream is) throws IOException {
// 		Set<String> names = new HashSet<String>();
// 		ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is));
// 		ZipEntry entry;
// 		while ((entry = zis.getNextEntry()) != null) {
// 			if (!entry.isDirectory())
// 				names.add(entry.getName());
// 		}
// 		zis.close();
// 		return names;
// 	}
// 	/**
// 	 * @param is
// 	 * @return
// 	 * @throws IOException
// 	 * @throws ArchiveException
// 	 */
// 	static public Set<String> getApacheNames(InputStream is) throws IOException, ArchiveException {
// 		Set<String> names = new HashSet<String>();
// 		ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream(is);
// 		ArchiveEntry entry;
// 		while ((entry = ais.getNextEntry()) != null) {
// 			if (!entry.isDirectory())
// 				names.add(entry.getName());
// 		}
// 		ais.close();
// 		return names;
// 	}
// 	
// 	/**
// 	 * @param is
// 	 * @param name
// 	 * @return
// 	 * @throws IOException
// 	 */
// 	static public InputStream getElementStream(InputStream is, String name) throws IOException {
// 		ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is));
// 		ZipEntry entry;
// 		while ((entry = zis.getNextEntry()) != null) {
// 			if (entry.getName().equals(name)) {
// 				return zis;
// 			}
// 		}
// 		zis.close();
// 		return null;
// 	}
// }


// Path: c19books/src/main/java/uk/ac/york/dlib/aqua/MetsParser.java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import uk.bl.dpt.parsers.ArchiveParser;

    public void parseMets(String folderName, String metsName) throws DocumentException, IOException {
        SAXReader saxReader = new SAXReader();
        Document metsDoc    = saxReader.read(new InputStreamReader(new FileInputStream(folderName+metsName), "UTF-8"));
        Element rootElt     = metsDoc.getRootElement();
		rootElt.addNamespace("mets", "http://www.loc.gov/METS/");
		rootElt.addNamespace("xlink", "http://www.w3.org/1999/xlink");
		
		Element imgGrpElt    = (Element) rootElt.selectSingleNode("//mets:fileGrp[@ID='IMGGRP']");
		Node imgZipAtt  = imgGrpElt.selectSingleNode("//mets:file[@ID='ZIPJP2']/mets:FLocat/@xlink:href");
		List<Node> fileElements4Img = imgGrpElt.selectNodes("mets:file/mets:file/mets:FLocat/@xlink:href");
		Set<String> imgNameSetFromMets = new HashSet<String>();
		for(Node file: fileElements4Img){
			imgNameSetFromMets.add(formatImgMetsFilename(file.getText()));
		} 
		
		String imgZipFileName = folderName + imgZipAtt.getText() + ".dat";
		Set<String> imgFileNameSetInZip = ArchiveParser.getNames(new FileInputStream(imgZipFileName));
		
		compareImgSets(imgNameSetFromMets, imgFileNameSetInZip);
		
		
		Element altoGrpElt           = (Element) rootElt.selectSingleNode("//mets:fileGrp[@ID='ALTOGRP']");
		Node altoZipAtt              = altoGrpElt.selectSingleNode("//mets:file[@ID='ZIPALTO']/mets:FLocat/@xlink:href");
		List<Node> fileElements4Alto = altoGrpElt.selectNodes("mets:file/mets:file/mets:FLocat/@xlink:href");
		Set<String> altoNameSetFromMets = new HashSet<String>();
		for(Node file: fileElements4Alto){
			altoNameSetFromMets.add(formatAltoMetsFilename(file.getText()));
		} 
				
		String altoZipFileName = folderName + altoZipAtt.getText() + ".dat";
		Set<String> altoFileNameSetInZip = ArchiveParser.getNames(new FileInputStream(altoZipFileName));