Package net.sourceforge.chaperon.model.pattern

Source Code of net.sourceforge.chaperon.model.pattern.CharacterClass

/*
*  Copyright (C) Chaperon. All rights reserved.
*  -------------------------------------------------------------------------
*  This software is published under the terms of the Apache Software License
*  version 1.1, a copy of which has been included  with this distribution in
*  the LICENSE file.
*/

package net.sourceforge.chaperon.model.pattern;

import net.sourceforge.chaperon.model.Violations;

import java.util.Vector;

/**
* This class describes a pattern for a character class, which means the a character matches
* against a element of this class.
*
* @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
* @version CVS $Id: CharacterClass.java,v 1.7 2003/12/10 16:34:38 benedikta Exp $
*/
public class CharacterClass extends Pattern
{
  private Vector childs = new Vector();
  private boolean exclusive = false;

  /**
   * Creates a pattern for a character class.
   */
  public CharacterClass() {}

  /**
   * Add a character class element
   *
   * @param element Element, which should be added
   */
  public void addCharacterClassElement(CharacterClassElement element)
  {
    if (element!=null)
      childs.addElement(element);
  }

  /**
   * Returns a character class element
   *
   * @param index Index of the character class element
   *
   * @return Character class element
   */
  public CharacterClassElement getCharacterClassElement(int index)
  {
    return (CharacterClassElement)childs.elementAt(index);
  }

  /**
   * Returns the count of character class elements
   *
   * @return Count of character class elements
   */
  public int getCharacterClassElementCount()
  {
    return childs.size();
  }

  /**
   * If the comparing character must match to the elements, or should not match to the elements.
   *
   * @param exclusive If the character class should be exclusive.
   */
  public void setExclusive(boolean exclusive)
  {
    this.exclusive = exclusive;
  }

  /**
   * If this character class is exclusive
   *
   * @return If the character class should be exclusive.
   */
  public boolean isExclusive()
  {
    return exclusive;
  }

  /**
   * Create a clone of this pattern.
   *
   * @return Clone of this pattern.
   *
   * @throws CloneNotSupportedException If an exception occurs during the cloning.
   */
  public String toString()
  {
    StringBuffer buffer = new StringBuffer();

    buffer.append("[");

    if (exclusive)
      buffer.append("^");

    for (int i = 0; i<getCharacterClassElementCount(); i++)
      buffer.append(getCharacterClassElement(i).toString());

    buffer.append("]");

    if ((getMinOccurs()==1) && (getMaxOccurs()==1))
    {
      // nothing
    }
    else if ((getMinOccurs()==0) && (getMaxOccurs()==1))
      buffer.append("?");
    else if ((getMinOccurs()==0) && (getMaxOccurs()==Integer.MAX_VALUE))
      buffer.append("*");
    else if ((getMinOccurs()==1) && (getMaxOccurs()==Integer.MAX_VALUE))
      buffer.append("+");
    else
    {
      buffer.append("{");
      buffer.append(String.valueOf(getMinOccurs()));
      buffer.append(",");
      buffer.append(String.valueOf(getMaxOccurs()));
      buffer.append("}");
    }

    return buffer.toString();
  }

  /**
   * Create a clone of this pattern.
   *
   * @return Clone of this pattern.
   *
   * @throws CloneNotSupportedException If an exception occurs during the cloning.
   */
  public Object clone()
  {
    CharacterClass clone = new CharacterClass();

    clone.setMinOccurs(getMinOccurs());
    clone.setMaxOccurs(getMaxOccurs());

    for (int i = 0; i<getCharacterClassElementCount(); i++)
      clone.addCharacterClassElement(getCharacterClassElement(i));

    return clone;
  }

  /**
   * Validates this pattern.
   *
   * @return Return a list of violations, if this pattern isn't valid.
   */
  public Violations validate()
  {
    Violations violations = new Violations();

    if (getCharacterClassElementCount()<1)
      violations.addViolation("Character class doesn't contain 1 or more elements", getLocation());

    for (int i = 0; i<getCharacterClassElementCount(); i++)
      violations.addViolations(getCharacterClassElement(i).validate());

    return violations;
  }
}
TOP

Related Classes of net.sourceforge.chaperon.model.pattern.CharacterClass

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.