view src/de/mpiwg/itgroup/eSciDoc/foxridge/IndexMetaIterator.java @ 1:6b0267cb40ed

minor
author dwinter
date Fri, 26 Nov 2010 09:09:25 +0100
parents c6929e63b0b8
children a844f6948dd8
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.Enumeration;
import java.util.Iterator;
import java.util.Stack;
import java.util.Vector;



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<File> stack;
	

	public IndexMetaIterator(File rootFolder){
		this.rootFolder=rootFolder;
		this.currentFolder=rootFolder;
		this.stack = new Stack<File>();
		for (File f:rootFolder.listFiles()){
			stack.push(f);
		}
	}
	@Override
	public boolean hasNext() {
		// TODO Auto-generated method stub
		return !stack.isEmpty();
	}

	@Override
	public ECHOObject next() {
		// TODO Auto-generated method stub
		File nextFile = stack.pop();
		while(!nextFile.getName().endsWith(".meta") && !stack.isEmpty()){
			System.out.println("CHECK_________"+nextFile.getName());
			if(!nextFile.getName().equals("pageimg")){ //skip pageimg
				if(nextFile.isDirectory()){
					for (File f:nextFile.listFiles()){
						stack.push(f);
					}
				}
			}
			nextFile = stack.pop();
		}
		if (!nextFile.getName().endsWith(".meta")) //der letzte Eintrag muss noch gretrennt getestet werden.
			nextFile = null;
		System.out.println("FOUND:"+nextFile);
		try {
			if (nextFile!=null)
				return createECHOObject(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();
		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
		
	}

 

}