at.spardat.enterprise.fmt.CompoundValidator Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* s IT Solutions AT Spardat GmbH - initial API and implementation
*******************************************************************************/
package at.spardat.enterprise.fmt;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Iterator;
import java.util.List;
/**
* Compound validator, which manages a chain of validators.
* It delegates all validation checks to a the validators contained in its chain.
* The input is accepted, if it is accepted by all enabled validators in the chain.
* If an formatter is explicitly set for formatting, it is used. Otherwise the
* first validator in the chain is used for formatting.
*
* @author gub
*/
public class CompoundValidator extends IFmt {
// list containing all IFmts used for validation
List chain = new ArrayList();
// enabled/disabled switches for the IFmts in chain
// each bit controls the IFmt with the corresponding index in chain
BitSet switches = new BitSet();
// IFmt used for formatting
IFmt formatter;
/**
* Creates a CompoundValidator with the given chain of validators.
* The chain must contain only IFmt objects.
* More validators can be added later on.
*/
public CompoundValidator(List chain) {
setChain(chain);
}
/**
* Creates a CompoundValidator with one validator in its chain.
* More validators can be added later on.
*/
public CompoundValidator(IFmt validator) {
addToChain(validator);
}
/**
* Creates a CompoundValidator with two validators in its chain.
* More validators can be added later on.
*/
public CompoundValidator(IFmt validator1,IFmt validator2) {
addToChain(validator1);
addToChain(validator2);
}
/**
* Transforms an internal encoding to an external.
* If a formatter is explicitly set by {@link #setFormatter(IFmt)},
* this formatter is used for formatting. Otherwise the first validatior
* of the chain is used.
*
* @param internal the provided internal encoding. This String must satisfy the
* condition isLegalInternal(), otherwise the behaviour of this
* method is undefined.
* @return the external representation. May be the empty string, but is never null.
*/
public String format(String internal) {
return getFmtUsedForFormatting().format(internal);
}
/**
* An IFmt is one way, if the external encoding may be generated out of
* the internal, but not vice versa. A CompoundValidator is oneWay, if the
* formatter is oneWay. (set explicitly or first of chain)
*
* @return true, if format may be called, but parse not.
*/
public boolean isOneWay() {
return getFmtUsedForFormatting().isOneWay();
}
/**
* Tries to transform an external encoding to an internal. Must not be called if
* isOneWay() yields true.
* Every enabled validator in the chain tries to parse the string. A AParseException is
* thrown if any of the enabled validators in the chain fails. Disabled validators in
* the chain are ignored. The result is the result of the used formatter.
*
* @param external the external encoding
* @return the internal encoding. May be the empty String, but never is null.
* @exception AParseException if the external encoding cannot be successfully parsed.
*/
public String parse(String external) throws AParseException {
for(int i=0;i