Search in sources :

Example 1 with XmlNs

use of javax.xml.bind.annotation.XmlNs in project groovity by disney.

the class ModelXmlWriter method getElementName.

protected String getElementName(Object o) {
    Class<?> c = o.getClass();
    String name = ELEMENT_NAME_CACHE.get(c);
    if (name != null) {
        return name;
    }
    XmlRootElement xre = c.getAnnotation(XmlRootElement.class);
    String namespace = null;
    if (xre != null) {
        Package p = c.getPackage();
        if (p != null) {
            XmlSchema schema = p.getAnnotation(XmlSchema.class);
            if (schema != null && schema.xmlns() != null) {
                if (usedNamespacePrefixs == null) {
                    usedNamespacePrefixs = new HashMap<>();
                }
                for (XmlNs xns : schema.xmlns()) {
                    if (!usedNamespacePrefixs.containsKey(xns.namespaceURI())) {
                        usedNamespacePrefixs.put(xns.namespaceURI(), xns.prefix());
                    }
                }
            }
        }
        namespace = xre.namespace();
        if (!"##default".equals(xre.name())) {
            name = getTagName(namespace, xre.name());
        }
    } else {
        XmlElement x = c.getAnnotation(XmlElement.class);
        if (x != null) {
            namespace = x.namespace();
            if (!"##default".equals(x.name())) {
                name = getTagName(namespace, x.name());
            }
        }
    }
    if (name == null) {
        String oname = o.getClass().getSimpleName();
        if (oname.endsWith("[]")) {
            oname = oname.substring(0, oname.length() - 2);
        }
        if (Character.isUpperCase(oname.charAt(0))) {
            char[] namechars = oname.toCharArray();
            int stop = 1;
            for (int i = 1; i < namechars.length; i++) {
                if (Character.isUpperCase(namechars[i])) {
                    stop++;
                    continue;
                }
                if (stop > 1) {
                    stop--;
                }
                break;
            }
            for (int i = 0; i < stop; i++) {
                namechars[i] = Character.toLowerCase(namechars[i]);
            }
            oname = new String(namechars);
        }
        name = getTagName(namespace, oname);
    }
    ELEMENT_NAME_CACHE.put(c, name);
    return name;
}
Also used : XmlRootElement(javax.xml.bind.annotation.XmlRootElement) XmlSchema(javax.xml.bind.annotation.XmlSchema) XmlNs(javax.xml.bind.annotation.XmlNs) XmlElement(javax.xml.bind.annotation.XmlElement)

Example 2 with XmlNs

use of javax.xml.bind.annotation.XmlNs 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;
}
Also used : XmlSchema(javax.xml.bind.annotation.XmlSchema) XmlNs(javax.xml.bind.annotation.XmlNs) XmlType(javax.xml.bind.annotation.XmlType)

Example 3 with XmlNs

use of javax.xml.bind.annotation.XmlNs in project sis by apache.

the class AnnotationConsistencyCheck method testPackageAnnotations.

/**
 * Tests the annotations in the {@code package-info} files of Apache SIS implementations of the
 * interfaces enumerated in the {@code #types} array. More specifically this method tests that:
 *
 * <ul>
 *   <li>The prefixes declared in the {@link XmlNs} annotations match the
 *       {@linkplain Namespaces#getPreferredPrefix expected prefixes}.</li>
 * </ul>
 */
@Test
public void testPackageAnnotations() {
    final Set<Package> packages = new HashSet<>();
    for (final Class<?> type : types) {
        if (!CodeList.class.isAssignableFrom(type)) {
            testingClass = type.getCanonicalName();
            final Class<?> impl = getImplementation(type);
            if (impl != null) {
                testingClass = impl.getCanonicalName();
                final Package p = impl.getPackage();
                assertNotNull("Missing package information.", p);
                packages.add(p);
            }
        }
    }
    for (final Package p : packages) {
        for (final XmlNs ns : p.getAnnotation(XmlSchema.class).xmlns()) {
            testingClass = p.getName();
            final String namespace = ns.namespaceURI();
            assertEquals("Unexpected namespace prefix.", Namespaces.getPreferredPrefix(namespace, null), ns.prefix());
        }
    }
}
Also used : CodeList(org.opengis.util.CodeList) XmlSchema(javax.xml.bind.annotation.XmlSchema) XmlNs(javax.xml.bind.annotation.XmlNs) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

XmlNs (javax.xml.bind.annotation.XmlNs)3 XmlSchema (javax.xml.bind.annotation.XmlSchema)3 HashSet (java.util.HashSet)1 XmlElement (javax.xml.bind.annotation.XmlElement)1 XmlRootElement (javax.xml.bind.annotation.XmlRootElement)1 XmlType (javax.xml.bind.annotation.XmlType)1 Test (org.junit.Test)1 CodeList (org.opengis.util.CodeList)1