source: gli/branches/rtl-gli/src/org/greenstone/gatherer/util/RemoveContentBeforeRootElementXMLReader.java@ 18353

Last change on this file since 18353 was 18353, checked in by kjdon, 15 years ago

updated the rtl-gli branch with files from trunk. Result of a merge 14807:18318

File size: 1.9 KB
Line 
1package org.greenstone.gatherer.util;
2
3import java.io.Reader;
4import java.io.IOException;
5
6public class RemoveContentBeforeRootElementXMLReader extends Reader {
7
8 static final byte[] xmlIndicator = "<?xml".getBytes();
9
10 Reader ur;
11 int foundBytesReturned = 0;
12 boolean allFoundBytesReturned = false; //redundant, but may help performance
13
14
15 public RemoveContentBeforeRootElementXMLReader( Reader ur ) {
16
17 this.ur = ur;
18
19 //read up to the xml indicator
20 int foundBytes = 0;
21 int c = 0;
22 while ( c != -1 ) {
23 try {
24 c = ur.read();
25 } catch( Exception e ) {
26 System.err.println( "Exception while reading underlying Reader in RemoveContentBeforeRootElementXMLReader" );
27 }
28
29 if ( c == (int)xmlIndicator[foundBytes] ) {
30 foundBytes++;
31 } else {
32 if ( foundBytes != 0 ) {
33 for ( int i=0; i<foundBytes; i++ ) {
34 System.out.print( (char)xmlIndicator[i] );
35 }
36 foundBytes = 0;
37 }
38 if ( c != -1 ) {
39 System.out.print( (char)c );
40 }
41 }
42
43 if ( foundBytes == xmlIndicator.length ) {
44 return;
45 }
46
47 }
48
49 System.err.println( "RemoveContentBeforeRootElementXMLReader:\n" +
50 "The XML being loaded did not contain the '<?xml' string as expected" );
51 }
52
53 public int read( char[] cbuf, int off, int len ) throws IOException {
54
55 for ( int i=off; i<off+len && i<cbuf.length; i++ ) {
56
57 //read from underlying reader
58 int c = read();
59
60 //catch end of stream
61 if ( c == -1 ) {
62 if ( i == off ) {
63 return -1;
64 }
65 return i - off;
66 }
67
68 //insert character into the array
69 cbuf[i] = (char)c;
70 }
71 return len;
72
73 }
74
75 public int read() throws IOException {
76
77 if ( allFoundBytesReturned ) {
78 return ur.read();
79 }
80
81 if ( foundBytesReturned < xmlIndicator.length ) {
82 return xmlIndicator[foundBytesReturned++];
83 }
84
85 allFoundBytesReturned = true;
86 return ur.read();
87 }
88
89 public void close() throws IOException {
90 ur.close();
91 }
92}
Note: See TracBrowser for help on using the repository browser.