import java.io.*; import java.util.*; import pds.label.*; public class createIndex{ public static LinkedList detachedLabelFiles = new LinkedList(); public static LinkedList infoFiles = new LinkedList(); public static LinkedList catalogFiles = new LinkedList(); public static ArrayList columns = new ArrayList(); public static int[] columnMaxLength = null; public static LinkedList tableRecords = new LinkedList(); public static PrintStream ps = null; public static String typeOfIndex = ""; public static void run(File path, ArrayList options, String type){ try { typeOfIndex = type; if(typeOfIndex.equals("MANIFEST")){ listFiles(path,Index.invalidManifestDirectories); columns = Index.manifestColumns; columnMaxLength = new int[columns.size()]; int populateIndex = 0; while(populateIndex < columns.size()){ columnMaxLength[populateIndex] = columns.get(populateIndex).length(); populateIndex++; } }else if (typeOfIndex.equals("INDEX")){ listFiles(path,Index.invalidIndexDirectories); columns = Index.indexColumns; columnMaxLength = new int[columns.size()]; int populateIndex = 0; while(populateIndex < columns.size()){ columnMaxLength[populateIndex] = columns.get(populateIndex).length(); populateIndex++; } } parseLabel(); } catch(Exception e) { e.printStackTrace(System.out); } } public static void listFiles(File dir, String[] invalidDirectories) throws Exception { if(!dir.exists()){ System.out.println("The path " + dir + " does not exist."); System.exit(1); } File[] filesInDir = dir.listFiles(); ArrayList directories = new ArrayList(); start: for(int index = 0; index < filesInDir.length; index++){ String filename = filesInDir[index].getName(); if(filesInDir[index].isFile()){ if(filename.endsWith("LBL")){ if(!filename.equals("AAREADME.LBL")){ detachedLabelFiles.add(filesInDir[index]); } }else if(filename.endsWith("INFO.TXT")){ infoFiles.add(filesInDir[index]); }else if(filename.endsWith(".CAT") || dir.getCanonicalPath().endsWith("CATALOG") || filename.startsWith("VOLDESC")){ if(!filename.endsWith("REF.CAT") && !filename.endsWith("REF.TXT")){ catalogFiles.add(filesInDir[index]); } } }else if(filesInDir[index].isDirectory()){ for(int invdDirs = 0; invdDirs < invalidDirectories.length; invdDirs++){ if(filename.equals(invalidDirectories[invdDirs])){ continue start; } } directories.add(filesInDir[index]); } } if(directories.size() == 0){ return; } for(int dirIndex = 0; dirIndex < directories.size(); dirIndex++){ listFiles((File)directories.get(dirIndex), invalidDirectories); } filesInDir = null; directories = null; } private static void parseLabel() throws Exception { File indexFile = null; File indexDir = new File(Index.path + "/INDEX"); if(typeOfIndex.equals("MANIFEST")){ indexFile = new File(Index.path + "/INDEX/MANIFEST.TAB"); }else{ indexFile = new File(Index.path + "/INDEX/INDEX_TAB.temp"); } if(!indexDir.exists()){ indexDir.mkdir(); } if(indexFile.exists()){ indexFile.delete(); } try{ ps = new PrintStream(new FileOutputStream(indexFile , true)); String header = ""; for(int i = 0; i < columns.size(); i++){ if( i < columns.size() - 1){ header = header + columns.get(i) + ","; }else{ header = header + columns.get(i); } } ps.println(header); } catch(Exception e) { System.out.println("Unable to open output file..."); System.out.println(e.getMessage()); return; } PDSLabel label = new PDSLabel(); LinkedList> records = new LinkedList>(); for(int fileIndex = 0; fileIndex < detachedLabelFiles.size();fileIndex++){ label.parse(detachedLabelFiles.get(fileIndex).toString()); for(int columnIndex = 0; columnIndex < columns.size(); columnIndex++){ PDSItem item = new PDSItem(); PDSElement element = null; LinkedList thisRecord = new LinkedList(); records.add(thisRecord); if(columns.get(columnIndex).equals("FILE_SPECIFICATION_NAME")){ String rootPath = Index.path.getCanonicalPath(); String filePath = detachedLabelFiles.get(fileIndex).getCanonicalPath(); if(filePath.indexOf("\\") != -1){ filePath = filePath.replaceAll("\\","/"); } String returnValue = "\"" + filePath.substring(rootPath.length() + 1) + "\""; thisRecord.add(returnValue); if(returnValue.length() > columnMaxLength[columnIndex]){ columnMaxLength[columnIndex] = returnValue.length(); } }else { item = label.findItem(columns.get(columnIndex)); element = label.getElement(item); if(columns.get(columnIndex).equals("VOLUME_ID")){ thisRecord.add(Index.volId); columnMaxLength[columnIndex] = Index.volId.length(); }else if(element == null){ thisRecord.add(Index.indexInitValues.get(columnIndex)); }else if(element.valueSize() > 0){ if(element.value(0).indexOf("{") != -1 || element.value(0).indexOf("(") != -1) { System.out.println("ERROR in Label \n" + detachedLabelFiles.get(fileIndex)); System.out.println("Column " + columns.get(columnIndex) + " = " + element.value(0) + "\n"); String parsedValues[] = element.value(0).replaceAll("\\{", "").replace('}', ' ').replace('(', ' ').replace(')', ' ').split(","); String str = ""; for( int multValInd = 0; multValInd < parsedValues.length; multValInd++) { if(columns.get(columnIndex).endsWith("TIME")) { str = parsedValues[multValInd].trim(); } else { str = "\"" + parsedValues[multValInd].trim() + "\""; } thisRecord.add(str); if(str.length() > columnMaxLength[columnIndex]){ columnMaxLength[columnIndex] = str.length(); } } } else { for(int multipleValueIndex = 0; multipleValueIndex < element.valueSize(); multipleValueIndex++){ String str = ""; if(columns.get(columnIndex).endsWith("TIME")) { str = element.value(multipleValueIndex).trim(); } else { str = "\"" + element.value(multipleValueIndex).trim() + "\""; } thisRecord.add(str); if(str.length() > columnMaxLength[columnIndex]){ columnMaxLength[columnIndex] = str.length(); } } } } } } String value1 = ""; for(int i = 0; i < records.size(); i++){ value1 = value1 + records.get(i).get(0) + ","; } ArrayList temp = new ArrayList(); temp.add(value1); ArrayList finalStrings = match(temp, records); print(finalStrings); } System.gc(); ps.close(); System.gc(); int worked = padColumns(); } private static void print(ArrayList strings){ for(int stringIndex = 0; stringIndex < strings.size(); stringIndex++) { ps.println(strings.get(stringIndex)); } } private static int padColumns(){ File input = new File(Index.path + "/INDEX/INDEX_TAB.temp"); File output = new File(Index.path + "/INDEX/INDEX.TAB"); BufferedReader fin = null; try{ fin = new BufferedReader(new FileReader(input)); ps = new PrintStream(new FileOutputStream(output)); String line = ""; while((line = fin.readLine()) != null){ boolean quotedValue = false; String[] parsedLine = line.split(","); String paddedLine = ""; for(int ind = 0; ind < parsedLine.length; ind++){ if(parsedLine[ind].endsWith("\"")){ parsedLine[ind] = parsedLine[ind].substring(0,parsedLine[ind].length() - 1); while(parsedLine[ind].length() < columnMaxLength[ind] - 1){ parsedLine[ind] = parsedLine[ind] + " "; } parsedLine[ind] = parsedLine[ind] + "\""; } else { while(parsedLine[ind].length() < columnMaxLength[ind]){ parsedLine[ind] = parsedLine[ind] + " "; } } if(ind < parsedLine.length - 1){ paddedLine = paddedLine + parsedLine[ind] + ","; }else{ paddedLine = paddedLine + parsedLine[ind] + "\r\n"; } } ps.print(paddedLine); } fin.close(); ps.close(); }catch(Exception e){ output.delete(); input.delete(); System.out.println("There was a problem padding all the columns in the file.\nNo INDEX.TAB was created."); return(1); } input.delete(); return(0); } private static ArrayList match(ArrayList strings, LinkedList> records){ LinkedList thisRecord; for(int columnIndex = 0; columnIndex < records.size(); columnIndex++){ if(records.get(columnIndex).size() == 1){ }else { int numberOfStrings = strings.size(); for(int i = 0; i < numberOfStrings; i++){ String[] parsed = strings.get(i).split(","); String returnValue = ""; thisRecord = records.get(columnIndex); for(int ind = 1; ind < thisRecord.size(); ind++){ parsed[columnIndex] = thisRecord.get(ind); for(int parsedIndex = 0; parsedIndex < parsed.length - 1; parsedIndex++){ returnValue = returnValue + parsed[parsedIndex] + ","; } returnValue = returnValue + parsed[parsed.length - 1]; strings.add(returnValue); returnValue = ""; } } } } return strings; } }