1 package de.matthias_burbach.deputy.core.repository;
2
3 import java.io.File;
4 import java.util.Iterator;
5 import java.util.List;
6
7 import javax.swing.tree.DefaultMutableTreeNode;
8
9
10 /***
11 * Is a group node in the repositories tree.
12 *
13 * @author Matthias Burbach
14 */
15 public class RepositoryGroup extends DefaultMutableTreeNode {
16 /***
17 * The group directory of pom files that are in this group.
18 */
19 private File groupDir;
20
21 /***
22 * The prefix of pom file names that go into this group.
23 * Can be <code>null</code> if all pom files of the group directory
24 * shall be considered.
25 */
26 private String prefix;
27
28 /***
29 * The prefixes of pom file names that must not be included in this group.
30 * Can be <code>null</code> if no exclusions are desired.
31 */
32 private String[] excludedPrefixes;
33
34 /***
35 * The scanner used to find all artifact ids of this group.
36 */
37 private ArtifactScanner artifactScanner = new ArtifactScanner();
38
39 /***
40 * Constructs a repository group node.
41 *
42 * @param groupDir The group directory of pom files that are in this group.
43 * @param prefix The prefix of artifact ids that go into this group.
44 * Can be <code>null</code> if all artifact ids of the group
45 * shall be considered.
46 * @param excludedPrefixes The prefixes of artifact ids that must not be
47 * included in this group. Can be <code>null</code>
48 * if no exclusions are desired.
49 */
50 public RepositoryGroup(
51 final File groupDir,
52 final String prefix,
53 final String[] excludedPrefixes) {
54 this.groupDir = groupDir;
55 this.prefix = prefix;
56 this.excludedPrefixes = excludedPrefixes;
57 initialize();
58 }
59
60 /***
61 * Initializes this node by creating and adding the child nodes.
62 */
63 private void initialize() {
64 if (groupDir.isDirectory()) {
65 List artifactIds =
66 artifactScanner.getAllArtifactIds(groupDir.getAbsolutePath());
67 for (Iterator iter = artifactIds.iterator(); iter.hasNext();) {
68 String artifactId = (String) iter.next();
69 if (includeArtifactId(artifactId)) {
70 RepositoryArtifact repositoryArtifact =
71 new RepositoryArtifact(
72 groupDir.getName(),
73 new File(groupDir + File.separator + "poms"),
74 artifactId);
75 add(repositoryArtifact);
76 }
77 }
78 }
79 }
80
81 /***
82 * @param artifactId The artifact id to check
83 * @return <code>true</code> if the artifact id is to be included in the
84 * group.
85 */
86 private boolean includeArtifactId(final String artifactId) {
87 boolean result = true;
88 if (prefix != null && !artifactId.startsWith(prefix)) {
89 result = false;
90 } else if (excludedPrefixes != null) {
91 for (int i = 0; i < excludedPrefixes.length; i++) {
92 if (artifactId.startsWith(excludedPrefixes[i])) {
93 result = false;
94 break;
95 }
96 }
97 }
98 return result;
99 }
100
101
102
103
104 /***
105 * {@inheritDoc}
106 */
107 public String toString() {
108 String result = groupDir.getName();
109 if (prefix != null) {
110 result = prefix;
111 }
112 return result;
113 }
114
115
116
117
118 /***
119 * {@inheritDoc}
120 */
121 public boolean isLeaf() {
122 return false;
123 }
124 }