Package net.sourceforge.chaperon.model.pattern

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

/*
*  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.common.Decoder;
import net.sourceforge.chaperon.model.Violations;

/**
* This class represents a pattern for a sequence of characters.
*
* @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
* @version CVS $Id: CharacterString.java,v 1.6 2003/12/09 19:55:52 benedikta Exp $
*/
public class CharacterString extends Pattern
{
  private String string = "";

  /**
   * Creates a pattern for a character sequence.
   */
  public CharacterString() {}

  /**
   * Creates a pattern for a character sequence.
   *
   * @param string Character sequence.
   */
  public CharacterString(String string)
  {
    setString(string);
  }

  /**
   * Set the sequence of characters for this pattern
   *
   * @param string Character sequence
   */
  public void setString(String string)
  {
    this.string = string;
  }

  /**
   * Returns the sequence of characters
   *
   * @return Seqence of characaters
   */
  public String getString()
  {
    return string;
  }

  /**
   * Return a string representation of this pattern
   *
   * @return String representation of the pattern.
   */
  public String toString()
  {
    StringBuffer buffer = new StringBuffer();

    buffer.append(Decoder.decode(string, Decoder.REGEX));

    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()
  {
    CharacterString clone = new CharacterString();

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

    clone.setString(getString());

    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 ((string==null) || (string.length()<=0))
      violations.addViolation("Character string contains no characters", getLocation());

    return violations;
  }
}
TOP

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

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.