| Home >> All >> com >> sitemesh >> [ parser Javadoc ] |
Source code: com/sitemesh/parser/AbstractPageParser.java
1 package com.sitemesh.parser; 2 3 import com.sitemesh.Page; 4 import com.sitemesh.PageParser; 5 import java.io.*; 6 import org.xml.sax.InputSource; 7 8 /** 9 * Abstract implementation of {@link com.sitemesh.PageParser}. 10 * All methods convert input into <code>byte[]</code>, meaning only that 11 * method need be implemented. 12 * 13 * @author <a href="joe@truemesh.com">Joe Walnes</a> 14 * @version $Revision: 1.5 $ 15 * 16 * @see com.sitemesh.PageParser 17 */ 18 public abstract class AbstractPageParser implements PageParser { 19 20 /** 21 * Method to be implemented by sub-classes. 22 * 23 * @see com.sitemesh.PageParser 24 */ 25 public abstract Page parse( byte[] data ) throws IOException; 26 27 public Page parse( InputStream in ) throws IOException { 28 ByteArrayOutputStream data = new ByteArrayOutputStream(); 29 while ( true ) { 30 int i = in.read(); 31 if ( i == -1 ) break; 32 data.write( i ); 33 } 34 return parse( data.toByteArray() ); // byte[] 35 } 36 37 public Page parse( Reader in ) throws IOException { 38 CharArrayWriter data = new CharArrayWriter(); 39 while ( true ) { 40 int i = in.read(); 41 if ( i == -1 ) break; 42 data.write( i ); 43 } 44 return parse( data.toCharArray() ); // char[] 45 } 46 47 public Page parse( InputSource in ) throws IOException { 48 if ( in.getCharacterStream() != null ) { 49 return parse( in.getCharacterStream() ); // Reader 50 } 51 else if ( in.getByteStream() != null ) { 52 return parse( in.getByteStream() ); // InputStream 53 } 54 else { 55 throw new IllegalStateException( "InputSource must contain Character-Stream or Byte-Stream" ); 56 } 57 } 58 59 public Page parse( char[] data ) throws IOException { 60 ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 61 new OutputStreamWriter( bytes ).write( data ); 62 return parse( bytes.toByteArray() ); // byte[] 63 } 64 65 }