//////////////////////////////////////////////////////////////////////////////
// 
//                    Copyright 2012, Cornutum Project
//                             www.cornutum.org
//
//////////////////////////////////////////////////////////////////////////////

package org.cornutum.tcases;

import java.util.Collection;
import java.util.regex.Pattern;

/**
 * Defines utility methods for constructing test definitions.
 *
 */
public final class DefUtils
  {
  private DefUtils()
    {
    // Static methods only
    }
      
  /**
   * Returns true if the given string is a valid identifier.
   */
  public static boolean isIdentifier( String id)
    {
    return id != null && identifierRegex_.matcher( id).matches();
    }  

  /**
   * Translates the given identifier path name into a corresponding
   * sequence of identifiers.
   */
  public static String[] toPath( String pathName)
    {
    return
      pathName == null
      ? null
      : pathName.split( "\\.", -1);
    }
  
  /**
   * Throws an exception if the given string is not a valid identifier.
   */
  public static void assertIdentifier( String id) throws IllegalArgumentException
    {
    if( !isIdentifier( id))
      {
      throw
        new IllegalArgumentException
        ( (id==null? "null" : ("\"" + String.valueOf( id) + "\""))
          + " is not a valid identifier");
      }
    }

  /**
   * Returns true if the given string is a valid variable value.
   */
  public static boolean isVarValue( Object val)
    {
    return
      val == null
      || !val.getClass().equals( String.class)
      || varValueRegex_.matcher( val.toString()).matches();
    }

  /**
    * Throws an exception if the given string is not a valid variable value.
    */
  public static void assertVarValue( Object val) throws IllegalArgumentException
    {
    if( !isVarValue( val))
      {
      throw new IllegalArgumentException( "\"" + String.valueOf( val) + "\"" + " is not a valid variable value");
      }
    }


    /**
   * Throws an exception if the given string is not a valid identifier path name.
   */
  public static void assertPath( String pathName) throws IllegalArgumentException
    {
    String ids[] = toPath( pathName);
    for( int i = 0; i < ids.length; i++)
      {
      assertIdentifier( ids[i]);
      }
    }
  
  /**
   * Throws an exception if any member of the given set of properties is not a valid identifier.
   */
  public static void assertPropertyIdentifiers( Collection<String> properties) throws IllegalArgumentException
    {
    if( properties != null)
      {
      for( String property : properties)
        {
        assertIdentifier( property);
        }
      }
    }

  private static final Pattern identifierRegex_ = Pattern.compile( "[\\p{Alpha}\\p{Digit}_\\-]+", Pattern.UNICODE_CHARACTER_CLASS);
  private static final Pattern varValueRegex_ = Pattern.compile( "([^\\p{Cntrl}]|\\s)*");
  }


@@ -2687,38 +2687,147 @@
 %0A%0A  
-private static final Pattern i
+/**%0A   * Converts the given name to a Tcases identifier%0A   */%0A  public static String toIdentifier( String name)%0A    %7B%0A    return%0A      nonI
 dent
@@ -2835,62 +2835,401 @@
 fier
+Char
 Regex_
- = Pattern.compile( %22%5B%5C%5Cp%7BAlpha%7D%5C%5Cp%7BDigit%7D_%5C%5C-%5D+
+.matcher( name.replaceAll( %22%5C%5Cs+%22, %22-%22))%0A      .replaceAll( %22%22);%0A    %7D%0A%0A  private static final String identifierChars_ = %22%5C%5Cp%7BAlpha%7D%5C%5Cp%7BDigit%7D_%5C%5C-%22;%0A  private static final Pattern identifierRegex_ = Pattern.compile( %22%5B%22 + identifierChars_ + %22%5D+%22, Pattern.UNICODE_CHARACTER_CLASS);%0A  private static final Pattern nonIdentifierCharRegex_ = Pattern.compile( %22%5B%5E%22 + identifierChars_ + %22%5D
 %22, P

Add toIdentifier()