Stuart McCulloch | d00f971 | 2009-07-13 10:06:47 +0000 | [diff] [blame^] | 1 | package aQute.bnd.build; |
| 2 | |
| 3 | import java.io.*; |
| 4 | import java.util.*; |
| 5 | |
| 6 | public class Container { |
| 7 | public enum TYPE { |
| 8 | REPO, PROJECT, EXTERNAL, LIBRARY, ERROR |
| 9 | } |
| 10 | |
| 11 | final File file; |
| 12 | final TYPE type; |
| 13 | final String bsn; |
| 14 | final String version; |
| 15 | final String error; |
| 16 | final Project project; |
| 17 | final Map<String, String> attributes; |
| 18 | |
| 19 | Container(Project project, String bsn, String version, TYPE type, |
| 20 | File source, String error, Map<String, String> attributes) { |
| 21 | this.bsn = bsn; |
| 22 | this.version = version; |
| 23 | this.type = type; |
| 24 | this.file = source != null ? source : new File("/" + bsn + ":" |
| 25 | + version + ":" + type); |
| 26 | this.project = project; |
| 27 | this.error = error; |
| 28 | if (attributes == null || attributes.isEmpty()) |
| 29 | this.attributes = Collections.emptyMap(); |
| 30 | else |
| 31 | this.attributes = attributes; |
| 32 | } |
| 33 | |
| 34 | public Container(Project project, File file) { |
| 35 | this(project, file.getName(), "project", TYPE.PROJECT, file, null, null); |
| 36 | } |
| 37 | |
| 38 | public Container(File file) { |
| 39 | this(null, file.getName(), "project", TYPE.EXTERNAL, file, null, null); |
| 40 | } |
| 41 | |
| 42 | public File getFile() { |
| 43 | return file; |
| 44 | } |
| 45 | |
| 46 | public String getBundleSymbolicName() { |
| 47 | return bsn; |
| 48 | } |
| 49 | |
| 50 | public String getVersion() { |
| 51 | return version; |
| 52 | } |
| 53 | |
| 54 | public TYPE getType() { |
| 55 | return type; |
| 56 | } |
| 57 | |
| 58 | public String getError() { |
| 59 | return error; |
| 60 | } |
| 61 | |
| 62 | public boolean equals(Object other) { |
| 63 | if (other instanceof Container) |
| 64 | return file.equals(((Container) other).file); |
| 65 | else |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | public int hashCode() { |
| 70 | return file.hashCode(); |
| 71 | } |
| 72 | |
| 73 | public Project getProject() { |
| 74 | return project; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Must show the file name or the error formatted as a file name |
| 79 | * |
| 80 | * @return |
| 81 | */ |
| 82 | public String toString() { |
| 83 | if (getError() != null) |
| 84 | return "/error/" + getError(); |
| 85 | else |
| 86 | return getFile().getAbsolutePath(); |
| 87 | } |
| 88 | |
| 89 | public Map<String, String> getAttributes() { |
| 90 | return attributes; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Return the this if this is anything else but a library. If it is a |
| 95 | * library, return the members. This could work recursively, e.g., libraries |
| 96 | * can point to libraries. |
| 97 | * |
| 98 | * @return |
| 99 | * @throws Exception |
| 100 | */ |
| 101 | public List<Container> getMembers() throws Exception { |
| 102 | List<Container> result = project.newList(); |
| 103 | |
| 104 | // Are ww a library? If no, we are the result |
| 105 | if (getType() != TYPE.LIBRARY) |
| 106 | result.add(this); |
| 107 | else { |
| 108 | // We are a library, parse the file. This is |
| 109 | // basically a specification clause per line. |
| 110 | // I.e. you can do bsn; version, bsn2; version. But also |
| 111 | // spread it out over lines. |
| 112 | InputStream in = new FileInputStream(file); |
| 113 | BufferedReader rd = new BufferedReader(new InputStreamReader(in, |
| 114 | "UTF-8")); |
| 115 | try { |
| 116 | String line; |
| 117 | while ((line = rd.readLine()) != null) { |
| 118 | line = line.trim(); |
| 119 | if (!line.startsWith("#") && line.length() > 0) { |
| 120 | List<Container> list = project.getBundles( |
| 121 | Workspace.STRATEGY_EXACT, line); |
| 122 | result.addAll(list); |
| 123 | } |
| 124 | } |
| 125 | } finally { |
| 126 | in.close(); |
| 127 | } |
| 128 | } |
| 129 | return result; |
| 130 | } |
| 131 | } |