arcgissamples\geodatabase\simplepointdatasource\SimplePointWorkspaceFactoryHelper.java
/* Copyright 2010 ESRI * * All rights reserved under the copyright laws of the United States * and applicable international laws, treaties, and conventions. * * You may freely redistribute and use this sample code, with or * without modification, provided you include the original copyright * notice and use restrictions. * * See the use restrictions. * */ package arcgissamples.geodatabase.simplepointdatasource; import java.io.File; import java.io.FilenameFilter; import java.io.IOException; import com.esri.arcgis.geodatabase.IPlugInWorkspaceFactoryHelper; import com.esri.arcgis.geodatabase.IPlugInWorkspaceHelper; import com.esri.arcgis.geodatabase.esriWorkspaceType; import com.esri.arcgis.interop.AutomationException; import com.esri.arcgis.interop.extn.ArcGISCategories; import com.esri.arcgis.interop.extn.ArcGISExtension; import com.esri.arcgis.system.EngineUtilities; import com.esri.arcgis.system.IFileNames; import com.esri.arcgis.system.IUID; @ArcGISExtension(categories={ArcGISCategories.PlugInDataSource}) public class SimplePointWorkspaceFactoryHelper implements IPlugInWorkspaceFactoryHelper { /** * The extension used by the simple point file type. */ public static String fileExtension = ".spt"; /** * The extension used by the simple point metadata file type. */ public static String metaFileExtension = ".spt.xml"; /** * This flag is used for debugging purpose */ private boolean blnDebug = true; public SimplePointWorkspaceFactoryHelper(){ } /** * Indicates if the specified directory contains a valid workspace, or is a valid file-system workspace. */ public boolean containsWorkspace(String arg0, IFileNames arg1)throws IOException, AutomationException { boolean blnReturn = false; if (arg0 == null) return false; else { File file = new File(arg0); if (!file.exists()) return false; } // If no file names are specified, return whether or not the parent // directory is itself a workspace. if (arg1 == null) return this.isWorkspace(arg0); // Iterate through the file names, checking for any with the .SPT extension. arg1.reset(); String fileName = arg1.next(); while (fileName != null & fileName.length() > 0) { // Check if the current file name is a directory; if so, ignore it. if (arg1.isDirectory()) { fileName = arg1.next(); continue; } // Check if the current file name ends with the SPT extension. if (fileName.endsWith(fileExtension)) { return true; } fileName = arg1.next(); } return false; } /** * The name of the data source. */ public String getDataSourceName() throws IOException, AutomationException { return "SimplePointPlugInJava"; } /** * A description of a dataset of the type specified. */ public String getDatasetDescription(int arg0) throws IOException,AutomationException { // Since this implementation only includes feature classes, we can always // return "SimplePoint Feature Class". return "SimplePoint Feature Class"; } /** * A singular or plural description of the type of workspace the workspace factory opens. */ public String getWorkspaceDescription(boolean arg0) throws IOException,AutomationException { if (arg0) return "Simple Points dataset"; else return "Simple Point dataset"; } /** * The class ID which is registered in the WorkspaceFactory category. */ public IUID getWorkspaceFactoryTypeID() throws IOException,AutomationException { return EngineUtilities.getWorkspaceFactoryTypeID(SimplePointWorkspaceFactoryHelper.class); } /** * A string that uniquely identifies the workspace, if the specified directory contains or * is a valid workspace. */ public String getWorkspaceString(String arg0, IFileNames arg1)throws IOException, AutomationException { // If the parent directory is null or is not directory, return an empty string. if (arg0 == null) { return ""; } // If the filenames are null, return the name of the parent directory. if (arg1 == null) { return arg0; } File file = new File(arg0); if (!file.isDirectory()) return ""; // Iterate through the file names, checking for any with the .SPT extension. arg1.reset(); String fileName = null; boolean bFound = false; while (((fileName = arg1.next()) != null) && !(fileName.length() == 0)) { // Check if the current file name is a directory; if so, ignore it. if (arg1.isDirectory()) continue; // Check if the current file name ends with the SPT extension. if (fileName.endsWith(fileExtension)){ bFound = true; arg1.remove(); } } // If SPT files were found, return the parent directory name, otherwise return an empty string. if (bFound) { return arg0; } else { return ""; } } /** * The type of workspace the factory opens. */ public int getWorkspaceType() throws IOException, AutomationException { return esriWorkspaceType.esriFileSystemWorkspace; } /** * Indicates whether datasets of this type can process SQL queries. */ public boolean isCanSupportSQL() throws IOException, AutomationException { return false; } /** * Tests if the workspace string represents a valid workspace. */ public boolean isWorkspace(String arg0) throws IOException,AutomationException { // Return false if the parameter is null or does not exist. if (arg0 == null) return false; File file = new File(arg0); if (!file.isDirectory()) return false; if (file.list(new SPTFilter()).length == 0) return false; return true; } /** * Opens a workspace helper for the workspace identified by the workspace string. */ public IPlugInWorkspaceHelper openWorkspace(String arg0)throws IOException, AutomationException { // Return null if the parameter is null. if (arg0 == null) return null; // Make sure the workspace string is a directory. File file = new File(arg0); if (! file.isDirectory()) return null; // Instantiate and return a new workspace helper for the specified workspace. SimplePointWorkspaceHelper sptWorkspaceHelper = new SimplePointWorkspaceHelper(arg0); return sptWorkspaceHelper; } class SPTFilter implements FilenameFilter{ public boolean accept(File dir, String name) { return (name.endsWith(fileExtension)); } } }