Search in sources :

Example 91 with XmlSchema

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

the class PortTypeVisitor method visitInterface.

// Visits a fully declared interface
private void visitInterface(AST identifierNode) {
    try {
        String interfaceName = identifierNode.toString();
        Scope interfaceScope = new Scope(getScope(), interfaceName);
        portType = definition.createPortType();
        String portTypeName = interfaceScope.toString();
        XmlSchema newSchema = schema;
        if (!mapper.isDefaultMapping()) {
            portTypeName = interfaceScope.tail();
            // add a schema based on the interface
            String tns = mapper.map(interfaceScope);
            newSchema = manager.createXmlSchemaForDefinition(definition, tns, schemas);
            definition.addNamespace(interfaceScope.toString("_"), tns);
        }
        String tns = definition.getTargetNamespace();
        portType.setQName(new QName(tns, portTypeName));
        definition.addPortType(portType);
        portType.setUndefined(false);
        Binding binding = createBinding(interfaceScope.toString());
        AST specNode = identifierNode.getNextSibling();
        if (specNode.getType() == IDLTokenTypes.LCURLY) {
            specNode = specNode.getNextSibling();
        }
        AST exportNode = null;
        if (specNode.getType() == IDLTokenTypes.RCURLY) {
            exportNode = specNode.getNextSibling();
        } else if (specNode.getType() == IDLTokenTypes.COLON) {
            exportNode = visitInterfaceInheritanceSpec(specNode, binding, interfaceScope);
            exportNode = exportNode.getNextSibling();
        } else {
            exportNode = specNode;
        }
        while (exportNode != null && exportNode.getType() != IDLTokenTypes.RCURLY) {
            if (TypeDclVisitor.accept(exportNode)) {
                TypeDclVisitor visitor = new TypeDclVisitor(interfaceScope, definition, newSchema, wsdlVisitor);
                visitor.visit(exportNode);
            } else if (ConstVisitor.accept(exportNode)) {
                ConstVisitor visitor = new ConstVisitor(interfaceScope, definition, newSchema, wsdlVisitor);
                visitor.visit(exportNode);
            } else if (ExceptionVisitor.accept(exportNode)) {
                ExceptionVisitor visitor = new ExceptionVisitor(interfaceScope, definition, newSchema, wsdlVisitor);
                visitor.visit(exportNode);
            } else if (AttributeVisitor.accept(exportNode)) {
                AttributeVisitor attributeVisitor = new AttributeVisitor(interfaceScope, definition, newSchema, wsdlVisitor, portType, binding);
                attributeVisitor.visit(exportNode);
            } else if (OperationVisitor.accept(interfaceScope, definition, newSchema, exportNode, wsdlVisitor)) {
                OperationVisitor visitor = new OperationVisitor(interfaceScope, definition, newSchema, wsdlVisitor, portType, binding);
                visitor.visit(exportNode);
            } else {
                throw new RuntimeException("[InterfaceVisitor] Invalid IDL: unknown element " + exportNode.toString());
            }
            exportNode = exportNode.getNextSibling();
        }
        // Once we've finished declaring the interface, we should make sure it has been removed
        // from the list of scopedNames so that we indicate that is no longer simply forward
        // declared.
        Scope scopedName = new Scope(getScope(), identifierNode);
        scopedNames.remove(scopedName);
        if (wsdlVisitor.getDeferredActions() != null) {
            handleDeferredActions(wsdlVisitor.getDeferredActions(), scopedName, identifierNode);
        }
        if (!mapper.isDefaultMapping()) {
            manager.deferAttachSchemaToWSDL(definition, newSchema, false);
        // manager.attachSchemaToWSDL(definition, newSchema, false);
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
Also used : Binding(javax.wsdl.Binding) AST(antlr.collections.AST) QName(javax.xml.namespace.QName) WSDLException(javax.wsdl.WSDLException) XmlSchema(org.apache.ws.commons.schema.XmlSchema)

Example 92 with XmlSchema

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

the class ScopedNameVisitor method findSchemaTypeInGlobalScope.

private static boolean findSchemaTypeInGlobalScope(Scope scope, Definition defn, XmlSchema currentSchema, AST node, WSDLASTVisitor wsdlVisitor, VisitorTypeHolder holder) {
    XmlSchemaCollection schemas = wsdlVisitor.getSchemas();
    TypeMappingType typeMap = wsdlVisitor.getTypeMap();
    ModuleToNSMapper mapper = wsdlVisitor.getModuleToNSMapper();
    WSDLSchemaManager manager = wsdlVisitor.getManager();
    Scope scopedName = new Scope(scope, node);
    String name = node.toString();
    if (isFullyScopedName(node)) {
        scopedName = getFullyScopedName(new Scope(), node);
        name = scopedName.toString();
    }
    boolean result = findNonSchemaType(name, wsdlVisitor, holder);
    if (!result) {
        XmlSchema xmlSchema = currentSchema;
        QName qname = null;
        String tns = mapper.map(scopedName.getParent());
        if (tns != null) {
            xmlSchema = manager.getXmlSchema(tns);
            if (xmlSchema != null) {
                qname = new QName(xmlSchema.getTargetNamespace(), scopedName.tail());
            }
        } else {
            qname = new QName(xmlSchema.getTargetNamespace(), name);
        }
        XmlSchemaType stype = null;
        if (qname != null) {
            // Exceptions are treated as a special case as above
            if (exceptionMode) {
                qname = new QName(xmlSchema.getTargetNamespace(), qname.getLocalPart() + "Type");
            }
            stype = xmlSchema.getTypeByName(qname);
            if (stype == null) {
                stype = schemas.getTypeByQName(qname);
            }
        }
        if (stype != null) {
            result = true;
            if (holder != null) {
                holder.setSchemaType(stype);
                holder.setCorbaType(getCorbaSchemaType(xmlSchema, typeMap, stype, scopedName));
                // add a xmlschema import
                if (!currentSchema.getTargetNamespace().equals(xmlSchema.getTargetNamespace())) {
                    String importFile = wsdlVisitor.getOutputDir() + System.getProperty("file.separator") + scopedName.getParent().toString("_");
                    manager.addXmlSchemaImport(currentSchema, xmlSchema, importFile);
                }
            }
        }
    }
    return result;
}
Also used : XmlSchema(org.apache.ws.commons.schema.XmlSchema) QName(javax.xml.namespace.QName) TypeMappingType(org.apache.cxf.binding.corba.wsdl.TypeMappingType) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType) XmlSchemaCollection(org.apache.ws.commons.schema.XmlSchemaCollection)

Example 93 with XmlSchema

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

the class ScopedNameVisitor method forwardDeclared.

protected static Scope forwardDeclared(Scope scope, Definition defn, XmlSchema schemaRef, AST node, WSDLASTVisitor wsdlVisitor) {
    // XmlSchemaType result = null;
    Scope result = null;
    Scope currentScope = scope;
    ScopeNameCollection scopedNames = wsdlVisitor.getScopedNames();
    // Check for forward declaration from local scope outwards
    if ((node.getFirstChild() == null) || (node.getFirstChild() != null && node.getFirstChild().getType() != IDLTokenTypes.SCOPEOP)) {
        while (result == null && currentScope != currentScope.getParent()) {
            Scope scopedName = null;
            if (isFullyScopedName(node)) {
                scopedName = getFullyScopedName(currentScope, node);
            } else {
                scopedName = new Scope(currentScope, node);
            }
            if (scopedNames.getScope(scopedName) != null) {
                XmlSchema xmlSchema = schemaRef;
                String tns = wsdlVisitor.getModuleToNSMapper().map(scopedName.getParent());
                if (tns != null) {
                    xmlSchema = wsdlVisitor.getManager().getXmlSchema(tns);
                }
                if (ObjectReferenceVisitor.accept(scope, xmlSchema, defn, node, wsdlVisitor)) {
                    // checks if its a forward
                    Visitor visitor = new ObjectReferenceVisitor(scope, defn, xmlSchema, wsdlVisitor);
                    visitor.visit(node);
                }
                result = scopedName;
            }
            currentScope = currentScope.getParent();
        }
    }
    // Check for forward declaration in global scope
    if (result == null) {
        Scope scopedName = null;
        if (isFullyScopedName(node)) {
            scopedName = getFullyScopedName(new Scope(), node);
        } else {
            scopedName = new Scope(new Scope(), node);
        }
        if (scopedNames.getScope(scopedName) != null) {
            XmlSchema xmlSchema = schemaRef;
            String tns = wsdlVisitor.getModuleToNSMapper().map(scopedName.getParent());
            if (tns != null) {
                xmlSchema = wsdlVisitor.getManager().getXmlSchema(tns);
            }
            if (ObjectReferenceVisitor.accept(scope, xmlSchema, defn, node, wsdlVisitor)) {
                // checks if an object ref
                Visitor visitor = new ObjectReferenceVisitor(scope, defn, xmlSchema, wsdlVisitor);
                visitor.visit(node);
            }
            result = scopedName;
        }
    }
    return result;
}
Also used : XmlSchema(org.apache.ws.commons.schema.XmlSchema)

Example 94 with XmlSchema

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

the class EndpointReferenceUtils method createSchema.

private static synchronized Schema createSchema(ServiceInfo serviceInfo, Bus b) {
    if (b == null) {
        b = BusFactory.getThreadDefaultBus(false);
    }
    Schema schema = serviceInfo.getProperty(Schema.class.getName(), Schema.class);
    if (schema == null) {
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Map<String, byte[]> schemaSourcesMap = new LinkedHashMap<String, byte[]>();
        Map<String, Source> schemaSourcesMap2 = new LinkedHashMap<String, Source>();
        XMLStreamWriter writer = null;
        try {
            for (SchemaInfo si : serviceInfo.getSchemas()) {
                Element el = si.getElement();
                unsetReadonly(el);
                String baseURI = null;
                try {
                    baseURI = el.getBaseURI();
                } catch (Exception ex) {
                // ignore - not DOM level 3
                }
                if (baseURI == null) {
                    baseURI = si.getSystemId();
                }
                DOMSource ds = new DOMSource(el, baseURI);
                schemaSourcesMap2.put(si.getSystemId() + ":" + si.getNamespaceURI(), ds);
                LoadingByteArrayOutputStream out = new LoadingByteArrayOutputStream();
                writer = StaxUtils.createXMLStreamWriter(out);
                StaxUtils.copy(el, writer);
                writer.flush();
                schemaSourcesMap.put(si.getSystemId() + ":" + si.getNamespaceURI(), out.toByteArray());
            }
            for (XmlSchema sch : serviceInfo.getXmlSchemaCollection().getXmlSchemas()) {
                if (sch.getSourceURI() != null && !schemaSourcesMap.containsKey(sch.getSourceURI() + ":" + sch.getTargetNamespace())) {
                    InputStream ins = null;
                    try {
                        URL url = new URL(sch.getSourceURI());
                        ins = url.openStream();
                    } catch (Exception e) {
                    // ignore, we'll just use what we have.  (though
                    // bugs in XmlSchema could make this less useful)
                    }
                    LoadingByteArrayOutputStream out = new LoadingByteArrayOutputStream();
                    if (ins == null) {
                        sch.write(out);
                    } else {
                        IOUtils.copyAndCloseInput(ins, out);
                    }
                    schemaSourcesMap.put(sch.getSourceURI() + ":" + sch.getTargetNamespace(), out.toByteArray());
                    Source source = new StreamSource(out.createInputStream(), sch.getSourceURI());
                    schemaSourcesMap2.put(sch.getSourceURI() + ":" + sch.getTargetNamespace(), source);
                }
            }
            factory.setResourceResolver(new SchemaLSResourceResolver(schemaSourcesMap, b));
            schema = factory.newSchema(schemaSourcesMap2.values().toArray(new Source[schemaSourcesMap2.size()]));
        } catch (Exception ex) {
            // Something not right with the schema from the wsdl.
            LOG.log(Level.WARNING, "SAXException for newSchema()", ex);
            for (SchemaInfo schemaInfo : serviceInfo.getSchemas()) {
                String s = StaxUtils.toString(schemaInfo.getElement(), 4);
                LOG.log(Level.INFO, "Schema for: " + schemaInfo.getNamespaceURI() + "\n" + s);
            }
        } finally {
            for (Source src : schemaSourcesMap2.values()) {
                if (src instanceof DOMSource) {
                    Node nd = ((DOMSource) src).getNode();
                    unsetReadonly(nd);
                }
            }
            StaxUtils.close(writer);
        }
        serviceInfo.setProperty(Schema.class.getName(), schema);
    }
    return schema;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Schema(javax.xml.validation.Schema) XmlSchema(org.apache.ws.commons.schema.XmlSchema) JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) StreamSource(javax.xml.transform.stream.StreamSource) Node(org.w3c.dom.Node) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) URISyntaxException(java.net.URISyntaxException) XMLStreamException(javax.xml.stream.XMLStreamException) JAXBException(javax.xml.bind.JAXBException) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) LinkedHashMap(java.util.LinkedHashMap) LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) XmlSchema(org.apache.ws.commons.schema.XmlSchema) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Example 95 with XmlSchema

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

