1 package org.andromda.core.configuration;
2
3 import java.io.Serializable;
4 import java.net.URL;
5 import java.util.ArrayList;
6 import java.util.Collection;
7 import java.util.List;
8 import org.andromda.core.common.ResourceUtils;
9
10 /**
11 * Represents a location within a module search or mappings search.
12 * @author Chad Brandon
13 */
14 public class Location
15 implements Serializable
16 {
17 private static final long serialVersionUID = 34L;
18
19 /**
20 * The path of the location.
21 */
22 private String path;
23
24 /**
25 * The patterns (a comma separated list) to
26 * include in the path search
27 */
28 private String patterns;
29
30 /**
31 * Gets the path to this location.
32 *
33 * @return Returns the path to this location.
34 */
35 public String getPath()
36 {
37 return path;
38 }
39
40 /**
41 * Sets the path to this location.
42 *
43 * @param path The path to this location.
44 */
45 public void setPath(String path)
46 {
47 this.path = path;
48 }
49
50 /**
51 * Gets the patterns to include in this location.
52 *
53 * @return Returns the patterns.
54 */
55 public String getPatterns()
56 {
57 return patterns;
58 }
59
60 /**
61 * Sets the patterns to include in this location.
62 *
63 * @param patterns The patterns to set.
64 */
65 public void setPatterns(String patterns)
66 {
67 this.patterns = patterns;
68 }
69
70 /**
71 * Gets all files that are valid for this location. It takes into
72 * consideration the given patterns. If the location is an actual
73 * file, the an array containing that single file is returned.
74 *
75 * @return the valid files.
76 */
77 public URL[] getResources()
78 {
79 URL[] resources;
80 final URL url = ResourceUtils.toURL(this.path);
81 if (url != null)
82 {
83 if (ResourceUtils.isFile(url))
84 {
85 resources = new URL[] {url};
86 }
87 else
88 {
89 String[] patterns = this.patterns != null ? this.patterns.split(PATTERN_DELIMITER) : new String[0];
90 final List<String> paths = ResourceUtils.getDirectoryContents(
91 url,
92 true,
93 patterns);
94 final Collection<URL> urls = new ArrayList<URL>();
95 for (String path : paths)
96 {
97 final URL resource = ResourceUtils.toURL(path);
98 if (resource != null)
99 {
100 urls.add(resource);
101 }
102 }
103 resources = urls.toArray(new URL[urls.size()]);
104 }
105 }
106 else
107 {
108 resources = new URL[0];
109 }
110 return resources;
111 }
112
113 /**
114 * The delimiter for separating location patterns.
115 */
116 private static final String PATTERN_DELIMITER = ",";
117 }