use of org.eclipse.persistence.oxm.NamespacePrefixMapper in project eclipselink by eclipse-ee4j.
the class JAXBUnmarshaller method setProperty.
/**
* Set a property on the JAXBUnmarshaller. Attempting to set any unsupported
* property will result in a jakarta.xml.bind.PropertyException.
* @see org.eclipse.persistence.jaxb.UnmarshallerProperties
*/
@Override
public void setProperty(String key, Object value) throws PropertyException {
if (key == null) {
throw new IllegalArgumentException();
}
SessionLog logger = AbstractSessionLog.getLog();
if (logger.shouldLog(SessionLog.FINE, SessionLog.MOXY)) {
logger.log(SessionLog.FINE, SessionLog.MOXY, "moxy_set_unmarshaller_property", new Object[] { key, value });
}
if (MOXySystemProperties.moxyLogPayload != null && xmlUnmarshaller.isLogPayload() == null) {
xmlUnmarshaller.setLogPayload(MOXySystemProperties.moxyLogPayload);
}
if (key.equals(UnmarshallerProperties.MEDIA_TYPE)) {
MediaType mType = null;
if (value instanceof MediaType) {
mType = (MediaType) value;
} else if (value instanceof String) {
mType = MediaType.getMediaType((String) value);
}
if (mType == null) {
throw new PropertyException(key, Constants.EMPTY_STRING);
}
xmlUnmarshaller.setMediaType(mType);
} else if (key.equals(UnmarshallerProperties.UNMARSHALLING_CASE_INSENSITIVE)) {
if (value == null) {
throw new PropertyException(key, Constants.EMPTY_STRING);
}
xmlUnmarshaller.setCaseInsensitive((Boolean) value);
} else if (key.equals(UnmarshallerProperties.AUTO_DETECT_MEDIA_TYPE)) {
if (value == null) {
throw new PropertyException(key, Constants.EMPTY_STRING);
}
xmlUnmarshaller.setAutoDetectMediaType((Boolean) value);
} else if (key.equals(UnmarshallerProperties.JSON_ATTRIBUTE_PREFIX)) {
xmlUnmarshaller.setAttributePrefix((String) value);
} else if (UnmarshallerProperties.JSON_INCLUDE_ROOT.equals(key)) {
if (value == null) {
throw new PropertyException(key, Constants.EMPTY_STRING);
}
xmlUnmarshaller.setIncludeRoot((Boolean) value);
} else if (UnmarshallerProperties.JSON_NAMESPACE_PREFIX_MAPPER.equals(key)) {
if (value == null) {
xmlUnmarshaller.setNamespaceResolver(null);
} else if (value instanceof Map) {
Map<String, String> namespaces = (Map<String, String>) value;
NamespaceResolver nr = new NamespaceResolver();
Iterator<Entry<String, String>> namesapcesIter = namespaces.entrySet().iterator();
for (int i = 0; i < namespaces.size(); i++) {
Entry<String, String> nextEntry = namesapcesIter.next();
nr.put(nextEntry.getValue(), nextEntry.getKey());
}
xmlUnmarshaller.setNamespaceResolver(nr);
} else if (value instanceof NamespacePrefixMapper) {
xmlUnmarshaller.setNamespaceResolver(new PrefixMapperNamespaceResolver((NamespacePrefixMapper) value, null));
}
} else if (UnmarshallerProperties.JSON_VALUE_WRAPPER.equals(key)) {
xmlUnmarshaller.setValueWrapper((String) value);
} else if (UnmarshallerProperties.JSON_NAMESPACE_SEPARATOR.equals(key)) {
if (value == null) {
throw new PropertyException(key, Constants.EMPTY_STRING);
}
xmlUnmarshaller.setNamespaceSeparator((Character) value);
} else if (UnmarshallerProperties.JSON_USE_XSD_TYPES_WITH_PREFIX.equals(key)) {
xmlUnmarshaller.getJsonTypeConfiguration().setUseXsdTypesWithPrefix((Boolean) value);
} else if (UnmarshallerProperties.JSON_TYPE_COMPATIBILITY.equals(key)) {
xmlUnmarshaller.getJsonTypeConfiguration().setJsonTypeCompatibility((Boolean) value);
} else if (UnmarshallerProperties.JSON_TYPE_ATTRIBUTE_NAME.equals(key)) {
xmlUnmarshaller.getJsonTypeConfiguration().setJsonTypeAttributeName((String) value);
} else if (UnmarshallerProperties.ID_RESOLVER.equals(key)) {
setIDResolver((IDResolver) value);
} else if (SUN_ID_RESOLVER.equals(key) || SUN_JSE_ID_RESOLVER.equals(key)) {
if (value == null) {
setIDResolver(null);
} else {
setIDResolver(new IDResolverWrapper(value));
}
} else if (UnmarshallerProperties.OBJECT_GRAPH.equals(key)) {
if (value instanceof ObjectGraphImpl) {
xmlUnmarshaller.setUnmarshalAttributeGroup(((ObjectGraphImpl) value).getAttributeGroup());
} else if (value instanceof String || value == null) {
xmlUnmarshaller.setUnmarshalAttributeGroup(value);
} else {
throw org.eclipse.persistence.exceptions.JAXBException.invalidValueForObjectGraph(value);
}
} else if (UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME.equals(key)) {
xmlUnmarshaller.setWrapperAsCollectionName((Boolean) value);
} else if (UnmarshallerProperties.BEAN_VALIDATION_MODE.equals(key)) {
if (value == null) {
throw new PropertyException(key, Constants.EMPTY_STRING);
}
this.beanValidationMode = ((BeanValidationMode) value);
} else if (UnmarshallerProperties.BEAN_VALIDATION_FACTORY.equals(key)) {
// Null value is allowed
this.prefValidatorFactory = value;
} else if (UnmarshallerProperties.BEAN_VALIDATION_GROUPS.equals(key)) {
if (value == null) {
throw new PropertyException(key, Constants.EMPTY_STRING);
}
this.beanValidationGroups = ((Class<?>[]) value);
} else if (UnmarshallerProperties.BEAN_VALIDATION_NO_OPTIMISATION.equals(key)) {
if (value == null) {
throw new PropertyException(key, Constants.EMPTY_STRING);
}
this.bvNoOptimisation = ((boolean) value);
} else if (UnmarshallerProperties.DISABLE_SECURE_PROCESSING.equals(key)) {
if (value == null) {
throw new PropertyException(key, Constants.EMPTY_STRING);
}
boolean disabled = value instanceof String ? Boolean.parseBoolean((String) value) : (boolean) value;
xmlUnmarshaller.setDisableSecureProcessing(disabled);
} else if (UnmarshallerProperties.MOXY_LOG_PAYLOAD.equals(key)) {
xmlUnmarshaller.setLogPayload(((boolean) value));
} else if (UnmarshallerProperties.MOXY_LOGGING_LEVEL.equals(key)) {
if (value instanceof String) {
AbstractSessionLog.getLog().setLevel(LogLevel.toValue((String) value).getId(), SessionLog.MOXY);
} else {
AbstractSessionLog.getLog().setLevel(((LogLevel) value).getId(), SessionLog.MOXY);
}
} else {
throw new PropertyException(key, value);
}
}
use of org.eclipse.persistence.oxm.NamespacePrefixMapper in project eclipselink by eclipse-ee4j.
the class PropertyTestCases method testMarshallerNamespacePrefixMapper.
public void testMarshallerNamespacePrefixMapper() throws Exception {
String SUN_NAMESPACE_PREFIX_MAPPER = "org.glassfish.jaxb.namespacePrefixMapper";
String SUN_JSE_NAMESPACE_PREFIX_MAPPER = "com.sun.xml.internal.bind.namespacePrefixMapper";
NamespacePrefixMapper mapper = new MyPrefixMapper();
m.setProperty(MarshallerProperties.NAMESPACE_PREFIX_MAPPER, mapper);
assertEquals(mapper, m.getProperty(MarshallerProperties.NAMESPACE_PREFIX_MAPPER));
m.setProperty(SUN_NAMESPACE_PREFIX_MAPPER, mapper);
assertEquals(mapper, m.getProperty(SUN_NAMESPACE_PREFIX_MAPPER));
m.setProperty(SUN_JSE_NAMESPACE_PREFIX_MAPPER, mapper);
assertEquals(mapper, m.getProperty(SUN_JSE_NAMESPACE_PREFIX_MAPPER));
m.setProperty(SUN_NAMESPACE_PREFIX_MAPPER, null);
}
use of org.eclipse.persistence.oxm.NamespacePrefixMapper in project eclipselink by eclipse-ee4j.
the class PropertyTestCases method testUnmarshallerJsonNamespacePrefixMapper.
public void testUnmarshallerJsonNamespacePrefixMapper() throws Exception {
NamespacePrefixMapper mapper = new MyPrefixMapper();
u.setProperty(UnmarshallerProperties.JSON_NAMESPACE_PREFIX_MAPPER, mapper);
assertEquals(mapper, u.getProperty(UnmarshallerProperties.JSON_NAMESPACE_PREFIX_MAPPER));
HashMap<String, String> map = new HashMap<String, String>();
map.put("mynamespace", "ns1");
u.setProperty(UnmarshallerProperties.JSON_NAMESPACE_PREFIX_MAPPER, map);
assertEquals(map, u.getProperty(UnmarshallerProperties.JSON_NAMESPACE_PREFIX_MAPPER));
u.setProperty(UnmarshallerProperties.JSON_NAMESPACE_PREFIX_MAPPER, null);
assertNull(u.getProperty(UnmarshallerProperties.JSON_NAMESPACE_PREFIX_MAPPER));
}
use of org.eclipse.persistence.oxm.NamespacePrefixMapper in project eclipselink by eclipse-ee4j.
the class XMLWithJSONMappingTestCases method testJSONUnmarshalFromInputSource.
public void testJSONUnmarshalFromInputSource() throws Exception {
if (isUnmarshalTest() && !(platform.name().equals(PLATFORM_DOC_PRES) || platform.name().equals(PLATFORM_DOM))) {
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(controlJSONLocation);
InputSource inputSource = new InputSource(inputStream);
xmlUnmarshaller.setMediaType(MediaType.APPLICATION_JSON);
PrefixMapperNamespaceResolver nr = null;
if (getNamespaces() != null) {
NamespacePrefixMapper mapper = new MapNamespacePrefixMapper(getNamespaces());
nr = new PrefixMapperNamespaceResolver(mapper, null);
xmlUnmarshaller.setNamespaceResolver(nr);
}
Object testObject = xmlUnmarshaller.unmarshal(inputSource);
inputStream.close();
if ((getJSONReadControlObject() instanceof XMLRoot) && (testObject instanceof XMLRoot)) {
XMLRoot controlObj = (XMLRoot) getReadControlObject();
XMLRoot testObj = (XMLRoot) testObject;
compareXMLRootObjects(controlObj, testObj);
} else {
assertEquals(getJSONReadControlObject(), testObject);
}
}
}
use of org.eclipse.persistence.oxm.NamespacePrefixMapper in project eclipselink by eclipse-ee4j.
the class JAXBMarshaller method setProperty.
/**
* Set a property on the JAXBMarshaller. Attempting to set any unsupported
* property will result in a jakarta.xml.bind.PropertyException
* @see org.eclipse.persistence.jaxb.MarshallerProperties
*/
@Override
public void setProperty(String key, Object value) throws PropertyException {
try {
if (key == null) {
throw new IllegalArgumentException();
} else {
SessionLog logger = AbstractSessionLog.getLog();
if (logger.shouldLog(SessionLog.FINE, SessionLog.MOXY)) {
logger.log(SessionLog.FINE, SessionLog.MOXY, "moxy_set_marshaller_property", new Object[] { key, value });
}
if (MOXySystemProperties.moxyLogPayload != null && xmlMarshaller.isLogPayload() == null) {
xmlMarshaller.setLogPayload(MOXySystemProperties.moxyLogPayload);
}
if (Constants.JAXB_FRAGMENT.equals(key)) {
if (value == null) {
throw new PropertyException(key, Constants.EMPTY_STRING);
}
Boolean fragment = (Boolean) value;
xmlMarshaller.setFragment(fragment);
} else if (JAXB_FORMATTED_OUTPUT.equals(key)) {
if (value == null) {
throw new PropertyException(key, Constants.EMPTY_STRING);
}
Boolean formattedOutput = (Boolean) value;
xmlMarshaller.setFormattedOutput(formattedOutput);
} else if (JAXB_ENCODING.equals(key)) {
xmlMarshaller.setEncoding((String) value);
} else if (JAXB_SCHEMA_LOCATION.equals(key)) {
xmlMarshaller.setSchemaLocation((String) value);
} else if (JAXB_NO_NAMESPACE_SCHEMA_LOCATION.equals(key)) {
xmlMarshaller.setNoNamespaceSchemaLocation((String) value);
} else if (MarshallerProperties.NAMESPACE_PREFIX_MAPPER.equals(key)) {
if (value == null) {
xmlMarshaller.setNamespacePrefixMapper(null);
} else if (value instanceof Map) {
NamespacePrefixMapper namespacePrefixMapper = new MapNamespacePrefixMapper((Map) value);
xmlMarshaller.setNamespacePrefixMapper(namespacePrefixMapper);
} else {
xmlMarshaller.setNamespacePrefixMapper((NamespacePrefixMapper) value);
}
} else if (SUN_NAMESPACE_PREFIX_MAPPER.equals(key) || SUN_JSE_NAMESPACE_PREFIX_MAPPER.equals(key)) {
if (value == null) {
xmlMarshaller.setNamespacePrefixMapper(null);
} else {
xmlMarshaller.setNamespacePrefixMapper(new NamespacePrefixMapperWrapper(value));
}
} else if (MarshallerProperties.INDENT_STRING.equals(key) || SUN_INDENT_STRING.equals(key) || SUN_JSE_INDENT_STRING.equals(key)) {
xmlMarshaller.setIndentString((String) value);
} else if (MarshallerProperties.JSON_MARSHAL_EMPTY_COLLECTIONS.equals(key)) {
xmlMarshaller.setMarshalEmptyCollections((Boolean) value);
} else if (MarshallerProperties.JSON_REDUCE_ANY_ARRAYS.equals(key)) {
xmlMarshaller.setReduceAnyArrays((Boolean) value);
} else if (MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME.equals(key)) {
xmlMarshaller.setWrapperAsCollectionName((Boolean) value);
} else if (MarshallerProperties.JSON_USE_XSD_TYPES_WITH_PREFIX.equals(key)) {
xmlMarshaller.getJsonTypeConfiguration().setUseXsdTypesWithPrefix((Boolean) value);
} else if (MarshallerProperties.JSON_TYPE_COMPATIBILITY.equals(key)) {
xmlMarshaller.getJsonTypeConfiguration().setJsonTypeCompatibility((Boolean) value);
} else if (MarshallerProperties.JSON_TYPE_ATTRIBUTE_NAME.equals(key)) {
xmlMarshaller.getJsonTypeConfiguration().setJsonTypeAttributeName((String) value);
} else if (MarshallerProperties.JSON_DISABLE_NESTED_ARRAY_NAME.equals(key)) {
xmlMarshaller.getJsonTypeConfiguration().setJsonDisableNestedArrayName((Boolean) value);
} else if (MarshallerProperties.CHARACTER_ESCAPE_HANDLER.equals(key)) {
xmlMarshaller.setCharacterEscapeHandler((CharacterEscapeHandler) value);
} else if (MarshallerProperties.MOXY_LOG_PAYLOAD.equals(key)) {
xmlMarshaller.setLogPayload(((Boolean) value));
} else if (MarshallerProperties.MOXY_LOGGING_LEVEL.equals(key)) {
if (value instanceof String) {
AbstractSessionLog.getLog().setLevel(LogLevel.toValue((String) value).getId(), SessionLog.MOXY);
} else {
AbstractSessionLog.getLog().setLevel(((LogLevel) value).getId(), SessionLog.MOXY);
}
} else if (SUN_CHARACTER_ESCAPE_HANDLER.equals(key) || SUN_JSE_CHARACTER_ESCAPE_HANDLER.equals(key) || SUN_CHARACTER_ESCAPE_HANDLER_MARSHALLER.equals(key) || SUN_JSE_CHARACTER_ESCAPE_HANDLER_MARSHALLER.equals(key)) {
if (value == null) {
xmlMarshaller.setCharacterEscapeHandler(null);
} else {
xmlMarshaller.setCharacterEscapeHandler(new CharacterEscapeHandlerWrapper(value));
}
} else if (XML_DECLARATION.equals(key)) {
if (value == null) {
throw new PropertyException(key, Constants.EMPTY_STRING);
}
Boolean fragment = !(Boolean) value;
xmlMarshaller.setFragment(fragment);
} else if (XML_HEADERS.equals(key)) {
xmlMarshaller.setXmlHeader((String) value);
} else if (OBJECT_IDENTITY_CYCLE_DETECTION.equals(key)) {
if (value == null) {
throw new PropertyException(key, Constants.EMPTY_STRING);
}
xmlMarshaller.setEqualUsingIdenity((Boolean) value);
} else if (MarshallerProperties.MEDIA_TYPE.equals(key)) {
MediaType mType = null;
if (value instanceof MediaType) {
mType = (MediaType) value;
} else if (value instanceof String) {
mType = MediaType.getMediaType((String) value);
}
if (mType == null) {
throw new PropertyException(key, Constants.EMPTY_STRING);
}
xmlMarshaller.setMediaType(mType);
} else if (MarshallerProperties.JSON_ATTRIBUTE_PREFIX.equals(key)) {
xmlMarshaller.setAttributePrefix((String) value);
} else if (MarshallerProperties.JSON_INCLUDE_ROOT.equals(key)) {
if (value == null) {
throw new PropertyException(key, Constants.EMPTY_STRING);
}
xmlMarshaller.setIncludeRoot((Boolean) value);
} else if (MarshallerProperties.JSON_VALUE_WRAPPER.equals(key)) {
if (value == null || (((String) value).length() == 0)) {
throw new PropertyException(key, Constants.EMPTY_STRING);
}
xmlMarshaller.setValueWrapper((String) value);
} else if (MarshallerProperties.JSON_NAMESPACE_SEPARATOR.equals(key)) {
if (value == null) {
throw new PropertyException(key, Constants.EMPTY_STRING);
}
xmlMarshaller.setNamespaceSeparator((Character) value);
} else if (MarshallerProperties.OBJECT_GRAPH.equals(key)) {
if (value == null) {
xmlMarshaller.setMarshalAttributeGroup(null);
} else if (value instanceof ObjectGraphImpl) {
xmlMarshaller.setMarshalAttributeGroup(((ObjectGraphImpl) value).getAttributeGroup());
} else if (value.getClass() == ClassConstants.STRING) {
xmlMarshaller.setMarshalAttributeGroup(value);
} else {
throw org.eclipse.persistence.exceptions.JAXBException.invalidValueForObjectGraph(value);
}
} else if (MarshallerProperties.BEAN_VALIDATION_MODE.equals(key)) {
if (value == null) {
throw new PropertyException(key, Constants.EMPTY_STRING);
}
this.beanValidationMode = ((BeanValidationMode) value);
} else if (MarshallerProperties.BEAN_VALIDATION_FACTORY.equals(key)) {
// noinspection StatementWithEmptyBody
if (value == null) {
// Allow null value for preferred validation factory.
}
this.prefValidatorFactory = value;
} else if (MarshallerProperties.BEAN_VALIDATION_GROUPS.equals(key)) {
if (value == null) {
throw new PropertyException(key, Constants.EMPTY_STRING);
}
this.beanValidationGroups = ((Class<?>[]) value);
} else if (MarshallerProperties.BEAN_VALIDATION_NO_OPTIMISATION.equals(key)) {
if (value == null) {
throw new PropertyException(key, Constants.EMPTY_STRING);
}
this.bvNoOptimisation = ((boolean) value);
} else {
throw new PropertyException(key, value);
}
}
} catch (ClassCastException exception) {
throw new PropertyException(key, exception);
}
}
Aggregations