Search in sources :

Example 31 with XmlSchema

use of org.apache.ws.commons.schema.XmlSchema in project cxf by apache.

the class TestDateMapping method testWriteCustomTypeSchemaType.

@Test
public void testWriteCustomTypeSchemaType() throws Exception {
    context = new AegisContext();
    Set<java.lang.reflect.Type> rootClasses = new HashSet<java.lang.reflect.Type>();
    rootClasses.add(BeanWithDate.class);
    context.setRootClasses(rootClasses);
    context.initialize();
    BeanWithDate bean = new BeanWithDate();
    java.sql.Date date = new java.sql.Date(0);
    bean.setFig(date);
    AegisType sbType = context.getTypeMapping().getType(bean.getClass());
    // dummy to put schema in.
    XmlSchema root = new XmlSchema();
    /* will explode if the type object created for the custom mapping isn't fully initialized.
      */
    sbType.writeSchema(root);
}
Also used : AegisType(org.apache.cxf.aegis.type.AegisType) AegisContext(org.apache.cxf.aegis.AegisContext) AegisType(org.apache.cxf.aegis.type.AegisType) XmlSchema(org.apache.ws.commons.schema.XmlSchema) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 32 with XmlSchema

use of org.apache.ws.commons.schema.XmlSchema in project cxf by apache.

the class JAXBSchemaInitializer method createBridgeXsElement.

private void createBridgeXsElement(MessagePartInfo part, QName qn, QName typeName) {
    XmlSchemaElement el = null;
    SchemaInfo schemaInfo = serviceInfo.getSchema(qn.getNamespaceURI());
    if (schemaInfo != null) {
        el = schemaInfo.getElementByQName(qn);
        if (el == null) {
            createXsElement(schemaInfo.getSchema(), part, typeName, schemaInfo);
        } else if (!typeName.equals(el.getSchemaTypeName())) {
            throw new Fault(new Message("CANNOT_CREATE_ELEMENT", LOG, qn, typeName, el.getSchemaTypeName()));
        }
        return;
    }
    XmlSchema schema = schemas.newXmlSchemaInCollection(qn.getNamespaceURI());
    if (qualifiedSchemas) {
        schema.setElementFormDefault(XmlSchemaForm.QUALIFIED);
    }
    schemaInfo = new SchemaInfo(qn.getNamespaceURI(), qualifiedSchemas, false);
    schemaInfo.setSchema(schema);
    el = createXsElement(schema, part, typeName, schemaInfo);
    NamespaceMap nsMap = new NamespaceMap();
    nsMap.add(WSDLConstants.CONVENTIONAL_TNS_PREFIX, schema.getTargetNamespace());
    nsMap.add(WSDLConstants.NP_SCHEMA_XSD, WSDLConstants.NS_SCHEMA_XSD);
    schema.setNamespaceContext(nsMap);
    serviceInfo.addSchema(schemaInfo);
}
Also used : Message(org.apache.cxf.common.i18n.Message) XmlSchema(org.apache.ws.commons.schema.XmlSchema) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) NamespaceMap(org.apache.ws.commons.schema.utils.NamespaceMap) Fault(org.apache.cxf.interceptor.Fault) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Example 33 with XmlSchema

use of org.apache.ws.commons.schema.XmlSchema in project cxf by apache.

the class JAXBSchemaInitializer method buildExceptionType.

