use of javax.xml.bind.annotation.XmlSchema in project camel by apache.
the class TypeNameStrategy method findQNameForSoapActionOrType.
/**
* @return determine element name by using the XmlType.name() of the type to
* be marshalled and the XmlSchema.namespace() of the package-info
*/
public QName findQNameForSoapActionOrType(String soapAction, Class<?> type) {
XmlType xmlType = type.getAnnotation(XmlType.class);
if (xmlType == null || xmlType.name() == null) {
throw new RuntimeException("The type " + type.getName() + " needs to have an XmlType annotation with name");
}
String nameSpace = xmlType.namespace();
if ("##default".equals(nameSpace)) {
XmlSchema xmlSchema = type.getPackage().getAnnotation(XmlSchema.class);
if (xmlSchema != null) {
nameSpace = xmlSchema.namespace();
}
}
// prefer name from the XmlType, and fallback to XmlRootElement
String localName = xmlType.name();
if (ObjectHelper.isEmpty(localName)) {
XmlRootElement root = type.getAnnotation(XmlRootElement.class);
if (root != null) {
localName = root.name();
}
}
return new QName(nameSpace, localName);
}
use of javax.xml.bind.annotation.XmlSchema in project camel by apache.
the class XmlRootElementPreferringElementNameStrategy method findQNameForSoapActionOrType.
@Override
public QName findQNameForSoapActionOrType(String soapAction, Class<?> type) {
XmlType xmlType = type.getAnnotation(XmlType.class);
if (xmlType == null || xmlType.name() == null) {
throw new RuntimeException("The type " + type.getName() + " needs to have an XmlType annotation with name");
}
// prefer name+ns from the XmlRootElement, and fallback to XmlType
String localName = null;
String nameSpace = null;
XmlRootElement root = type.getAnnotation(XmlRootElement.class);
if (root != null) {
localName = ObjectHelper.isEmpty(localName) ? root.name() : localName;
nameSpace = isInValidNamespace(nameSpace) ? root.namespace() : nameSpace;
}
if (ObjectHelper.isEmpty(localName)) {
localName = xmlType.name();
}
if (isInValidNamespace(nameSpace)) {
XmlSchema xmlSchema = type.getPackage().getAnnotation(XmlSchema.class);
if (xmlSchema != null) {
nameSpace = xmlSchema.namespace();
}
}
if (isInValidNamespace(nameSpace)) {
nameSpace = xmlType.namespace();
}
if (ObjectHelper.isEmpty(localName) || isInValidNamespace(nameSpace)) {
throw new IllegalStateException("Unable to determine localName or namespace for type <" + type.getName() + ">");
}
return new QName(nameSpace, localName);
}
use of javax.xml.bind.annotation.XmlSchema in project spring-framework by spring-projects.
the class Jaxb2XmlDecoder method toQName.
/**
* Returns the qualified name for the given class, according to the mapping rules
* in the JAXB specification.
*/
QName toQName(Class<?> outputClass) {
String localPart;
String namespaceUri;
if (outputClass.isAnnotationPresent(XmlRootElement.class)) {
XmlRootElement annotation = outputClass.getAnnotation(XmlRootElement.class);
localPart = annotation.name();
namespaceUri = annotation.namespace();
} else if (outputClass.isAnnotationPresent(XmlType.class)) {
XmlType annotation = outputClass.getAnnotation(XmlType.class);
localPart = annotation.name();
namespaceUri = annotation.namespace();
} else {
throw new IllegalArgumentException("Outputclass [" + outputClass + "] is " + "neither annotated with @XmlRootElement nor @XmlType");
}
if (JAXB_DEFAULT_ANNOTATION_VALUE.equals(localPart)) {
localPart = ClassUtils.getShortNameAsProperty(outputClass);
}
if (JAXB_DEFAULT_ANNOTATION_VALUE.equals(namespaceUri)) {
Package outputClassPackage = outputClass.getPackage();
if (outputClassPackage != null && outputClassPackage.isAnnotationPresent(XmlSchema.class)) {
XmlSchema annotation = outputClassPackage.getAnnotation(XmlSchema.class);
namespaceUri = annotation.namespace();
} else {
namespaceUri = XMLConstants.NULL_NS_URI;
}
}
return new QName(namespaceUri, localPart);
}
use of javax.xml.bind.annotation.XmlSchema in project opennms by OpenNMS.
the class JaxbUtils method getValidatorFor.
private static Schema getValidatorFor(final Class<?> clazz) {
LOG.trace("finding XSD for class {}", clazz);
if (m_schemas.containsKey(clazz)) {
return m_schemas.get(clazz);
}
final List<Source> sources = new ArrayList<Source>();
final SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
for (final String schemaFileName : getSchemaFilesFor(clazz)) {
InputStream schemaInputStream = null;
try {
if (schemaInputStream == null) {
final File schemaFile = new File(System.getProperty("opennms.home") + "/share/xsds/" + schemaFileName);
if (schemaFile.exists()) {
LOG.trace("Found schema file {} related to {}", schemaFile, clazz);
schemaInputStream = new FileInputStream(schemaFile);
}
;
}
if (schemaInputStream == null) {
final File schemaFile = new File("target/xsds/" + schemaFileName);
if (schemaFile.exists()) {
LOG.trace("Found schema file {} related to {}", schemaFile, clazz);
schemaInputStream = new FileInputStream(schemaFile);
}
;
}
if (schemaInputStream == null) {
final URL schemaResource = Thread.currentThread().getContextClassLoader().getResource("xsds/" + schemaFileName);
if (schemaResource == null) {
LOG.debug("Unable to load resource xsds/{} from the classpath.", schemaFileName);
} else {
LOG.trace("Found schema resource {} related to {}", schemaResource, clazz);
schemaInputStream = schemaResource.openStream();
}
}
if (schemaInputStream == null) {
LOG.trace("Did not find a suitable XSD. Skipping.");
continue;
} else {
sources.add(new StreamSource(schemaInputStream));
}
} catch (final Throwable t) {
LOG.warn("an error occurred while attempting to load {} for validation", schemaFileName);
continue;
}
}
if (sources.size() == 0) {
LOG.debug("No schema files found for validating {}", clazz);
return null;
}
LOG.trace("Schema sources: {}", sources);
try {
final Schema schema = factory.newSchema(sources.toArray(EMPTY_SOURCE_LIST));
m_schemas.put(clazz, schema);
return schema;
} catch (final SAXException e) {
LOG.warn("an error occurred while attempting to load schema validation files for class {}", clazz, e);
return null;
}
}
use of javax.xml.bind.annotation.XmlSchema in project midpoint by Evolveum.
the class JAXBUtil method findClassForType.
public static <T> Class<T> findClassForType(QName typeName, Package pkg) {
String namespace = packageNamespaces.get(pkg);
if (namespace == null) {
XmlSchema xmlSchemaAnnotation = pkg.getAnnotation(XmlSchema.class);
namespace = xmlSchemaAnnotation.namespace();
packageNamespaces.put(pkg, namespace);
}
if (!namespace.equals(typeName.getNamespaceURI())) {
throw new IllegalArgumentException("Looking for type in namespace " + typeName.getNamespaceURI() + ", but the package annotation indicates namespace " + namespace);
}
Class clazz = classQNames.get(typeName);
if (clazz != null && pkg.equals(clazz.getPackage())) {
return clazz;
}
if (!scannedPackages.contains(pkg.getName())) {
scannedPackages.add(pkg.getName());
Class foundClass = null;
for (Class c : ClassPathUtil.listClasses(pkg)) {
QName foundTypeQName = getTypeQName(c);
if (foundTypeQName != null) {
classQNames.put(foundTypeQName, c);
}
if (typeName.equals(foundTypeQName)) {
foundClass = c;
}
}
// may be null but that's OK
return foundClass;
}
return null;
}
Aggregations