/*
* Copyright (C) Chaperon. All rights reserved.
* -------------------------------------------------------------------------
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package net.sourceforge.chaperon.model.extended;
import net.sourceforge.chaperon.common.Decoder;
import net.sourceforge.chaperon.model.Violations;
/**
* This class represents a pattern for a sequence of characters.
*
* @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
* @version CVS $Id: SingleCharacter.java,v 1.4 2004/01/07 08:28:49 benedikta Exp $
*/
public class SingleCharacter extends Pattern
{
private char character;
/**
* Creates a pattern for a character sequence.
*/
public SingleCharacter() {}
/**
* Creates a pattern for a character sequence.
*
* @param string Character sequence.
*/
public SingleCharacter(char character)
{
setCharacter(character);
}
/**
* Set the sequence of characters for this pattern
*
* @param string Character sequence
*/
public void setCharacter(char character)
{
this.character = character;
}
/**
* Set the sequence of characters for this pattern
*
* @param string Character sequence
*/
public void setCharacterAsString(String character)
{
if (character.startsWith("#"))
this.character = (char)Integer.parseInt(character.substring(1));
else
this.character = character.charAt(0);
}
/**
* Returns the sequence of characters
*
* @return Seqence of characaters
*/
public char getCharacter()
{
return character;
}
public String getCharacterAsString()
{
return String.valueOf(character);
}
public boolean isNullable()
{
return false;
}
public PatternSet getFirstSet()
{
PatternSet set = new PatternSet();
set.addPattern(this);
return set;
}
public PatternSet getLastSet()
{
PatternSet set = new PatternSet();
set.addPattern(this);
return set;
}
public char[] getLimits()
{
return new char[]{getCharacter()};
}
public boolean contains(char minimum, char maximum)
{
return (getCharacter()==minimum) && (getCharacter()==maximum);
}
public boolean contains(char c)
{
return c==character;
}
public String getSymbol()
{
return null;
}
/**
* Return a string representation of this pattern
*
* @return String representation of the pattern.
*/
public String toString()
{
return Decoder.toChar(character)+"["+index+"]";
}
/**
* Create a clone of this pattern.
*
* @return Clone of this pattern.
*
* @throws CloneNotSupportedException If an exception occurs during the cloning.
*/
public Object clone()
{
SingleCharacter clone = new SingleCharacter();
clone.setCharacter(getCharacter());
return clone;
}
/**
* Validates this pattern.
*
* @return Return a list of violations, if this pattern isn't valid.
*/
public Violations validate()
{
Violations violations = new Violations();
/*if (string.length()==0)
violations.addViolation("Character string contains no characters",
getLocation());*/
return violations;
}
}