KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > model > pattern > CharacterString


1 /*
2  * Copyright (C) Chaperon. All rights reserved.
3  * -------------------------------------------------------------------------
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8
9 package net.sourceforge.chaperon.model.pattern;
10
11 import net.sourceforge.chaperon.common.Decoder;
12 import net.sourceforge.chaperon.model.Violations;
13
14 /**
15  * This class represents a pattern for a sequence of characters.
16  *
17  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
18  * @version CVS $Id: CharacterString.java,v 1.6 2003/12/09 19:55:52 benedikta Exp $
19  */

20 public class CharacterString extends Pattern
21 {
22   private String JavaDoc string = "";
23
24   /**
25    * Creates a pattern for a character sequence.
26    */

27   public CharacterString() {}
28
29   /**
30    * Creates a pattern for a character sequence.
31    *
32    * @param string Character sequence.
33    */

34   public CharacterString(String JavaDoc string)
35   {
36     setString(string);
37   }
38
39   /**
40    * Set the sequence of characters for this pattern
41    *
42    * @param string Character sequence
43    */

44   public void setString(String JavaDoc string)
45   {
46     this.string = string;
47   }
48
49   /**
50    * Returns the sequence of characters
51    *
52    * @return Seqence of characaters
53    */

54   public String JavaDoc getString()
55   {
56     return string;
57   }
58
59   /**
60    * Return a string representation of this pattern
61    *
62    * @return String representation of the pattern.
63    */

64   public String JavaDoc toString()
65   {
66     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
67
68     buffer.append(Decoder.decode(string, Decoder.REGEX));
69
70     if ((getMinOccurs()==1) && (getMaxOccurs()==1))
71     {
72       // nothing
73
}
74     else if ((getMinOccurs()==0) && (getMaxOccurs()==1))
75       buffer.append("?");
76     else if ((getMinOccurs()==0) && (getMaxOccurs()==Integer.MAX_VALUE))
77       buffer.append("*");
78     else if ((getMinOccurs()==1) && (getMaxOccurs()==Integer.MAX_VALUE))
79       buffer.append("+");
80     else
81     {
82       buffer.append("{");
83       buffer.append(String.valueOf(getMinOccurs()));
84       buffer.append(",");
85       buffer.append(String.valueOf(getMaxOccurs()));
86       buffer.append("}");
87     }
88
89     return buffer.toString();
90   }
91
92   /**
93    * Create a clone of this pattern.
94    *
95    * @return Clone of this pattern.
96    *
97    * @throws CloneNotSupportedException If an exception occurs during the cloning.
98    */

99   public Object JavaDoc clone()
100   {
101     CharacterString clone = new CharacterString();
102
103     clone.setMinOccurs(getMinOccurs());
104     clone.setMaxOccurs(getMaxOccurs());
105
106     clone.setString(getString());
107
108     return clone;
109   }
110
111   /**
112    * Validates this pattern.
113    *
114    * @return Return a list of violations, if this pattern isn't valid.
115    */

116   public Violations validate()
117   {
118     Violations violations = new Violations();
119
120     if ((string==null) || (string.length()<=0))
121       violations.addViolation("Character string contains no characters", getLocation());
122
123     return violations;
124   }
125 }
126
Popular Tags