Package net.sourceforge.chaperon.model.extended

Source Code of net.sourceforge.chaperon.model.extended.Choice

/*
*  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.extended;

import net.sourceforge.chaperon.model.Violations;

/**
* This class describes a alternation of pattern.
*
* @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
* @version CVS $Id: Choice.java,v 1.4 2004/01/07 08:28:49 benedikta Exp $
*/
public class Choice extends PatternList
{
  /**
   * Create a pattern for alternation of pattern.
   */
  public Choice() {}

  public boolean isNullable()
  {
    boolean nullable = (getPatternCount()==0);
    for (int i = 0; i<getPatternCount(); i++)
      nullable |= getPattern(i).isNullable();

    return nullable;
  }

  public PatternSet getFirstSet()
  {
    PatternSet set = new PatternSet();
    for (int i = 0; i<getPatternCount(); i++)
      set.addPattern(getPattern(i).getFirstSet());

    return set;
  }

  public PatternSet getLastSet()
  {
    PatternSet set = new PatternSet();
    for (int i = 0; i<getPatternCount(); i++)
      set.addPattern(getPattern(i).getLastSet());

    return set;
  }

  public void update()
  {
    for (PatternIterator pattern = getPattern(); pattern.hasNext();)
      pattern.next().update();
  }

  public String toString()
  {
    StringBuffer buffer = new StringBuffer();

    if (getPatternCount()>1)
    {
      buffer.append("(");
      for (int i = 0; i<getPatternCount(); i++)
      {
        if (i>0)
          buffer.append("|");

        buffer.append(getPattern(i).toString());
      }

      buffer.append(")");
    }
    else if (getPatternCount()==1)
      buffer.append(getPattern(0).toString());

    return buffer.toString();
  }

  public String toString(PatternSet previous, PatternSet next)
  {
    StringBuffer buffer = new StringBuffer();

    if (getPatternCount()>1)
    {
      buffer.append("(");
      for (int i = 0; i<getPatternCount(); i++)
      {
        if (i>0)
          buffer.append("|");

        buffer.append(getPattern(i).toString(previous, next));
      }

      buffer.append(")");
    }
    else if (getPatternCount()==1)
      buffer.append(getPattern(0).toString(previous, next));

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

    for (int i = 0; i<getPatternCount(); i++)
      clone.addPattern(getPattern(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 (getPatternCount()==0)
      violations.addViolation("Choice doesn't contain any elements", getLocation());

    for (int i = 0; i<getPatternCount(); i++)
      violations.addViolations(getPattern(i).validate());

    return violations;
  }
}
TOP

Related Classes of net.sourceforge.chaperon.model.extended.Choice

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.