001: package test.it.unimi.dsi.mg4j.document;
002:
003: /*
004: * MG4J: Managing Gigabytes for Java
005: *
006: * Copyright (C) 2006-2007 Sebastiano Vigna
007: *
008: * This library is free software; you can redistribute it and/or modify it
009: * under the terms of the GNU Lesser General Public License as published by the Free
010: * Software Foundation; either version 2.1 of the License, or (at your option)
011: * any later version.
012: *
013: * This library is distributed in the hope that it will be useful, but
014: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
015: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
016: * for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: *
022: */
023:
024: import it.unimi.dsi.fastutil.objects.Reference2ObjectMap;
025: import it.unimi.dsi.io.FastBufferedReader;
026: import it.unimi.dsi.io.NullReader;
027: import it.unimi.dsi.io.WordReader;
028: import it.unimi.dsi.mg4j.document.AbstractDocument;
029: import it.unimi.dsi.mg4j.document.Document;
030: import it.unimi.dsi.mg4j.document.PropertyBasedDocumentFactory;
031: import it.unimi.dsi.util.Properties;
032:
033: import java.io.CharArrayReader;
034: import java.io.IOException;
035: import java.io.InputStream;
036:
037: /** A factory (with a single text field named <samp>text</samp>) that reads the first byte
038: * in the input stream and returns it as a character.
039: */
040:
041: @SuppressWarnings("unused")
042: public class FirstByteDocumentFactory extends
043: PropertyBasedDocumentFactory {
044: private static final long serialVersionUID = 1L;
045:
046: public FirstByteDocumentFactory() {
047: }
048:
049: public FirstByteDocumentFactory(
050: final Reference2ObjectMap<Enum<?>, Object> defaultMetadata) {
051: }
052:
053: public FirstByteDocumentFactory(final Properties properties) {
054: }
055:
056: public FirstByteDocumentFactory(final String[] property) {
057: }
058:
059: public FirstByteDocumentFactory copy() {
060: return this ;
061: }
062:
063: public int numberOfFields() {
064: return 1;
065: }
066:
067: public String fieldName(final int field) {
068: if (field != 0)
069: throw new IndexOutOfBoundsException();
070: return "text";
071: }
072:
073: public int fieldIndex(final String fieldName) {
074: return fieldName.equals("text") ? 0 : -1;
075: }
076:
077: public FieldType fieldType(final int field) {
078: return FieldType.TEXT;
079: }
080:
081: public Document getDocument(final InputStream rawContent,
082: final Reference2ObjectMap<Enum<?>, Object> metadata) {
083: return new AbstractDocument() {
084:
085: public CharSequence title() {
086: return "dummy title";
087: }
088:
089: public String toString() {
090: return title().toString();
091: }
092:
093: public CharSequence uri() {
094: return "dummy://";
095: }
096:
097: public Object content(final int field) throws IOException {
098: if (field != 0)
099: throw new IndexOutOfBoundsException();
100: int firstByte = rawContent.read();
101: if (firstByte == -1)
102: return NullReader.getInstance();
103: return new CharArrayReader(
104: new char[] { (char) firstByte });
105: }
106:
107: public WordReader wordReader(final int field) {
108: return new FastBufferedReader();
109: }
110: };
111: }
112: }
|