Package org.apache.tuscany.sca.xsd

Examples of org.apache.tuscany.sca.xsd.XSDefinition


        }
        list.add(definition);
    }

    public Object removeModel(Object resolved) {
        XSDefinition definition = (XSDefinition)resolved;
        List<XSDefinition> list = map.get(definition.getNamespace());
        if (list == null) {
            return null;
        } else {
            return list.remove(definition);
        }
View Full Code Here


        }
    }

    public <T> T resolveModel(Class<T> modelClass, T unresolved) {

        XSDefinition definition = (XSDefinition)unresolved;
        String namespace = definition.getNamespace();

        // Lookup a definition for the given namespace within the
        // current contribution.
        List<XSDefinition> list = map.get(namespace);
        XSDefinition modelXSD = null;
        if (list != null && definition.getDocument() != null) {
            // Set the document for the inline schema
            int index = list.indexOf(definition);
            if (index != -1) {  // a matching (not identical) document was found
                modelXSD = list.get(index);
                modelXSD.setDocument(definition.getDocument());
            }
        }
        if (list == null && definition.getDocument() != null) {
          // Hit for the 1st time
          list = new ArrayList<XSDefinition>();
          list.add(definition);
          map.put(namespace, list);
        }
        XSDefinition resolved = null;
        try {
            resolved = aggregate(list);
        } catch (ContributionRuntimeException e) {
            throw new ContributionRuntimeException(e);
        }
        if (resolved != null && !resolved.isUnresolved()) {
            if (definition.isUnresolved() && definition.getSchema() == null && modelXSD != null) {
                // Update the unresolved model with schema information and mark it
                // resolved.  This information in the unresolved model is needed when
                // this method is called by WSDLModelResolver.readInlineSchemas().
                definition.setSchema(modelXSD.getSchema());
                definition.setSchemaCollection(modelXSD.getSchemaCollection());
                definition.setUnresolved(false);
            }
            return modelClass.cast(resolved);
        }

        // No definition found, delegate the resolution to the imports
        for (Import import_ : this.contribution.getImports()) {
            if (import_ instanceof NamespaceImport) {
                NamespaceImport namespaceImport = (NamespaceImport)import_;
                if (namespaceImport.getNamespace().equals(namespace)) {

                    // Delegate the resolution to the namespace import resolver
                    resolved =
                        namespaceImport.getModelResolver().resolveModel(XSDefinition.class, (XSDefinition)unresolved);
                    if (!resolved.isUnresolved()) {
                        return modelClass.cast(resolved);
                    }
                }
            } else if (import_ instanceof DefaultImport) {

                // Delegate the resolution to the default import resolver
                resolved =
                    import_.getModelResolver().resolveModel(XSDefinition.class, (XSDefinition)unresolved);
                if (!resolved.isUnresolved()) {
                    return modelClass.cast(resolved);
                }
            }
        }
        return modelClass.cast(unresolved);
View Full Code Here

    private XSDefinition aggregate(List<XSDefinition> definitions) throws ContributionRuntimeException {
        if (definitions == null || definitions.size() == 0) {
            return null;
        }
        if (definitions.size() == 1) {
            XSDefinition d = definitions.get(0);
            loadOnDemand(d);
            return d;
        }
        XSDefinition aggregated = factory.createXSDefinition();
        for (XSDefinition d : definitions) {
            loadOnDemand(d);
        }
        String ns = definitions.get(0).getNamespace();

        XmlSchema facade = null;
        // Check if the facade XSD is already in the collection
        for (XmlSchema s : schemaCollection.getXmlSchema(AGGREGATED_XSD)) {
            if (ns.equals(s.getTargetNamespace())) {
                facade = s;
                break;
            }
        }
        if (facade == null) {
            // This will add the facade into the collection
            facade = new XmlSchema(ns, AGGREGATED_XSD, schemaCollection);
        }

        for (XmlSchema d : schemaCollection.getXmlSchemas()) {
            if (ns.equals(d.getTargetNamespace())) {
                if (d == facade) {
                    continue;
                }
                XmlSchemaInclude include = new XmlSchemaInclude();
                include.setSchema(d);
                include.setSourceURI(d.getSourceURI());
                include.setSchemaLocation(d.getSourceURI());
                facade.getIncludes().add(include);
                facade.getItems().add(include);
            }
        }
        aggregated.setUnresolved(true);
        aggregated.setSchema(facade);
        aggregated.setNamespace(ns);
        aggregated.setAggregatedDefinitions(definitions);
        aggregated.setUnresolved(false);

        // FIXME: [rfeng] This is hacky
        //definitions.clear();
        //definitions.add(aggregated);
        return aggregated;
View Full Code Here

        throws IOException {
        DOMResolverImpl resolver = new DOMResolverImpl();
        context.generateSchema(resolver);
        Map<String, DOMResult> results = resolver.getResults();
        for (Map.Entry<String, DOMResult> entry : results.entrySet()) {
            XSDefinition definition = factory.createXSDefinition();
            definition.setUnresolved(true);
            definition.setDocument((Document)entry.getValue().getNode());
            definition.setNamespace(entry.getKey());
            URI location = null;
            try {
                location = new URI(entry.getValue().getSystemId());
            } catch (URISyntaxException e) {
                // ignore: use null value
            }   
            definition.setLocation(location);
            definitions.add(definition);
        }
    }
View Full Code Here

                    InputSource inputSource = new InputSource(inputStream);
                    inputSource.setSystemId(xmlString.getBaseURI());
                    XmlSchema schema = schemaCollection.read(inputSource, null);
                    inputStream.close();
                   
                    XSDefinition xsdDefinition = xsdFactory.createXSDefinition();
                    xsdDefinition.setSchema(schema);
                   
                    ((XSDInfo)xmlString).setXsdDefinition(xsdDefinition);
                    xsdResolver.addModel(xsdDefinition, processorContext);
                }
            }
           
            // resolve
            for (XMLString xmlString : xmlMap.values()){
                if (xmlString instanceof WSDLInfo){
                   WSDLDefinition wsdlDefinition = ((WSDLInfo)xmlString).getWsdlDefintion();
                  
                   // link to imports
                   for (Map.Entry<String, List<javax.wsdl.Import>> entry :
                       ((Map<String, List<javax.wsdl.Import>>)wsdlDefinition.getDefinition().getImports()).entrySet()) {
                       for (javax.wsdl.Import imp : entry.getValue()) {
                           String wsdlName = imp.getDefinition().getDocumentBaseURI();
                           WSDLInfo wsdlInfo = (WSDLInfo)xmlMap.get(getFilenameWithoutPath(wsdlName));
                           wsdlDefinition.getImportedDefinitions().add(wsdlInfo.getWsdlDefintion());
                       }
                   }
                  
                   // extract any in-line types in the Tuscany model
                   Types types = wsdlDefinition.getDefinition().getTypes();
                   if ( types != null){                      
/*  read XSD from WSDL rather than from registry
                       for (int i=0; i < types.getExtensibilityElements().size(); i++){
                      
                           String schemaName = xmlString.getBaseURI() + "#" + i++;
                           XSDInfo xsdInfo = (XSDInfo)xmlMap.get(getFilenameWithoutPath(schemaName));
                           if (xsdInfo != null){
                               wsdlDefinition.getXmlSchemas().add(xsdInfo.getXsdDefinition());
                           }
*/
                       int index = 0;
                       for (Object ext : types.getExtensibilityElements()) {
                           ExtensibilityElement extElement = (ExtensibilityElement)ext;
                           Element element = null;
                           if (extElement instanceof Schema) {
                               element = ((Schema)extElement).getElement();
                           }
                           if (element != null) {
                               XSDefinition xsDefinition = xsdFactory.createXSDefinition();
                               xsDefinition.setUnresolved(true);
                               xsDefinition.setNamespace(element.getAttribute("targetNamespace"));
                               xsDefinition.setDocument(element.getOwnerDocument());
                               XmlSchema schema = schemaCollection.read(element, null);
                               xsDefinition.setSchema(schema);
                               xsDefinition.setLocation(URI.create(xmlString.getBaseURI() + "#" + index));
                               wsdlDefinition.getXmlSchemas().add(xsDefinition);
                               index++;
                           }
                       }
                   }
View Full Code Here

            Map<Element, Map<String, String>> prefixMaps = new HashMap<Element, Map<String, String>>();
            for (Map.Entry<QName, List<ElementInfo>> entry: wrappers.entrySet()) {
                String targetNS = entry.getKey().getNamespaceURI();
                Document schemaDoc = null;
                Element schema = null;
                XSDefinition xsDef = wrapperXSDs.get(targetNS);
                if (xsDef != null) {
                    schemaDoc = xsDef.getDocument();
                    schema = schemaDoc.getDocumentElement();
                } else {
                    // TUSCANY-3283 - if we have to generate a new schema check to see if the
                    //                WSDL doc already has a schema in this namespace                      
                    xsDef = wsdlDefinition.getSchema(targetNS);
                    if (xsDef != null) {
                        schemaDoc = xsDef.getDocument();
                        schema = schemaDoc.getDocumentElement();
                        //wrapperXSDs.put(targetNS, xsDef);
                        Map<String, String> prefixMap = prefixMaps.get(schema);
                        if (prefixMap == null){
                            prefixMap = new HashMap<String, String>();
                            prefixMaps.put(schema, prefixMap);
                            String [] prefixes = xsDef.getSchema().getNamespaceContext().getDeclaredPrefixes();
                            for (int j = 0; j < prefixes.length; j++){
                                prefixMap.put(xsDef.getSchema().getNamespaceContext().getNamespaceURI(prefixes[j]),
                                             prefixes[j]);
                            }
                        }
                        reloadSchema = true;
                    } else {                   
                        schemaDoc = createDocument();
                        schema = schemaDoc.createElementNS(SCHEMA_NS, "xs:schema");
                        // The elementFormDefault should be set to unqualified, see TUSCANY-2388
                        schema.setAttribute("elementFormDefault", "unqualified");
                        schema.setAttribute("attributeFormDefault", "qualified");
                        schema.setAttribute("targetNamespace", targetNS);
                        schema.setAttributeNS(XMLNS_NS, "xmlns:xs", SCHEMA_NS);
                        schemaDoc.appendChild(schema);
                        // TUSCANY-3283 - the extension is created at the bottom
                        //Schema schemaExt = createSchemaExt(definition);
                        //schemaExt.setElement(schema);
                        prefixMaps.put(schema, new HashMap<String, String>());
                        xsDef = xsdFactory.createXSDefinition();
                        xsDef.setUnresolved(true);
                        xsDef.setNamespace(targetNS);
                        xsDef.setDocument(schemaDoc);
                        // TUSCANY-2465: Set the system id to avoid schema conflict
                        xsDef.setLocation(URI.create("xsd_" + index + ".xsd"));
                        index++;
                        wrapperXSDs.put(targetNS, xsDef);
                        wsdlDefinition.getXmlSchemas().add(xsDef);
                    }
                }
                Element wrapper = schemaDoc.createElementNS(SCHEMA_NS, "xs:element");
                schema.appendChild(wrapper);
                wrapper.setAttribute("name", entry.getKey().getLocalPart());
                if (entry.getValue().size() == 1 && entry.getValue().get(0).getQName() == null) {
                    // special case for global fault element
                    QName typeName = entry.getValue().get(0).getType().getQName();
                    String nsURI = typeName.getNamespaceURI();
                    if ("".equals(nsURI)) {
                        wrapper.setAttribute("type", typeName.getLocalPart());
                        addSchemaImport(schema, "", schemaDoc);
                    } else if (targetNS.equals(nsURI)) {
                        wrapper.setAttribute("type", typeName.getLocalPart());
                    } else if (SCHEMA_NS.equals(nsURI)) {
                        wrapper.setAttribute("type", "xs:" + typeName.getLocalPart());
                    } else {
                        Map<String, String> prefixMap = prefixMaps.get(schema);
                        String prefix = prefixMap.get(nsURI);
                        if (prefix == null) {
                            prefix = "ns" + i++;
                            prefixMap.put(nsURI, prefix);
                            schema.setAttributeNS(XMLNS_NS, "xmlns:" + prefix, nsURI);
                            addSchemaImport(schema, nsURI, schemaDoc);
                        }
                        wrapper.setAttribute("type", prefix + ":" + typeName.getLocalPart());
                    }                   
                } else {
                    // normal wrapper containing type definition inline
                    Element complexType = schemaDoc.createElementNS(SCHEMA_NS, "xs:complexType");
                    wrapper.appendChild(complexType);
                    if (entry.getValue().size() > 0) {
                        Element sequence = schemaDoc.createElementNS(SCHEMA_NS, "xs:sequence");
                        complexType.appendChild(sequence);
                        for (ElementInfo element: entry.getValue()) {
                            Element xsElement = schemaDoc.createElementNS(SCHEMA_NS, "xs:element");
                            if (element.isMany()) {
                                xsElement.setAttribute("maxOccurs", "unbounded");
                            }
                            xsElement.setAttribute("minOccurs", "0");
                            xsElement.setAttribute("name", element.getQName().getLocalPart());
                            if (element.isNillable()) {
                                xsElement.setAttribute("nillable", "true");
                            }
                            QName typeName = element.getType().getQName();
                            String nsURI = typeName.getNamespaceURI();
                            if ("".equals(nsURI)) {
                                xsElement.setAttribute("type", typeName.getLocalPart());
                                addSchemaImport(schema, "", schemaDoc);
                            } else if (SCHEMA_NS.equals(nsURI)) {
                                xsElement.setAttribute("type", "xs:" + typeName.getLocalPart());
                            } else {
                                Map<String, String> prefixMap = prefixMaps.get(schema);
                                String prefix = prefixMap.get(nsURI);
                                if (prefix == null) {
                                    if (targetNS.equals(nsURI)) {
                      prefix = "tns";
                  } else {
                                        prefix = "ns" + i++;
                                        addSchemaImport(schema, nsURI, schemaDoc);
                  }
                                    prefixMap.put(nsURI, prefix);
                                    schema.setAttributeNS(XMLNS_NS, "xmlns:" + prefix, nsURI);
                                }
                                xsElement.setAttribute("type", prefix + ":" + typeName.getLocalPart());
                            }
                            sequence.appendChild(xsElement);
                        }
                    }
                }
            }
        }
       
        if (reloadSchema){
            schemaCollection = new XmlSchemaCollection();
            for (XSDefinition xsDef: wsdlDefinition.getXmlSchemas()){
                xsDef.setSchema(null);
                xsDef.setSchemaCollection(null);
            }
        }
       
        for (XSDefinition xsDef: wsdlDefinition.getXmlSchemas()){
            addSchemaExtension(xsDef, schemaCollection, wsdlDefinition, definition);
View Full Code Here

   
    // TUSCANY-3283 - merge the nonamespace schema into the default namespace schema
    private void mergeNoNamespaceSchema(String toNamespace, List<XSDefinition> xsDefinitions){
        String fromNamespace = "";
        Document fromDoc = null;
        XSDefinition fromXsDef = null;
        Document toDoc = null;
        List<Document> relatedDocs = new ArrayList<Document>();
       
        for (XSDefinition xsDef: xsDefinitions) {
            if(xsDef.getNamespace().equals("")){
View Full Code Here

    private void resolvePropertyType(String parentName, Property property, Contribution contribution, ProcessorContext context){
        // resolve the reference to a complex property
        // we ignore any types in the schema namespace
        if (property.getXSDType() != null &&
            property.getXSDType().getNamespaceURI().equals("http://www.w3.org/2001/XMLSchema") != true){
            XSDefinition xsdDefinition = xsdFactory.createXSDefinition();
            xsdDefinition.setUnresolved(true);
            xsdDefinition.setNamespace(property.getXSDType().getNamespaceURI());
            // some unit tests don't set up contribution and model resolvers properly
            if (contribution != null && contribution.getModelResolver() != null) {
                XSDefinition resolved = contribution.getModelResolver().resolveModel(XSDefinition.class, xsdDefinition, context);
                if (resolved == null || resolved.isUnresolved()){
                    // raise an error
                    // [rfeng] The XSD might be not available if we use JAXB annotated classes, report it as a warning for now
                    warning(context.getMonitor(), "PropertyTypeNotFound", property, property.getXSDType().toString(), property.getName(), parentName);
                } else {
                    // store the schema in the property
View Full Code Here

        DOMImplementationLS ls = (DOMImplementationLS)impl.getFeature("LS", "3.0");
        LSParser parser = ls.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, SCHEMA_NS);
        LSInput input = ls.createLSInput();
        input.setCharacterStream(new StringReader(schema));
        Document document = parser.parse(input);
        XSDefinition definition = factory.createXSDefinition();
        definition.setUnresolved(true);
        definition.setDocument(document);
        definition.setNamespace(ns);
        definitions.add(definition);
    }
View Full Code Here

    private void addResolvedXSDs(List<XSDefinition> definitions,
                                 XSDFactory factory,
                                 ModelResolver resolver,
                                 Map<String, List<Type>> map) {
        for (Map.Entry<String, List<Type>> entry : map.entrySet()) {
            XSDefinition definition = factory.createXSDefinition();
            definition.setUnresolved(true);
            definition.setNamespace(entry.getKey());
            //FIXME: set location URI
     
            XSDefinition resolved = resolver.resolveModel(XSDefinition.class, definition,context);
            if (resolved.getSchema() == null) {
                //FIXME: create a checked exception and propagate it back up to the activator
                throw new RuntimeException("No XSD found for namespace " + entry.getKey());
            }
            // make sure all the required types are defined in the resolved schema
            for (Type type : entry.getValue()) {
                String name = xsdHelper.getLocalName(type);
                QName typeName = null;
                if (name.endsWith("_._type")) {
                    // FIXME: Anonymous tyype
                    name = name.substring(0, name.length() - "_._type".length());
                    typeName = new QName(type.getURI(), name);
                    if (resolved.getXmlSchemaElement(typeName) == null) {
                        //FIXME: create a checked exception and propagate it back up to the activator
                        throw new RuntimeException("No XSD found for " + typeName.toString());
                    }
                } else {
                    typeName = new QName(type.getURI(), name);
                    if (resolved.getXmlSchemaType(typeName) == null) {
                        //FIXME: create a checked exception and propagate it back up to the activator
                        throw new RuntimeException("No XSD found for " + typeName.toString());
                    }
                }
View Full Code Here

TOP

Related Classes of org.apache.tuscany.sca.xsd.XSDefinition

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.