Example usage for javax.xml.bind.annotation XmlNsForm QUALIFIED

List of usage examples for javax.xml.bind.annotation XmlNsForm QUALIFIED

Introduction

In this page you can find the example usage for javax.xml.bind.annotation XmlNsForm QUALIFIED.

Prototype

XmlNsForm QUALIFIED

To view the source code for javax.xml.bind.annotation XmlNsForm QUALIFIED.

Click Source Link

Usage

From source file:org.javelin.sws.ext.bind.internal.model.AnalyzeSimpleTypesTest.java

@Test
public void analyzeBuiltInDatatype() throws Exception {
    TypedPatternRegistry context = (TypedPatternRegistry) SweJaxbContextFactory.createContext("", null);

    PropertyCallback<String> pc = new PropertyCallback<String>(context, String.class,
            SweJaxbConstants.XSD_STRING, XmlAccessType.NONE, XmlNsForm.QUALIFIED, XmlNsForm.UNQUALIFIED);
    TypedPattern<String> pattern = pc.analyze();
    assertTrue(pattern.isSimpleType());//from   ww w .  j a va2 s .co  m
    assertThat(pattern.getJavaType(), equalTo(String.class));
    assertThat(pattern.getSchemaType(), equalTo(new QName(Constants.URI_2001_SCHEMA_XSD, "string")));

    @SuppressWarnings("unchecked")
    Map<Class<?>, TypedPattern<?>> patterns = (Map<Class<?>, TypedPattern<?>>) ReflectionTestUtils
            .getField(context, "patterns");
    TypedPattern<?> pattern2 = patterns.get(String.class);

    assertSame("Primitive and built-in types should be analyzed only at context creation time.", pattern,
            pattern2);
}

From source file:com._4dconcept.springframework.data.marklogic.core.mapping.BasicMarklogicPersistentProperty.java

@Override
public QName getQName() {
    String namespaceUri = null;/*from   ww  w.j a v  a  2 s. c  om*/
    String localName = null;

    XmlElement xmlElement = this.findAnnotation(XmlElement.class);
    if (xmlElement != null) {
        if (!xmlElement.namespace().equals("##default")) {
            namespaceUri = xmlElement.namespace();
        }

        localName = xmlElement.name().equals("##default") ? getName() : xmlElement.name();
    }

    if (namespaceUri == null && this.getField() != null) {
        XmlSchema xmlSchema = this.getField().getDeclaringClass().getPackage().getAnnotation(XmlSchema.class);
        if (xmlSchema != null) {
            if (xmlSchema.elementFormDefault().equals(XmlNsForm.QUALIFIED)) {
                namespaceUri = xmlSchema.namespace();
            }
        }
    }

    if (namespaceUri == null)
        namespaceUri = "";
    if (localName == null)
        localName = getName();

    return new QName(namespaceUri, localName);
}

From source file:org.javelin.sws.ext.bind.internal.metadata.PropertyCallback.java

/**
 * @param field/*from w  ww  .  j  ava 2s  .  c om*/
 * @param class1
 */
