use of org.eclipse.persistence.oxm.NamespaceResolver 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.NamespaceResolver in project eclipselink by eclipse-ee4j.
the class DynamicJAXBUsingXMLNamesTestCases method testCreateEntityByXPathNameCollision2.
public void testCreateEntityByXPathNameCollision2() throws Exception {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream iStream = classLoader.getResourceAsStream(EXAMPLE_XSD_2);
if (iStream == null) {
fail("Couldn't load metadata file [" + EXAMPLE_XSD_2 + "]");
}
Map<String, InputStream> properties = new HashMap<String, InputStream>();
properties.put(DynamicJAXBContextFactory.XML_SCHEMA_KEY, iStream);
NamespaceResolver nsResolver = new NamespaceResolver();
nsResolver.put("ns0", "mynamespace");
DynamicJAXBContext jaxbContext = (DynamicJAXBContext) JAXBContext.newInstance("org.eclipse.persistence.testing.jaxb.dynamic", classLoader, properties);
DynamicEntity person = (DynamicEntity) jaxbContext.createByQualifiedName("mynamespace", "person", false);
assertNotNull("Could not create Dynamic Entity.", person);
// Set name by XPath
jaxbContext.setValueByXPath(person, "ns0:first-name/text()", nsResolver, "Larry");
// Create address by XPath, set street by XPath
DynamicEntity address = jaxbContext.createByXPath(person, "ns0:address", nsResolver, DynamicEntity.class);
jaxbContext.setValueByXPath(address, "ns0:street/text()", nsResolver, "400 CNN Plaza");
// Create person's address by XPath
jaxbContext.setValueByXPath(person, "ns0:address", nsResolver, address);
Document marshalDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
jaxbContext.createMarshaller().marshal(person, marshalDoc);
DynamicEntity person2 = (DynamicEntity) jaxbContext.createUnmarshaller().unmarshal(marshalDoc);
DynamicEntity address2 = jaxbContext.getValueByXPath(person2, "ns0:address", nsResolver, DynamicEntity.class);
String newName = jaxbContext.getValueByXPath(person2, "ns0:first-name/text()", nsResolver, String.class);
String newStreet = jaxbContext.getValueByXPath(address2, "ns0:street/text()", nsResolver, String.class);
String newStreet2 = jaxbContext.getValueByXPath(person2, "ns0:address/ns0:street/text()", nsResolver, String.class);
assertEquals("Larry", newName);
assertEquals("400 CNN Plaza", newStreet);
assertEquals("400 CNN Plaza", newStreet2);
}
use of org.eclipse.persistence.oxm.NamespaceResolver in project eclipselink by eclipse-ee4j.
the class DynamicJAXBUsingXMLNamesTestCases method testCreateEntityByXMLName.
public void testCreateEntityByXMLName() throws Exception {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream iStream = classLoader.getResourceAsStream(EXAMPLE_OXM);
if (iStream == null) {
fail("Couldn't load metadata file [" + EXAMPLE_OXM + "]");
}
HashMap<String, Source> metadataSourceMap = new HashMap<String, Source>();
metadataSourceMap.put("org.eclipse.persistence.testing.jaxb.dynamic", new StreamSource(iStream));
Map<String, Map<String, Source>> properties = new HashMap<String, Map<String, Source>>();
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, metadataSourceMap);
DynamicJAXBContext jaxbContext = (DynamicJAXBContext) JAXBContext.newInstance("org.eclipse.persistence.testing.jaxb.dynamic", classLoader, properties);
DynamicEntity employee = (DynamicEntity) jaxbContext.createByQualifiedName(EMPLOYEE_NAMESPACE, EMPLOYEE_TYPE_NAME, false);
assertNotNull("Could not create Dynamic Entity.", employee);
NamespaceResolver nsResolver = new NamespaceResolver();
nsResolver.put("ns0", "mynamespace");
jaxbContext.setValueByXPath(employee, "name/text()", nsResolver, "Larry King");
jaxbContext.setValueByXPath(employee, "employee-id/text()", nsResolver, "CA34287");
Document marshalDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
jaxbContext.createMarshaller().marshal(employee, marshalDoc);
DynamicEntity employee2 = (DynamicEntity) jaxbContext.createUnmarshaller().unmarshal(marshalDoc);
String newName = jaxbContext.getValueByXPath(employee2, "name/text()", nsResolver, String.class);
String newId = jaxbContext.getValueByXPath(employee2, "employee-id/text()", nsResolver, String.class);
assertEquals("Larry King", newName);
assertEquals("CA34287", newId);
}
use of org.eclipse.persistence.oxm.NamespaceResolver in project eclipselink by eclipse-ee4j.
the class AnnotationsProcessor method generateWrapperForMapClass.
private Class<?> generateWrapperForMapClass(JavaClass mapClass, JavaClass keyClass, JavaClass valueClass, TypeMappingInfo typeMappingInfo) {
String packageName = JAXB_DEV;
NamespaceResolver combinedNamespaceResolver = new NamespaceResolver();
if (!helper.isBuiltInJavaType(keyClass)) {
String keyPackageName = keyClass.getPackageName();
packageName = packageName + DOT_CHR + keyPackageName;
NamespaceInfo keyNamespaceInfo = getPackageInfoForPackage(keyClass).getNamespaceInfo();
if (keyNamespaceInfo != null) {
java.util.Vector<Namespace> namespaces = keyNamespaceInfo.getNamespaceResolver().getNamespaces();
for (Namespace n : namespaces) {
combinedNamespaceResolver.put(n.getPrefix(), n.getNamespaceURI());
}
}
}
if (!helper.isBuiltInJavaType(valueClass)) {
String valuePackageName = valueClass.getPackageName();
packageName = packageName + DOT_CHR + valuePackageName;
NamespaceInfo valueNamespaceInfo = getPackageInfoForPackage(valueClass).getNamespaceInfo();
if (valueNamespaceInfo != null) {
java.util.Vector<Namespace> namespaces = valueNamespaceInfo.getNamespaceResolver().getNamespaces();
for (Namespace n : namespaces) {
combinedNamespaceResolver.put(n.getPrefix(), n.getNamespaceURI());
}
}
}
String namespace = this.defaultTargetNamespace;
if (namespace == null) {
namespace = EMPTY_STRING;
}
PackageInfo packageInfo = packageToPackageInfoMappings.get(mapClass.getPackageName());
if (packageInfo == null) {
packageInfo = getPackageToPackageInfoMappings().get(packageName);
} else {
if (packageInfo.getNamespace() != null) {
namespace = packageInfo.getNamespace();
}
getPackageToPackageInfoMappings().put(packageName, packageInfo);
}
if (packageInfo == null) {
packageInfo = new PackageInfo();
packageInfo.setNamespaceInfo(new NamespaceInfo());
packageInfo.setNamespace(namespace);
packageInfo.setNamespaceResolver(combinedNamespaceResolver);
getPackageToPackageInfoMappings().put(packageName, packageInfo);
}
int beginIndex = keyClass.getName().lastIndexOf(DOT_CHR) + 1;
String keyName = keyClass.getName().substring(beginIndex);
int dollarIndex = keyName.indexOf(DOLLAR_SIGN_CHR);
if (dollarIndex > -1) {
keyName = keyName.substring(dollarIndex + 1);
}
beginIndex = valueClass.getName().lastIndexOf(DOT_CHR) + 1;
String valueName = valueClass.getName().substring(beginIndex);
dollarIndex = valueName.indexOf(DOLLAR_SIGN_CHR);
if (dollarIndex > -1) {
valueName = valueName.substring(dollarIndex + 1);
}
String collectionClassShortName = mapClass.getRawName().substring(mapClass.getRawName().lastIndexOf(DOT_CHR) + 1);
String suggestedClassName = keyName + valueName + collectionClassShortName;
String qualifiedClassName = packageName + DOT_CHR + suggestedClassName;
qualifiedClassName = getNextAvailableClassName(qualifiedClassName);
String qualifiedInternalClassName = qualifiedClassName.replace(DOT_CHR, SLASH_CHR);
String internalKeyName = keyClass.getQualifiedName().replace(DOT_CHR, SLASH_CHR);
String internalValueName = valueClass.getQualifiedName().replace(DOT_CHR, SLASH_CHR);
Type mapType = Type.getType(L + mapClass.getRawName().replace(DOT_CHR, SLASH_CHR) + SEMI_COLON);
EclipseLinkASMClassWriter cw = new EclipseLinkASMClassWriter();
String sig = "Lorg/eclipse/persistence/internal/jaxb/many/MapValue<L" + mapType.getInternalName() + "<L" + internalKeyName + ";L" + internalValueName + ";>;>;";
cw.visit(Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, qualifiedInternalClassName, sig, "org/eclipse/persistence/internal/jaxb/many/MapValue", null);
// Write Field: @... public Map entry
String fieldSig = L + mapType.getInternalName() + "<L" + internalKeyName + ";L" + internalValueName + ";>;";
FieldVisitor fv = cw.visitField(Opcodes.ACC_PUBLIC, "entry", L + mapType.getInternalName() + SEMI_COLON, fieldSig, null);
fv.visitAnnotation(Type.getDescriptor(XmlElement.class), true);
if (typeMappingInfo != null) {
Annotation[] annotations = typeMappingInfo.getAnnotations();
if (annotations != null) {
for (Annotation nextAnnotation : annotations) {
if (nextAnnotation != null && !(nextAnnotation instanceof XmlElement) && !(nextAnnotation instanceof XmlJavaTypeAdapter)) {
String annotationClassName = nextAnnotation.annotationType().getName();
AnnotationVisitor av = fv.visitAnnotation(L + annotationClassName.replace(DOT_CHR, SLASH_CHR) + SEMI_COLON, true);
for (Method next : nextAnnotation.annotationType().getDeclaredMethods()) {
try {
Object nextValue = next.invoke(nextAnnotation);
if (nextValue instanceof Class) {
Type nextType = Type.getType(L + ((Class) nextValue).getName().replace(DOT_CHR, SLASH_CHR) + SEMI_COLON);
nextValue = nextType;
}
av.visit(next.getName(), nextValue);
} catch (InvocationTargetException ignored) {
// ignore the invocation target exception here.
} catch (IllegalAccessException ignored) {
// ignore the illegal access exception here.
}
}
av.visitEnd();
}
}
}
}
fv.visitEnd();
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "org/eclipse/persistence/internal/jaxb/many/MapValue", "<init>", "()V", false);
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
// Write: @XmlTransient public void setItem(???)
String methodSig = "(L" + mapType.getInternalName() + "<L" + internalKeyName + ";L" + internalValueName + ";>;)V";
mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "setItem", "(L" + mapType.getInternalName() + ";)V", methodSig, null);
// TODO: Verify that we really want to put @XmlTranient on setItem
// method
mv.visitAnnotation("Ljakarta/xml/bind/annotation/XmlTransient;", true);
Label l0 = new Label();
mv.visitLabel(l0);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitVarInsn(Opcodes.ALOAD, 1);
mv.visitFieldInsn(Opcodes.PUTFIELD, qualifiedInternalClassName, "entry", L + mapType.getInternalName() + SEMI_COLON);
mv.visitInsn(Opcodes.RETURN);
Label l1 = new Label();
mv.visitLabel(l1);
// Replacement?:LocalVariableTypeTableAttribute cvAttr = new
// LocalVariableTypeTableAttribute();
// mv.visitAttribute(cvAttr);
mv.visitMaxs(2, 2);
mv.visitEnd();
// Write @XmlTransient public ??? getItem()
methodSig = "()L" + mapType.getInternalName() + "<L" + internalKeyName + ";L" + internalValueName + ";>;";
mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getItem", "()L" + mapType.getInternalName() + SEMI_COLON, methodSig, null);
mv.visitAnnotation("Ljakarta/xml/bind/annotation/XmlTransient;", true);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitFieldInsn(Opcodes.GETFIELD, qualifiedInternalClassName, "entry", L + mapType.getInternalName() + SEMI_COLON);
mv.visitInsn(Opcodes.ARETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
mv = cw.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_BRIDGE + Opcodes.ACC_SYNTHETIC, "getItem", "()Ljava/lang/Object;", null, null);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, qualifiedInternalClassName, "getItem", "()L" + mapType.getInternalName() + SEMI_COLON, false);
mv.visitInsn(Opcodes.ARETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
mv = cw.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_BRIDGE + Opcodes.ACC_SYNTHETIC, "setItem", "(Ljava/lang/Object;)V", null, null);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitVarInsn(Opcodes.ALOAD, 1);
mv.visitTypeInsn(Opcodes.CHECKCAST, mapType.getInternalName());
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, qualifiedInternalClassName, "setItem", "(L" + mapType.getInternalName() + ";)V", false);
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(2, 2);
mv.visitEnd();
// Write @XmlType(namespace)
AnnotationVisitor av = cw.visitAnnotation("Ljakarta/xml/bind/annotation/XmlType;", true);
av.visit("namespace", namespace);
cw.visitEnd();
byte[] classBytes = cw.toByteArray();
return generateClassFromBytes(qualifiedClassName, classBytes);
}
use of org.eclipse.persistence.oxm.NamespaceResolver in project eclipselink by eclipse-ee4j.
the class Property method isPositional.
/**
* Indicates if this property is mapped by position, i.e. 'name="data[1]"',
* or is mapped by attribute value (predicate mapping), i.e.
* 'personal-info[@pi-type='last-name']/name[@name-type='surname']/text()'
*/
public boolean isPositional() {
if (getXmlPath() == null) {
return false;
}
Field<XMLConversionManager, NamespaceResolver> field = new XMLField(getXmlPath());
XPathFragment frag = field.getXPathFragment();
// loop until we have the last non-null, non-attribute, non-text fragment
while (true) {
if (frag.getNextFragment() != null && !frag.getNextFragment().isAttribute() && !frag.getNextFragment().nameIsText()) {
frag = frag.getNextFragment();
} else {
break;
}
}
return frag.containsIndex() || frag.getPredicate() != null;
}
Aggregations