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 pattern for a sequence of characters.
016: *
017: * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
018: * @version CVS $Id: CharacterString.java,v 1.6 2003/12/09 19:55:52 benedikta Exp $
019: */
020: public class CharacterString extends Pattern {
021: private String string = "";
022:
023: /**
024: * Creates a pattern for a character sequence.
025: */
026: public CharacterString() {
027: }
028:
029: /**
030: * Creates a pattern for a character sequence.
031: *
032: * @param string Character sequence.
033: */
034: public CharacterString(String string) {
035: setString(string);
036: }
037:
038: /**
039: * Set the sequence of characters for this pattern
040: *
041: * @param string Character sequence
042: */
043: public void setString(String string) {
044: this .string = string;
045: }
046:
047: /**
048: * Returns the sequence of characters
049: *
050: * @return Seqence of characaters
051: */
052: public String getString() {
053: return string;
054: }
055:
056: /**
057: * Return a string representation of this pattern
058: *
059: * @return String representation of the pattern.
060: */
061: public String toString() {
062: StringBuffer buffer = new StringBuffer();
063:
064: buffer.append(Decoder.decode(string, Decoder.REGEX));
065:
066: if ((getMinOccurs() == 1) && (getMaxOccurs() == 1)) {
067: // nothing
068: } else if ((getMinOccurs() == 0) && (getMaxOccurs() == 1))
069: buffer.append("?");
070: else if ((getMinOccurs() == 0)
071: && (getMaxOccurs() == Integer.MAX_VALUE))
072: buffer.append("*");
073: else if ((getMinOccurs() == 1)
074: && (getMaxOccurs() == Integer.MAX_VALUE))
075: buffer.append("+");
076: else {
077: buffer.append("{");
078: buffer.append(String.valueOf(getMinOccurs()));
079: buffer.append(",");
080: buffer.append(String.valueOf(getMaxOccurs()));
081: buffer.append("}");
082: }
083:
084: return buffer.toString();
085: }
086:
087: /**
088: * Create a clone of this pattern.
089: *
090: * @return Clone of this pattern.
091: *
092: * @throws CloneNotSupportedException If an exception occurs during the cloning.
093: */
094: public Object clone() {
095: CharacterString clone = new CharacterString();
096:
097: clone.setMinOccurs(getMinOccurs());
098: clone.setMaxOccurs(getMaxOccurs());
099:
100: clone.setString(getString());
101:
102: return clone;
103: }
104:
105: /**
106: * Validates this pattern.
107: *
108: * @return Return a list of violations, if this pattern isn't valid.
109: */
110: public Violations validate() {
111: Violations violations = new Violations();
112:
113: if ((string == null) || (string.length() <= 0))
114: violations.addViolation(
115: "Character string contains no characters",
116: getLocation());
117:
118: return violations;
119: }
120: }
|