use of eu.esdihumboldt.hale.io.xsd.reader.XmlSchemaReader in project hale by halestudio.
the class WFSDescribeFeatureSource method updateConfiguration.
@Override
public boolean updateConfiguration(ImportProvider provider) {
boolean success = super.updateConfiguration(provider);
if (success && provider instanceof XmlSchemaReader) {
// analyze URI and determine relevant elements
URI loc = provider.getSource().getLocation();
if (loc != null) {
URIBuilder b = new URIBuilder(loc);
String namespace = null;
String typename = null;
for (NameValuePair param : b.getQueryParams()) {
switch(param.getName().toLowerCase()) {
case "namespace":
case "namespaces":
namespace = param.getValue();
break;
case "typename":
case "typenames":
typename = param.getValue();
break;
}
}
if (typename != null && !typename.isEmpty()) {
// parse namespaces
Map<String, String> prefixToNamespace = new HashMap<>();
if (namespace != null && !namespace.isEmpty()) {
Pattern ex = Pattern.compile("xmlns\\((([\\w\\d_\\.\\-]+)(=|,))?([^)]+)\\)");
Matcher matcher = ex.matcher(namespace);
while (matcher.find()) {
String prefix = matcher.group(2);
if (prefix == null) {
prefix = XMLConstants.DEFAULT_NS_PREFIX;
}
String ns = matcher.group(4);
prefixToNamespace.put(prefix, ns);
}
// previously used implementation below does not support
// comma separator inside xmlns(...)
// for (String xmlns : Splitter.on(',').omitEmptyStrings().trimResults()
// .split(namespace)) {
// if (xmlns.startsWith("xmlns(") && xmlns.endsWith(")")) {
// String mapping = xmlns.substring("xmlns(".length(),
// xmlns.length() - 1);
// List<String> mp = Splitter.on('=').limit(2).trimResults()
// .splitToList(mapping);
// if (mp.size() == 2) {
// prefixToNamespace.put(mp.get(0), mp.get(1));
// }
// else {
// // mapping for default namespace
// prefixToNamespace.put(XMLConstants.DEFAULT_NS_PREFIX, mp.get(0));
// }
// }
// }
}
Set<QName> elements = new HashSet<>();
// parse type names
for (String type : Splitter.on(',').omitEmptyStrings().trimResults().split(typename)) {
List<String> nameParts = Splitter.on(':').limit(2).trimResults().splitToList(type);
QName name;
if (nameParts.size() == 2) {
// prefix and name
String prefix = nameParts.get(0);
String ns = prefixToNamespace.get(prefix);
if (ns != null) {
name = new QName(ns, nameParts.get(1), prefix);
} else {
// namespace unknown - fall back to local name
// only
name = new QName(nameParts.get(1));
}
} else {
// only local name
name = new QName(nameParts.get(0));
}
elements.add(name);
}
((XmlSchemaReader) provider).setRelevantElements(elements);
}
}
}
return success;
}
use of eu.esdihumboldt.hale.io.xsd.reader.XmlSchemaReader in project hale by halestudio.
the class GmlInstanceCollectionTest method loadInstances.
private GmlInstanceCollection loadInstances(URI schemaLocation, URI xmlLocation, boolean restrictToFeatures, boolean ignoreNamespace) throws IOException, IOProviderConfigurationException {
SchemaReader reader = new XmlSchemaReader();
reader.setSharedTypes(null);
reader.setSource(new DefaultInputSupplier(schemaLocation));
IOReport schemaReport = reader.execute(null);
assertTrue(schemaReport.isSuccess());
Schema sourceSchema = reader.getSchema();
return new GmlInstanceCollection(new DefaultInputSupplier(xmlLocation), sourceSchema, restrictToFeatures, false, true, ignoreNamespace, null, reader);
}
use of eu.esdihumboldt.hale.io.xsd.reader.XmlSchemaReader in project hale by halestudio.
the class OMLReaderTest method readXMLSchema.
/**
* Reads a XML schema
*
* @param input the input supplier
* @return the schema
* @throws IOProviderConfigurationException if the configuration of the
* reader is invalid
* @throws IOException if reading the schema fails
*/
private static Schema readXMLSchema(LocatableInputSupplier<? extends InputStream> input) throws IOProviderConfigurationException, IOException {
XmlSchemaReader reader = new XmlSchemaReader();
reader.setSharedTypes(new DefaultTypeIndex());
reader.setSource(input);
reader.validate();
IOReport report = reader.execute(null);
assertTrue(report.isSuccess());
assertTrue("Errors are contained in the report", report.getErrors().isEmpty());
return reader.getSchema();
}
use of eu.esdihumboldt.hale.io.xsd.reader.XmlSchemaReader in project hale by halestudio.
the class AbstractHandlerTest method loadXMLInstances.
/**
* Load an instance collection from a GML file.
*
* @param schemaLocation the GML application schema location
* @param xmlLocation the GML file location
* @param interpolConfig the interpolation configuration
* @return the instance collection
* @throws IOException if reading schema or instances failed
* @throws IOProviderConfigurationException if the I/O providers were not
* configured correctly
*/
public static InstanceCollection loadXMLInstances(URI schemaLocation, URI xmlLocation, @Nullable ReaderConfiguration interpolConfig) throws IOException, IOProviderConfigurationException {
SchemaReader reader = new XmlSchemaReader();
reader.setSharedTypes(null);
reader.setSource(new DefaultInputSupplier(schemaLocation));
IOReport schemaReport = reader.execute(null);
assertTrue(schemaReport.isSuccess());
Schema sourceSchema = reader.getSchema();
InstanceReader instanceReader = new GmlInstanceReader();
instanceReader.setSource(new DefaultInputSupplier(xmlLocation));
instanceReader.setSourceSchema(sourceSchema);
if (interpolConfig != null) {
interpolConfig.apply(instanceReader);
}
IOReport instanceReport = instanceReader.execute(null);
assertTrue(instanceReport.isSuccess());
return instanceReader.getInstances();
}
use of eu.esdihumboldt.hale.io.xsd.reader.XmlSchemaReader in project hale by halestudio.
the class FilterTest method loadXML.
@Before
public void loadXML() throws Exception {
if (init == false) {
SchemaReader reader = new XmlSchemaReader();
reader.setSharedTypes(null);
reader.setSource(new DefaultInputSupplier((getClass().getResource("/testdata/inspire3/HydroPhysicalWaters.xsd").toURI())));
IOReport report = reader.execute(null);
assertTrue(report.isSuccess());
Schema schema = reader.getSchema();
StreamGmlReader instanceReader = new GmlInstanceReader();
instanceReader.setSource(new DefaultInputSupplier(getClass().getResource("/testdata/out/transformWrite_ERM_HPW.gml").toURI()));
instanceReader.setSourceSchema(schema);
instanceReader.validate();
report = instanceReader.execute(null);
assertTrue(report.isSuccess());
FilterTest.complexinstances = instanceReader.getInstances();
assertFalse(FilterTest.complexinstances.isEmpty());
init = true;
}
}
Aggregations