|
|
|
@ -18,11 +18,8 @@ import java.util.List;
@@ -18,11 +18,8 @@ import java.util.List;
|
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.Map.Entry; |
|
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
|
|
import javax.xml.bind.JAXBContext; |
|
|
|
|
import javax.xml.bind.JAXBException; |
|
|
|
|
import javax.xml.bind.Marshaller; |
|
|
|
|
import javax.xml.bind.PropertyException; |
|
|
|
|
import javax.xml.bind.Unmarshaller; |
|
|
|
|
import javax.xml.bind.*; |
|
|
|
|
import javax.xml.validation.Schema; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Creates and caches JAXB contexts as well as creates Marshallers and Unmarshallers for each |
|
|
|
@ -35,18 +32,44 @@ public final class JAXBContextFactory {
@@ -35,18 +32,44 @@ public final class JAXBContextFactory {
|
|
|
|
|
new ConcurrentHashMap<>(64); |
|
|
|
|
private final Map<String, Object> properties; |
|
|
|
|
private final JAXBContextInstantationMode jaxbContextInstantationMode; |
|
|
|
|
private final ValidationEventHandler marshallerEventHandler; |
|
|
|
|
private final ValidationEventHandler unmarshallerEventHandler; |
|
|
|
|
private final Schema marshallerSchema; |
|
|
|
|
private final Schema unmashallerSchema; |
|
|
|
|
|
|
|
|
|
private JAXBContextFactory(Map<String, Object> properties, |
|
|
|
|
JAXBContextInstantationMode jaxbContextInstantationMode) { |
|
|
|
|
JAXBContextInstantationMode jaxbContextInstantationMode, |
|
|
|
|
ValidationEventHandler marshallerEventHandler, |
|
|
|
|
ValidationEventHandler unmarshallerEventHandler, |
|
|
|
|
Schema marshallerSchema, |
|
|
|
|
Schema unmashallerSchema) { |
|
|
|
|
this.properties = properties; |
|
|
|
|
this.jaxbContextInstantationMode = jaxbContextInstantationMode; |
|
|
|
|
this.marshallerEventHandler = marshallerEventHandler; |
|
|
|
|
this.unmarshallerEventHandler = unmarshallerEventHandler; |
|
|
|
|
this.marshallerSchema = marshallerSchema; |
|
|
|
|
this.unmashallerSchema = unmashallerSchema; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @deprecated please use the constructor with all parameters. |
|
|
|
|
*/ |
|
|
|
|
@Deprecated |
|
|
|
|
private JAXBContextFactory(Map<String, Object> properties, |
|
|
|
|
JAXBContextInstantationMode jaxbContextInstantationMode) { |
|
|
|
|
this(properties, jaxbContextInstantationMode, null, null, null, null); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Creates a new {@link javax.xml.bind.Unmarshaller} that handles the supplied class. |
|
|
|
|
*/ |
|
|
|
|
public Unmarshaller createUnmarshaller(Class<?> clazz) throws JAXBException { |
|
|
|
|
return getContext(clazz).createUnmarshaller(); |
|
|
|
|
Unmarshaller unmarshaller = getContext(clazz).createUnmarshaller(); |
|
|
|
|
if (unmarshallerEventHandler != null) { |
|
|
|
|
unmarshaller.setEventHandler(unmarshallerEventHandler); |
|
|
|
|
} |
|
|
|
|
unmarshaller.setSchema(unmashallerSchema); |
|
|
|
|
return unmarshaller; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -55,6 +78,10 @@ public final class JAXBContextFactory {
@@ -55,6 +78,10 @@ public final class JAXBContextFactory {
|
|
|
|
|
public Marshaller createMarshaller(Class<?> clazz) throws JAXBException { |
|
|
|
|
Marshaller marshaller = getContext(clazz).createMarshaller(); |
|
|
|
|
setMarshallerProperties(marshaller); |
|
|
|
|
if (marshallerEventHandler != null) { |
|
|
|
|
marshaller.setEventHandler(marshallerEventHandler); |
|
|
|
|
} |
|
|
|
|
marshaller.setSchema(marshallerSchema); |
|
|
|
|
return marshaller; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -99,6 +126,14 @@ public final class JAXBContextFactory {
@@ -99,6 +126,14 @@ public final class JAXBContextFactory {
|
|
|
|
|
private JAXBContextInstantationMode jaxbContextInstantationMode = |
|
|
|
|
JAXBContextInstantationMode.CLASS; |
|
|
|
|
|
|
|
|
|
private ValidationEventHandler marshallerEventHandler; |
|
|
|
|
|
|
|
|
|
private ValidationEventHandler unmarshallerEventHandler; |
|
|
|
|
|
|
|
|
|
private Schema marshallerSchema; |
|
|
|
|
|
|
|
|
|
private Schema unmarshallerSchema; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Sets the jaxb.encoding property of any Marshaller created by this factory. |
|
|
|
|
*/ |
|
|
|
@ -157,6 +192,38 @@ public final class JAXBContextFactory {
@@ -157,6 +192,38 @@ public final class JAXBContextFactory {
|
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Sets the validation event handler of any Marshaller created by this factory. |
|
|
|
|
*/ |
|
|
|
|
public Builder withMarshallerEventHandler(ValidationEventHandler handler) { |
|
|
|
|
this.marshallerEventHandler = handler; |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Sets the validation event handler of any Unmarshaller created by this factory. |
|
|
|
|
*/ |
|
|
|
|
public Builder withUnmarshallerEventHandler(ValidationEventHandler handler) { |
|
|
|
|
this.unmarshallerEventHandler = handler; |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Sets the schema of any Marshaller created by this factory. |
|
|
|
|
*/ |
|
|
|
|
public Builder withMarshallerSchema(Schema schema) { |
|
|
|
|
this.marshallerSchema = schema; |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Sets the schema of any Unmarshaller created by this factory. |
|
|
|
|
*/ |
|
|
|
|
public Builder withUnmarshallerSchema(Schema schema) { |
|
|
|
|
this.unmarshallerSchema = schema; |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Provide an instantiation mode for JAXB Contexts, can be class or package, default is class if |
|
|
|
|
* this method is not called. |
|
|
|
@ -181,7 +248,8 @@ public final class JAXBContextFactory {
@@ -181,7 +248,8 @@ public final class JAXBContextFactory {
|
|
|
|
|
* context |
|
|
|
|
*/ |
|
|
|
|
public JAXBContextFactory build() { |
|
|
|
|
return new JAXBContextFactory(properties, jaxbContextInstantationMode); |
|
|
|
|
return new JAXBContextFactory(properties, jaxbContextInstantationMode, marshallerEventHandler, |
|
|
|
|
unmarshallerEventHandler, marshallerSchema, unmarshallerSchema); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -194,7 +262,7 @@ public final class JAXBContextFactory {
@@ -194,7 +262,7 @@ public final class JAXBContextFactory {
|
|
|
|
|
* likely due to missing JAXB annotations |
|
|
|
|
*/ |
|
|
|
|
public JAXBContextFactory build(List<Class<?>> classes) throws JAXBException { |
|
|
|
|
JAXBContextFactory factory = new JAXBContextFactory(properties, jaxbContextInstantationMode); |
|
|
|
|
JAXBContextFactory factory = build(); |
|
|
|
|
factory.preloadContextCache(classes); |
|
|
|
|
return factory; |
|
|
|
|
} |
|
|
|
|