private void buildExceptionType(MessagePartInfo part, Class<?> cls) {
    SchemaInfo schemaInfo = null;
    for (SchemaInfo s : serviceInfo.getSchemas()) {
        if (s.getNamespaceURI().equals(part.getElementQName().getNamespaceURI())) {
            schemaInfo = s;
            break;
        }
    }
    XmlAccessorOrder xmlAccessorOrder = cls.getAnnotation(XmlAccessorOrder.class);
    XmlType xmlTypeAnno = cls.getAnnotation(XmlType.class);
    String[] propertyOrder = null;
    boolean respectXmlTypeNS = false;
    XmlSchema faultBeanSchema = null;
    if (xmlTypeAnno != null && !StringUtils.isEmpty(xmlTypeAnno.namespace()) && !xmlTypeAnno.namespace().equals(part.getElementQName().getNamespaceURI())) {
        respectXmlTypeNS = true;
        NamespaceMap nsMap = new NamespaceMap();
        nsMap.add(WSDLConstants.CONVENTIONAL_TNS_PREFIX, xmlTypeAnno.namespace());
        nsMap.add(WSDLConstants.NP_SCHEMA_XSD, WSDLConstants.NS_SCHEMA_XSD);
        SchemaInfo faultBeanSchemaInfo = createSchemaIfNeeded(xmlTypeAnno.namespace(), nsMap);
        faultBeanSchema = faultBeanSchemaInfo.getSchema();
    }
    if (xmlTypeAnno != null && xmlTypeAnno.propOrder().length > 0) {
        propertyOrder = xmlTypeAnno.propOrder();
    // TODO: handle @XmlAccessOrder
    }
    XmlSchema schema = null;
    if (schemaInfo == null) {
        NamespaceMap nsMap = new NamespaceMap();
        nsMap.add(WSDLConstants.CONVENTIONAL_TNS_PREFIX, part.getElementQName().getNamespaceURI());
        nsMap.add(WSDLConstants.NP_SCHEMA_XSD, WSDLConstants.NS_SCHEMA_XSD);
        schemaInfo = createSchemaIfNeeded(part.getElementQName().getNamespaceURI(), nsMap);
    }
    schema = schemaInfo.getSchema();
    // Before updating everything, make sure we haven't added this
    // type yet.  Multiple methods that throw the same exception
    // types will cause duplicates.
    String faultTypeName = xmlTypeAnno != null && !StringUtils.isEmpty(xmlTypeAnno.name()) ? xmlTypeAnno.name() : part.getElementQName().getLocalPart();
    XmlSchemaType existingType = schema.getTypeByName(faultTypeName);
    if (existingType != null) {
        return;
    }
    XmlSchemaElement el = new XmlSchemaElement(schema, true);
    el.setName(part.getElementQName().getLocalPart());
    part.setXmlSchema(el);
    schemaInfo.setElement(null);
    if (respectXmlTypeNS) {
        // create complexType in the new created schema for xmlType
        schema = faultBeanSchema;
    }
    XmlSchemaComplexType ct = new XmlSchemaComplexType(schema, true);
    ct.setName(faultTypeName);
    el.setSchemaTypeName(ct.getQName());
    XmlSchemaSequence seq = new XmlSchemaSequence();
    ct.setParticle(seq);
    String namespace = part.getElementQName().getNamespaceURI();
    XmlAccessType accessType = Utils.getXmlAccessType(cls);
    // 
    for (Field f : Utils.getFields(cls, accessType)) {
        // map field
        Type type = Utils.getFieldType(f);
        // generic return type.
        if ((type == null) && (f.getGenericType() instanceof ParameterizedType)) {
            type = f.getGenericType();
        }
        if (generateGenericType(type)) {
            buildGenericElements(schema, seq, f);
        } else {
            JAXBBeanInfo beanInfo = getBeanInfo(type);
            if (beanInfo != null) {
                XmlElement xmlElementAnno = f.getAnnotation(XmlElement.class);
                addElement(schema, seq, beanInfo, new QName(namespace, f.getName()), isArray(type), xmlElementAnno);
            }
        }
    }
    for (Method m : Utils.getGetters(cls, accessType)) {
        // map method
        Type type = Utils.getMethodReturnType(m);
        // generic return type.
        if ((type == null) && (m.getGenericReturnType() instanceof ParameterizedType)) {
            type = m.getGenericReturnType();
        }
        if (generateGenericType(type)) {
            buildGenericElements(schema, seq, m, type);
        } else {
            JAXBBeanInfo beanInfo = getBeanInfo(type);
            if (beanInfo != null) {
                int idx = m.getName().startsWith("get") ? 3 : 2;
                String name = m.getName().substring(idx);
                name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
                XmlElement xmlElementAnno = m.getAnnotation(XmlElement.class);
                addElement(schema, seq, beanInfo, new QName(namespace, name), isArray(type), xmlElementAnno);
            }
        }
    }
    // Create element in xsd:sequence for Exception.class
    if (Exception.class.isAssignableFrom(cls)) {
        addExceptionMessage(cls, schema, seq);
    }
    if (propertyOrder != null) {
        if (propertyOrder.length == seq.getItems().size()) {
            sortItems(seq, propertyOrder);
        } else if (propertyOrder.length > 1 || (propertyOrder.length == 1 && !propertyOrder[0].isEmpty())) {
            LOG.log(Level.WARNING, "propOrder in @XmlType doesn't define all schema elements :" + Arrays.toString(propertyOrder));
        }
    }
    if (xmlAccessorOrder != null && xmlAccessorOrder.value().equals(XmlAccessOrder.ALPHABETICAL) && propertyOrder == null) {
        sort(seq);
    }
    schemas.addCrossImports();
    part.setProperty(JAXBDataBinding.class.getName() + ".CUSTOM_EXCEPTION", Boolean.TRUE);
}
Also used : XmlAccessorOrder(javax.xml.bind.annotation.XmlAccessorOrder) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) QName(javax.xml.namespace.QName) Method(java.lang.reflect.Method) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType) XmlType(javax.xml.bind.annotation.XmlType) ParameterizedType(java.lang.reflect.ParameterizedType) XmlSchemaSequence(org.apache.ws.commons.schema.XmlSchemaSequence) Field(java.lang.reflect.Field) GenericArrayType(java.lang.reflect.GenericArrayType) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType) XmlAccessType(javax.xml.bind.annotation.XmlAccessType) Type(java.lang.reflect.Type) XmlSchemaSimpleType(org.apache.ws.commons.schema.XmlSchemaSimpleType) XmlType(javax.xml.bind.annotation.XmlType) ParameterizedType(java.lang.reflect.ParameterizedType) XmlSchema(org.apache.ws.commons.schema.XmlSchema) NamespaceMap(org.apache.ws.commons.schema.utils.NamespaceMap) JAXBBeanInfo(org.apache.cxf.common.jaxb.JAXBBeanInfo) XmlElement(javax.xml.bind.annotation.XmlElement) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) XmlAccessType(javax.xml.bind.annotation.XmlAccessType) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Example 34 with XmlSchema

