001: /*
002: * Copyright (c) 2007, intarsys consulting GmbH
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * - Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * - Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * - Neither the name of intarsys nor the names of its contributors may be used
015: * to endorse or promote products derived from this software without specific
016: * prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028: * POSSIBILITY OF SUCH DAMAGE.
029: */
030: package de.intarsys.tools.locator;
031:
032: import java.util.ArrayList;
033: import java.util.Enumeration;
034: import java.util.Iterator;
035: import java.util.List;
036: import java.util.StringTokenizer;
037: import de.intarsys.tools.file.FileTools;
038:
039: /**
040: * The factory for {@link FileLocator} objects.
041: *
042: * <p>
043: * {@link FileLocator} instances are created either using an absolute path name
044: * or are looked up relative to the factorys search path. Multiple search paths
045: * may be defined.
046: * </p>
047: */
048: public class FileLocatorFactory implements ILocatorFactory {
049: /** The separator character for the definition of multiple search paths */
050: public static final String PATH_SEPARATOR = ";";
051:
052: /** The collection of search paths to be looked up when creating a locator */
053: private List searchPaths;
054:
055: /** The root path where we look up relative references */
056: private String searchPathDefinition;
057:
058: /** flag if we synchronize synchronously with every check */
059: private boolean synchSynchronous = true;
060:
061: /**
062: * Create a new factory.
063: */
064: public FileLocatorFactory() {
065: super ();
066: setSearchPathDefinition("./");
067: }
068:
069: public void setSearchPathDefinition(String searchPath) {
070: this .searchPathDefinition = searchPath;
071: // set up search paths
072: searchPaths = new ArrayList();
073: for (Enumeration e = new StringTokenizer(searchPathDefinition,
074: PATH_SEPARATOR); e.hasMoreElements();) {
075: String path = (String) e.nextElement();
076: if ((path != null) && (path.trim().length() > 0)
077: && !searchPaths.contains(path)) {
078: searchPaths.add(path);
079: }
080: }
081: }
082:
083: public String getSearchPathDefinition() {
084: return searchPathDefinition;
085: }
086:
087: public void setSearchPaths(List searchPaths) {
088: this .searchPaths = searchPaths;
089: }
090:
091: public List getSearchPaths() {
092: return searchPaths;
093: }
094:
095: public void setSynchSynchronous(boolean synchSynchronous) {
096: this .synchSynchronous = synchSynchronous;
097: }
098:
099: public boolean isSynchSynchronous() {
100: return synchSynchronous;
101: }
102:
103: /**
104: * The file locator factory supports looking up resources in multiple paths.
105: * To preserve compatibility to ILocatorFactory, the last locator created is
106: * returned if no match is found. This is a valid locator, even so no
107: * existing physical resource is designated.
108: *
109: * @see de.intarsys.tools.locator.ILocatorFactory#createLocator(java.lang.String)
110: */
111: public ILocator createLocator(String path) {
112: FileLocator result = null;
113: for (Iterator it = getSearchPaths().iterator(); it.hasNext();) {
114: String searchPath = (String) it.next();
115: String absolutePath = FileTools.getPathRelative(searchPath,
116: path);
117: result = new FileLocator(absolutePath);
118: if (result.exists()) {
119: break;
120: }
121: }
122: if (result != null) {
123: result.setSynchSynchronous(isSynchSynchronous());
124: }
125: return result;
126: }
127: }
|