Search in sources :

Example 41 with SchemaInfo

use of org.apache.cxf.service.model.SchemaInfo in project cxf by apache.

the class JAXBSchemaInitializer method end.

public void end(FaultInfo fault) {
    MessagePartInfo part = fault.getFirstMessagePart();
    Class<?> cls = part.getTypeClass();
    Class<?> cl2 = (Class<?>) fault.getProperty(Class.class.getName());
    if (cls != cl2) {
        QName name = (QName) fault.getProperty("elementName");
        part.setElementQName(name);
        JAXBBeanInfo beanInfo = getBeanInfo(cls);
        if (beanInfo == null) {
            throw new Fault(new Message("NO_BEAN_INFO", LOG, cls.getName()));
        }
        SchemaInfo schemaInfo = serviceInfo.getSchema(part.getElementQName().getNamespaceURI());
        if (schemaInfo != null && !isExistSchemaElement(schemaInfo.getSchema(), part.getElementQName())) {
            XmlSchemaElement el = new XmlSchemaElement(schemaInfo.getSchema(), true);
            el.setName(part.getElementQName().getLocalPart());
            el.setNillable(true);
            schemaInfo.setElement(null);
            Iterator<QName> itr = beanInfo.getTypeNames().iterator();
            if (!itr.hasNext()) {
                return;
            }
            QName typeName = itr.next();
            el.setSchemaTypeName(typeName);
        }
    } else if (part.getXmlSchema() == null) {
        try {
            cls.getConstructor(new Class[] { String.class });
        } catch (Exception e) {
            try {
                cls.getConstructor(new Class[0]);
            } catch (Exception e2) {
                // no String or default constructor, we cannot use it
                return;
            }
        }
        // not mappable in JAXBContext directly, we'll have to do it manually :-(
        SchemaInfo schemaInfo = serviceInfo.getSchema(part.getElementQName().getNamespaceURI());
        if (schemaInfo == null || isExistSchemaElement(schemaInfo.getSchema(), part.getElementQName())) {
            return;
        }
        XmlSchemaElement el = new XmlSchemaElement(schemaInfo.getSchema(), true);
        el.setName(part.getElementQName().getLocalPart());
        schemaInfo.setElement(null);
        part.setXmlSchema(el);
        XmlSchemaComplexType ct = new XmlSchemaComplexType(schemaInfo.getSchema(), false);
        el.setSchemaType(ct);
        XmlSchemaSequence seq = new XmlSchemaSequence();
        ct.setParticle(seq);
        Method[] methods = cls.getMethods();
        for (Method m : methods) {
            if (m.getName().startsWith("get") || m.getName().startsWith("is")) {
                int beginIdx = m.getName().startsWith("get") ? 3 : 2;
                try {
                    m.getDeclaringClass().getMethod("set" + m.getName().substring(beginIdx), m.getReturnType());
                    JAXBBeanInfo beanInfo = getBeanInfo(m.getReturnType());
                    if (beanInfo != null) {
                        el = new XmlSchemaElement(schemaInfo.getSchema(), false);
                        el.setName(m.getName().substring(beginIdx));
                        Iterator<QName> itr = beanInfo.getTypeNames().iterator();
                        if (!itr.hasNext()) {
                            return;
                        }
                        QName typeName = itr.next();
                        el.setSchemaTypeName(typeName);
                    }
                    seq.getItems().add(el);
                } catch (Exception e) {
                // not mappable
                }
            }
        }
    }
}
Also used : Message(org.apache.cxf.common.i18n.Message) QName(javax.xml.namespace.QName) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) Fault(org.apache.cxf.interceptor.Fault) Method(java.lang.reflect.Method) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) XmlSchemaSequence(org.apache.ws.commons.schema.XmlSchemaSequence) JAXBBeanInfo(org.apache.cxf.common.jaxb.JAXBBeanInfo) Iterator(java.util.Iterator) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Example 42 with SchemaInfo

use of org.apache.cxf.service.model.SchemaInfo in project cxf by apache.

the class JAXBSchemaInitializer method createSchemaIfNeeded.

private SchemaInfo createSchemaIfNeeded(String namespace, NamespaceMap nsMap) {
    SchemaInfo schemaInfo = serviceInfo.getSchema(namespace);
    if (schemaInfo == null) {
        XmlSchema xmlSchema = schemas.newXmlSchemaInCollection(namespace);
        if (qualifiedSchemas) {
            xmlSchema.setElementFormDefault(XmlSchemaForm.QUALIFIED);
        }
        xmlSchema.setNamespaceContext(nsMap);
        schemaInfo = new SchemaInfo(namespace);
        schemaInfo.setSchema(xmlSchema);
        serviceInfo.addSchema(schemaInfo);
    }
    return schemaInfo;
}
Also used : XmlSchema(org.apache.ws.commons.schema.XmlSchema) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Aggregations

SchemaInfo (org.apache.cxf.service.model.SchemaInfo)42 XmlSchema (org.apache.ws.commons.schema.XmlSchema)17 QName (javax.xml.namespace.QName)11 ServiceInfo (org.apache.cxf.service.model.ServiceInfo)11 Element (org.w3c.dom.Element)10 XmlSchemaElement (org.apache.ws.commons.schema.XmlSchemaElement)9 IOException (java.io.IOException)8 HashMap (java.util.HashMap)7 XMLStreamException (javax.xml.stream.XMLStreamException)7 Message (org.apache.cxf.common.i18n.Message)6 FileNotFoundException (java.io.FileNotFoundException)5 URL (java.net.URL)5 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)5 InputSource (org.xml.sax.InputSource)5 SAXException (org.xml.sax.SAXException)5 SAXParseException (org.xml.sax.SAXParseException)5 File (java.io.File)4 Method (java.lang.reflect.Method)4 HashSet (java.util.HashSet)4 SchemaCollection (org.apache.cxf.common.xmlschema.SchemaCollection)4