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.extended;
010:
011: import net.sourceforge.chaperon.common.Decoder;
012: import net.sourceforge.chaperon.model.Violations;
013:
014: /**
015: * This class represents a pattern for a sequence of characters.
016: *
017: * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
018: * @version CVS $Id: SingleCharacter.java,v 1.4 2004/01/07 08:28:49 benedikta Exp $
019: */
020: public class SingleCharacter extends Pattern {
021: private char character;
022:
023: /**
024: * Creates a pattern for a character sequence.
025: */
026: public SingleCharacter() {
027: }
028:
029: /**
030: * Creates a pattern for a character sequence.
031: *
032: * @param string Character sequence.
033: */
034: public SingleCharacter(char character) {
035: setCharacter(character);
036: }
037:
038: /**
039: * Set the sequence of characters for this pattern
040: *
041: * @param string Character sequence
042: */
043: public void setCharacter(char character) {
044: this .character = character;
045: }
046:
047: /**
048: * Set the sequence of characters for this pattern
049: *
050: * @param string Character sequence
051: */
052: public void setCharacterAsString(String character) {
053: if (character.startsWith("#"))
054: this .character = (char) Integer.parseInt(character
055: .substring(1));
056: else
057: this .character = character.charAt(0);
058: }
059:
060: /**
061: * Returns the sequence of characters
062: *
063: * @return Seqence of characaters
064: */
065: public char getCharacter() {
066: return character;
067: }
068:
069: public String getCharacterAsString() {
070: return String.valueOf(character);
071: }
072:
073: public boolean isNullable() {
074: return false;
075: }
076:
077: public PatternSet getFirstSet() {
078: PatternSet set = new PatternSet();
079: set.addPattern(this );
080: return set;
081: }
082:
083: public PatternSet getLastSet() {
084: PatternSet set = new PatternSet();
085: set.addPattern(this );
086: return set;
087: }
088:
089: public char[] getLimits() {
090: return new char[] { getCharacter() };
091: }
092:
093: public boolean contains(char minimum, char maximum) {
094: return (getCharacter() == minimum)
095: && (getCharacter() == maximum);
096: }
097:
098: public boolean contains(char c) {
099: return c == character;
100: }
101:
102: public String getSymbol() {
103: return null;
104: }
105:
106: /**
107: * Return a string representation of this pattern
108: *
109: * @return String representation of the pattern.
110: */
111: public String toString() {
112: return Decoder.toChar(character) + "[" + index + "]";
113: }
114:
115: /**
116: * Create a clone of this pattern.
117: *
118: * @return Clone of this pattern.
119: *
120: * @throws CloneNotSupportedException If an exception occurs during the cloning.
121: */
122: public Object clone() {
123: SingleCharacter clone = new SingleCharacter();
124: clone.setCharacter(getCharacter());
125: return clone;
126: }
127:
128: /**
129: * Validates this pattern.
130: *
131: * @return Return a list of violations, if this pattern isn't valid.
132: */
133: public Violations validate() {
134: Violations violations = new Violations();
135:
136: /*if (string.length()==0)
137: violations.addViolation("Character string contains no characters",
138: getLocation());*/
139: return violations;
140: }
141: }
|