private <P> void doWithPropertySafe(PropertyMetadata<T, P> metadata)
        throws IllegalArgumentException, IllegalAccessException {

    AnnotatedElement[] accessors = metadata.getAccessors();

    if (this.findJaxbAnnotation(accessors, XmlTransient.class) != null)
        return;

    XmlSchemaType xmlSchemaType = this.findJaxbAnnotation(accessors, XmlSchemaType.class);
    // a pattern for property's class - force creating
    TypedPattern<P> pattern = null;
    if (xmlSchemaType != null) {
        // the schema type determines the pattern - if it is not present in the registry, it will be present after determining it on the basis of Java class
        // of the property
        QName typeName = new QName(xmlSchemaType.namespace(), xmlSchemaType.name());
        pattern = this.patternRegistry.findPatternByType(typeName, metadata.getPropertyClass());
        if (log.isTraceEnabled() && pattern != null)
            log.trace("-- @XmlSchemaType points to {}", pattern.toString());
    }

    if (pattern == null)
        pattern = this.patternRegistry.determineAndCacheXmlPattern(metadata.getPropertyClass());

    // is it value or list?
    XmlValue xmlValue = this.findJaxbAnnotation(accessors, XmlValue.class);
    XmlList xmlList = this.findJaxbAnnotation(accessors, XmlList.class);
    if (xmlValue != null || xmlList != null) {
        // the field's class must be a simpleType, i.e., a type convertible to String, which is either:
        //  - a type registered in org.javelin.sws.ext.bind.internal.BuiltInMappings.initialize()
        //  - a type which has only one property annotated with @XmlValue
        // a type with one @XmlValue property + non-zero @XmlAttribute properties is complex type with simple content, not a simple type
        if (!(pattern.isSimpleType() && pattern instanceof SimpleContentPattern))
            throw new RuntimeException("TODO: should be simpleType");

        if (log.isTraceEnabled())
            log.trace("-- @XmlValue property \"{}\" of type {} mapped to {}", metadata.getPropertyName(),
                    pattern.getJavaType().getName(), pattern.toString());

        metadata.setPattern(pattern);
        if (this.valueMetadata != null)
            throw new RuntimeException("TODO: Only one @XmlValue allowed!");

        this.valueMetadata = metadata;
        return;
    }

    // is it an attribute?
    XmlAttribute xmlAttribute = this.findJaxbAnnotation(accessors, XmlAttribute.class);
    if (xmlAttribute != null) {
        String namespace = XMLConstants.NULL_NS_URI;
        if (this.attributeFormDefault == XmlNsForm.QUALIFIED) {
            // the attribute MUST have namespace
            namespace = "##default".equals(xmlAttribute.namespace()) ? this.typeName.getNamespaceURI()
                    : xmlAttribute.namespace();
        } else {
            // the attribute MAY have namespace
            // TODO: handle org.javelin.sws.ext.bind.annotations.XmlAttribute
            if (!"##default".equals(xmlAttribute.namespace()))
                namespace = xmlAttribute.namespace();
        }
        String name = "##default".equals(xmlAttribute.name()) ? metadata.getPropertyName()
                : xmlAttribute.name();

        if (!(pattern.isSimpleType() && pattern instanceof SimpleContentPattern))
            throw new RuntimeException("TODO: should be simpleType");

        QName attributeQName = new QName(namespace, name);

        if (log.isTraceEnabled())
            log.trace("-- @XmlAttribute property \"{}\" of type {} mapped to {} attribute {}",
                    metadata.getPropertyName(), pattern.getJavaType().getName(), attributeQName,
                    pattern.toString());

        metadata.setPattern(
                AttributePattern.newAttributePattern(attributeQName, (SimpleContentPattern<P>) pattern));
        this.childAttributeMetadata.add(metadata);
        return;
    }

    // is it an element?
    XmlElement xmlElement = this.findJaxbAnnotation(accessors, XmlElement.class);

    // field is also an element when told so using XmlAccessorType
    boolean isElement = false;

    if (accessors[0] instanceof Field) {
        if (this.accessType == XmlAccessType.FIELD)
            isElement = true;
        else if (this.accessType == XmlAccessType.PUBLIC_MEMBER
                && Modifier.isPublic(((Field) accessors[0]).getModifiers()))
            isElement = true;
    } else if (accessors[0] instanceof Method) {
        if (this.accessType == XmlAccessType.PROPERTY)
            isElement = true;
        else if (this.accessType == XmlAccessType.PUBLIC_MEMBER
                && Modifier.isPublic(((Method) accessors[0]).getModifiers())) {
            // TODO: what if getter is private and setter is public?
            isElement = true;
        }
    }

    if (xmlElement != null || isElement) {
        String namespace = XMLConstants.NULL_NS_URI;
        if (this.elementFormDefault == XmlNsForm.QUALIFIED) {
            // the element MUST have namespace
            namespace = xmlElement == null || "##default".equals(xmlElement.namespace())
                    ? this.typeName.getNamespaceURI()
                    : xmlElement.namespace();
        } else {
            // the element MAY have namespace
            if (xmlElement != null && !"##default".equals(xmlElement.namespace()))
                namespace = xmlElement.namespace();
        }
        String name = xmlElement == null || "##default".equals(xmlElement.name()) ? metadata.getPropertyName()
                : xmlElement.name();
        QName elementQName = new QName(namespace, name);

        if (log.isTraceEnabled())
            log.trace("-- @XmlElement property \"{}\" of type {} mapped to {} element with {}",
                    metadata.getPropertyName(), pattern.getJavaType().getName(), elementQName,
                    pattern.toString());

        ElementPattern<?> elementPattern = ElementPattern.newElementPattern(elementQName, pattern);
        XmlElementWrapper xmlElementWrapper = this.findJaxbAnnotation(accessors, XmlElementWrapper.class);
        if (xmlElementWrapper != null) {
            if (!"##default".equals(xmlElementWrapper.namespace()))
                namespace = xmlElementWrapper.namespace();
            name = !"##default".equals(xmlElementWrapper.name()) ? xmlElementWrapper.name()
                    : metadata.getPropertyName();

            // XmlElementWrapper creates (in XSD Category) a new complex, anonymous, nested (inside element declaration) type
            // DESIGNFLAW: XmlElementWrapper works, but not as clean as it should
            PropertyMetadata<T, ?> md = PropertyMetadata.newPropertyMetadata(this.clazz,
                    metadata.getCollectionClass(), "", PropertyKind.PASSTHROUGH);
            md.setPattern(elementPattern);
            ComplexTypePattern<T> newAnonymousType = ComplexTypePattern.newContentModelPattern(null, this.clazz,
                    md);
            // TODO: Handle @XmlElementWrapper for collection properties
            // TODO: JAXB2 doesn't allow this, but maybe it's a good idea to be able to wrap non-collection properties also?
            // TODO: maybe it's a good idea to create @XmlElementWrappers class (aggregating multime @XmlElementWrapper annotations?)
            elementPattern = ElementPattern.newElementPattern(new QName(namespace, name), newAnonymousType);
            metadata.setWrappedCollection(true);
        }

        metadata.setPattern(elementPattern);
        this.childElementMetadata.add(metadata);
        return;
    }
}