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.common.Decoder;
012: import net.sourceforge.chaperon.model.Violations;
013:
014: /**
015: * This class represents a interval of characters within a character class.
016: *
017: * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
018: * @version CVS $Id: CharacterInterval.java,v 1.5 2003/12/09 19:55:52 benedikta Exp $
019: */
020: public class CharacterInterval implements CharacterClassElement {
021: private char minimum = 'a';
022: private char maximum = 'z';
023: private String location = null;
024:
025: /**
026: * Creates a interval of characters.
027: */
028: public CharacterInterval() {
029: }
030:
031: /**
032: * Sets the minimum character for the interval.
033: *
034: * @param minimum Minimum character.
035: */
036: public void setMinimum(char minimum) {
037: this .minimum = minimum;
038: }
039:
040: /**
041: * Returns the minimum character for the interval.
042: *
043: * @return Minimum character.
044: */
045: public char getMinimum() {
046: return this .minimum;
047: }
048:
049: /**
050: * Sets the maximum character for the interval.
051: *
052: * @param maximum Maximum character.
053: */
054: public void setMaximum(char maximum) {
055: this .maximum = maximum;
056: }
057:
058: /**
059: * Returns the maximum character for the interval.
060: *
061: * @return Maximum character.
062: */
063: public char getMaximum() {
064: return this .maximum;
065: }
066:
067: /**
068: * Set the location from the input source.
069: *
070: * @param location Location in the input source.
071: */
072: public void setLocation(String location) {
073: this .location = location;
074: }
075:
076: /**
077: * Returns the location from the input source.
078: *
079: * @return Location in the input source.
080: */
081: public String getLocation() {
082: return location;
083: }
084:
085: /**
086: * Return a string representation of this pattern
087: *
088: * @return String representation of the pattern.
089: */
090: public String toString() {
091: StringBuffer buffer = new StringBuffer();
092:
093: buffer.append(Decoder.decode(this .minimum, Decoder.CLASS));
094: buffer.append("-");
095: buffer.append(Decoder.decode(this .maximum, Decoder.CLASS));
096: return buffer.toString();
097: }
098:
099: /**
100: * Create a clone of this pattern.
101: *
102: * @return Clone of this pattern.
103: *
104: * @throws CloneNotSupportedException If an exception occurs during the cloning.
105: */
106: public Object clone() {
107: CharacterInterval clone = new CharacterInterval();
108:
109: clone.setMinimum(getMinimum());
110: clone.setMaximum(getMaximum());
111:
112: return clone;
113: }
114:
115: /**
116: * Validates this pattern.
117: *
118: * @return Return a list of violations, if this pattern isn't valid.
119: */
120: public Violations validate() {
121: Violations violations = new Violations();
122:
123: if (minimum > maximum)
124: violations.addViolation(
125: "Minimum is greater than the maximum",
126: getLocation());
127:
128: if (minimum == maximum)
129: violations.addViolation(
130: "Minimum is equal than the maximum", getLocation());
131:
132: return violations;
133: }
134: }
|