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