use of org.apache.ws.commons.schema.XmlSchema in project cxf by apache.

the class BeanTest method testByteMappings.

@Test
public void testByteMappings() throws Exception {
    defaultContext();
    BeanType type = (BeanType) mapping.getTypeCreator().createType(SimpleBean.class);
    type.setTypeClass(SimpleBean.class);
    type.setTypeMapping(mapping);
    XmlSchema schema = newXmlSchema("urn:Bean");
    type.writeSchema(schema);
    XmlSchemaComplexType btype = (XmlSchemaComplexType) schema.getTypeByName("SimpleBean");
    XmlSchemaSequence seq = (XmlSchemaSequence) btype.getParticle();
    boolean littleByteOk = false;
    boolean bigByteOk = false;
    for (int x = 0; x < seq.getItems().size(); x++) {
        XmlSchemaSequenceMember o = seq.getItems().get(x);
        if (o instanceof XmlSchemaElement) {
            XmlSchemaElement oe = (XmlSchemaElement) o;
            if ("littleByte".equals(oe.getName())) {
                littleByteOk = true;
                assertNotNull(oe.getSchemaTypeName());
                assertEquals(Constants.XSD_BYTE, oe.getSchemaTypeName());
            } else if ("bigByte".equals(oe.getName())) {
                bigByteOk = true;
                assertNotNull(oe.getSchemaTypeName());
                assertEquals(Constants.XSD_BYTE, oe.getSchemaTypeName());
            }
        }
    }
    assertTrue(littleByteOk);
    assertTrue(bigByteOk);
    SimpleBean bean = new SimpleBean();
    bean.setBigByte(new Byte((byte) 0xfe));
    bean.setLittleByte((byte) 0xfd);
    Element element = writeObjectToElement(type, bean, getContext());
    Byte bb = new Byte((byte) 0xfe);
    String bbs = bb.toString();
    assertValid("/b:root/bz:bigByte[text()='" + bbs + "']", element);
    // Test reading
    ElementReader reader = new ElementReader(getResourceAsStream("byteBeans.xml"));
    bean = (SimpleBean) type.readObject(reader, getContext());
    assertEquals(-5, bean.getLittleByte());
    assertEquals(25, bean.getBigByte().byteValue());
    reader.getXMLStreamReader().close();
}
Also used : XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) Element(org.w3c.dom.Element) XmlElement(javax.xml.bind.annotation.XmlElement) XmlSchemaSequence(org.apache.ws.commons.schema.XmlSchemaSequence) XmlSchema(org.apache.ws.commons.schema.XmlSchema) SimpleBean(org.apache.cxf.aegis.services.SimpleBean) XmlSchemaSequenceMember(org.apache.ws.commons.schema.XmlSchemaSequenceMember) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) ElementReader(org.apache.cxf.aegis.xml.stax.ElementReader) AbstractAegisTest(org.apache.cxf.aegis.AbstractAegisTest) Test(org.junit.Test)

