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: import java.util.Vector;
014:
015: /**
016: * This class describes a pattern for a character class, which means the a character matches
017: * against a element of this class.
018: *
019: * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
020: * @version CVS $Id: CharacterClass.java,v 1.7 2003/12/10 16:34:38 benedikta Exp $
021: */
022: public class CharacterClass extends Pattern {
023: private Vector childs = new Vector();
024: private boolean exclusive = false;
025:
026: /**
027: * Creates a pattern for a character class.
028: */
029: public CharacterClass() {
030: }
031:
032: /**
033: * Add a character class element
034: *
035: * @param element Element, which should be added
036: */
037: public void addCharacterClassElement(CharacterClassElement element) {
038: if (element != null)
039: childs.addElement(element);
040: }
041:
042: /**
043: * Returns a character class element
044: *
045: * @param index Index of the character class element
046: *
047: * @return Character class element
048: */
049: public CharacterClassElement getCharacterClassElement(int index) {
050: return (CharacterClassElement) childs.elementAt(index);
051: }
052:
053: /**
054: * Returns the count of character class elements
055: *
056: * @return Count of character class elements
057: */
058: public int getCharacterClassElementCount() {
059: return childs.size();
060: }
061:
062: /**
063: * If the comparing character must match to the elements, or should not match to the elements.
064: *
065: * @param exclusive If the character class should be exclusive.
066: */
067: public void setExclusive(boolean exclusive) {
068: this .exclusive = exclusive;
069: }
070:
071: /**
072: * If this character class is exclusive
073: *
074: * @return If the character class should be exclusive.
075: */
076: public boolean isExclusive() {
077: return exclusive;
078: }
079:
080: /**
081: * Create a clone of this pattern.
082: *
083: * @return Clone of this pattern.
084: *
085: * @throws CloneNotSupportedException If an exception occurs during the cloning.
086: */
087: public String toString() {
088: StringBuffer buffer = new StringBuffer();
089:
090: buffer.append("[");
091:
092: if (exclusive)
093: buffer.append("^");
094:
095: for (int i = 0; i < getCharacterClassElementCount(); i++)
096: buffer.append(getCharacterClassElement(i).toString());
097:
098: buffer.append("]");
099:
100: if ((getMinOccurs() == 1) && (getMaxOccurs() == 1)) {
101: // nothing
102: } else if ((getMinOccurs() == 0) && (getMaxOccurs() == 1))
103: buffer.append("?");
104: else if ((getMinOccurs() == 0)
105: && (getMaxOccurs() == Integer.MAX_VALUE))
106: buffer.append("*");
107: else if ((getMinOccurs() == 1)
108: && (getMaxOccurs() == Integer.MAX_VALUE))
109: buffer.append("+");
110: else {
111: buffer.append("{");
112: buffer.append(String.valueOf(getMinOccurs()));
113: buffer.append(",");
114: buffer.append(String.valueOf(getMaxOccurs()));
115: buffer.append("}");
116: }
117:
118: return buffer.toString();
119: }
120:
121: /**
122: * Create a clone of this pattern.
123: *
124: * @return Clone of this pattern.
125: *
126: * @throws CloneNotSupportedException If an exception occurs during the cloning.
127: */
128: public Object clone() {
129: CharacterClass clone = new CharacterClass();
130:
131: clone.setMinOccurs(getMinOccurs());
132: clone.setMaxOccurs(getMaxOccurs());
133:
134: for (int i = 0; i < getCharacterClassElementCount(); i++)
135: clone.addCharacterClassElement(getCharacterClassElement(i));
136:
137: return clone;
138: }
139:
140: /**
141: * Validates this pattern.
142: *
143: * @return Return a list of violations, if this pattern isn't valid.
144: */
145: public Violations validate() {
146: Violations violations = new Violations();
147:
148: if (getCharacterClassElementCount() < 1)
149: violations
150: .addViolation(
151: "Character class doesn't contain 1 or more elements",
152: getLocation());
153:
154: for (int i = 0; i < getCharacterClassElementCount(); i++)
155: violations.addViolations(getCharacterClassElement(i)
156: .validate());
157:
158: return violations;
159: }
160: }
|