001: /*
002: * Copyright (C) Chaperon. All rights reserved.
003: * -------------------------------------------------------------------------
004: * This software is published under the terms of the Apache Software License
005: * version 1.1, a copy of which has been included with this distribution in
006: * the LICENSE file.
007: */
008:
009: package net.sourceforge.chaperon.model.pattern;
010:
011: import net.sourceforge.chaperon.model.Violations;
012:
013: /**
014: * This class represents a special marked concatenation of pattern elements. This group is used get
015: * parts of the matched pattern.
016: *
017: * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
018: * @version CVS $Id: PatternGroup.java,v 1.3 2003/12/09 19:55:53 benedikta Exp $
019: */
020: public class PatternGroup extends PatternList {
021: /**
022: * Creates a pattern for a group.
023: */
024: public PatternGroup() {
025: }
026:
027: /**
028: * Return a string representation of this pattern
029: *
030: * @return String representation of the pattern.
031: */
032: public String toString() {
033: StringBuffer buffer = new StringBuffer();
034:
035: buffer.append("(");
036:
037: for (int i = 0; i < getPatternCount(); i++)
038: buffer.append(getPattern(i).toString());
039:
040: buffer.append(")");
041:
042: if ((getMinOccurs() == 1) && (getMaxOccurs() == 1)) {
043: // nothing
044: } else if ((getMinOccurs() == 0) && (getMaxOccurs() == 1))
045: buffer.append("?");
046: else if ((getMinOccurs() == 0)
047: && (getMaxOccurs() == Integer.MAX_VALUE))
048: buffer.append("*");
049: else if ((getMinOccurs() == 1)
050: && (getMaxOccurs() == Integer.MAX_VALUE))
051: buffer.append("+");
052: else {
053: buffer.append("{");
054: buffer.append(String.valueOf(getMinOccurs()));
055: buffer.append(",");
056: buffer.append(String.valueOf(getMaxOccurs()));
057: buffer.append("}");
058: }
059:
060: return buffer.toString();
061: }
062:
063: /**
064: * Create a clone of this pattern.
065: *
066: * @return Clone of this pattern.
067: *
068: * @throws CloneNotSupportedException If an exception occurs during the cloning.
069: */
070: public Object clone() throws CloneNotSupportedException {
071: PatternGroup clone = new PatternGroup();
072:
073: clone.setMinOccurs(getMinOccurs());
074: clone.setMaxOccurs(getMaxOccurs());
075:
076: for (int i = 0; i < getPatternCount(); i++)
077: clone.addPattern((Pattern) getPattern(i).clone());
078:
079: return clone;
080: }
081:
082: /**
083: * Validates this pattern.
084: *
085: * @return Return a list of violations, if this pattern isn't valid.
086: */
087: public Violations validate() {
088: Violations violations = new Violations();
089:
090: if (getPatternCount() < 1)
091: violations.addViolation(
092: "Pattern group doesn't contain elements",
093: getLocation());
094:
095: for (int i = 0; i < getPatternCount(); i++)
096: violations.addViolations(getPattern(i).validate());
097:
098: return violations;
099: }
100: }
|