/*
* 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;
/**
* This class describes a alternation of pattern.
*
* @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
* @version CVS $Id: Alternation.java,v 1.4 2003/12/09 19:55:52 benedikta Exp $
*/
public class Alternation extends PatternList
{
/**
* Create a pattern for alternation of pattern.
*/
public Alternation() {}
/**
* 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();
for (int i = 0; i<getPatternCount(); i++)
{
if (i>0)
buffer.append("|");
buffer.append(getPattern(i).toString());
}
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()
{
Alternation clone = new Alternation();
clone.setMinOccurs(getMinOccurs());
clone.setMaxOccurs(getMaxOccurs());
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("Alternation doesn't contain any elements", getLocation());
for (int i = 0; i<getPatternCount(); i++)
violations.addViolations(getPattern(i).validate());
return violations;
}
}