view src/de/mpiwg/itgroup/eSciDoc/foxridge/IndexMetaIterator.java @ 10:ad5c0748bd08

minor
author dwinter
date Mon, 20 Aug 2012 12:53:45 +0200
parents b6cf6462d709
children
line wrap: on
line source

package de.mpiwg.itgroup.eSciDoc.foxridge;


/*
 * Copyright  2000-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;
import java.util.Vector;



import org.apache.log4j.Logger;
import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import de.mpiwg.itgroup.eSciDoc.echoObjects.ECHOObject;
import de.mpiwg.itgroup.eSciDoc.echoObjects.ECHORessource;

/**
 * An iterator which iterates through the contents of a java directory. The
 * iterator should be created with the directory at the root of the Java
 * namespace.
 *
 */
public class IndexMetaIterator implements Iterator<ECHOObject> {

	private File rootFolder;
	private File currentFolder;
	private Stack<String> stack;
	private ArrayList<String>filter; //Array of paths which shouldn'T be indexed

	public IndexMetaIterator(File rootFolder) throws IOException{

		filter = new ArrayList<String>();
		filter.add("/mpiwg/online/permanent/SudanRockArt"); // TODO: make this configurable

		this.rootFolder=rootFolder;
		this.currentFolder=rootFolder;
		this.stack = new Stack<String>();

		for (String f:rootFolder.list()){
			String fn = rootFolder.getCanonicalPath()+"/"+f;
			if (!filter.contains(fn)){
				if (!f.equals("")){ // FIXME some filesystems (sshfs?) gives empty filenames if the path contains special characters.
					stack.push(fn);}
				else {
					Logger.getLogger("notAddedFilesLogger").info("Folder -" +fn+" contains files with charakters I cannot read!" );
				}
			}
		}
	}
	@Override
	public boolean hasNext() {
		// TODO Auto-generated method stub
		return !stack.isEmpty();
	}

	@Override
	public ECHOObject next() {
		// TODO Auto-generated method stub
		String nextFile = stack.pop();
		while(!nextFile.endsWith(".meta") && !stack.isEmpty()){
			System.out.println("CHECK_________"+nextFile);


			if(!nextFile.endsWith("pageimg") & !nextFile.endsWith("pages") & !nextFile.endsWith("pagesHi") & !nextFile.endsWith("pagesLo") 
					& !nextFile.endsWith("pageimg")){ //skip pageimg

				File nf = new File(nextFile);

				if(nf.isDirectory()){
					String[] filesInDirectory=nf.list();
					if (filesInDirectory != null){
						for (String f:filesInDirectory){
							String fn;
							try {
								if (!f.startsWith(".")){
									fn = nf.getCanonicalPath()+"/"+f;
									if (!filter.contains(fn)){
										if (!f.equals("")) {// FIXME some filesystems (sshfs?) gives empty filenames if the path contains special characters.
											stack.push(fn);}
										else {
											Logger.getLogger("notAddedFilesLogger").info("Folder -" +fn+" contains files with characters I cannot read!" );
										}

									}
								}
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}

						}
					}
				}
			}

			nextFile = stack.pop();

		}
		if (!nextFile.endsWith(".meta")) //der letzte Eintrag muss noch gretrennt getestet werden.
			nextFile = null;
		System.out.println("FOUND:"+nextFile);
		try {
			if (nextFile!=null)
				return createECHOObject(new File(nextFile));
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	private ECHOObject createECHOObject(File nextFile) throws Exception {

		//Document doc = new SAXBuilder().build(nextFile);
		try{
			FoxridgeRessource er = new FoxridgeRessource(nextFile.getParentFile().getName(),nextFile.getParentFile().getAbsolutePath(),null);

			er.metaData = er.correctML(nextFile.getAbsolutePath());
			//er.pid=er.getPid(); //TODO: not needed any more?
			er.echoUrl=er.metaData; //TODO find a better solution, what to present here, z.b. texttool-tag auswerten.
			return er;
		} catch  (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new Exception();
		}
	}
	@Override
	public void remove() {
		// TODO Auto-generated method stub

	}



}