001    package org.relaxng.datatype.helpers;
002    
003    import org.relaxng.datatype.*;
004    
005    /**
006     * Dummy implementation of {@link DatatypeStreamingValidator}.
007     * 
008     * <p>
009     * This implementation can be used as a quick hack when the performance
010     * of streaming validation is not important. And this implementation
011     * also shows you how to implement the DatatypeStreamingValidator interface.
012     * 
013     * <p>
014     * Typical usage would be:
015     * <PRE><XMP>
016     * class MyDatatype implements Datatype {
017     *     ....
018     *     public DatatypeStreamingValidator createStreamingValidator( ValidationContext context ) {
019     *         return new StreamingValidatorImpl(this,context);
020     *     }
021     *     ....
022     * }
023     * </XMP></PRE>
024     * 
025     * @author <a href="mailto:kohsuke.kawaguchi@sun.com">Kohsuke KAWAGUCHI</a>
026     */
027    public final class StreamingValidatorImpl implements DatatypeStreamingValidator {
028            
029            /** This buffer accumulates characters. */
030            private final StringBuffer buffer = new StringBuffer();
031            
032            /** Datatype obejct that creates this streaming validator. */
033            private final Datatype baseType;
034            
035            /** The current context. */
036            private final ValidationContext context;
037            
038            public void addCharacters( char[] buf, int start, int len ) {
039                    // append characters to the current buffer.
040                    buffer.append(buf,start,len);
041            }
042            
043            public boolean isValid() {
044                    return baseType.isValid(buffer.toString(),context);
045            }
046            
047            public void checkValid() throws DatatypeException {
048                    baseType.checkValid(buffer.toString(),context);
049            }
050            
051            public StreamingValidatorImpl( Datatype baseType, ValidationContext context ) {
052                    this.baseType = baseType;
053                    this.context = context;
054            }
055    }