Java Code Examples for javax.xml.bind.annotation.XmlSchema#namespace()

The following examples show how to use javax.xml.bind.annotation.XmlSchema#namespace() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TypeInfoSetImpl.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
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();
}
 
Example 2
Source File: TypeInfoSetImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
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();
}
 
Example 3
Source File: TypeInfoImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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());
}
 
Example 4
Source File: ElementInfoImpl.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
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());
}
 
Example 5
Source File: ElementInfoImpl.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
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());
}
 
Example 6
Source File: TypeInfoImpl.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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());
}
 
Example 7
Source File: TypeInfoSetImpl.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
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();
}
 
Example 8
Source File: TypeInfoSetImpl.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
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();
}
 
Example 9
Source File: RawXJC2Mojo.java    From maven-jaxb2-plugin with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void setupBindInfoPackage() {
	String packageInfoClassName = "com.sun.tools.xjc.reader.xmlschema.bindinfo.package-info";
	try {
		final Class<?> packageInfoClass = Class.forName(packageInfoClassName);
		final XmlSchema xmlSchema = packageInfoClass.getAnnotation(XmlSchema.class);
		if (xmlSchema == null) {
			getLog().warn(MessageFormat.format(
					"Class [{0}] is missing the [{1}] annotation. Processing bindings will probably fail.",
					packageInfoClassName, XmlSchema.class.getName()));
		} else {
			final String namespace = xmlSchema.namespace();
			if (!JAXB_NSURI.equals(namespace)) {
				getLog().warn(MessageFormat.format(
						"Namespace of the [{0}] annotation is [{1}] and does not match [{2}]. Processing bindings will probably fail.",
						namespace, XmlSchema.class.getName(), JAXB_NSURI));
			}
		}

	} catch (ClassNotFoundException cnfex) {
		getLog().warn(
				MessageFormat.format("Class [{0}] could not be found. Processing bindings will probably faile.",
						packageInfoClassName),
				cnfex);
	}

}
 
Example 10
Source File: XmlTypeUtils.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 6 votes vote down vote up
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;
}
 
Example 11
Source File: TypeInfoSetImpl.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
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();
}
 
Example 12
Source File: MarshallerHelper.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
private static QName translate(Class<?> clazz) {
   Annotation[] arr$ = clazz.getPackage().getAnnotations();
   int len$ = arr$.length;

   for(int i$ = 0; i$ < len$; ++i$) {
      Annotation annotation = arr$[i$];
      if (annotation instanceof XmlSchema) {
         XmlSchema schema = (XmlSchema)annotation;
         return new QName(schema.namespace(), clazz.getSimpleName());
      }
   }

   LOG.debug("Unable to determine QName for class:" + clazz + " using package as namespace.");
   return new QName(clazz.getPackage().getName(), clazz.getSimpleName());
}
 
Example 13
Source File: MarshallerHelper.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
private static QName translate(Class<?> clazz) {
   Annotation[] arr$ = clazz.getPackage().getAnnotations();
   int len$ = arr$.length;

   for(int i$ = 0; i$ < len$; ++i$) {
      Annotation annotation = arr$[i$];
      if (annotation instanceof XmlSchema) {
         XmlSchema schema = (XmlSchema)annotation;
         return new QName(schema.namespace(), clazz.getSimpleName());
      }
   }

   LOG.debug("Unable to determine QName for class:" + clazz + " using package as namespace.");
   return new QName(clazz.getPackage().getName(), clazz.getSimpleName());
}
 
Example 14
Source File: MarshallerHelper.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
private static QName translate(Class<?> clazz) {
   Annotation[] arr$ = clazz.getPackage().getAnnotations();
   int len$ = arr$.length;

   for(int i$ = 0; i$ < len$; ++i$) {
      Annotation annotation = arr$[i$];
      if (annotation instanceof XmlSchema) {
         XmlSchema schema = (XmlSchema)annotation;
         return new QName(schema.namespace(), clazz.getSimpleName());
      }
   }

   LOG.debug("Unable to determine QName for class:" + clazz + " using package as namespace.");
   return new QName(clazz.getPackage().getName(), clazz.getSimpleName());
}
 
Example 15
Source File: TypeInfoImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Parses a (potentially-null) {@link XmlType} annotation on a class
 * and determine the actual value.
 *
 * @param clazz
 *      The class on which the XmlType annotation is checked.
 * @param t
 *      The {@link XmlType} annotation on the clazz. This value
 *      is taken as a parameter to improve the performance for the case where
 *      't' is pre-computed.
 */
