use of eu.esdihumboldt.hale.io.xsd.model.XmlIndex in project hale by halestudio.
the class StreamGmlWriter method checkCompatibility.
/**
* @see AbstractInstanceWriter#checkCompatibility()
*/
@Override
public void checkCompatibility() throws IOProviderConfigurationException {
super.checkCompatibility();
XmlIndex xmlIndex = getXMLIndex();
if (xmlIndex == null) {
fail("No XML target schema");
}
if (requiresDefaultContainer()) {
XmlElement element;
try {
element = findDefaultContainter(xmlIndex, null);
} catch (Exception e) {
// ignore
element = null;
}
if (element == null) {
fail("Cannot find container element in schema.");
}
}
}
use of eu.esdihumboldt.hale.io.xsd.model.XmlIndex in project hale by halestudio.
the class StreamGmlWriter method createWriter.
/**
* Create and configure an <code>XMLStreamWriter</code> that writes to the
* given <code>OutputStream</code>
*
* @param outStream <code>OutputStream</code> to write to
* @param reporter the reporter
* @return the configured <code>XMLStreamWriter</code>
* @throws XMLStreamException if creating or configuring the
* <code>XMLStreamWriter</code> fails
*/
protected PrefixAwareStreamWriter createWriter(OutputStream outStream, IOReporter reporter) throws XMLStreamException {
// create and set-up a writer
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
// will set namespaces if these not set explicitly
// $NON-NLS-1$
outputFactory.setProperty(// $NON-NLS-1$
"javax.xml.stream.isRepairingNamespaces", Boolean.valueOf(true));
// create XML stream writer with specified encoding
PrefixAwareStreamWriter tmpWriter = new PrefixAwareStreamWriterDecorator(// $NON-NLS-1$
outputFactory.createXMLStreamWriter(outStream, getCharset().name()));
XmlIndex index = getXMLIndex();
// read the namespaces from the map containing namespaces
if (index.getPrefixes() != null) {
for (Entry<String, String> entry : index.getPrefixes().entrySet()) {
if (entry.getValue().isEmpty()) {
// XXX don't use a default namespace, as this results in
// problems with schemas w/o elementFormQualified=true
// defNamespace = entry.getKey();
} else {
tmpWriter.setPrefix(entry.getValue(), entry.getKey());
}
}
}
// $NON-NLS-1$
GmlWriterUtil.addNamespace(tmpWriter, SCHEMA_INSTANCE_NS, "xsi");
String defNamespace = null;
// determine default namespace
// if (defNamespace == null) {
// XXX don't use a default namespace, as this results in problems
// with schemas w/o elementFormQualified=true
// defNamespace = index.getNamespace();
// TODO remove prefix for target schema namespace?
// }
tmpWriter.setDefaultNamespace(defNamespace);
if (documentWrapper != null) {
documentWrapper.configure(tmpWriter, reporter);
}
// prettyPrint if enabled
if (isPrettyPrint()) {
return new IndentingXMLStreamWriter(tmpWriter);
} else {
return tmpWriter;
}
}
use of eu.esdihumboldt.hale.io.xsd.model.XmlIndex in project hale by halestudio.
the class StreamGmlWriter method init.
/**
* Create and setup the type index and the GML namespace (Initializes
* {@link #gmlNs} and {@link #targetIndex}, resets {@link #geometryWriter}
* and {@link #additionalSchemas}).
*/
private void init() {
// reset target index
targetIndex = null;
// reset geometry writer
geometryWriter = null;
// reset additional schemas
additionalSchemas.clear();
additionalSchemaPrefixes.clear();
// determine GML namespace from target schema
String gml = null;
XmlIndex index = getXMLIndex();
if (index.getPrefixes() != null) {
Set<String> candidates = new TreeSet<>();
for (String ns : index.getPrefixes().keySet()) {
if (ns.startsWith(GML_NAMESPACE_CORE)) {
// $NON-NLS-1$
candidates.add(ns);
}
}
if (!candidates.isEmpty()) {
if (candidates.size() == 1) {
gml = candidates.iterator().next();
} else {
log.warn("Multiple candidates for GML namespace found");
// prefer known GML namespaces
if (candidates.contains(NS_GML_32)) {
gml = NS_GML_32;
} else if (candidates.contains(NS_GML)) {
gml = NS_GML;
} else {
// fall back to first namespace
gml = candidates.iterator().next();
}
}
}
}
if (gml == null) {
// default to GML 2/3 namespace
gml = GML.NAMESPACE;
}
gmlNs = gml;
if (log.isDebugEnabled()) {
// $NON-NLS-1$
log.debug("GML namespace is " + gmlNs);
}
}
use of eu.esdihumboldt.hale.io.xsd.model.XmlIndex in project hale by halestudio.
the class OmlReader method findElementType.
private QName findElementType(TypeIndex schema, QName elementName) {
if (schema instanceof SchemaSpace) {
SchemaSpace ss = (SchemaSpace) schema;
for (Schema schem : ss.getSchemas()) {
if (schem instanceof XmlIndex) {
XmlElement xmlelem = ((XmlIndex) schem).getElements().get(elementName);
if (xmlelem != null) {
return xmlelem.getType().getName();
}
// if there is no element try to find one with an extra "/"
// sign in the namespace because in earlier version this
// case can occur
xmlelem = ((XmlIndex) schem).getElements().get(new QName(elementName.getNamespaceURI() + "/", elementName.getLocalPart()));
if (xmlelem != null) {
return xmlelem.getType().getName();
}
}
}
} else {
for (TypeDefinition typedef : schema.getTypes()) {
XmlElements xmlelem = typedef.getConstraint(XmlElements.class);
for (XmlElement elem : xmlelem.getElements()) {
if (elem.getName().equals(elementName) || elem.getName().equals(new QName(elementName.getNamespaceURI() + "/", elementName.getLocalPart()))) {
return typedef.getName();
}
}
}
}
return elementName;
}
use of eu.esdihumboldt.hale.io.xsd.model.XmlIndex in project hale by halestudio.
the class XmlSchemaReaderTest method testRead_definitive_annotated.
/**
* Test reading a simple XML schema with an annotated element.
*
* @throws Exception if reading the schema fails
*/
@Test
public void testRead_definitive_annotated() throws Exception {
URI location = getClass().getResource("/testdata/definitive/documentation_ex.xsd").toURI();
LocatableInputSupplier<? extends InputStream> input = new DefaultInputSupplier(location);
XmlIndex schema = (XmlIndex) readSchema(input);
// product element
XmlElement product = schema.getElements().get(new QName("product"));
assertNotNull(product);
assertTrue(product.getDescription().contains("This element represents a product."));
}
Aggregations