Package org.apache.sis.xml
Class TransformingReader
java.lang.Object
org.apache.sis.xml.Transformer
org.apache.sis.xml.TransformingReader
- All Implemented Interfaces:
Iterator,XMLEventReader
A XML reader replacing the namespaces found in XML documents by the namespaces expected by SIS at unmarshalling time.
This class forwards every method calls to the wrapped
XMLEventReader, but with some namespaceURI
modified before being transferred. This class uses a dictionary for identifying the XML namespaces expected by JAXB
implementation. This is needed when a single namespace in a legacy schema has been splitted into many namespaces
in the newer schema. This happen for example in the upgrade from ISO 19139:2007 to ISO 19115-3.
In such cases, we need to check which attribute is being mapped in order to determine the new namespace.- Since:
- 1.0
- Version:
- 1.0
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) static final StringLocation of the file listing types and their properties contained in various namespaces.private final XMLEventReaderThe reader from which to read events.Namespaces of classes containing elements to move in different namespaces.The mapping from (type, attribute) pairs to new namespaces.private XMLEventThe next event to return after a call topeek().The prefixes for namespace URIs.Fields inherited from class org.apache.sis.xml.Transformer
NO_NAMESPACE, renamedAttributes, version -
Constructor Summary
ConstructorsConstructorDescriptionTransformingReader(XMLEventReader in, TransformVersion version) Creates a new reader for the given version of the standards. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Frees any resources associated with this reader.private XMLEventKeeps trace of XML elements opened up to this point and imports the given event.Reads the content of a text-only element.getProperty(String name) Get the value of a feature/property from the underlying implementation.booleanhasNext()Checks if there are more events.Imports the namespaces read from the XML document.private NamespaceConverts a namespace read from the XML document to the namespace used by JAXB annotations.private static booleanReturnstrueif the givenwrapperis a wrapper for the givenevent.(package private) static StringReturns the namespace for the given ISO type, ornullif unknown.next()Returns the next element.Forwards the call and keep trace of the XML elements opened up to this point.nextTag()Forwards the call and keep trace of the XML elements opened up to this point.peek()Checks the nextXMLEventwithout removing it from the stream.(package private) final StringprefixReplacement(String previous, String namespace) Returns the prefix to use for a name in a new namespace.(package private) final StringReturns the new namespace for elements (types and properties) in the given namespace.renamingMap(String namespace) Returns the map loaded byTransformer.load(boolean, String, Set, int)if the given namespace is a known legacy namespace.Methods inherited from class org.apache.sis.xml.Transformer
attributes, close, convert, convert, isNamespace, load, notify, open, removeTrailingSlashMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Iterator
forEachRemaining, remove
-
Field Details
-
FILENAME
Location of the file listing types and their properties contained in various namespaces. This is used for mapping legacy ISO 19139:2007 namespace to newer ISO 19115-3:2016 ones, where the same legacy"http://www.isotc211.org/2005/gmd"URI can be replaced by different URIs under"http://standards.iso.org/iso/19115/-3/…"depending on the class name. Syntax is documented in the readme.html page.- See Also:
-
LEGACY_NAMESPACES
Namespaces of classes containing elements to move in different namespaces. This set will contain at least the following namespaces:- "http://standards.iso.org/iso/19115/-2/gmi/1.0"
- "http://www.isotc211.org/2005/gmi"
- "http://www.isotc211.org/2005/gmd"
- "http://www.isotc211.org/2005/srv"
- "http://www.isotc211.org/2005/gco"
- "http://www.isotc211.org/2005/gmx"
- "http://www.opengis.net/gml"
sis-french-profileadds"http://www.cnig.gouv.fr/2005/fra". -
NAMESPACES
The mapping from (type, attribute) pairs to new namespaces. This mapping will be applied only to namespaces enumerated inLEGACY_NAMESPACES.- Keys are XML names of types, ignoring
"_TYPE"suffix (e.g."CI_Citation") - Values are maps where:
- Keys are XML names of properties (e.g.
"title"). - Values are either:
- Namespace URI if
Transformer.isNamespace(String)returnstruefor that value. - New name of the element otherwise. In such case, the map must be queried again with that new name for obtaining the namespace.
- Namespace URI if
- Keys are XML names of properties (e.g.
- Keys are XML names of types, ignoring
-
in
The reader from which to read events. -
prefixes
The prefixes for namespace URIs. Keys are URIs used in JAXB annotations and values are prefixes computed byNamespaces.getPreferredPrefix(String, String)or any other means. We store the prefix both for performance reasons and for improving the guarantees that the URI → prefix mapping is stable.- See Also:
-
nextEvent
The next event to return after a call topeek(). This is used for avoiding to recompute the same object many times whenpeek()is invoked before a call tonextEvent(). This is also required for avoiding to duplicate additions and removals of elements in theouterElementslist.
-
-
Constructor Details
-
TransformingReader
TransformingReader(XMLEventReader in, TransformVersion version) Creates a new reader for the given version of the standards.
-
-
Method Details
-
namespace
Returns the namespace for the given ISO type, ornullif unknown. This is the namespace used in JAXB annotations.- Parameters:
type- a class name defined by ISO 19115 or related standards (e.g."CI_Citation").- Returns:
- a namespace for the given type, or
nullif unknown.
-
isWrapper
Returnstrueif the givenwrapperis a wrapper for the givenevent. This method is used for assertions only. -
hasNext
public boolean hasNext()Checks if there are more events.- Specified by:
hasNextin interfaceIterator- Specified by:
hasNextin interfaceXMLEventReader
-
peek
Checks the nextXMLEventwithout removing it from the stream.- Specified by:
peekin interfaceXMLEventReader- Throws:
XMLStreamException
-
next
Returns the next element. UsenextEvent()instead. -
nextEvent
Forwards the call and keep trace of the XML elements opened up to this point.- Specified by:
nextEventin interfaceXMLEventReader- Throws:
XMLStreamException
-
nextTag
Forwards the call and keep trace of the XML elements opened up to this point.- Specified by:
nextTagin interfaceXMLEventReader- Throws:
XMLStreamException
-
convert
Keeps trace of XML elements opened up to this point and imports the given event. This method replaces the namespaces used in XML document by the namespace used by JAXB annotations. It is caller's responsibility to ensure that this method is invoked exactly once for each element, or at least for eachSTART_ELEMENTandEND_ELEMENT.- Parameters:
event- the event read from the underlying event reader.- Returns:
- the converted event (may be the same instance).
- Throws:
XMLStreamException
-
renamingMap
Returns the map loaded byTransformer.load(boolean, String, Set, int)if the given namespace is a known legacy namespace. This method returns a non-empty map only for legacy namespaces for which the "RenameOnImport.lst" file has been designed. This is necessary for avoiding confusion with classes of the same name defined in other standards. For example, theRecordclass name is used by other standards like Catalog Service for the Web (OGC CSW), and we don't want to replace the namespace of CSW classes.- Specified by:
renamingMapin classTransformer- Parameters:
namespace- the namespace URI for which to get the substitution map.- Returns:
- the substitution map for the given namespace, or an empty map if none.
-
relocate
Returns the new namespace for elements (types and properties) in the given namespace. This method is used only for default relocations, i.e. the fallback to apply when no explicit rule has been found.- Specified by:
relocatein classTransformer
-
prefixReplacement
Returns the prefix to use for a name in a new namespace. The prefix should have been specified (indirectly) by a previous call toimportNS(Namespace, …), for example as a result of aNAMESPACEevent. If not, we compute it now using the same algorithm than inimportNS.- Specified by:
prefixReplacementin classTransformer- Parameters:
previous- the prefix associated to old namespace.namespace- the new namespace URI.- Returns:
- prefix to use for the new namespace.
-
importNS
Converts a namespace read from the XML document to the namespace used by JAXB annotations. This methods can convert the namespace for which there is a bijective mapping, for example"http://www.isotc211.org/2005/gco"to"http://standards.iso.org/iso/19115/-3/gco/1.0". However, some namespaces like"http://www.isotc211.org/2005/gmd"may be left unchanged, because that namespace from legacy ISO 19139:2007 can be mapped to many different namespaces in newer ISO 19115-3:2016 standard. However, in some cases the context allows us to determines which newer namespace is used. In such case, that mapping is specified by the (oldURI,newURI) pair.- Parameters:
namespace- the namespace to import.oldURI- an old URI which has been renamed asnewURI, ornullif none.newURI- the new URI foroldURI, ornullifnewURIis null.
-
importNS
private List<Namespace> importNS(Iterator<Namespace> namespaces, String oldURI, String newURI, boolean changed) Imports the namespaces read from the XML document.- Parameters:
namespaces- the namespaces to transform.oldURI- an old URI which has been renamed asnewURI, ornullif none.newURI- the new URI foroldURI, ornullifnewURIis null.changed- whether to unconditionally pretend that there is a change.- Returns:
- the updated namespaces, or
nullif there is no change.
-
getElementText
Reads the content of a text-only element. Forwards from the underlying reader as-is.- Specified by:
getElementTextin interfaceXMLEventReader- Throws:
XMLStreamException
-
getProperty
Get the value of a feature/property from the underlying implementation.- Specified by:
getPropertyin interfaceXMLEventReader
-
close
Frees any resources associated with this reader. This method does not close the underlying input source.- Specified by:
closein interfaceXMLEventReader- Overrides:
closein classTransformer- Throws:
XMLStreamException
-