highsource / ogc-schemas

XML<->Java and XML<->JS for OGC XSDs.
BSD 2-Clause "Simplified" License
82 stars 49 forks source link

Incorrect namespace prefix assignments results in invalid documents #197

Open charlesmoore99 opened 6 years ago

charlesmoore99 commented 6 years ago

I'm not entirely certain that this is the correct place to post this issue. If it isn't, please direct me to the proper venue.

There are two methods that accept data of type ANY in an SOS 2.0 Insert Observation document.

Via the JaxB API they are accessed by:

The namespace prefixes of these elements are being corrupted when they are marshalled.

Example (with an excessive number of namespaces): Consider the below SOS2 InsertObservation document...

… when unmarshalled and remarshalled you’ll get a document fragment that looks something that looks like this...

The original namespace prefixes on the InsertObservation element have been renumbered. The namespace URIs are the same, but the prefixes are all different. However, the original namespace prefixes still appear on the swes:extension element.

The namespace prefix of the swes:extension element was originally ‘swes’ but is now ‘ns1’. Also The original ‘swes’ namespace prefix still appears in list of namespaces of the extension element. There are now two prefixes for the same namespace URI.

Here’s where it gets nasty:

Consider this SOS2 InsertObservation document. This one already has namespaces numbered ns1 through ns19.

When this document is unmarshalled and remarshalled the namespaces are shuffled like they were in the first case. Except this time when the swes extension element is assigned a new namespace, that prefix has already been used...

.

The {http://www.opengis.net/swes/2.0}extension element originally had a namespace prefix of ‘ns2’. After being unmarshalled and remarshalled it was assigned the namespace prefix of ‘ns1’. Where we get into trouble is that the {http://www.opengis.net/swes/2.0}extension element carries with it the namespace definitions of the original InsertObservation document. In the original document the namespace prefix ‘ns1’ is assigned to {http://www.w3.org/1999/xlink}. This results in two namespace URIs assigned to the same namespace prefix. This corrupts the document and causes it to fail validation.

charlesmoore99 commented 6 years ago

Possible work around.

Add the following dependencies to whatever project you're using the bindings on...

<!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl -->
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.3.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.glassfish.jaxb/jaxb-core -->
<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-core</artifactId>
    <version>2.3.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.googlecode.jaxb-namespaceprefixmapper-interfaces/JAXBNamespacePrefixMapper -->
<dependency>
    <groupId>com.googlecode.jaxb-namespaceprefixmapper-interfaces</groupId>
    <artifactId>JAXBNamespacePrefixMapper</artifactId>
    <version>2.2.4</version>
</dependency>

Then implement a com.sun.xml.bind.marshaller.NamespacePrefixMapper that covers whatever namespace URIs are causing trouble.