Search in sources :

Example 11 with XmlIndex

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.");
        }
    }
}
Also used : XmlIndex(eu.esdihumboldt.hale.io.xsd.model.XmlIndex) XmlElement(eu.esdihumboldt.hale.io.xsd.model.XmlElement) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) URISyntaxException(java.net.URISyntaxException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException)

Example 12 with XmlIndex

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;
    }
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) XmlIndex(eu.esdihumboldt.hale.io.xsd.model.XmlIndex)

Example 13 with XmlIndex

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);
    }
}
Also used : TreeSet(java.util.TreeSet) XmlIndex(eu.esdihumboldt.hale.io.xsd.model.XmlIndex)

Example 14 with XmlIndex

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;
}
Also used : XmlElements(eu.esdihumboldt.hale.io.xsd.constraint.XmlElements) SchemaSpace(eu.esdihumboldt.hale.common.schema.model.SchemaSpace) QName(javax.xml.namespace.QName) Schema(eu.esdihumboldt.hale.common.schema.model.Schema) XmlIndex(eu.esdihumboldt.hale.io.xsd.model.XmlIndex) XmlElement(eu.esdihumboldt.hale.io.xsd.model.XmlElement) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 15 with XmlIndex

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."));
}
Also used : DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) QName(javax.xml.namespace.QName) XmlIndex(eu.esdihumboldt.hale.io.xsd.model.XmlIndex) XmlElement(eu.esdihumboldt.hale.io.xsd.model.XmlElement) URI(java.net.URI) Test(org.junit.Test)

Aggregations

XmlIndex (eu.esdihumboldt.hale.io.xsd.model.XmlIndex)24 XmlElement (eu.esdihumboldt.hale.io.xsd.model.XmlElement)16 DefaultInputSupplier (eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier)15 URI (java.net.URI)15 Test (org.junit.Test)13 QName (javax.xml.namespace.QName)11 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)8 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)7 GroupPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition)6 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)4 SchemaSpace (eu.esdihumboldt.hale.common.schema.model.SchemaSpace)4 Cardinality (eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality)4 IOException (java.io.IOException)4 XMLStreamException (javax.xml.stream.XMLStreamException)3 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)2 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)2 XmlSchemaReader (eu.esdihumboldt.hale.io.xsd.reader.XmlSchemaReader)2 File (java.io.File)2 URISyntaxException (java.net.URISyntaxException)2 Geometry (com.vividsolutions.jts.geom.Geometry)1