arcgissamples\geodatabase\classextension\FeatureValidator.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Feature Validator Runner custom Java class extension
arcgissamples\geodatabase\classextension\FeatureValidator.java
/* Copyright 2015 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 at <your ArcGIS install location>/DeveloperKit10.4/userestrictions.txt.
* 
*/
package arcgissamples.geodatabase.classextension;

import java.io.IOException;
import java.util.regex.*;
import com.esri.arcgis.geodatabase.*;
import com.esri.arcgis.interop.AutomationException;
import com.esri.arcgis.interop.extn.*;
import com.esri.arcgis.system.IPropertySet;

/**
 * An example of a class extension that implements custom validation. This implementation
 * assumes that a text field named "email" exists, and validates that field using a regular
 * expression to ensure that the value is a valid email address of the form:
 *     [\w-]+@([\w-]+\.)+[\w-]+
 * For example, "mlucic@bostonbruins.com" would be considered valid, while
 * "skoivu@montreal canadiens.com" would not, because whitespace is not allowed. 
 */
@ArcGISExtension(categories = { ArcGISCategories.GeoObjectClassExtensions })
public class FeatureValidator implements IClassExtension, IObjectClassExtension, IFeatureClassExtension, IObjectClassValidation 
{
  private static final long serialVersionUID = -87498810130813769L;
  
  /**
   * The name of the email address field to be validated.
   */
  private static final String EMAIL_FIELD_NAME = "email";
  
  /**
   * A regular expression pattern representing an email address.
   */
  private static final String EMAIL_REGEX = "[\\w-]+@([\\w-]+\\.)+[\\w-]+";
  
  /**
   * The index of the email address field in the extension's class. A value of -1
   * indicates that it could not be found during initialization.
   */
  int emailFieldIndex = -1;
  
  /**
   * The Regex pattern used to evaluate email field values.
   */
  private Pattern regexPattern = null;

  /************************************************************************************************
   * IClassExtension members
   ************************************************************************************************/
  /**
   * Initializes the extension, passing in a reference to its class helper and its extension properties.
   */
  public void init(IClassHelper classHelper, IPropertySet extensionProperties) throws IOException, AutomationException
  {
    // Get the index of the email field.
    IClass baseClass = classHelper.esri_getClass();
    emailFieldIndex = baseClass.findField(EMAIL_FIELD_NAME);
    
    // Compile a regex pattern for email validation.
    regexPattern = Pattern.compile(EMAIL_REGEX);
  }
  
  /**
   * Called when the extension's class is being disposed of from memory.
   */
  public void shutdown() throws IOException, AutomationException
  {
    // Do nothing.
  }

  /************************************************************************************************
   * IObjectClassValidation members
   ************************************************************************************************/
  /**
   * Validates the row.
   */
  public String validateRow(IRow row) throws IOException, AutomationException
  {
    return validateField(row, EMAIL_FIELD_NAME);
  }  

  /**
   * Validates the row's specified attribute. 
   */
  public String validateField(IRow row, String fieldName) throws IOException, AutomationException
  {
    // Make sure the email field is the field being validated, and that its index was
    // successfully determined during validation.
    if (fieldName.equalsIgnoreCase(EMAIL_FIELD_NAME) && emailFieldIndex != -1)
    {
      try
      {
        // Check that the field value isn't null before matching. 
        Object emailObj = row.getValue(emailFieldIndex);
        if (emailObj != null)
        {
          // Try to match the value with the regular expression matcher.
          String emailAddress = (String)emailObj;
          Matcher matcher = regexPattern.matcher(emailAddress);
          if (matcher.matches())
          {
            // The value is a match, return null to indicate it's valid.
            return null;
          }
          else
          {
            // The value is not a match.
            return emailAddress.concat(" is not a valid email address.");
          }
        }
      }
      catch (Exception exc)
      {
        return "An error occurred during validation: " + exc.getMessage();
      }
    }
    
    return null;
  }
}