/src/org/ooc/frontend/PathList.java

http://github.com/nddrylliog/ooc · Java · 164 lines · 84 code · 42 blank · 38 comment · 18 complexity · a1f02b9d7024a4f6af4a60823ff7f4b6 MD5 · raw file

  1. package org.ooc.frontend;
  2. import java.io.File;
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. /**
  8. * Somehow like the 'classpath' in Java. E.g. holds where to find ooc
  9. * modules, and well, find them when asked kindly to.
  10. *
  11. * @author Amos Wenger
  12. */
  13. public class PathList {
  14. protected HashMap<String, File> paths;
  15. /**
  16. * Default constructor
  17. */
  18. public PathList() {
  19. paths = new HashMap<String, File> ();
  20. }
  21. /**
  22. * Add an element to the classpath
  23. * @param path
  24. */
  25. public void add(String path) {
  26. File file = new File(path);
  27. if(!file.exists()) {
  28. System.err.println("Classpath element cannot be found: "+path);
  29. return;
  30. } else if(!file.isDirectory()) {
  31. System.err.println("Classpath element is not a directory: "+path);
  32. return;
  33. }
  34. String absolutePath = file.getAbsolutePath();
  35. if(!paths.containsKey(absolutePath)) {
  36. paths.put(absolutePath, file);
  37. }
  38. }
  39. /**
  40. * Remove an element from the sourcepath
  41. * @param path
  42. */
  43. public void remove(String path) {
  44. File file = new File(path);
  45. if(!file.exists()) {
  46. System.err.println("Classpath element cannot be found: "+file.getPath());
  47. return;
  48. } else if(!file.isDirectory()) {
  49. System.err.println("Classpath element is not a directory: "+file.getPath());
  50. return;
  51. } else if(!paths.containsKey(file.getAbsolutePath())) {
  52. System.err.println("Trying to remove a previously unexisting element: "+file.getPath()+", ignoring.");
  53. return;
  54. }
  55. paths.remove(file.getAbsolutePath());
  56. }
  57. /**
  58. * Remove all elements from the sourcepath
  59. */
  60. public void clear() {
  61. paths.clear();
  62. }
  63. /**
  64. * @return all files in the source path
  65. */
  66. public Collection<File> getPaths() {
  67. return paths.values();
  68. }
  69. /**
  70. * Return a list of all files found in a directory in the whole sourcepath
  71. * @param path
  72. * @return
  73. */
  74. public Collection<String> getRelativePaths(String path) {
  75. List<String> files = new ArrayList<String>();
  76. for(File element: paths.values()) {
  77. File candidate = new File(element, path);
  78. if(candidate.exists()) addChildren(path, files, candidate);
  79. }
  80. return files;
  81. }
  82. private void addChildren(String basePath, List<String> list, File parent) {
  83. File[] children = parent.listFiles();
  84. for(File child: children) {
  85. if(child.isFile()) {
  86. list.add(basePath + '/' + child.getName());
  87. } else if(child.isDirectory()) {
  88. addChildren(basePath + '/' + child.getName(), list, child);
  89. }
  90. }
  91. }
  92. /**
  93. * Find the file in the source path and return a File object associated to it
  94. */
  95. public File getFile(String path) {
  96. File element = getElement(path);
  97. return element == null ? null : new File(element, path);
  98. }
  99. /**
  100. * Find the file in the source path and return the element of the path list
  101. * it has been found in.
  102. */
  103. public File getElement(String path) {
  104. for(File element: paths.values()) {
  105. File candidate = new File(element, path);
  106. if(candidate.exists()) {
  107. return element;
  108. }
  109. }
  110. return null;
  111. }
  112. /**
  113. * @return true if the source path is empty
  114. */
  115. public boolean isEmpty() {
  116. return paths.isEmpty();
  117. }
  118. @Override
  119. public String toString() {
  120. return paths.toString();
  121. }
  122. }