| 1 |
// Copyright 2003, FreeHEP. |
| 2 |
package org.freehep.util.io; |
| 3 |
|
| 4 |
import java.io.IOException; |
| 5 |
import java.io.InputStream; |
| 6 |
import java.util.NoSuchElementException; |
| 7 |
|
| 8 |
/** |
| 9 |
* The XMLSequence allows parsing by an XML Parser of concatenated XML segments. |
| 10 |
* Each segment needs to start with "<?xml". If the underlying stream contains |
| 11 |
* just one segment "<xml?" may be left out. Methods hasNext() and next() |
| 12 |
* should be called to retrieve the inputstream for the XMLParser. |
| 13 |
* |
| 14 |
* Typical usage: |
| 15 |
* |
| 16 |
* <PRE> |
| 17 |
* |
| 18 |
* XMLSequence sequence = new XMLSequence(new FileInputStream("file.xml")); |
| 19 |
* |
| 20 |
* SAXParserFactory factory = SAXParserFactory.newInstance(); XMLReader |
| 21 |
* xmlReader = factory.newSAXParser().getXMLReader(); |
| 22 |
* |
| 23 |
* while (sequence.hasNext()) { InputStream input = sequence.next(); InputSource |
| 24 |
* source = new InputSource(input); xmlReader.parse(source); input.close(); } |
| 25 |
* sequence.close(); |
| 26 |
* |
| 27 |
* </PRE> |
| 28 |
* |
| 29 |
* IMPORTANT: inherits from InputStream rather than FilterInputStream so that |
| 30 |
* the correct read(byte[], int, int) method is used. |
| 31 |
* |
| 32 |
* @author Mark Donszelmann |
| 33 |
* @version $Id: XMLSequence.java 8584 2006-08-10 23:06:37Z duns $ |
| 34 |
*/ |
| 35 |
public class XMLSequence extends InputStream { |
| 36 |
|
| 37 |
// xml declaration (lowercase!) |
| 38 |
private int[] xml = new int[] { '<', '?', 'x', 'm', 'l' }; |
| 39 |
|
| 40 |
private int xmlIndex; |
| 41 |
|
| 42 |
// look ahead buffer |
| 43 |
private int buffer[] = new int[xml.length]; |
| 44 |
|
| 45 |
private int index; |
| 46 |
|
| 47 |
private boolean bufferEmpty; |
| 48 |
|
| 49 |
// underlying stream |
| 50 |
private InputStream in; |
| 51 |
|
| 52 |
private boolean closed; |
| 53 |
|
| 54 |
private boolean eof; |
| 55 |
|
| 56 |
/** |
| 57 |
* Create a XML Sequence. |
| 58 |
* |
| 59 |
* @param input stream to read from |
| 60 |
*/ |
| 61 |
public XMLSequence(InputStream input) { |
| 62 |
super(); |
| 63 |
in = input; |
| 64 |
closed = false; |
| 65 |
eof = false; |
| 66 |
xmlIndex = 0; |
| 67 |
index = 0; |
| 68 |
bufferEmpty = true; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Is another XML segment available. |
| 73 |
* @return true if another XML segment can be read |
| 74 |
*/ |
| 75 |
public boolean hasNext() { |
| 76 |
if (closed) |
| 77 |
return false; |
| 78 |
|
| 79 |
try { |
| 80 |
return readUntilXMLDeclaration(); |
| 81 |
} catch (IOException e) { |
| 82 |
return false; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Returns the next XML segment. |
| 88 |
* @return stream to read next XML segment from. |
| 89 |
* @throws IOException if read fails |
| 90 |
*/ |
| 91 |
public InputStream next() throws IOException { |
| 92 |
if (eof) |
| 93 |
throw new NoSuchElementException(getClass() + ": at EOF."); |
| 94 |
if (closed) |
| 95 |
throw new NoSuchElementException(getClass() + ": already closed."); |
| 96 |
|
| 97 |
if (!readUntilXMLDeclaration()) |
| 98 |
throw new NoSuchElementException(getClass() |
| 99 |
+ ": No more sequences."); |
| 100 |
|
| 101 |
xmlIndex = 0; |
| 102 |
return new NoCloseInputStream(this); |
| 103 |
} |
| 104 |
|
| 105 |
public int read() throws IOException { |
| 106 |
if (xmlIndex == xml.length) |
| 107 |
return -1; |
| 108 |
if (closed) |
| 109 |
return -1; |
| 110 |
|
| 111 |
int a = -1; |
| 112 |
if (!eof) { |
| 113 |
|
| 114 |
// fill buffer (at start of stream) |
| 115 |
if (bufferEmpty) { |
| 116 |
index = 0; |
| 117 |
for (int i = 0; i < xml.length; i++) { |
| 118 |
int c = in.read(); |
| 119 |
buffer[i] = c; |
| 120 |
if (buffer[i] == xml[xmlIndex]) { |
| 121 |
// we are reading <?xml or < |
| 122 |
xmlIndex++; |
| 123 |
} else if (xmlIndex > 0) { |
| 124 |
// we are reading <..., which is also fine. |
| 125 |
xmlIndex++; |
| 126 |
} else { |
| 127 |
xmlIndex = 0; |
| 128 |
} |
| 129 |
} |
| 130 |
bufferEmpty = false; |
| 131 |
|
| 132 |
if (xmlIndex == xml.length) |
| 133 |
return -1; |
| 134 |
} |
| 135 |
|
| 136 |
// read next character |
| 137 |
a = in.read(); |
| 138 |
if (a != -1) { |
| 139 |
// check if seq |
| 140 |
if (a == xml[xmlIndex]) { |
| 141 |
xmlIndex++; |
| 142 |
} else { |
| 143 |
xmlIndex = 0; |
| 144 |
} |
| 145 |
} else { |
| 146 |
// underlying end-of-file |
| 147 |
eof = true; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
// returned all... |
| 152 |
if (eof && (buffer[index] == -1)) |
| 153 |
return -1; |
| 154 |
|
| 155 |
// replace and return next character |
| 156 |
int b = buffer[index]; |
| 157 |
buffer[index] = a; |
| 158 |
index = (index + 1) % buffer.length; |
| 159 |
return b; |
| 160 |
} |
| 161 |
|
| 162 |
public void mark(int readLimit) { |
| 163 |
in.mark(readLimit); |
| 164 |
} |
| 165 |
|
| 166 |
public boolean markSupported() { |
| 167 |
return in.markSupported(); |
| 168 |
} |
| 169 |
|
| 170 |
public void reset() throws IOException { |
| 171 |
if (closed) |
| 172 |
throw new IOException(getClass() + ": already closed."); |
| 173 |
|
| 174 |
if (!in.markSupported()) |
| 175 |
throw new IOException(getClass() + ": does not support reset()."); |
| 176 |
|
| 177 |
in.reset(); |
| 178 |
xmlIndex = 0; |
| 179 |
index = 0; |
| 180 |
bufferEmpty = true; |
| 181 |
eof = false; |
| 182 |
} |
| 183 |
|
| 184 |
public void close() throws IOException { |
| 185 |
if (!closed) { |
| 186 |
in.close(); |
| 187 |
closed = true; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
private boolean readUntilXMLDeclaration() throws IOException { |
| 192 |
while (read() >= 0) |
| 193 |
; |
| 194 |
return (xmlIndex == xml.length); |
| 195 |
} |
| 196 |
} |