/*
* 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 represents a concatenation of pattern.
*
* @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
* @version CVS $Id: Concatenation.java,v 1.3 2003/12/09 19:55:52 benedikta Exp $
*/
public class Concatenation extends PatternList
{
/**
* Create a pattern for a concatenation of pattern.
*/
public Concatenation() {}
/**
* Return a string representation of this pattern
*
* @return String representation of the pattern.
*/
public String toString()
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i<getPatternCount(); i++)
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() throws CloneNotSupportedException
{
Concatenation clone = new Concatenation();
clone.setMinOccurs(getMinOccurs());
clone.setMaxOccurs(getMaxOccurs());
for (int i = 0; i<getPatternCount(); i++)
clone.addPattern((Pattern)getPattern(i).clone());
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("Concatenation doesn't contain any elements", getLocation());
for (int i = 0; i<getPatternCount(); i++)
violations.addViolations(getPattern(i).validate());
return violations;
}
}