1 package org.jical; 2 3 11 import java.io.Reader ; 12 import java.util.Iterator ; 13 import java.util.List ; 14 import java.util.ArrayList ; 15 16 public class UnfoldingLineIterator implements Iterator { 17 18 private Iterator m_iterator; 19 private List m_lines = new ArrayList (); 20 21 public UnfoldingLineIterator( Reader reader ) { 22 this( new LineIterator( reader ) ); 23 } 24 public UnfoldingLineIterator( Iterator iterator ) { 25 m_iterator = iterator; 26 } 27 28 public boolean hasNext() { 29 checkLines(); 30 return ( !m_lines.isEmpty() ); 31 } 32 public Object next() { 33 if ( hasNext() ) { 34 return m_lines.remove( 0 ); 35 } 36 return null; 37 } 38 public void remove() throws UnsupportedOperationException { 39 throw new UnsupportedOperationException (); 40 } 41 42 43 44 private void checkLines() { 45 synchronized ( m_lines ) { 46 if ( m_lines.size() < 2 && m_iterator.hasNext() ) { 47 StringBuffer line = (StringBuffer ) m_iterator.next(); 48 if ( line != null ) { 49 m_lines.add( line ); 50 unfoldLines(); 51 checkLines(); 52 } 53 } 54 } 55 } 56 57 private void unfoldLines() { 58 synchronized ( m_lines ) { 59 int i = 1; 60 while ( i < m_lines.size() ) { 61 StringBuffer line = (StringBuffer ) m_lines.get( i ); 62 char c = line.charAt( 0 ); 63 if ( c == ' ' || c == '\t' ) { 64 m_lines.remove( i ); 65 StringBuffer pline = (StringBuffer ) m_lines.get( i - 1 ); 66 line.deleteCharAt( 0 ); 67 pline.append( line ); 68 } else { 69 i++; 70 } 71 } 72 } 73 } 74 75 76 } 77 | Popular Tags |