package com.fasterxml.jackson.dataformat.javaprop;
import java.io.*;
import java.net.URL;
import java.util.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.format.InputAccessor;
import com.fasterxml.jackson.core.format.MatchStrength;
import com.fasterxml.jackson.core.io.IOContext;
import com.fasterxml.jackson.dataformat.javaprop.impl.PropertiesBackedGenerator;
import com.fasterxml.jackson.dataformat.javaprop.impl.WriterBackedGenerator;
import com.fasterxml.jackson.dataformat.javaprop.io.Latin1Reader;
@SuppressWarnings("resource")
public class JavaPropsFactory extends JsonFactory
{
private static final long serialVersionUID = 1L;
public final static String FORMAT_NAME_JAVA_PROPERTIES = "java_properties";
protected final static String CHARSET_ID_LATIN1 = "ISO-8859-1";
public JavaPropsFactory() { }
public JavaPropsFactory(ObjectCodec codec) {
super(codec);
}
protected JavaPropsFactory(JavaPropsFactory src, ObjectCodec oc)
{
super(src, oc);
}
protected JavaPropsFactory(JavaPropsFactoryBuilder b)
{
super(b, false);
}
@Override
public JavaPropsFactoryBuilder rebuild() {
return new JavaPropsFactoryBuilder(this);
}
public static JavaPropsFactoryBuilder builder() {
return new JavaPropsFactoryBuilder();
}
@Override
public JavaPropsFactory copy()
{
_checkInvalidCopy(JavaPropsFactory.class);
return new JavaPropsFactory(this, null);
}
@Override
public Version version() {
return PackageVersion.VERSION;
}
@Override
public String getFormatName() {
return FORMAT_NAME_JAVA_PROPERTIES;
}
@Override
public MatchStrength hasFormat(InputAccessor acc) throws IOException
{
return MatchStrength.INCONCLUSIVE;
}
@Override
public boolean requiresPropertyOrdering() {
return false;
}
@Override
public boolean canHandleBinaryNatively() {
return false;
}
@Override
public boolean canUseCharArrays() { return false; }
@Override
public boolean canUseSchema(FormatSchema schema) {
return schema instanceof JavaPropsSchema;
}
public JavaPropsParser createParser(Map<?,?> content) {
return new JavaPropsParser(_createContext(_createContentReference(content), true),
_parserFeatures, content, _objectCodec, content);
}
@Deprecated
public JavaPropsParser createParser(Properties props) {
return new JavaPropsParser(_createContext(_createContentReference(props), true),
_parserFeatures, props, _objectCodec, props);
}
@Deprecated
public JavaPropsGenerator createGenerator(Properties props) {
IOContext ctxt = _createContext(_createContentReference(props), true);
return new PropertiesBackedGenerator(ctxt,
props, _generatorFeatures, _objectCodec);
}
public JavaPropsGenerator createGenerator(Map<?,?> target, JavaPropsSchema schema) {
IOContext ctxt = _createContext(_createContentReference(target), true);
return new PropertiesBackedGenerator(ctxt,
target, _generatorFeatures, _objectCodec);
}
@Override
public JsonParser createParser(File f) throws IOException {
IOContext ctxt = _createContext(_createContentReference(f), true);
return _createParser(_decorate(new FileInputStream(f), ctxt), ctxt);
}
@Override
public JsonParser createParser(URL url) throws IOException {
IOContext ctxt = _createContext(_createContentReference(url), true);
return _createParser(_decorate(_optimizedStreamFromURL(url), ctxt), ctxt);
}
@Override
public JsonParser createParser(InputStream in) throws IOException {
IOContext ctxt = _createContext(_createContentReference(in), false);
return _createParser(_decorate(in, ctxt), ctxt);
}
@Override
public JsonParser createParser(byte[] data) throws IOException {
IOContext ctxt = _createContext(_createContentReference(data), true);
if (_inputDecorator != null) {
InputStream in = _inputDecorator.decorate(ctxt, data, 0, data.length);
if (in != null) {
return _createParser(in, ctxt);
}
}
return _createParser(data, 0, data.length, ctxt);
}
@Override
public JsonParser createParser(byte[] data, int offset, int len) throws IOException {
IOContext ctxt = _createContext(_createContentReference(data, offset, len),
true);
if (_inputDecorator != null) {
InputStream in = _inputDecorator.decorate(ctxt, data, offset, len);
if (in != null) {
return _createParser(in, ctxt);
}
}
return _createParser(data, offset, len, ctxt);
}
@Override
public JsonGenerator createGenerator(OutputStream out, JsonEncoding enc) throws IOException {
IOContext ctxt = _createContext(_createContentReference(out), false);
ctxt.setEncoding(enc);
return _createJavaPropsGenerator(ctxt, _generatorFeatures, _objectCodec,
_decorate(out, ctxt));
}
@Override
public JsonGenerator createGenerator(OutputStream out) throws IOException {
IOContext ctxt = _createContext(_createContentReference(out), false);
return _createJavaPropsGenerator(ctxt, _generatorFeatures, _objectCodec,
_decorate(out, ctxt));
}
@Override
protected JsonParser _createParser(InputStream in, IOContext ctxt) throws IOException
{
Properties props = _loadProperties(in, ctxt);
return new JavaPropsParser(ctxt, _parserFeatures, in, _objectCodec, props);
}
@Override
protected JsonParser _createParser(Reader r, IOContext ctxt) throws IOException {
Properties props = _loadProperties(r, ctxt);
return new JavaPropsParser(ctxt, _parserFeatures, r, _objectCodec, props);
}
@Override
protected JsonParser _createParser(char[] data, int offset, int len, IOContext ctxt,
boolean recyclable) throws IOException
{
return _createParser(new CharArrayReader(data, offset, len), ctxt);
}
@Override
protected JsonParser _createParser(byte[] data, int offset, int len, IOContext ctxt) throws IOException
{
return _createParser(new Latin1Reader(data, offset, len), ctxt);
}
@Override
protected JsonGenerator _createGenerator(Writer out, IOContext ctxt) throws IOException
{
return new WriterBackedGenerator(ctxt, out, _generatorFeatures, _objectCodec);
}
@Override
protected JsonGenerator _createUTF8Generator(OutputStream out, IOContext ctxt) throws IOException {
return _createJavaPropsGenerator(ctxt, _generatorFeatures, _objectCodec, out);
}
@Override
protected Writer _createWriter(OutputStream out, JsonEncoding enc, IOContext ctxt) throws IOException {
return new OutputStreamWriter(out, CHARSET_ID_LATIN1);
}
protected Properties _loadProperties(InputStream in, IOContext ctxt)
throws IOException
{
return _loadProperties(new Latin1Reader(ctxt, in), ctxt);
}
protected Properties _loadProperties(Reader r0, IOContext ctxt)
throws IOException
{
Properties props = new Properties();
if (ctxt.isResourceManaged() || isEnabled(StreamReadFeature.AUTO_CLOSE_SOURCE)) {
try (Reader r = r0) {
props.load(r);
}
} else {
props.load(r0);
}
return props;
}
private final JsonGenerator _createJavaPropsGenerator(IOContext ctxt,
int stdFeat, ObjectCodec codec, OutputStream out) throws IOException
{
return new WriterBackedGenerator(ctxt, _createWriter(out, null, ctxt),
stdFeat, _objectCodec);
}
}