Choice.java :  » Parser » chaperon-3.0 » net » sourceforge » chaperon » model » extended » Java Open Source

Java Open Source » Parser » chaperon 3.0 
chaperon 3.0 » net » sourceforge » chaperon » model » extended » Choice.java
/*
 *  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;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.