1 /*
   2  * Copyright (c) 2005, 2010, Thai Open Source Software Center Ltd
   3  * All rights reserved.
   4  *
   5  * Redistribution and use in source and binary forms, with or without
   6  * modification, are permitted provided that the following conditions are
   7  * met:
   8  *
   9  *     Redistributions of source code must retain the above copyright
  10  *     notice, this list of conditions and the following disclaimer.
  11  *
  12  *     Redistributions in binary form must reproduce the above copyright
  13  *     notice, this list of conditions and the following disclaimer in
  14  *     the documentation and/or other materials provided with the
  15  *     distribution.
  16  *
  17  *     Neither the name of the Thai Open Source Software Center Ltd nor
  18  *     the names of its contributors may be used to endorse or promote
  19  *     products derived from this software without specific prior written
  20  *     permission.
  21  *
  22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
  26  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  29  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33  */
  34 
  35 package com.sun.xml.internal.org.relaxng.datatype.helpers;
  36 
  37 import com.sun.xml.internal.org.relaxng.datatype.*;
  38 
  39 /**
  40  * Dummy implementation of {@link DatatypeStreamingValidator}.
  41  *
  42  * <p>
  43  * This implementation can be used as a quick hack when the performance
  44  * of streaming validation is not important. And this implementation
  45  * also shows you how to implement the DatatypeStreamingValidator interface.
  46  *
  47  * <p>
  48  * Typical usage would be:
  49  * <PRE><XMP>
  50  * class MyDatatype implements Datatype {
  51  *     ....
  52  *     public DatatypeStreamingValidator createStreamingValidator( ValidationContext context ) {
  53  *         return new StreamingValidatorImpl(this,context);
  54  *     }
  55  *     ....
  56  * }
  57  * </XMP></PRE>
  58  *
  59  * @author <a href="mailto:kohsuke.kawaguchi@sun.com">Kohsuke KAWAGUCHI</a>
  60  */
  61 public final class StreamingValidatorImpl implements DatatypeStreamingValidator {
  62 
  63         /** This buffer accumulates characters. */
  64         private final StringBuffer buffer = new StringBuffer();
  65 
  66         /** Datatype obejct that creates this streaming validator. */
  67         private final Datatype baseType;
  68 
  69         /** The current context. */
  70         private final ValidationContext context;
  71 
  72         public void addCharacters( char[] buf, int start, int len ) {
  73                 // append characters to the current buffer.
  74                 buffer.append(buf,start,len);
  75         }
  76 
  77         public boolean isValid() {
  78                 return baseType.isValid(buffer.toString(),context);
  79         }
  80 
  81         public void checkValid() throws DatatypeException {
  82                 baseType.checkValid(buffer.toString(),context);
  83         }
  84 
  85         public StreamingValidatorImpl( Datatype baseType, ValidationContext context ) {
  86                 this.baseType = baseType;
  87                 this.context = context;
  88         }
  89 }