下面列出了怎么用com.fasterxml.jackson.core.format.MatchStrength的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Sub-classes need to override this method
*/
@Override
public MatchStrength hasFormat(InputAccessor acc) throws IOException
{
if (getClass() == MappingJsonFactory.class) {
return hasJSONFormat(acc);
}
return null;
}
private static MatchStrength tryMatch(InputAccessor acc, String matchStr, MatchStrength fullMatchStrength)
throws IOException
{
for (int i = 0, len = matchStr.length(); i < len; ++i) {
if (!acc.hasMoreBytes()) {
return MatchStrength.INCONCLUSIVE;
}
if (acc.nextByte() != matchStr.charAt(i)) {
return MatchStrength.NO_MATCH;
}
}
return fullMatchStrength;
}
/**
* Convenience method for trying to determine whether input via given accessor
* is of format type supported by this factory.
*/
public MatchStrength hasFormat(InputAccessor acc) throws IOException
{
// since we can't keep this abstract, only implement for "vanilla" instance
if (getClass() == JsonFactory.class) {
return hasJSONFormat(acc);
}
return null;
}
private static MatchStrength tryMatch(InputAccessor acc, String matchStr, MatchStrength fullMatchStrength)
throws IOException
{
for (int i = 0, len = matchStr.length(); i < len; ++i) {
if (!acc.hasMoreBytes()) {
return MatchStrength.INCONCLUSIVE;
}
if (acc.nextByte() != matchStr.charAt(i)) {
return MatchStrength.NO_MATCH;
}
}
return fullMatchStrength;
}
/**
* Convenience method for trying to determine whether input via given accessor
* is of format type supported by this factory.
*/
public MatchStrength hasFormat(InputAccessor acc) throws IOException
{
// since we can't keep this abstract, only implement for "vanilla" instance
if (getClass() == JsonFactory.class) {
return hasJSONFormat(acc);
}
return null;
}
@Override
public MatchStrength hasFormat(final InputAccessor acc) throws IOException {
final MatchStrength matchStrength;
if (acc.hasMoreBytes()) {
final byte b = acc.nextByte();
// First byte has to be one of the record types, possibly capital denoting a root object.
// (Only '.' cannot appear at the start of Mincode.)
switch (b) {
case '(':
case 'a':
case 's':
case 't':
case 'f':
case 'n':
case 'd':
case 'j':
matchStrength = MatchStrength.SOLID_MATCH;
break;
default:
matchStrength = MatchStrength.NO_MATCH;
}
} else {
// Zero-length isn't supported.
matchStrength = MatchStrength.NO_MATCH;
}
return matchStrength;
}
/**
* Sub-classes need to override this method (as of 1.8)
*/
@Override
public MatchStrength hasFormat(InputAccessor acc) throws IOException
{
if (!acc.hasMoreBytes()) {
return MatchStrength.INCONCLUSIVE;
}
byte b = acc.nextByte();
// Very first thing, a UTF-8 BOM?
if (b == UTF8_BOM_1) { // yes, looks like UTF-8 BOM
if (!acc.hasMoreBytes()) {
return MatchStrength.INCONCLUSIVE;
}
if (acc.nextByte() != UTF8_BOM_2) {
return MatchStrength.NO_MATCH;
}
if (!acc.hasMoreBytes()) {
return MatchStrength.INCONCLUSIVE;
}
if (acc.nextByte() != UTF8_BOM_3) {
return MatchStrength.NO_MATCH;
}
if (!acc.hasMoreBytes()) {
return MatchStrength.INCONCLUSIVE;
}
b = acc.nextByte();
}
if (b == '{' || Character.isLetter((char) b) || Character.isDigit((char) b)) {
return MatchStrength.WEAK_MATCH;
}
return MatchStrength.INCONCLUSIVE;
}
/**
* Current implementation is not as thorough as other functionality
* ({@link com.fasterxml.jackson.core.json.ByteSourceJsonBootstrapper});
* supports UTF-8, for example. But it should work, for now, and can
* be improved as necessary.
*/
public static MatchStrength hasJSONFormat(InputAccessor acc) throws IOException
{
// Ideally we should see "[" or "{"; but if not, we'll accept double-quote (String)
// in future could also consider accepting non-standard matches?
if (!acc.hasMoreBytes()) {
return MatchStrength.INCONCLUSIVE;
}
byte b = acc.nextByte();
// Very first thing, a UTF-8 BOM?
if (b == UTF8_BOM_1) { // yes, looks like UTF-8 BOM
if (!acc.hasMoreBytes()) {
return MatchStrength.INCONCLUSIVE;
}
if (acc.nextByte() != UTF8_BOM_2) {
return MatchStrength.NO_MATCH;
}
if (!acc.hasMoreBytes()) {
return MatchStrength.INCONCLUSIVE;
}
if (acc.nextByte() != UTF8_BOM_3) {
return MatchStrength.NO_MATCH;
}
if (!acc.hasMoreBytes()) {
return MatchStrength.INCONCLUSIVE;
}
b = acc.nextByte();
}
// Then possible leading space
int ch = skipSpace(acc, b);
if (ch < 0) {
return MatchStrength.INCONCLUSIVE;
}
// First, let's see if it looks like a structured type:
if (ch == '{') { // JSON object?
// Ideally we need to find either double-quote or closing bracket
ch = skipSpace(acc);
if (ch < 0) {
return MatchStrength.INCONCLUSIVE;
}
if (ch == '"' || ch == '}') {
return MatchStrength.SOLID_MATCH;
}
// ... should we allow non-standard? Let's not yet... can add if need be
return MatchStrength.NO_MATCH;
}
MatchStrength strength;
if (ch == '[') {
ch = skipSpace(acc);
if (ch < 0) {
return MatchStrength.INCONCLUSIVE;
}
// closing brackets is easy; but for now, let's also accept opening...
if (ch == ']' || ch == '[') {
return MatchStrength.SOLID_MATCH;
}
return MatchStrength.SOLID_MATCH;
} else {
// plain old value is not very convincing...
strength = MatchStrength.WEAK_MATCH;
}
if (ch == '"') { // string value
return strength;
}
if (ch <= '9' && ch >= '0') { // number
return strength;
}
if (ch == '-') { // negative number
ch = skipSpace(acc);
if (ch < 0) {
return MatchStrength.INCONCLUSIVE;
}
return (ch <= '9' && ch >= '0') ? strength : MatchStrength.NO_MATCH;
}
// or one of literals
if (ch == 'n') { // null
return tryMatch(acc, "ull", strength);
}
if (ch == 't') { // true
return tryMatch(acc, "rue", strength);
}
if (ch == 'f') { // false
return tryMatch(acc, "alse", strength);
}
return MatchStrength.NO_MATCH;
}
/**
* Helper method that can be called to determine if content accessed
* using given accessor seems to be JSON content.
*/
protected MatchStrength hasJSONFormat(InputAccessor acc) throws IOException
{
return ByteSourceJsonBootstrapper.hasJSONFormat(acc);
}
/**
* Creates a single data input stream from either file or InputStream depending on which one is present. Preference
* for file if both present. Additionally also detects input data format of the result stream, throws
* IllegalArgumentException if unable to determine data format.
*
* @param stream InputStream of the data if present
* @param inputFile path to file to use as input
* @param addRootElementToXml to add vespafeed root element around the input data stream
* @throws IOException on errors
*/
public FormatInputStream(InputStream stream, Optional<String> inputFile, boolean addRootElementToXml)
throws IOException {
DataFormatDetector dataFormatDetector = new DataFormatDetector(new JsonFactory(), new XmlFactory());
DataFormatMatcher formatMatcher;
if (inputFile.isPresent()) {
try (FileInputStream fileInputStream = new FileInputStream(inputFile.get())) {
formatMatcher = dataFormatDetector.findFormat(fileInputStream);
}
inputStream = new FileInputStream(inputFile.get());
} else {
if (stream.available() == 0)
System.out.println("No data in stream yet and no file specified, waiting for data.");
inputStream = stream.markSupported() ? stream : new BufferedInputStream(stream);
inputStream.mark(DataFormatDetector.DEFAULT_MAX_INPUT_LOOKAHEAD);
formatMatcher = dataFormatDetector.findFormat(inputStream);
inputStream.reset();
}
if (addRootElementToXml) {
inputStream = addVespafeedTag(inputStream);
format = Format.XML;
return;
}
if (formatMatcher.getMatchStrength() == MatchStrength.INCONCLUSIVE
|| formatMatcher.getMatchStrength() == MatchStrength.NO_MATCH) {
throw new IllegalArgumentException("Could not detect input format");
}
switch (formatMatcher.getMatchedFormatName().toLowerCase()) {
case "json":
format = Format.JSON;
break;
case "xml":
format = Format.XML;
break;
default:
throw new IllegalArgumentException("Unknown data format");
}
}
/**
* Current implementation is not as thorough as other functionality
* ({@link com.fasterxml.jackson.core.json.ByteSourceJsonBootstrapper});
* supports UTF-8, for example. But it should work, for now, and can
* be improved as necessary.
*/
public static MatchStrength hasJSONFormat(InputAccessor acc) throws IOException
{
// Ideally we should see "[" or "{"; but if not, we'll accept double-quote (String)
// in future could also consider accepting non-standard matches?
if (!acc.hasMoreBytes()) {
return MatchStrength.INCONCLUSIVE;
}
byte b = acc.nextByte();
// Very first thing, a UTF-8 BOM?
if (b == UTF8_BOM_1) { // yes, looks like UTF-8 BOM
if (!acc.hasMoreBytes()) {
return MatchStrength.INCONCLUSIVE;
}
if (acc.nextByte() != UTF8_BOM_2) {
return MatchStrength.NO_MATCH;
}
if (!acc.hasMoreBytes()) {
return MatchStrength.INCONCLUSIVE;
}
if (acc.nextByte() != UTF8_BOM_3) {
return MatchStrength.NO_MATCH;
}
if (!acc.hasMoreBytes()) {
return MatchStrength.INCONCLUSIVE;
}
b = acc.nextByte();
}
// Then possible leading space
int ch = skipSpace(acc, b);
if (ch < 0) {
return MatchStrength.INCONCLUSIVE;
}
// First, let's see if it looks like a structured type:
if (ch == '{') { // JSON object?
// Ideally we need to find either double-quote or closing bracket
ch = skipSpace(acc);
if (ch < 0) {
return MatchStrength.INCONCLUSIVE;
}
if (ch == '"' || ch == '}') {
return MatchStrength.SOLID_MATCH;
}
// ... should we allow non-standard? Let's not yet... can add if need be
return MatchStrength.NO_MATCH;
}
MatchStrength strength;
if (ch == '[') {
ch = skipSpace(acc);
if (ch < 0) {
return MatchStrength.INCONCLUSIVE;
}
// closing brackets is easy; but for now, let's also accept opening...
if (ch == ']' || ch == '[') {
return MatchStrength.SOLID_MATCH;
}
return MatchStrength.SOLID_MATCH;
} else {
// plain old value is not very convincing...
strength = MatchStrength.WEAK_MATCH;
}
if (ch == '"') { // string value
return strength;
}
if (ch <= '9' && ch >= '0') { // number
return strength;
}
if (ch == '-') { // negative number
ch = skipSpace(acc);
if (ch < 0) {
return MatchStrength.INCONCLUSIVE;
}
return (ch <= '9' && ch >= '0') ? strength : MatchStrength.NO_MATCH;
}
// or one of literals
if (ch == 'n') { // null
return tryMatch(acc, "ull", strength);
}
if (ch == 't') { // true
return tryMatch(acc, "rue", strength);
}
if (ch == 'f') { // false
return tryMatch(acc, "alse", strength);
}
return MatchStrength.NO_MATCH;
}
/**
* Helper method that can be called to determine if content accessed
* using given accessor seems to be JSON content.
*/
protected MatchStrength hasJSONFormat(InputAccessor acc) throws IOException
{
return ByteSourceJsonBootstrapper.hasJSONFormat(acc);
}