the class ImportRepairTest method testImportRepairs.

@Test
public void testImportRepairs() throws Exception {
    if (System.getProperty("java.vendor").contains("IBM")) {
        // and we cannot get a good version unless we endorse it
        return;
    }
    collection = new SchemaCollection();
    XmlSchema importingSchema = newSchema(IMPORTING_SCHEMA);
    XmlSchema baseTypeSchema1 = newSchema(BASE_TYPE_SCHEMA1);
    XmlSchema baseTypeSchema2 = newSchema(BASE_TYPE_SCHEMA2);
    XmlSchema elementTypeSchema = newSchema(ELEMENT_TYPE_SCHEMA);
    XmlSchema elementSchema = newSchema(ELEMENT_SCHEMA);
    XmlSchema attributeSchema = newSchema(ATTRIBUTE_SCHEMA);
    XmlSchema attributeTypeSchema = newSchema(ATTRIBUTE_TYPE_SCHEMA);
    createBaseType1(baseTypeSchema1);
    createBaseType2(baseTypeSchema2);
    XmlSchemaComplexContentExtension derivedType1Extension = createDerivedType1(importingSchema);
    createDerivedType2(importingSchema);
    createImportedElement(elementSchema);
    createTypeImportingElement(importingSchema);
    createTypeImportedByElement(elementTypeSchema);
    createElementWithImportedType(importingSchema);
    createImportedAttribute(attributeSchema);
    XmlSchemaAttribute importingAttribute = new XmlSchemaAttribute(importingSchema, false);
    importingAttribute.getRef().setTargetQName(new QName(ATTRIBUTE_SCHEMA, "imported"));
    // borrow derivedType1 to make the reference.
    derivedType1Extension.getAttributes().add(importingAttribute);
    createImportedAttributeType(attributeTypeSchema);
    createAttributeImportingType(importingSchema);
    /*
         * Notice that no imports have been added. In an ideal world, XmlSchema would do this for us.
         */
    try {
        tryToParseSchemas();
        fail("Expected an exception");
    } catch (DOMErrorException e) {
    // ignore, expected
    }
    LOG.info("adding imports");
    collection.addCrossImports();
    tryToParseSchemas();
}
Also used : XmlSchemaComplexContentExtension(org.apache.ws.commons.schema.XmlSchemaComplexContentExtension) XmlSchema(org.apache.ws.commons.schema.XmlSchema) QName(javax.xml.namespace.QName) XmlSchemaAttribute(org.apache.ws.commons.schema.XmlSchemaAttribute) 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