}
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 1 xsd:include if schema location is specified
try {
FeatureType featureType = featureTypeInfos[0].getFeatureType();
Object schemaUri = featureType.getUserData().get("schemaURI");
if (schemaUri != null) {
// should always be a string.. set in AppSchemaDataAccessConfigurator
assert schemaUri instanceof String;
// schema is supplied by the user.. just include the top level schema instead of
// building the type
if (!findTypeInSchema(featureTypeInfos[0], schema, factory)) {
addInclude(schema, factory, targetNamespace, (String) schemaUri);
}
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
XSDImport imprt = factory.createXSDImport();
imprt.setNamespace(gmlNamespace);
imprt.setSchemaLocation(ResponseUtils.buildSchemaURL(baseUrl, gmlSchemaLocation));
XSDSchema gmlSchema = gmlSchema();
imprt.setResolvedSchema(gmlSchema);
schema.getContents().add(imprt);
schema.getQNamePrefixToNamespaceMap().put(gmlPrefix, gmlNamespace);
schema.getQNamePrefixToNamespaceMap().put("gml", "http://www.opengis.net/gml");