001: package net.refractions.udig.catalog.ui;
002:
003: import java.io.File;
004: import java.io.Serializable;
005: import java.net.MalformedURLException;
006: import java.net.URL;
007: import java.util.ArrayList;
008: import java.util.List;
009: import java.util.Map;
010:
011: import net.refractions.udig.catalog.CatalogPlugin;
012: import net.refractions.udig.core.internal.ExtensionPointProcessor;
013: import net.refractions.udig.core.internal.ExtensionPointUtil;
014:
015: import org.eclipse.core.runtime.IConfigurationElement;
016: import org.eclipse.core.runtime.IExtension;
017:
018: public class FileConnectionFactory extends UDIGConnectionFactory {
019:
020: private ArrayList<String> extensionList;
021:
022: public boolean canProcess(Object context) {
023: return createConnectionURL(context) != null;
024: }
025:
026: public Map<String, Serializable> createConnectionParameters(
027: Object context) {
028: //do nothing, we are not connecting to a specific data store
029: return null;
030: }
031:
032: public URL createConnectionURL(Object context) {
033: URL url = CatalogPlugin.locateURL(context);
034:
035: if (url == null) {
036: return url;
037: }
038: url = checkedURL(url);
039: if (url == null || url.getFile() == null) {
040: return null;
041: }
042:
043: //Checks whether file is acceptable based on extension.
044: String fileExt = url.getFile().substring(
045: url.getFile().lastIndexOf('.') + 1);
046: if (fileExt != null)
047: fileExt = fileExt.toLowerCase();
048:
049: for (String goodExt : getExtensionList()) {
050: goodExt = goodExt.toLowerCase();
051: if (fileExt.equals(goodExt.substring(goodExt
052: .lastIndexOf('.') + 1))) {
053: //actually do a test
054: File f = new File(url.getFile());
055: if (f.exists())
056: return url;
057: }
058: }
059: return null;
060: }
061:
062: /** Check that any trailing #layer is removed from the url */
063: static public URL checkedURL(URL url) {
064: String check = url.toExternalForm();
065: int hash = check.indexOf('#');
066: if (hash == -1) {
067: return url;
068: }
069: try {
070: return new URL(check.substring(0, hash));
071: } catch (MalformedURLException e) {
072: return null;
073: }
074: }
075:
076: @SuppressWarnings("unchecked")
077: List<String> getExtensionList() {
078: if (extensionList == null) {
079: extensionList = new ArrayList<String>();
080: ExtensionPointUtil
081: .process(
082: CatalogUIPlugin.getDefault(),
083: "net.refractions.udig.catalog.ui.fileFormat", new ExtensionPointProcessor() { //$NON-NLS-1$
084: public void process(
085: IExtension extension,
086: IConfigurationElement element)
087: throws Exception {
088: try {
089: String ext = element
090: .getAttribute("fileExtension"); //$NON-NLS-1$
091: extensionList.add(ext);
092: } catch (Throwable t) {
093: CatalogUIPlugin.log(t
094: .getLocalizedMessage(),
095: t);
096: }
097: }
098: });
099: }
100: return (List<String>) extensionList.clone();
101: }
102: }
|