KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > model > extended > SingleCharacter


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.extended;
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: SingleCharacter.java,v 1.4 2004/01/07 08:28:49 benedikta Exp $
19  */

20 public class SingleCharacter extends Pattern
21 {
22   private char character;
23
24   /**
25    * Creates a pattern for a character sequence.
26    */

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

34   public SingleCharacter(char character)
35   {
36     setCharacter(character);
37   }
38
39   /**
40    * Set the sequence of characters for this pattern
41    *
42    * @param string Character sequence
43    */

44   public void setCharacter(char character)
45   {
46     this.character = character;
47   }
48
49   /**
50    * Set the sequence of characters for this pattern
51    *
52    * @param string Character sequence
53    */

54   public void setCharacterAsString(String JavaDoc character)
55   {
56     if (character.startsWith("#"))
57       this.character = (char)Integer.parseInt(character.substring(1));
58     else
59       this.character = character.charAt(0);
60   }
61
62   /**
63    * Returns the sequence of characters
64    *
65    * @return Seqence of characaters
66    */

67   public char getCharacter()
68   {
69     return character;
70   }
71
72   public String JavaDoc getCharacterAsString()
73   {
74     return String.valueOf(character);
75   }
76
77   public boolean isNullable()
78   {
79     return false;
80   }
81
82   public PatternSet getFirstSet()
83   {
84     PatternSet set = new PatternSet();
85     set.addPattern(this);
86     return set;
87   }
88
89   public PatternSet getLastSet()
90   {
91     PatternSet set = new PatternSet();
92     set.addPattern(this);
93     return set;
94   }
95
96   public char[] getLimits()
97   {
98     return new char[]{getCharacter()};
99   }
100
101   public boolean contains(char minimum, char maximum)
102   {
103     return (getCharacter()==minimum) && (getCharacter()==maximum);
104   }
105
106   public boolean contains(char c)
107   {
108     return c==character;
109   }
110
111   public String JavaDoc getSymbol()
112   {
113     return null;
114   }
115
116   /**
117    * Return a string representation of this pattern
118    *
119    * @return String representation of the pattern.
120    */

121   public String JavaDoc toString()
122   {
123     return Decoder.toChar(character)+"["+index+"]";
124   }
125
126   /**
127    * Create a clone of this pattern.
128    *
129    * @return Clone of this pattern.
130    *
131    * @throws CloneNotSupportedException If an exception occurs during the cloning.
132    */

133   public Object JavaDoc clone()
134   {
135     SingleCharacter clone = new SingleCharacter();
136     clone.setCharacter(getCharacter());
137     return clone;
138   }
139
140   /**
141    * Validates this pattern.
142    *
143    * @return Return a list of violations, if this pattern isn't valid.
144    */

145   public Violations validate()
146   {
147     Violations violations = new Violations();
148
149     /*if (string.length()==0)
150       violations.addViolation("Character string contains no characters",
151                               getLocation());*/

152     return violations;
153   }
154 }
155
Popular Tags