return underlyingFactory.fieldType( field );
}
public Document getDocument( final InputStream rawContent, final Reference2ObjectMap<Enum<?>,Object> metadata ) throws IOException {
return new AbstractDocument() {
final DataInputStream rawContentDataInputStream = new DataInputStream( rawContent );
int nextFieldToRead = 0;
final MutableString uri = new MutableString();
{
uri.readSelfDelimUTF8( rawContent ).compact();
}
@Override
public void close() throws IOException {
super.close();
rawContent.close();
}
public CharSequence title() {
return (CharSequence)metadata.get( MetadataKeys.TITLE );
}
public String toString() {
return title().toString();
}
public CharSequence uri() {
return uri.length() == 0 ? null : uri;
}
/** Skips until the end of the current field, and increments <code>nextFieldToRead</code>.
* @throws ClassNotFoundException
* @throws IOException
*/
private void skipOneField() throws IOException, ClassNotFoundException {
switch( fieldType( nextFieldToRead ) ) {
case TEXT:
MutableString word = new MutableString();
MutableString nonWord = new MutableString();
do {
word.readSelfDelimUTF8( rawContent );
if ( exact ) nonWord.readSelfDelimUTF8( rawContent );
} while ( word.length() > 0 || ( exact && nonWord.length() > 0 ) );
break;
case VIRTUAL:
final int nfrag = rawContentDataInputStream.readInt();
for ( int i = 0; i < 2 * nfrag; i++ ) MutableString.skipSelfDelimUTF8( rawContent );
break;
default: // Non-text and non-virtual
new ObjectInputStream( rawContent ).readObject();
}
nextFieldToRead++;
}
/** Skips to the given field.
*
* @param field the field to skip to.
* @throws IOException
* @throws ClassNotFoundException
*/
private void skipToField( final int field ) throws IOException, ClassNotFoundException {
if ( nextFieldToRead > field ) throw new IllegalStateException( "Trying to skip to field " + field + " after " + nextFieldToRead );
while ( nextFieldToRead < field ) skipOneField();
}
public Object content( final int field ) {
ensureFieldIndex( field );
Object result = null;
if ( DEBUG ) LOGGER.debug( "Called content(" + field + "); nextField:" + nextFieldToRead );
try {
skipToField( field );
if ( fieldType( nextFieldToRead ) == FieldType.VIRTUAL ) {
final int nfrag = rawContentDataInputStream.readInt();
MutableString doc = new MutableString();
MutableString text = new MutableString();
VirtualDocumentFragment[] fragArray = new VirtualDocumentFragment[ nfrag ];
for ( int i = 0; i < nfrag; i++ ) {
doc.readSelfDelimUTF8( rawContent );