com.netflix.msl.io.MslEncoderFactory Maven / Gradle / Ivy
/**
* Copyright (c) 2015-2017 Netflix, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.msl.io;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import com.netflix.msl.util.Base64;
/**
* An abstract factory class for producing {@link MslTokener},
* {@link MslObject}, and {@link MslArray} instances of various encoder
* formats.
*
* A concrete implementations must identify its supported and preferred
* encoder formats and provide implementations for encoding and decoding those
* formats.
*
* @author Wesley Miaw
*/
public abstract class MslEncoderFactory {
/**
* Escape and quote a string for print purposes.
*
* This is based on the org.json {@code MslObject.quote()} code.
*
* @param string the string to quote. May be {@code null}.
* @return the quoted string.
*/
static String quote(final String string) {
final StringBuilder sb = new StringBuilder();
// Return "" for null or zero-length string.
if (string == null || string.length() == 0) {
sb.append("\"\"");
return sb.toString();
}
char c = 0;
final int len = string.length();
sb.append('"');
for (int i = 0; i < len; i += 1) {
final char b = c;
c = string.charAt(i);
switch (c) {
case '\\':
case '"':
sb.append('\\');
sb.append(c);
break;
case '/':
if (b == '<') {
sb.append('\\');
}
sb.append(c);
break;
case '\b':
sb.append("\\b");
break;
case '\t':
sb.append("\\t");
break;
case '\n':
sb.append("\\n");
break;
case '\f':
sb.append("\\f");
break;
case '\r':
sb.append("\\r");
break;
default:
if (c < ' ' || (c >= '\u0080' && c < '\u00a0')
|| (c >= '\u2000' && c < '\u2100'))
{
sb.append("\\u");
final String hhhh = Integer.toHexString(c);
sb.append("0000", 0, 4 - hhhh.length());
sb.append(hhhh);
} else {
sb.append(c);
}
}
}
sb.append('"');
return sb.toString();
}
/**
* Convert a value to a string for print purposes.
*
* This is based on the org.json {@code MslObject.writeValue()} code.
*
* @param value the value to convert to a string. May be {@code null}.
* @return the string.
*/
@SuppressWarnings("unchecked")
static String stringify(final Object value) {
if (value == null || value.equals(null)) {
return "null";
} else if (value instanceof MslObject || value instanceof MslArray) {
return value.toString();
} else if (value instanceof Map) {
return new MslObject((Map,?>)value).toString();
} else if (value instanceof Collection) {
return new MslArray((Collection
© 2015 - 2024 Weber Informatics LLC | Privacy Policy