[7] | 1 | package org.etsi.common; |
---|
| 2 | |
---|
| 3 | import java.io.File; |
---|
| 4 | import java.io.FileFilter; |
---|
| 5 | import java.io.IOException; |
---|
| 6 | import java.util.LinkedList; |
---|
| 7 | import java.util.List; |
---|
| 8 | |
---|
| 9 | public class InputInterface { |
---|
| 10 | //TODO: can be transformed to static |
---|
| 11 | |
---|
| 12 | private String projectExt = "t3p"; |
---|
| 13 | private String extRegExp = "ttcn|ttcn3"; |
---|
| 14 | private boolean recursive = true; |
---|
| 15 | |
---|
| 16 | public InputInterface(String resourceExtRegExp, boolean recursive){ |
---|
| 17 | this.extRegExp = resourceExtRegExp; |
---|
| 18 | this.recursive = recursive; |
---|
| 19 | } |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | public InputInterface(String resourceExtRegExp, String projectExt, boolean recursive){ |
---|
| 23 | this.extRegExp = resourceExtRegExp; |
---|
| 24 | this.projectExt = projectExt; |
---|
| 25 | this.recursive = recursive; |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | public void setExtRegExp(String extRegExp) { |
---|
| 29 | this.extRegExp = extRegExp; |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | public String getExtRegExp() { |
---|
| 33 | return extRegExp; |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | public void setRecursive(boolean recursive) { |
---|
| 37 | this.recursive = recursive; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | public boolean isRecursive() { |
---|
| 41 | return recursive; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | public List<String> inputFromFile(String filename){ |
---|
| 45 | List<String> files = new LinkedList<String>(); |
---|
| 46 | File file = new File(filename); |
---|
| 47 | if (file.isFile() && ExtensionFilter.checkFileExtension(file, extRegExp)){ |
---|
| 48 | files.add(file.getPath()); |
---|
| 49 | } |
---|
| 50 | return files; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | public List<String> inputFromDirectory(String directory){ |
---|
| 54 | List<String> files = new LinkedList<String>(); |
---|
| 55 | File inputDir = new File(directory); |
---|
| 56 | File[] fileList = inputDir.listFiles(new ExtensionFilter(this.getExtRegExp())); |
---|
| 57 | |
---|
| 58 | for (int i = 0; i < fileList.length; i++) { |
---|
| 59 | files.add(fileList[i].getPath()); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | File[] dirList = inputDir.listFiles(new FileFilter() { |
---|
| 63 | public boolean accept(File pathname) { |
---|
| 64 | if (pathname.isDirectory()) |
---|
| 65 | return true; |
---|
| 66 | return false; |
---|
| 67 | } |
---|
| 68 | }); |
---|
| 69 | |
---|
| 70 | if (this.isRecursive()){ |
---|
| 71 | for (int i = 0; i < dirList.length; i++) { |
---|
| 72 | files.addAll(inputFromDirectory(dirList[i].getPath())); |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | return files; |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | public List<String> inputFromWildcard(String wildcard){ |
---|
| 80 | List<String> files = new LinkedList<String>(); |
---|
| 81 | File wildcardFile = new File(wildcard); |
---|
| 82 | System.out.println("Expanding wildcard \""+wildcard+"\"..."); |
---|
| 83 | File wildcardParentFile = wildcardFile.getParentFile(); |
---|
| 84 | if (wildcardParentFile == null) { |
---|
| 85 | wildcardParentFile = new File("."); |
---|
| 86 | } |
---|
| 87 | //TODO: needs to check if it is null and handle the situation |
---|
| 88 | if (wildcardParentFile.isDirectory()){ |
---|
| 89 | File[] fileList = wildcardParentFile.listFiles(new WildcardFilter(wildcardFile)); |
---|
| 90 | for (int i = 0; i < fileList.length; i++) { |
---|
| 91 | System.out.println(" ->"+fileList[i].getPath()); |
---|
| 92 | files.addAll(this.getInputFromParameter(fileList[i].getPath())); |
---|
| 93 | } |
---|
| 94 | } else { |
---|
| 95 | //invalid parent directory, return |
---|
| 96 | } |
---|
| 97 | if (files.isEmpty()) { |
---|
| 98 | System.out.println(" ->No files found!"); |
---|
| 99 | } |
---|
| 100 | return files; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | public List<String> inputFromProject(String projectFilename){ |
---|
| 104 | List<String> files = new LinkedList<String>(); |
---|
| 105 | System.out.println("Reading input files from project file \""+projectFilename+"\"..."); |
---|
| 106 | //TODO: Fill with content |
---|
| 107 | String projectFile = MiscTools.readFile(projectFilename); |
---|
| 108 | String[] lines = projectFile.split("\n"); |
---|
| 109 | for (String line : lines) { |
---|
| 110 | if (!line.matches("\\s*")) {//.equals("")) { |
---|
| 111 | System.out.println(" "+line); |
---|
| 112 | files.addAll(this.getInputFromParameter(line)); |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | return files; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | public void addToProject(List<String> files, String projectFilename){ |
---|
| 119 | //TODO: Fill with content |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | //TODO: this will make other duplicate checks obsolete |
---|
| 123 | public static List<String> filterAbsoluteDuplicates(List<String> inputFiles) { |
---|
| 124 | List<String> filteredInputFiles = new LinkedList<String>(); |
---|
| 125 | List<String> absolutePaths = new LinkedList<String>(); |
---|
| 126 | for (String inputFile : inputFiles) { |
---|
| 127 | try { |
---|
| 128 | String canonicalPath = new File(inputFile).getCanonicalPath(); |
---|
| 129 | if (!absolutePaths.contains(canonicalPath)) { |
---|
| 130 | absolutePaths.add(canonicalPath); |
---|
| 131 | filteredInputFiles.add(inputFile); |
---|
| 132 | } |
---|
| 133 | } catch (IOException e) { |
---|
| 134 | // TODO Auto-generated catch block |
---|
| 135 | e.printStackTrace(); |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | return filteredInputFiles; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | //TODO: review, may be superfluous |
---|
| 142 | public List<String> getInputFromParameterList(List<String> parameters) { |
---|
| 143 | List<String> inputFiles = new LinkedList<String>(); |
---|
| 144 | for (int i = 0; i < parameters.size(); i++) { |
---|
| 145 | List<String> fileList = getInputFromParameter(parameters.get(i)); |
---|
| 146 | for (int f = 0; f < fileList.size(); f++) { |
---|
| 147 | String filename = fileList.get(f); |
---|
| 148 | if (!inputFiles.contains(filename)) { |
---|
| 149 | inputFiles.add(filename); |
---|
| 150 | } else { |
---|
| 151 | System.out.println("Entry \"" + filename |
---|
| 152 | + "\" already present in the list, skipping.."); |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | return inputFiles; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | |
---|
| 160 | public List<String> getInputFromParameter(String inputParameter){ |
---|
| 161 | List<String> inputFiles = new LinkedList<String>(); |
---|
| 162 | File inputParameterFile = new File(inputParameter.trim()); |
---|
| 163 | if (inputParameterFile.isDirectory()){ |
---|
| 164 | if (this.isRecursive()){ |
---|
| 165 | System.out.println("Collecting ttcn3 files recursively in \"" |
---|
| 166 | + inputParameter + "\" ..."); |
---|
| 167 | } else { |
---|
| 168 | System.out.println("Collecting ttcn3 files non-recursively in \"" |
---|
| 169 | + inputParameter + "\" ..."); |
---|
| 170 | } |
---|
| 171 | inputFiles = inputFromDirectory(inputParameter); |
---|
| 172 | } else { |
---|
| 173 | if (inputParameterFile.isFile()){ |
---|
| 174 | if (inputParameterFile.getName().endsWith("."+projectExt)){ |
---|
| 175 | inputFiles = inputFromProject(inputParameter); |
---|
| 176 | } else { |
---|
| 177 | inputFiles = inputFromFile(inputParameter); |
---|
| 178 | } |
---|
| 179 | } else { |
---|
| 180 | inputFiles = inputFromWildcard(inputParameter); |
---|
| 181 | } |
---|
| 182 | } |
---|
| 183 | return inputFiles; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | } |
---|