view src/de/mpiwg/itgroup/indexMeta2RDF/IndexMetaIterator.java @ 4:e93de4e99b52 default tip

indexMeta2rdf in dieses Projekt verschoben
author dwinter
date Thu, 21 Jun 2012 14:37:55 +0200
parents
children
line wrap: on
line source

package de.mpiwg.itgroup.indexMeta2RDF;


/*
 * 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;


/**
 * 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<String> {

	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 String 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")){ //skip pageimg
				
			
				File nf = new File(nextFile);
				
			
				if(nf.isDirectory()){
					String[] ls = nf.list();
					if (ls==null){
						return null;
					}
					for (String f:ls){
						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);
	
		if (nextFile!=null)
			return nextFile;
		
		return null;
	}

	
	@Override
	public void remove() {
		// TODO Auto-generated method stub
		
	}

 

}