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 represents a set of characters within a character class.
016: *
017: * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
018: * @version CVS $Id: CharacterSet.java,v 1.6 2003/12/09 19:55:52 benedikta Exp $
019: */
020: public class CharacterSet implements CharacterClassElement {
021: private String set = "";
022: private String location = null;
023:
024: /**
025: * Creates set of characters.
026: */
027: public CharacterSet() {
028: }
029:
030: /**
031: * Set the set of characters.
032: *
033: * @param set Set of characters.
034: */
035: public void setCharacters(String set) {
036: this .set = set;
037: }
038:
039: /**
040: * Returns the to comparing characters
041: *
042: * @return To comparing characters
043: */
044: public String getCharacters() {
045: return set;
046: }
047:
048: /**
049: * Set the location from the input source.
050: *
051: * @param location Location in the input source.
052: */
053: public void setLocation(String location) {
054: this .location = location;
055: }
056:
057: /**
058: * Returns the location from the input source.
059: *
060: * @return Location in the input source.
061: */
062: public String getLocation() {
063: return location;
064: }
065:
066: /**
067: * Return a string representation of this pattern
068: *
069: * @return String representation of the pattern.
070: */
071: public String toString() {
072: return Decoder.decode(set, Decoder.CLASS);
073: }
074:
075: /**
076: * Create a clone of this pattern.
077: *
078: * @return Clone of this pattern.
079: *
080: * @throws CloneNotSupportedException If an exception occurs during the cloning.
081: */
082: public Object clone() {
083: CharacterSet clone = new CharacterSet();
084:
085: clone.setCharacters(getCharacters());
086:
087: return clone;
088: }
089:
090: /**
091: * Validates this pattern.
092: *
093: * @return Return a list of violations, if this pattern isn't valid.
094: */
095: public Violations validate() {
096: Violations violations = new Violations();
097:
098: if ((set == null) || (set.length() == 0))
099: violations.addViolation(
100: "Character set contains no characters",
101: getLocation());
102:
103: return violations;
104: }
105: }
|