/*
* 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 special marked concatenation of pattern elements. This group is used get
* parts of the matched pattern.
*
* @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
* @version CVS $Id: PatternGroup.java,v 1.3 2003/12/09 19:55:53 benedikta Exp $
*/
public class PatternGroup extends PatternList
{
/**
* Creates a pattern for a group.
*/
public PatternGroup() {}
/**
* Return a string representation of this pattern
*
* @return String representation of the pattern.
*/
public String toString()
{
StringBuffer buffer = new StringBuffer();
buffer.append("(");
for (int i = 0; i<getPatternCount(); i++)
buffer.append(getPattern(i).toString());
buffer.append(")");
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
{
PatternGroup clone = new PatternGroup();
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()<1)
violations.addViolation("Pattern group doesn't contain elements", getLocation());
for (int i = 0; i<getPatternCount(); i++)
violations.addViolations(getPattern(i).validate());
return violations;
}
}