1 package org.relaxng.datatype.helpers;
   2 
   3 import org.relaxng.datatype.*;
   4 
   5 /**
   6  * Dummy implementation of {@link DatatypeStreamingValidator}.
   7  *
   8  * <p>
   9  * This implementation can be used as a quick hack when the performance
  10  * of streaming validation is not important. And this implementation
  11  * also shows you how to implement the DatatypeStreamingValidator interface.
  12  *
  13  * <p>
  14  * Typical usage would be:
  15  * <PRE><XMP>
  16  * class MyDatatype implements Datatype {
  17  *     ....
  18  *     public DatatypeStreamingValidator createStreamingValidator( ValidationContext context ) {
  19  *         return new StreamingValidatorImpl(this,context);
  20  *     }
  21  *     ....
  22  * }
  23  * </XMP></PRE>
  24  *
  25  * @author <a href="mailto:kohsuke.kawaguchi@sun.com">Kohsuke KAWAGUCHI</a>
  26  */
  27 public final class StreamingValidatorImpl implements DatatypeStreamingValidator {
  28 
  29         /** This buffer accumulates characters. */
  30         private final StringBuffer buffer = new StringBuffer();
  31 
  32         /** Datatype obejct that creates this streaming validator. */
  33         private final Datatype baseType;
  34 
  35         /** The current context. */
  36         private final ValidationContext context;
  37 
  38         public void addCharacters( char[] buf, int start, int len ) {
  39                 // append characters to the current buffer.
  40                 buffer.append(buf,start,len);
  41         }
  42 
  43         public boolean isValid() {
  44                 return baseType.isValid(buffer.toString(),context);
  45         }
  46 
  47         public void checkValid() throws DatatypeException {
  48                 baseType.checkValid(buffer.toString(),context);
  49         }
  50 
  51         public StreamingValidatorImpl( Datatype baseType, ValidationContext context ) {
  52                 this.baseType = baseType;
  53                 this.context = context;
  54         }
  55 }