com.fitbur.fasterxml.jackson.databind.ext.DOMDeserializer Maven / Gradle / Ivy
package com.fitbur.fasterxml.jackson.databind.ext;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilderFactory;
import com.fitbur.w3c.dom.Document;
import com.fitbur.w3c.dom.Node;
import com.fitbur.xml.sax.InputSource;
import com.fitbur.fasterxml.jackson.databind.DeserializationContext;
import com.fitbur.fasterxml.jackson.databind.com.fitburser.std.FromStringDeserializer;
/**
* Base for serializers that allows parsing DOM Documents from JSON Strings.
* Nominal type can be either {@link com.fitbur.w3c.dom.Node} or
* {@link com.fitbur.w3c.dom.Document}.
*/
public abstract class DOMDeserializer extends FromStringDeserializer
{
final static DocumentBuilderFactory _parserFactory;
static {
_parserFactory = DocumentBuilderFactory.newInstance();
// yup, only cave men do XML without recognizing namespaces...
_parserFactory.setNamespaceAware(true);
}
protected DOMDeserializer(Class cls) { super(cls); }
@Override
public abstract T _deserialize(String value, DeserializationContext ctxt);
protected final Document parse(String value) throws IllegalArgumentException
{
try {
return _parserFactory.newDocumentBuilder().parse(new InputSource(new StringReader(value)));
} catch (Exception e) {
throw new IllegalArgumentException("Failed to parse JSON String as XML: "+e.getMessage(), e);
}
}
/*
/**********************************************************
/* Concrete com.fitburserializers
/**********************************************************
*/
public static class NodeDeserializer extends DOMDeserializer
{
public NodeDeserializer() { super(Node.class); }
@Override
public Node _deserialize(String value, DeserializationContext ctxt) throws IllegalArgumentException {
return parse(value);
}
}
public static class DocumentDeserializer extends DOMDeserializer
{
public DocumentDeserializer() { super(Document.class); }
@Override
public Document _deserialize(String value, DeserializationContext ctxt) throws IllegalArgumentException {
return parse(value);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy