001: package org.andromda.core.configuration;
002:
003: import java.io.Serializable;
004:
005: import java.net.URL;
006:
007: import java.util.List;
008: import java.util.ListIterator;
009:
010: import org.andromda.core.common.ResourceUtils;
011:
012: /**
013: * Represents a location within a module search or mappings search.
014: * @author Chad Brandon
015: */
016: public class Location implements Serializable {
017: /**
018: * The path of the location.
019: */
020: private String path;
021:
022: /**
023: * The patterns (a comma seperated list) to
024: * include in the path search
025: */
026: private String patterns;
027:
028: /**
029: * Gets the path to this location.
030: *
031: * @return Returns the path to this location.
032: */
033: public String getPath() {
034: return path;
035: }
036:
037: /**
038: * Sets the path to this location.
039: *
040: * @param path The path to this location.
041: */
042: public void setPath(String path) {
043: this .path = path;
044: }
045:
046: /**
047: * Gets the patterns to include in this location.
048: *
049: * @return Returns the patterns.
050: */
051: public String getPatterns() {
052: return patterns;
053: }
054:
055: /**
056: * Sets the patterns to include in this location.
057: *
058: * @param patterns The patterns to set.
059: */
060: public void setPatterns(String patterns) {
061: this .patterns = patterns;
062: }
063:
064: /**
065: * Gets all files that are valid for this location. It takes into
066: * consideration the given patterns. If the location is an actual
067: * file, the an array containing that single file is returned.
068: *
069: * @return the valid files.
070: */
071: public URL[] getResources() {
072: URL[] resources;
073: final URL url = ResourceUtils.toURL(this .path);
074: if (url != null) {
075: if (ResourceUtils.isFile(url)) {
076: resources = new URL[] { url };
077: } else {
078: String[] patterns = this .patterns != null ? this .patterns
079: .split(PATTERN_DELIMITER)
080: : new String[0];
081: final List paths = ResourceUtils.getDirectoryContents(
082: url, true, patterns);
083: for (final ListIterator iterator = paths.listIterator(); iterator
084: .hasNext();) {
085: final URL resource = ResourceUtils
086: .toURL((String) iterator.next());
087: if (resource != null) {
088: iterator.set(resource);
089: } else {
090: iterator.remove();
091: }
092: }
093: resources = (URL[]) paths.toArray(new URL[0]);
094: }
095: } else {
096: resources = new URL[0];
097: }
098: return resources;
099: }
100:
101: /**
102: * The delimiter for seperating location patterns.
103: */
104: private static final String PATTERN_DELIMITER = ",";
105: }
|