Example 35 with XmlSchema

use of org.apache.ws.commons.schema.XmlSchema in project cxf by apache.

the class BeanTest method testAttributeMap.

@Test
public void testAttributeMap() throws Exception {
    defaultContext();
    BeanTypeInfo info = new BeanTypeInfo(SimpleBean.class, "urn:Bean");
    info.mapAttribute("howdy", new QName("urn:Bean", "howdy"));
    info.mapAttribute("bleh", new QName("urn:Bean", "bleh"));
    info.setTypeMapping(mapping);
    BeanType type = new BeanType(info);
    type.setTypeClass(SimpleBean.class);
    type.setTypeMapping(mapping);
    type.setSchemaType(new QName("urn:Bean", "bean"));
    ElementReader reader = new ElementReader(getResourceAsStream("bean4.xml"));
    SimpleBean bean = (SimpleBean) type.readObject(reader, getContext());
    assertEquals("bleh", bean.getBleh());
    assertEquals("howdy", bean.getHowdy());
    reader.getXMLStreamReader().close();
    // Test writing
    Element element = writeObjectToElement(type, bean, getContext());
    assertValid("/b:root[@b:bleh='bleh']", element);
    assertValid("/b:root[@b:howdy='howdy']", element);
    XmlSchema schema = newXmlSchema("urn:Bean");
    type.writeSchema(schema);
    XmlSchemaComplexType stype = (XmlSchemaComplexType) schema.getTypeByName("bean");
    boolean howdy = false;
    boolean bleh = false;
    for (int x = 0; x < stype.getAttributes().size(); x++) {
        XmlSchemaObject o = stype.getAttributes().get(x);
        if (o instanceof XmlSchemaAttribute) {
            XmlSchemaAttribute a = (XmlSchemaAttribute) o;
            if ("howdy".equals(a.getName())) {
                howdy = true;
            }
            if ("bleh".equals(a.getName())) {
                bleh = true;
            }
        }
    }
    assertTrue(howdy);
    assertTrue(bleh);
}
Also used : XmlSchemaObject(org.apache.ws.commons.schema.XmlSchemaObject) XmlSchema(org.apache.ws.commons.schema.XmlSchema) QName(javax.xml.namespace.QName) SimpleBean(org.apache.cxf.aegis.services.SimpleBean) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) Element(org.w3c.dom.Element) XmlElement(javax.xml.bind.annotation.XmlElement) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) XmlSchemaAttribute(org.apache.ws.commons.schema.XmlSchemaAttribute) ElementReader(org.apache.cxf.aegis.xml.stax.ElementReader) AbstractAegisTest(org.apache.cxf.aegis.AbstractAegisTest) Test(org.junit.Test)

Aggregations

XmlSchema (org.apache.ws.commons.schema.XmlSchema)116 QName (javax.xml.namespace.QName)61 XmlSchemaElement (org.apache.ws.commons.schema.XmlSchemaElement)52 Test (org.junit.Test)49 FeatureMetacardType (org.codice.ddf.spatial.ogc.wfs.catalog.common.FeatureMetacardType)37 XmlSchemaSimpleType (org.apache.ws.commons.schema.XmlSchemaSimpleType)32 XmlSchemaComplexType (org.apache.ws.commons.schema.XmlSchemaComplexType)23 XmlSchemaType (org.apache.ws.commons.schema.XmlSchemaType)18 SchemaInfo (org.apache.cxf.service.model.SchemaInfo)17 ArrayList (java.util.ArrayList)14 XmlSchemaSequence (org.apache.ws.commons.schema.XmlSchemaSequence)13 Element (org.w3c.dom.Element)12 AbstractAegisTest (org.apache.cxf.aegis.AbstractAegisTest)10 XmlSchemaCollection (org.apache.ws.commons.schema.XmlSchemaCollection)10 Document (org.w3c.dom.Document)10 HashMap (java.util.HashMap)9 Map (java.util.Map)8 NamespaceMap (org.apache.ws.commons.schema.utils.NamespaceMap)8 List (java.util.List)7 XmlSchemaSequenceMember (org.apache.ws.commons.schema.XmlSchemaSequenceMember)7