protected final QName parseTypeName(ClassDeclT clazz, XmlType t) {
    String nsUri="##default";
    String local="##default";
    if(t!=null) {
        nsUri = t.namespace();
        local = t.name();
    }

    if(local.length()==0)
        return null; // anonymous


    if(local.equals("##default"))
        // if defaulted ...
        local = NameConverter.standard.toVariableName(nav().getClassShortName(clazz));

    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());
}
 
Example 16
Source File: PropertyInfoImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private QName calcXmlName(String uri,String local) {
    // compute the default
    TODO.checkSpec();
    if(local.length()==0 || local.equals("##default"))
        local = seed.getName();
    if(uri.equals("##default")) {
        XmlSchema xs = reader().getPackageAnnotation( XmlSchema.class, parent.getClazz(), this );
        // JAX-RPC doesn't want the default namespace URI swapping to take effect to
        // local "unqualified" elements. UGLY.
        if(xs!=null) {
            switch(xs.elementFormDefault()) {
            case QUALIFIED:
                QName typeName = parent.getTypeName();
                if(typeName!=null)
                    uri = typeName.getNamespaceURI();
                else
                    uri = xs.namespace();
                if(uri.length()==0)
                    uri = parent.builder.defaultNsUri;
                break;
            case UNQUALIFIED:
            case UNSET:
                uri = "";
            }
        } else {
            uri = "";
        }
    }
    return new QName(uri.intern(),local.intern());
}
 
Example 17
Source File: PropertyInfoImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private QName calcXmlName(String uri,String local) {
    // compute the default
    TODO.checkSpec();
    if(local.length()==0 || local.equals("##default"))
        local = seed.getName();
    if(uri.equals("##default")) {
        XmlSchema xs = reader().getPackageAnnotation( XmlSchema.class, parent.getClazz(), this );
        // JAX-RPC doesn't want the default namespace URI swapping to take effect to
        // local "unqualified" elements. UGLY.
        if(xs!=null) {
            switch(xs.elementFormDefault()) {
            case QUALIFIED:
                QName typeName = parent.getTypeName();
                if(typeName!=null)
                    uri = typeName.getNamespaceURI();
                else
                    uri = xs.namespace();
                if(uri.length()==0)
                    uri = parent.builder.defaultNsUri;
                break;
            case UNQUALIFIED:
            case UNSET:
                uri = "";
            }
        } else {
            uri = "";
        }
    }
    return new QName(uri.intern(),local.intern());
}
 
Example 18
Source File: TypeInfoImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Parses a (potentially-null) {@link XmlType} annotation on a class
 * and determine the actual value.
 *
 * @param clazz
 *      The class on which the XmlType annotation is checked.
 * @param t
 *      The {@link XmlType} annotation on the clazz. This value
 *      is taken as a parameter to improve the performance for the case where
 *      't' is pre-computed.
 */
protected final QName parseTypeName(ClassDeclT clazz, XmlType t) {
    String nsUri="##default";
    String local="##default";
    if(t!=null) {
        nsUri = t.namespace();
        local = t.name();
    }

    if(local.length()==0)
        return null; // anonymous


    if(local.equals("##default"))
        // if defaulted ...
        local = NameConverter.standard.toVariableName(nav().getClassShortName(clazz));

    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());
}
 
Example 19
Source File: TypeInfoImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Parses a (potentially-null) {@link XmlType} annotation on a class
 * and determine the actual value.
 *
 * @param clazz
 *      The class on which the XmlType annotation is checked.
 * @param t
 *      The {@link XmlType} annotation on the clazz. This value
 *      is taken as a parameter to improve the performance for the case where
 *      't' is pre-computed.
 */
protected final QName parseTypeName(ClassDeclT clazz, XmlType t) {
    String nsUri="##default";
    String local="##default";
    if(t!=null) {
        nsUri = t.namespace();
        local = t.name();
    }

    if(local.length()==0)
        return null; // anonymous


    if(local.equals("##default"))
        // if defaulted ...
        local = NameConverter.standard.toVariableName(nav().getClassShortName(clazz));

    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());
}
 
Example 20
Source File: AnnotationConsistencyCheck.java    From sis with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
    final String schemaNamespace = schema.namespace();      // May be XMLConstants.NULL_NS_URI
    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;
}