Package org.eclipse.xsd

Examples of org.eclipse.xsd.XSDFactory


        return build(new FeatureTypeInfo[] { featureTypeInfo }, baseUrl);
    }

    public XSDSchema build(FeatureTypeInfo[] featureTypeInfos, String baseUrl)
        throws IOException {
        XSDFactory factory = XSDFactory.eINSTANCE;
        XSDSchema schema = factory.createXSDSchema();
        schema.setSchemaForSchemaQNamePrefix("xsd");
        schema.getQNamePrefixToNamespaceMap().put("xsd", XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001);
        schema.setElementFormDefault(XSDForm.get(XSDForm.QUALIFIED));

        //group the feature types by namespace
        HashMap ns2featureTypeInfos = new HashMap();

        for (int i = 0; i < featureTypeInfos.length; i++) {
            String prefix = featureTypeInfos[i].getNamespace().getPrefix();
            List l = (List) ns2featureTypeInfos.get(prefix);

            if (l == null) {
                l = new ArrayList();
            }

            l.add(featureTypeInfos[i]);

            ns2featureTypeInfos.put(prefix, l);
        }
       
        if (baseUrl == null)
            baseUrl = wfs.getSchemaBaseURL();
               
        if (ns2featureTypeInfos.entrySet().size() == 1) {
            // only 1 namespace, write target namespace out
            String targetPrefix = (String) ns2featureTypeInfos.keySet().iterator().next();
            String targetNamespace = catalog.getNamespaceByPrefix(targetPrefix).getURI();
            schema.setTargetNamespace(targetNamespace);
            schema.getQNamePrefixToNamespaceMap().put(targetPrefix, targetNamespace);
            // add secondary namespaces from catalog
            for (NamespaceInfo nameSpaceinfo : catalog.getNamespaces()) {
                if (!schema.getQNamePrefixToNamespaceMap().containsKey(nameSpaceinfo.getPrefix())) {
                    schema.getQNamePrefixToNamespaceMap().put(nameSpaceinfo.getPrefix(),
                            nameSpaceinfo.getURI());
                }
            }
            // would result in some xsd:include or xsd:import if schema location is specified
            try {
                FeatureType featureType = featureTypeInfos[0].getFeatureType();
                Object schemaUri = featureType.getUserData().get("schemaURI");
                if (schemaUri != null && schemaUri instanceof Map) {
                    // should always be a Map.. set in AppSchemaDataAccessConfigurator
                    Map<String, String> schemaURIs = (Map<String, String>) schemaUri;
                    // schema is supplied by the user.. just include the top level schema instead of
                    // building the type
                    if (!findTypeInSchema(featureTypeInfos[0], schema, factory)) {
                        ArrayList<String> importedNamespaces = new ArrayList<String>(
                                ns2featureTypeInfos.size() + 1);
                        for (String namespace : schemaURIs.keySet()) {
                            if (targetNamespace.equals(namespace)) {
                                addInclude(schema, factory, schemaURIs.get(namespace));
                            } else {
                                addImport(schema, factory, namespace, schemaURIs.get(namespace),
                                        importedNamespaces);
                                // ensure there's only 1 import per namespace
                                importedNamespaces.add(namespace);
                            }
                        }
                    }
                    return schema;
                }
            } catch (IOException e) {
                logger.warning("Unable to get schema location for feature type '"
                        + featureTypeInfos[0].getPrefixedName() + "'. Reason: '" + e.getMessage()
                        + "'. Building the schema manually instead.");
            }

            // user didn't define schema location
            // import gml schema
            importGMLSchema(schema, factory, baseUrl);
           
            schema.getQNamePrefixToNamespaceMap().put(gmlPrefix, gmlNamespace);
            //schema.getQNamePrefixToNamespaceMap().put("gml", "http://www.opengis.net/gml");
            // then manually build schema
            for (int i = 0; i < featureTypeInfos.length; i++) {
                try {
                    buildSchemaContent(featureTypeInfos[i], schema, factory, baseUrl);
                } catch (Exception e) {
                    logger.log(Level.WARNING, "Could not build xml schema for type: "
                            + featureTypeInfos[i].getName(), e);
                }
            }
        } else {
            //different namespaces, write out import statements
            ArrayList<String> importedNamespaces = new ArrayList<String>(
                    ns2featureTypeInfos.size() + 1);
            for (Iterator i = ns2featureTypeInfos.entrySet().iterator(); i.hasNext();) {
                Map.Entry entry = (Map.Entry) i.next();
                String prefix = (String) entry.getKey();
                List types = (List) entry.getValue();

                StringBuffer typeNames = new StringBuffer();
                for (Iterator t = types.iterator(); t.hasNext();) {
                    FeatureTypeInfo info = (FeatureTypeInfo) t.next();
                    FeatureType featureType = info.getFeatureType();
                    Object schemaUri = featureType.getUserData().get("schemaURI");
                    if (schemaUri != null && schemaUri instanceof Map) {
                        // should always be a Map.. set in AppSchemaDataAccessConfigurator
                        Map<String, String> schemaURIs = (Map<String, String>) schemaUri;
                        // schema is supplied by the user.. just import the specified location
                        for (String namespace : schemaURIs.keySet()) {
                            addImport(schema, factory, namespace, schemaURIs.get(namespace),
                                    importedNamespaces);
                            // ensure there's only 1 import per namespace
                            importedNamespaces.add(namespace);
                        }
                    } else {
                        typeNames.append(info.getPrefixedName()).append(",");
                    }
                }
                if (typeNames.length() > 0) {
                    typeNames.setLength(typeNames.length()-1);
                   
                    // schema not found, encode describe feature type URL
                    Map<String, String> params = new LinkedHashMap<String, String>(describeFeatureTypeParams);
                    params.put("typeName", typeNames.toString().trim());
   
                    String schemaLocation = buildURL(baseUrl, "wfs", params, URLType.RESOURCE);
                    String namespace = catalog.getNamespaceByPrefix(prefix).getURI();
   
                    XSDImport imprt = factory.createXSDImport();
                    imprt.setNamespace(namespace);
                    imprt.setSchemaLocation(schemaLocation);
   
                    schema.getContents().add(imprt);
                }
View Full Code Here

TOP

Related Classes of org.eclipse.xsd.XSDFactory

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.