use of org.eclipse.persistence.internal.oxm.record.namespaces.PrefixMapperNamespaceResolver in project eclipselink by eclipse-ee4j.
the class JAXBUnmarshaller method getProperty.
/**
* Get a property from the JAXBMarshaller. Attempting to get any unsupported
* property will result in a jakarta.xml.bind.PropertyException
* See <a href="#supportedProps">Supported Properties</a>.
* @see org.eclipse.persistence.jaxb.UnmarshallerProperties
*/
@Override
public Object getProperty(String key) throws PropertyException {
if (key == null) {
throw new IllegalArgumentException();
}
if (key.equals(UnmarshallerProperties.MEDIA_TYPE)) {
return xmlUnmarshaller.getMediaType();
} else if (key.equals(UnmarshallerProperties.AUTO_DETECT_MEDIA_TYPE)) {
return xmlUnmarshaller.isAutoDetectMediaType();
} else if (key.equals(UnmarshallerProperties.UNMARSHALLING_CASE_INSENSITIVE)) {
return xmlUnmarshaller.isCaseInsensitive();
} else if (key.equals(UnmarshallerProperties.JSON_ATTRIBUTE_PREFIX)) {
return xmlUnmarshaller.getAttributePrefix();
} else if (key.equals(UnmarshallerProperties.JSON_INCLUDE_ROOT)) {
return xmlUnmarshaller.isIncludeRoot();
} else if (key.equals(UnmarshallerProperties.JSON_NAMESPACE_SEPARATOR)) {
return xmlUnmarshaller.getNamespaceSeparator();
} else if (key.equals(UnmarshallerProperties.JSON_NAMESPACE_PREFIX_MAPPER)) {
if (xmlUnmarshaller.getNamespaceResolver() == null) {
return null;
}
if (xmlUnmarshaller.getNamespaceResolver() instanceof PrefixMapperNamespaceResolver) {
PrefixMapperNamespaceResolver wrapper = (PrefixMapperNamespaceResolver) xmlUnmarshaller.getNamespaceResolver();
return wrapper.getPrefixMapper();
} else {
Map<String, String> nsMap = new HashMap<String, String>();
Map<String, String> prefixesToNS = xmlUnmarshaller.getNamespaceResolver().getPrefixesToNamespaces();
// Reverse the prefixesToNS map
Iterator<Entry<String, String>> namesapcesIter = prefixesToNS.entrySet().iterator();
for (int i = 0; i < prefixesToNS.size(); i++) {
Entry<String, String> nextEntry = namesapcesIter.next();
nsMap.put(nextEntry.getValue(), nextEntry.getKey());
}
return nsMap;
}
} else if (key.equals(UnmarshallerProperties.JSON_VALUE_WRAPPER)) {
return xmlUnmarshaller.getValueWrapper();
} else if (UnmarshallerProperties.JSON_USE_XSD_TYPES_WITH_PREFIX.equals(key)) {
return xmlUnmarshaller.getJsonTypeConfiguration().isUseXsdTypesWithPrefix();
} else if (UnmarshallerProperties.JSON_TYPE_COMPATIBILITY.equals(key)) {
return xmlUnmarshaller.getJsonTypeConfiguration().isJsonTypeCompatibility();
} else if (MarshallerProperties.JSON_TYPE_ATTRIBUTE_NAME.equals(key)) {
return xmlUnmarshaller.getJsonTypeConfiguration().getJsonTypeAttributeName();
} else if (UnmarshallerProperties.ID_RESOLVER.equals(key)) {
return xmlUnmarshaller.getIDResolver();
} else if (SUN_ID_RESOLVER.equals(key) || SUN_JSE_ID_RESOLVER.equals(key)) {
IDResolverWrapper wrapper = (IDResolverWrapper) xmlUnmarshaller.getIDResolver();
if (wrapper == null) {
return null;
}
return wrapper.getResolver();
} else if (UnmarshallerProperties.OBJECT_GRAPH.equals(key)) {
Object graph = xmlUnmarshaller.getUnmarshalAttributeGroup();
if (graph instanceof CoreAttributeGroup) {
return new ObjectGraphImpl((CoreAttributeGroup) graph);
}
return graph;
} else if (UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME.equals(key)) {
return xmlUnmarshaller.isWrapperAsCollectionName();
} else if (UnmarshallerProperties.BEAN_VALIDATION_MODE.equals(key)) {
return this.beanValidationMode;
} else if (UnmarshallerProperties.BEAN_VALIDATION_FACTORY.equals(key)) {
return this.prefValidatorFactory;
} else if (UnmarshallerProperties.BEAN_VALIDATION_GROUPS.equals(key)) {
return this.beanValidationGroups;
} else if (UnmarshallerProperties.BEAN_VALIDATION_NO_OPTIMISATION.equals(key)) {
return this.bvNoOptimisation;
} else if (UnmarshallerProperties.DISABLE_SECURE_PROCESSING.equals(key)) {
return xmlUnmarshaller.isSecureProcessingDisabled();
} else if (UnmarshallerProperties.MOXY_LOG_PAYLOAD.equals(key)) {
return xmlUnmarshaller.isLogPayload();
}
throw new PropertyException(key);
}
use of org.eclipse.persistence.internal.oxm.record.namespaces.PrefixMapperNamespaceResolver 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.internal.oxm.record.namespaces.PrefixMapperNamespaceResolver in project eclipselink by eclipse-ee4j.
the class XMLMarshaller method marshal.
/**
* Convert the given object to XML and update the given marshal record with
* that XML Document.
* @param object the object to marshal
* @param marshalRecord the marshalRecord to marshal the object to
* @param descriptor the XMLDescriptor for the object being marshalled
*/
protected void marshal(Object object, MarshalRecord marshalRecord, ABSTRACT_SESSION session, DESCRIPTOR descriptor, boolean isXMLRoot) {
SessionLog logger = AbstractSessionLog.getLog();
if (logger.shouldLog(SessionLog.FINE, SessionLog.MOXY)) {
logger.log(SessionLog.FINE, SessionLog.MOXY, "moxy_start_marshalling", new Object[] { (object != null) ? object.getClass().getName() : "N/A", this.mediaType });
}
if (object != null && logPayload != null && this.isLogPayload()) {
AbstractSessionLog.getLog().log(SessionLog.FINEST, SessionLog.MOXY, object.toString(), new Object[0], false);
}
if (null != schema) {
marshalRecord = new ValidatingMarshalRecord(marshalRecord, this);
}
if (this.attachmentMarshaller != null) {
marshalRecord.setXOPPackage(this.attachmentMarshaller.isXOPPackage());
}
marshalRecord.setMarshaller(this);
Root root = null;
if (isXMLRoot) {
root = (Root) object;
}
Node node = getNode(object, marshalRecord.getDOM(), session, descriptor, isXMLRoot);
if (this.mapper == null) {
if (null == node) {
addDescriptorNamespacesToXMLRecord(descriptor, marshalRecord);
}
} else {
if (descriptor == null || null != node) {
marshalRecord.setNamespaceResolver(new PrefixMapperNamespaceResolver(mapper, null));
} else {
marshalRecord.setNamespaceResolver(new PrefixMapperNamespaceResolver(mapper, descriptor.getNamespaceResolver()));
}
marshalRecord.setCustomNamespaceMapper(true);
}
if (this.getMarshalAttributeGroup() != null) {
if (marshalAttributeGroup.getClass() == CoreClassConstants.STRING) {
CoreAttributeGroup group = descriptor.getAttributeGroup((String) marshalAttributeGroup);
if (group != null) {
marshalRecord.pushAttributeGroup(group);
} else {
throw XMLMarshalException.invalidAttributeGroupName((String) marshalAttributeGroup, descriptor.getJavaClassName());
}
} else if (marshalAttributeGroup instanceof CoreAttributeGroup) {
marshalRecord.pushAttributeGroup((CoreAttributeGroup) marshalAttributeGroup);
} else {
// Error case
}
}
NamespaceResolver nr = marshalRecord.getNamespaceResolver();
if (node != null) {
if (isXMLRoot) {
if (isFragment()) {
marshalRecord.node(node, null, root.getNamespaceURI(), root.getLocalName());
} else {
String encoding = root.getEncoding();
if (null == encoding) {
encoding = Constants.DEFAULT_XML_ENCODING;
}
String version = root.getXMLVersion();
if (null == version) {
version = DEFAULT_XML_VERSION;
}
marshalRecord.startDocument(encoding, version);
marshalRecord.node(node, marshalRecord.getNamespaceResolver(), root.getNamespaceURI(), root.getLocalName());
marshalRecord.endDocument();
}
} else {
marshalRecord.node(node, nr);
}
marshalRecord.flush();
return;
}
if (isXMLRoot) {
if (descriptor != null) {
marshalRecord.beforeContainmentMarshal(root.getObject());
}
} else {
marshalRecord.beforeContainmentMarshal(object);
}
if (!isFragment()) {
String encoding = getEncoding();
String version = DEFAULT_XML_VERSION;
if (!isXMLRoot && descriptor != null) {
marshalRecord.setLeafElementType(descriptor.getDefaultRootElementType());
} else {
if (root.getEncoding() != null) {
encoding = root.getEncoding();
}
if (root.getXMLVersion() != null) {
version = root.getXMLVersion();
}
}
marshalRecord.startDocument(encoding, version);
}
if (getXmlHeader() != null) {
marshalRecord.writeHeader();
}
if (isXMLRoot) {
if (root.getObject() instanceof Node) {
marshalRecord.node((Node) root.getObject(), new NamespaceResolver(), root.getNamespaceURI(), root.getLocalName());
marshalRecord.endDocument();
return;
}
}
XPathFragment rootFragment = buildRootFragment(object, descriptor, isXMLRoot, marshalRecord);
String schemaLocation = getSchemaLocation();
String noNsSchemaLocation = getNoNamespaceSchemaLocation();
boolean isNil = false;
if (isXMLRoot) {
object = root.getObject();
if (root.getSchemaLocation() != null) {
schemaLocation = root.getSchemaLocation();
}
if (root.getNoNamespaceSchemaLocation() != null) {
noNsSchemaLocation = root.getNoNamespaceSchemaLocation();
}
marshalRecord.setLeafElementType(root.getSchemaType());
isNil = root.isNil();
}
String xsiPrefix = null;
if ((null != getSchemaLocation()) || (null != getNoNamespaceSchemaLocation()) || (isNil)) {
xsiPrefix = nr.resolveNamespaceURI(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
if (null == xsiPrefix) {
xsiPrefix = Constants.SCHEMA_INSTANCE_PREFIX;
nr.put(Constants.SCHEMA_INSTANCE_PREFIX, javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
}
}
OBJECT_BUILDER treeObjectBuilder = null;
if (descriptor != null) {
treeObjectBuilder = (OBJECT_BUILDER) descriptor.getObjectBuilder();
}
if (session == null) {
session = (ABSTRACT_SESSION) context.getSession();
}
marshalRecord.setSession(session);
if (null != rootFragment && !(rootFragment.getLocalName().equals(Constants.EMPTY_STRING))) {
marshalRecord.startPrefixMappings(nr);
if (!isXMLRoot && descriptor != null && descriptor.getNamespaceResolver() == null && rootFragment.hasNamespace()) {
// throw an exception if the name has a : in it but the namespaceresolver is null
throw XMLMarshalException.namespaceResolverNotSpecified(rootFragment.getShortName());
}
if (isIncludeRoot()) {
marshalRecord.openStartElement(rootFragment, nr);
}
if (null != schemaLocation) {
marshalRecord.attributeWithoutQName(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, Constants.SCHEMA_LOCATION, xsiPrefix, schemaLocation);
}
if (null != noNsSchemaLocation) {
marshalRecord.attributeWithoutQName(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, Constants.NO_NS_SCHEMA_LOCATION, xsiPrefix, noNsSchemaLocation);
}
if (isNil) {
marshalRecord.nilSimple(nr);
}
marshalRecord.namespaceDeclarations(nr);
if (descriptor != null && !isNil) {
marshalRecord.addXsiTypeAndClassIndicatorIfRequired(descriptor, null, descriptor.getDefaultRootElementField(), root, object, isXMLRoot, true);
treeObjectBuilder.marshalAttributes(marshalRecord, object, session);
}
if (isIncludeRoot()) {
marshalRecord.closeStartElement();
}
} else {
// no rootfragment
marshalRecord.marshalWithoutRootElement(treeObjectBuilder, object, descriptor, root, isXMLRoot);
}
if (treeObjectBuilder != null && !isNil) {
treeObjectBuilder.buildRow(marshalRecord, object, session, this, rootFragment);
} else if (isXMLRoot) {
if (object != null && !isNil) {
if (root.getDeclaredType() != null && root.getObject() != null && root.getDeclaredType() != root.getObject().getClass()) {
QName type = marshalRecord.getConversionManager().schemaType(object.getClass());
if (type != null) {
xsiPrefix = nr.resolveNamespaceURI(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
if (null == xsiPrefix) {
xsiPrefix = Constants.SCHEMA_INSTANCE_PREFIX;
marshalRecord.namespaceDeclaration(xsiPrefix, javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
}
marshalRecord.namespaceDeclaration(Constants.SCHEMA_PREFIX, javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
String typeValue = type.getLocalPart();
if (marshalRecord.isNamespaceAware() && (mediaType.isApplicationXML() || getJsonTypeConfiguration().useXsdTypesWithPrefix())) {
typeValue = Constants.SCHEMA_PREFIX + marshalRecord.getNamespaceSeparator() + typeValue;
}
marshalRecord.attribute(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, Constants.SCHEMA_TYPE_ATTRIBUTE, xsiPrefix + Constants.COLON + Constants.SCHEMA_TYPE_ATTRIBUTE, typeValue);
}
}
marshalRecord.characters(root.getSchemaType(), object, null, false);
}
}
if (null != rootFragment && !(rootFragment.getLocalName().equals(Constants.EMPTY_STRING)) && isIncludeRoot()) {
marshalRecord.endElement(rootFragment, nr);
marshalRecord.endPrefixMappings(nr);
}
if (!isFragment()) {
marshalRecord.endDocument();
}
if (isXMLRoot) {
if (descriptor != null) {
marshalRecord.afterContainmentMarshal(null, root.getObject());
}
} else {
marshalRecord.afterContainmentMarshal(null, object);
}
marshalRecord.flush();
}
use of org.eclipse.persistence.internal.oxm.record.namespaces.PrefixMapperNamespaceResolver 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);
}
}
}
Aggregations