public static <T> XMLFilter getXMLFilterForClass(final Class<T> clazz) throws SAXException { final XMLFilter filter; final XmlSchema schema = clazz.getPackage().getAnnotation(XmlSchema.class); if (schema != null) { final String namespace = schema.namespace(); if (namespace != null && !"".equals(namespace)) { LogUtils.tracef(clazz, "found namespace %s for class %s", namespace, clazz); filter = new SimpleNamespaceFilter(namespace, true); } else { filter = new SimpleNamespaceFilter("", false); } } else { filter = new SimpleNamespaceFilter("", false); } final XMLReader xmlReader = XMLReaderFactory.createXMLReader(); filter.setParent(xmlReader); return filter; }
public Map<String,String> getXmlNs(String namespaceUri) { if(xmlNsCache==null) { xmlNsCache = new HashMap<String,Map<String,String>>(); for (ClassInfoImpl<T, C, F, M> ci : beans().values()) { XmlSchema xs = reader.getPackageAnnotation( XmlSchema.class, ci.getClazz(), null ); if(xs==null) continue; String uri = xs.namespace(); Map<String,String> m = xmlNsCache.get(uri); if(m==null) xmlNsCache.put(uri,m=new HashMap<String, String>()); for( XmlNs xns : xs.xmlns() ) { m.put(xns.prefix(),xns.namespaceURI()); } } } Map<String,String> r = xmlNsCache.get(namespaceUri); if(r!=null) return r; else return Collections.emptyMap(); }
/** * Parses an {@link XmlRootElement} annotation on a class * and determine the element name. * * @return null * if none was found. */ protected final QName parseElementName(ClassDeclT clazz) { XmlRootElement e = reader().getClassAnnotation(XmlRootElement.class,clazz,this); if(e==null) return null; String local = e.name(); if(local.equals("##default")) { // if defaulted... local = NameConverter.standard.toVariableName(nav().getClassShortName(clazz)); } String nsUri = e.namespace(); if(nsUri.equals("##default")) { // if defaulted ... XmlSchema xs = reader().getPackageAnnotation(XmlSchema.class,clazz,this); if(xs!=null) nsUri = xs.namespace(); else { nsUri = builder.defaultNsUri; } } return new QName(nsUri.intern(),local.intern()); }
final QName parseElementName(XmlElementDecl e) { String local = e.name(); String nsUri = e.namespace(); if(nsUri.equals("##default")) { // if defaulted ... XmlSchema xs = reader().getPackageAnnotation(XmlSchema.class, nav().getDeclaringClassForMethod(method),this); if(xs!=null) nsUri = xs.namespace(); else { nsUri = builder.defaultNsUri; } } return new QName(nsUri.intern(),local.intern()); }
/** * Returns the schema namespace URI for the specified JAXB type. The URI * can then be used to create a {@link QName} instance that is necessary * to instantiate a {@link JAXBElement}. * * @param type a JAXB generated type. * @return the namespace URI. */ @SuppressWarnings("unchecked") public static String getNamespaceUriForType( Class<?> type ) { String pkgInfoFqcn = type.getPackage().getName() + ".package-info"; try { /* * package-info.class is a special class generated by JAXB - it contains the schema namespace * URI for all generated classes. */ Class<?> pkgInfo = Class.forName( pkgInfoFqcn ); XmlSchema schema = pkgInfo.getAnnotation( XmlSchema.class ); return schema.namespace(); } catch ( ClassNotFoundException e ) { return null; } }
private String determineNamespaceUncached(Class<? extends Object> beanClass) { XmlType xmlType = beanClass.getAnnotation(XmlType.class); if (xmlType == null) { return null; } String namespace = xmlType.namespace(); if (BeanMarshaller.DEFAULT_PLACEHOLDER.equals(namespace)) { XmlSchema xmlSchema = beanClass.getPackage().getAnnotation(XmlSchema.class); namespace = xmlSchema.namespace(); } if (StringUtils.isBlank(namespace) || BeanMarshaller.DEFAULT_PLACEHOLDER.equals(namespace)) { return null; } return namespace; }
private QName determineTypeForClassUncached(Class<? extends Object> beanClass) { XmlType xmlType = beanClass.getAnnotation(XmlType.class); if (xmlType == null) { return null; } String namespace = xmlType.namespace(); if (BeanMarshaller.DEFAULT_PLACEHOLDER.equals(namespace)) { XmlSchema xmlSchema = beanClass.getPackage().getAnnotation(XmlSchema.class); namespace = xmlSchema.namespace(); } if (StringUtils.isBlank(namespace) || BeanMarshaller.DEFAULT_PLACEHOLDER.equals(namespace)) { return null; } return new QName(namespace, xmlType.name()); }
/** * Gets the xml schema namespace. * * @param clazz the clazz * * @return the xml schema namespace */ public static String getXmlSchemaNamespace(Class<?> clazz) { AnnotatedElement pack = clazz.getPackage(); if (pack == null) { return ""; } XmlSchema schema = pack.getAnnotation(XmlSchema.class); String namespace = null; if (schema != null) { namespace = schema.namespace(); } else { namespace = ""; } return namespace; }
/** * @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); }
public void write(OutputStream stream, T object) { try { JAXBContext context = JAXBContext.newInstance(object.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); XmlSchema schemaAnnotation = object.getClass().getPackage().getAnnotation(XmlSchema.class); if (schemaAnnotation != null && schemaAnnotation.location() != null) { marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaAnnotation.location()); // TODO: May just this ArtifactoryConfigVersion.getCurrent().getXsdLocation()); } marshaller.marshal(object, stream); } catch (Exception e) { throw new RuntimeException("Failed to write object to stream.", e); } finally { IOUtils.closeQuietly(stream); } }
private static String getPrefix(final Package targetPackage, String namespaceURI) { String prefix; final Map<String, String> namespacePrefixes = new HashMap<String, String>(); if (targetPackage != null) { final XmlSchema xmlSchemaAnnotation = targetPackage .getAnnotation(XmlSchema.class); if (xmlSchemaAnnotation != null) { for (XmlNs xmlns : xmlSchemaAnnotation.xmlns()) { namespacePrefixes.put(xmlns.namespaceURI(), xmlns.prefix()); } } } prefix = namespacePrefixes.get(namespaceURI); return prefix; }
private static String getNamespace(final Package targetPackage) { String namespaceURI; if (targetPackage == null) { namespaceURI = ""; } else { final XmlSchema xmlSchemaAnnotation = targetPackage .getAnnotation(XmlSchema.class); if (xmlSchemaAnnotation == null) { namespaceURI = ""; } else { final String packageNamespace = xmlSchemaAnnotation.namespace(); if (packageNamespace == null || "".equals(packageNamespace)) { namespaceURI = ""; } else { namespaceURI = packageNamespace; } } } return namespaceURI; }
private String determineNamespaceUncached(Class<?> beanClass) { XmlType xmlType = beanClass.getAnnotation(XmlType.class); if (xmlType == null) { return null; } String namespace = xmlType.namespace(); if (BeanMarshaller.DEFAULT_PLACEHOLDER.equals(namespace)) { XmlSchema xmlSchema = beanClass.getPackage().getAnnotation(XmlSchema.class); namespace = xmlSchema.namespace(); } if (StringUtils.isBlank(namespace) || BeanMarshaller.DEFAULT_PLACEHOLDER.equals(namespace)) { return null; } return namespace; }
private QName determineTypeForClassUncached(Class<?> beanClass) { XmlType xmlType = beanClass.getAnnotation(XmlType.class); if (xmlType == null) { return null; } String namespace = xmlType.namespace(); if (BeanMarshaller.DEFAULT_PLACEHOLDER.equals(namespace)) { XmlSchema xmlSchema = beanClass.getPackage().getAnnotation(XmlSchema.class); namespace = xmlSchema.namespace(); } if (StringUtils.isBlank(namespace) || BeanMarshaller.DEFAULT_PLACEHOLDER.equals(namespace)) { return null; } return new QName(namespace, xmlType.name()); }
/** * Get the id for the specified type. * * @param type The type. * @return The type id. */ public static String getTypeIdName(Class<?> type) { String ns = ""; if (type.getPackage() != null && type.getPackage().isAnnotationPresent(XmlSchema.class)) { ns = type.getPackage().getAnnotation(XmlSchema.class).namespace(); } String name = Introspector.decapitalize(type.getSimpleName()); if (type.isAnnotationPresent(XmlType.class)) { XmlType typeMeta = type.getAnnotation(XmlType.class); if (!"##default".equals(typeMeta.name())) { name = typeMeta.name(); } if (!"##default".equals(typeMeta.namespace())) { ns = typeMeta.namespace(); } } return ns + name; }
public static String getNamespace(Class<?> klass) { String namespace = null; XmlType xmlType = klass.getAnnotation(XmlType.class); if (xmlType != null) { String ns = xmlType.namespace(); if (!DEFAULT_NAMESPACE_VALUE.equals(ns)) { namespace = ns; } } if (namespace == null) { namespace = DEFAULT_NAMESPACE_VALUE; Package pkg = klass.getPackage(); XmlSchema xmlSchema = pkg.getAnnotation(XmlSchema.class); if (xmlSchema != null) { namespace = xmlSchema.namespace(); } } return namespace; }
public static <T> XMLFilter getXMLFilterForClass(final Class<T> clazz) throws SAXException { final XMLFilter filter; final XmlSchema schema = clazz.getPackage().getAnnotation(XmlSchema.class); if (schema != null) { final String namespace = schema.namespace(); if (namespace != null && !"".equals(namespace)) { LogUtils.debugf(clazz, "found namespace %s for class %s", namespace, clazz); filter = new SimpleNamespaceFilter(namespace, true); } else { filter = new SimpleNamespaceFilter("", false); } } else { filter = new SimpleNamespaceFilter("", false); } final XMLReader xmlReader = XMLReaderFactory.createXMLReader(); filter.setParent(xmlReader); return filter; }