use of javax.xml.bind.annotation.XmlType in project sis by apache.
the class AnnotationConsistencyCheck method assertExpectedNamespace.
/**
* Replaces {@value #DEFAULT} value by the {@link XmlSchema} namespace if needed,
* then performs validity check on the resulting namespace. This method checks that:
*
* <ul>
* <li>The namespace is not redundant with the package-level {@link XmlSchema} namespace.</li>
* <li>The namespace is declared in a package-level {@link XmlNs} annotation.</li>
* <li>The namespace starts with the {@linkplain #getExpectedNamespaceStart expected namespace}.</li>
* </ul>
*
* @param namespace the namespace given by the {@code @XmlRootElement} or {@code @XmlElement} annotation.
* @param impl the implementation or wrapper class from which to get the package namespace.
* @param uml the {@code @UML} annotation, or {@code null} if none.
* @return the actual namespace (same as {@code namespace} if it was not {@value #DEFAULT}).
*/
private String assertExpectedNamespace(String namespace, final Class<?> impl, final UML uml) {
assertNotNull("Missing namespace.", namespace);
assertFalse("Missing namespace.", namespace.trim().isEmpty());
/*
* Get the namespace declared at the package level, and ensure the the
* given namespace is not redundant with that package-level namespace.
*/
final XmlSchema schema = impl.getPackage().getAnnotation(XmlSchema.class);
assertNotNull("Missing @XmlSchema annotation in package-info.", schema);
// May be XMLConstants.NULL_NS_URI
final String schemaNamespace = schema.namespace();
assertFalse("Namespace declaration is redundant with package-info @XmlSchema.", namespace.equals(schemaNamespace));
/*
* Resolve the namespace given in argument: using the class-level namespace if needed,
* or the package-level namespace if the class-level one is not defined.
*/
if (DEFAULT.equals(namespace)) {
final XmlType type = impl.getAnnotation(XmlType.class);
if (type == null || DEFAULT.equals(namespace = type.namespace())) {
namespace = schemaNamespace;
}
assertFalse("No namespace defined.", XMLConstants.NULL_NS_URI.equals(namespace));
}
/*
* Check that the namespace is declared in the package-level @XmlNs annotation.
* We do not verify the validity of those @XmlNs annotations, since this is the
* purpose of the 'testPackageAnnotations()' method.
*/
boolean found = false;
for (final XmlNs ns : schema.xmlns()) {
if (namespace.equals(ns.namespaceURI())) {
found = true;
break;
}
}
if (!found) {
fail("Namespace for " + impl + " is not declared in the package @XmlSchema.xmlns().");
}
/*
* Check that the namespace is one of the namespaces controlled by the specification.
* We check only the namespace start, since some specifications define many namespaces
* under a common root (e.g. "http://standards.iso.org/iso/19115/-3/").
*/
if (uml != null && false) {
// This verification is available only on development branches.
final String expected = getExpectedNamespaceStart(impl, uml);
if (!namespace.startsWith(expected)) {
fail("Expected " + expected + "… namespace for that ISO specification but got " + namespace);
}
}
return namespace;
}
use of javax.xml.bind.annotation.XmlType in project sis by apache.
the class PropertyComparator method defineOrder.
/**
* Uses the {@link XmlType} annotation for defining the property order.
*
* @param implementation the implementation class where to search for {@code XmlType} annotation.
* @param order the {@link #order} map where to store the properties order.
*/
private static void defineOrder(Class<?> implementation, final Map<Object, Integer> order) {
do {
final XmlType xml = implementation.getAnnotation(XmlType.class);
if (xml != null) {
final String[] propOrder = xml.propOrder();
for (int i = propOrder.length; --i >= 0; ) {
/*
* Add the entries in reverse order because we are iterating from the child class to
* the parent class, and we want the properties in the parent class to be sorted first.
* If duplicated properties are found, keep the first occurence (i.e. sort the property
* with the most specialized child that declared it).
*
* We make an exception for ResponsibleParty.role, which should be replaced by Party.role
* but this replacement is not yet effective in GeoAPI 3.0.
*/
final String prop = propOrder[i];
if (!"role".equals(prop) || !ResponsibleParty.class.isAssignableFrom(implementation)) {
order.putIfAbsent(prop, order.size());
}
}
}
implementation = implementation.getSuperclass();
} while (implementation != null);
}
use of javax.xml.bind.annotation.XmlType in project herd by FINRAOS.
the class DefinitionGenerator method getPropertyFromType.
/**
* Gets a property from the given fieldType. This method may be called recursively.
*
* @param fieldType the field type class.
*
* @return the property.
* @throws MojoExecutionException if any problems were encountered.
*/
private Property getPropertyFromType(Class<?> fieldType) throws MojoExecutionException {
Property property;
if (String.class.isAssignableFrom(fieldType)) {
property = new StringProperty();
} else if (Integer.class.isAssignableFrom(fieldType) || int.class.isAssignableFrom(fieldType)) {
property = new IntegerProperty();
} else if (Long.class.isAssignableFrom(fieldType) || long.class.isAssignableFrom(fieldType)) {
property = new LongProperty();
} else if (BigDecimal.class.isAssignableFrom(fieldType)) {
property = new DecimalProperty();
} else if (XMLGregorianCalendar.class.isAssignableFrom(fieldType)) {
property = new DateTimeProperty();
} else if (Boolean.class.isAssignableFrom(fieldType) || boolean.class.isAssignableFrom(fieldType)) {
property = new BooleanProperty();
} else if (Collection.class.isAssignableFrom(fieldType)) {
property = new ArrayProperty(new StringProperty());
} else if (fieldType.getAnnotation(XmlEnum.class) != null) {
/*
* Enums are a string property which have enum constants
*/
List<String> enums = new ArrayList<>();
for (Enum<?> anEnum : (Enum<?>[]) fieldType.getEnumConstants()) {
enums.add(anEnum.name());
}
property = new StringProperty()._enum(enums);
} else /*
* Recursively process complex objects which is a XmlType
*/
if (fieldType.getAnnotation(XmlType.class) != null) {
processDefinitionClass(fieldType);
property = new RefProperty(fieldType.getAnnotation(XmlType.class).name());
} else {
// Default to a string property in other cases.
property = new StringProperty();
}
log.debug("Field type \"" + fieldType.getName() + "\" is a property type \"" + property.getType() + "\".");
return property;
}
use of javax.xml.bind.annotation.XmlType in project zm-mailbox by Zimbra.
the class JaxbInfo method gatherInfo.
private void gatherInfo() {
XmlAccessorType accessorType;
rootElementName = null;
XmlAccessType accessType = null;
XmlRootElement rootE = jaxbClass.getAnnotation(XmlRootElement.class);
if (rootE != null) {
rootElementName = rootE.name();
}
xmlType = jaxbClass.getAnnotation(XmlType.class);
accessorType = jaxbClass.getAnnotation(XmlAccessorType.class);
if (accessorType == null) {
Package pkg = jaxbClass.getPackage();
accessorType = pkg.getAnnotation(XmlAccessorType.class);
}
if (accessorType != null) {
accessType = accessorType.value();
}
if (accessType == null) {
// Default value for JAXB
accessType = XmlAccessType.PUBLIC_MEMBER;
}
Field[] fields = jaxbClass.getDeclaredFields();
for (Field field : fields) {
XmlTransient xmlTransient = field.getAnnotation(XmlTransient.class);
if (xmlTransient != null) {
continue;
}
Annotation[] fAnnots = field.getAnnotations();
if ((fAnnots == null) || (fAnnots.length == 0)) {
boolean autoFields = (accessType.equals(XmlAccessType.PUBLIC_MEMBER) || accessType.equals(XmlAccessType.FIELD));
if (!autoFields) {
continue;
}
}
processFieldRelatedAnnotations(fAnnots, field.getName(), field.getGenericType());
}
Method[] methods = jaxbClass.getDeclaredMethods();
for (Method method : methods) {
XmlTransient xmlTransient = method.getAnnotation(XmlTransient.class);
if (xmlTransient != null) {
continue;
}
if (!isGetterOrSetter(method)) {
continue;
}
Annotation[] mAnnots = method.getAnnotations();
if ((mAnnots == null) || (mAnnots.length == 0)) {
boolean autoGettersSetters = (accessType.equals(XmlAccessType.PUBLIC_MEMBER) || accessType.equals(XmlAccessType.PROPERTY));
if (!autoGettersSetters) {
continue;
}
}
processFieldRelatedAnnotations(mAnnots, guessFieldNameFromGetterOrSetter(method.getName()), method.getGenericReturnType());
}
}
use of javax.xml.bind.annotation.XmlType in project tomee by apache.
the class JAXBEncoderDecoder method marshallException.
public static void marshallException(Marshaller marshaller, Exception elValue, MessagePartInfo part, Object source) {
XMLStreamWriter writer = getStreamWriter(source);
QName qn = part.getElementQName();
try {
writer.writeStartElement("ns1", qn.getLocalPart(), qn.getNamespaceURI());
Class<?> cls = part.getTypeClass();
XmlAccessType accessType = Utils.getXmlAccessType(cls);
String namespace = part.getElementQName().getNamespaceURI();
String attNs = namespace;
SchemaInfo sch = part.getMessageInfo().getOperation().getInterface().getService().getSchema(namespace);
if (sch == null) {
LOG.warning("Schema associated with " + namespace + " is null");
namespace = null;
attNs = null;
} else {
if (!sch.isElementFormQualified()) {
namespace = null;
}
if (!sch.isAttributeFormQualified()) {
attNs = null;
}
}
List<Member> combinedMembers = new ArrayList<>();
for (Field f : Utils.getFields(cls, accessType)) {
XmlAttribute at = f.getAnnotation(XmlAttribute.class);
if (at == null) {
combinedMembers.add(f);
} else {
QName fname = new QName(attNs, StringUtils.isEmpty(at.name()) ? f.getName() : at.name());
ReflectionUtil.setAccessible(f);
Object o = Utils.getFieldValue(f, elValue);
DocumentFragment frag = DOMUtils.getEmptyDocument().createDocumentFragment();
writeObject(marshaller, frag, newJAXBElement(fname, String.class, o));
if (attNs != null) {
writer.writeAttribute(attNs, fname.getLocalPart(), DOMUtils.getAllContent(frag));
} else {
writer.writeAttribute(fname.getLocalPart(), DOMUtils.getAllContent(frag));
}
}
}
for (Method m : Utils.getGetters(cls, accessType)) {
if (!m.isAnnotationPresent(XmlAttribute.class)) {
combinedMembers.add(m);
} else {
int idx = m.getName().startsWith("get") ? 3 : 2;
String name = m.getName().substring(idx);
name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
XmlAttribute at = m.getAnnotation(XmlAttribute.class);
QName mname = new QName(namespace, StringUtils.isEmpty(at.name()) ? name : at.name());
DocumentFragment frag = DOMUtils.getEmptyDocument().createDocumentFragment();
Object o = Utils.getMethodValue(m, elValue);
writeObject(marshaller, frag, newJAXBElement(mname, String.class, o));
if (attNs != null) {
writer.writeAttribute(attNs, mname.getLocalPart(), DOMUtils.getAllContent(frag));
} else {
writer.writeAttribute(mname.getLocalPart(), DOMUtils.getAllContent(frag));
}
}
}
XmlAccessorOrder xmlAccessorOrder = cls.getAnnotation(XmlAccessorOrder.class);
if (xmlAccessorOrder != null && xmlAccessorOrder.value().equals(XmlAccessOrder.ALPHABETICAL)) {
Collections.sort(combinedMembers, new Comparator<Member>() {
public int compare(Member m1, Member m2) {
return m1.getName().compareTo(m2.getName());
}
});
}
XmlType xmlType = cls.getAnnotation(XmlType.class);
if (xmlType != null && xmlType.propOrder().length > 1 && !xmlType.propOrder()[0].isEmpty()) {
final List<String> orderList = Arrays.asList(xmlType.propOrder());
Collections.sort(combinedMembers, new Comparator<Member>() {
public int compare(Member m1, Member m2) {
String m1Name = getName(m1);
String m2Name = getName(m2);
int m1Index = orderList.indexOf(m1Name);
int m2Index = orderList.indexOf(m2Name);
if (m1Index != -1 && m2Index != -1) {
return m1Index - m2Index;
}
if (m1Index == -1 && m2Index != -1) {
return 1;
}
if (m1Index != -1 && m2Index == -1) {
return -1;
}
return 0;
}
});
}
for (Member member : combinedMembers) {
if (member instanceof Field) {
Field f = (Field) member;
QName fname = new QName(namespace, f.getName());
ReflectionUtil.setAccessible(f);
if (JAXBSchemaInitializer.isArray(f.getGenericType())) {
writeArrayObject(marshaller, writer, fname, f.get(elValue));
} else {
Object o = Utils.getFieldValue(f, elValue);
writeObject(marshaller, writer, newJAXBElement(fname, String.class, o));
}
} else {
// it's a Method
Method m = (Method) member;
int idx = m.getName().startsWith("get") ? 3 : 2;
String name = m.getName().substring(idx);
name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
QName mname = new QName(namespace, name);
if (JAXBSchemaInitializer.isArray(m.getGenericReturnType())) {
writeArrayObject(marshaller, writer, mname, m.invoke(elValue));
} else {
Object o = Utils.getMethodValue(m, elValue);
writeObject(marshaller, writer, newJAXBElement(mname, String.class, o));
}
}
}
writer.writeEndElement();
writer.flush();
} catch (Exception e) {
throw new Fault(new Message("MARSHAL_ERROR", LOG, e.getMessage()), e);
} finally {
StaxUtils.close(writer);
}
}
Aggregations