001         package com.croftsoft.core.text.xml;
002    
003         import java.io.*;
004    
005         /*********************************************************************
006         * Filters out characters not permitted in XML.
007         *
008         * @see
009         *   <a target="_blank" href="http://www.w3.org/TR/REC-xml#charsets">
010         *   Extensible Markup Language (XML) 1.0 (Second Edition),
011         *   2.2 Characters</a>
012         *
013         * @version
014         *   2002-09-24
015         * @since
016         *   2002-09-19
017         * @author
018         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
019         *********************************************************************/
020    
021         public final class  XmlCharFilterReader
022           extends FilterReader
023         //////////////////////////////////////////////////////////////////////
024         //////////////////////////////////////////////////////////////////////
025         {
026    
027         /*********************************************************************
028         * Main constructor.
029         *********************************************************************/
030         public  XmlCharFilterReader ( Reader  in )
031         //////////////////////////////////////////////////////////////////////
032         {
033           super ( in );
034         }
035    
036         //////////////////////////////////////////////////////////////////////
037         //////////////////////////////////////////////////////////////////////
038    
039         public int  read ( )
040           throws IOException
041         //////////////////////////////////////////////////////////////////////
042         {
043           int  ch;
044    
045           while ( ( ch = super.read ( ) ) > -1 )
046           {
047             if ( ( ch == 0x9 )
048               || ( ch == 0xA )
049               || ( ch == 0xD )
050               || ( ( ch >= 0x00020 ) && ( ch <= 0x00D7FF ) )
051               || ( ( ch >= 0x0E000 ) && ( ch <= 0x00FFFD ) )
052               || ( ( ch >= 0x10000 ) && ( ch <= 0x10FFFF ) ) )
053             {
054               return ch;
055             }
056           }
057    
058           return -1;
059         }
060    
061         public int  read (
062           char [ ]  cbuf,
063           int       off,
064           int       len )
065           throws IOException
066         //////////////////////////////////////////////////////////////////////
067         {
068           int  count = 0;
069    
070           for ( int  i = 0; i < len; i++ )
071           {
072             int  ch = read ( );
073    
074             if ( ch < 0 )
075             {
076               return count > 0 ? count : -1;
077             }
078    
079             cbuf [ off + count ] = ( char ) ch;
080    
081             count++;
082           }
083    
084           return len;
085         }
086    
087         public int  read ( char [ ]  cbuf )
088           throws IOException
089         //////////////////////////////////////////////////////////////////////
090         {
091           return read ( cbuf, 0, cbuf.length );
092         }
093    
094         //////////////////////////////////////////////////////////////////////
095         //////////////////////////////////////////////////////////////////////
096         }