blob: c501fbfc5000295b39a6667bb59686491fb53d41 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.http.server;
import com.yahoo.document.DocumentTypeManager;
import com.yahoo.document.json.JsonFeedReader;
import com.yahoo.text.Utf8;
import com.yahoo.vespaxmlparser.FeedReader;
import com.yahoo.vespaxmlparser.VespaXMLFeedReader;
import java.io.InputStream;
/**
* Class for creating FeedReader based on dataFormat.
* @author dybis
*/
public class FeedReaderFactory {
private static final int MARK_READLIMIT = 200;
private final boolean debug;
public FeedReaderFactory(boolean debug) {
this.debug = debug;
}
/**
* Creates FeedReader
* @param inputStream source of feed data
* @param docTypeManager handles the parsing of the document
* @param dataFormat specifies the format
* @return a feedreader
*/
public FeedReader createReader(
InputStream inputStream,
DocumentTypeManager docTypeManager,
FeedParams.DataFormat dataFormat) {
switch (dataFormat) {
case XML_UTF8:
byte [] peek = null;
int bytesPeeked = 0;
try {
if (debug && inputStream.markSupported()) {
peek = new byte[MARK_READLIMIT];
inputStream.mark(MARK_READLIMIT);
bytesPeeked = inputStream.read(peek);
inputStream.reset();
}
return new VespaXMLFeedReader(inputStream, docTypeManager);
} catch (Exception e) {
if (bytesPeeked > 0) {
throw new RuntimeException("Could not create VespaXMLFeedReader. First characters are: '" + Utf8.toString(peek, 0, bytesPeeked) + "'", e);
} else {
throw new RuntimeException("Could not create VespaXMLFeedReader.", e);
}
}
case JSON_UTF8:
return new JsonFeedReader(inputStream, docTypeManager);
default:
throw new IllegalStateException("Can not create feed reader for format: " + dataFormat);
}
}
}
|