private SchemaCollection getSchemaCollection()

in rt/rs/description/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java [1465:1593]


    private SchemaCollection getSchemaCollection(ResourceTypes resourceTypes, JAXBContext context) {
        if (context == null) {
            return null;
        }
        SchemaCollection xmlSchemaCollection = new SchemaCollection();
        Collection<DOMSource> schemas = new HashSet<>();
        List<String> targetNamespaces = new ArrayList<>();
        try {
            for (DOMResult r : JAXBUtils.generateJaxbSchemas(context, CastUtils.cast(Collections.emptyMap(),
                                                                                     String.class,
                                                                                     DOMResult.class))) {
                Document doc = (Document)r.getNode();
                ElementQNameResolver theResolver = createElementQNameResolver(context);
                String tns = doc.getDocumentElement().getAttribute("targetNamespace");

                String tnsPrefix = doc.getDocumentElement().lookupPrefix(tns);
                if (tnsPrefix == null) {
                    String tnsDecl =
                        doc.getDocumentElement().getAttribute("xmlns:tns");
                    tnsPrefix = tnsDecl != null && tnsDecl.equals(tns) ? "tns:" : "";
                } else {
                    tnsPrefix += ":";
                }

                if (supportJaxbXmlType) {
                    for (Class<?> cls : resourceTypes.getAllTypes().keySet()) {
                        if (isXmlRoot(cls)) {
                            continue;
                        }
                        XmlType root = cls.getAnnotation(XmlType.class);
                        if (root != null) {
                            QName typeName = theResolver.resolve(cls, new Annotation[] {},
                                                           Collections.<Class<?>, QName> emptyMap());
                            if (typeName != null && tns.equals(typeName.getNamespaceURI())) {
                                QName elementName = resourceTypes.getXmlNameMap().get(cls);
                                if (elementName == null) {
                                    elementName = typeName;
                                }
                                Element newElement = doc
                                    .createElementNS(Constants.URI_2001_SCHEMA_XSD, "xs:element");
                                newElement.setAttribute("name", elementName.getLocalPart());
                                newElement.setAttribute("type", tnsPrefix + typeName.getLocalPart());

                                if (Modifier.isAbstract(cls.getModifiers())
                                    && resourceTypes.getSubstitutions().values().contains(cls)) {
                                    newElement.setAttribute("abstract", "true");
                                }

                                doc.getDocumentElement().appendChild(newElement);
                            }
                        }
                    }
                    if (supportJaxbSubstitutions) {
                        for (Map.Entry<Class<?>, Class<?>> entry : resourceTypes.getSubstitutions().entrySet()) {
                            QName typeName = theResolver.resolve(entry.getKey(), new Annotation[] {},
                                                                 Collections.<Class<?>, QName> emptyMap());
                            for (Element element : DOMUtils.findAllElementsByTagNameNS(doc.getDocumentElement(),
                                                                                       Constants.URI_2001_SCHEMA_XSD,
                                                                                       "element")) {
                                if (element.getAttribute("name").equals(typeName.getLocalPart())) {
                                    QName groupName = theResolver.resolve(entry.getValue(), new Annotation[] {},
                                                                         Collections.<Class<?>, QName> emptyMap());
                                    if (groupName != null) {
                                        element.setAttribute("substitutionGroup", tnsPrefix + groupName.getLocalPart());
                                    }
                                }
                            }
                        }
                    }
                }
                if (supportCollections && !resourceTypes.getCollectionMap().isEmpty()) {
                    for (Map.Entry<Class<?>, QName> entry : resourceTypes.getCollectionMap().entrySet()) {
                        QName colQName = entry.getValue();
                        if (colQName == null) {
                            colQName = theResolver.resolve(entry.getKey(), new Annotation[] {},
                                                Collections.<Class<?>, QName> emptyMap());
                            if (colQName != null) {
                                colQName = new QName(colQName.getNamespaceURI(),
                                                     colQName.getLocalPart() + "s",
                                                     colQName.getPrefix());
                            }
                        }
                        if (colQName == null) {
                            continue;
                        }
                        if (tns.equals(colQName.getNamespaceURI())) {
                            QName typeName = theResolver.resolve(entry.getKey(), new Annotation[] {},
                                                                 Collections.<Class<?>, QName> emptyMap());
                            if (typeName != null) {
                                Element newElement = doc
                                    .createElementNS(Constants.URI_2001_SCHEMA_XSD, "xs:element");
                                newElement.setAttribute("name", colQName.getLocalPart());
                                Element ctElement = doc.createElementNS(Constants.URI_2001_SCHEMA_XSD,
                                                                        "xs:complexType");
                                newElement.appendChild(ctElement);
                                Element seqElement = doc
                                    .createElementNS(Constants.URI_2001_SCHEMA_XSD, "xs:sequence");
                                ctElement.appendChild(seqElement);
                                Element xsElement = doc.createElementNS(Constants.URI_2001_SCHEMA_XSD,
                                                                        "xs:element");
                                seqElement.appendChild(xsElement);
                                xsElement.setAttribute("ref", tnsPrefix + typeName.getLocalPart());
                                xsElement.setAttribute("minOccurs", "0");
                                xsElement.setAttribute("maxOccurs", "unbounded");

                                doc.getDocumentElement().appendChild(newElement);
                            }
                        }
                    }
                }
                DOMSource source = new DOMSource(doc, r.getSystemId());
                schemas.add(source);
                if (!StringUtils.isEmpty(tns)) {
                    targetNamespaces.add(tns);
                }
            }
        } catch (IOException e) {
            LOG.fine("No schema can be generated");
            return null;
        }

        boolean hackAroundEmptyNamespaceIssue = false;
        for (DOMSource r : schemas) {
            hackAroundEmptyNamespaceIssue = addSchemaDocument(xmlSchemaCollection, targetNamespaces,
                                                              (Document)r.getNode(), r.getSystemId(),
                                                              hackAroundEmptyNamespaceIssue);
        }
        return xmlSchemaCollection;
    }