Hello Emmanuel
The Anchor class is for internal SIS mechanic. Classes in
"org.apache.sis.internal", while public, are more at risk to change in
future SIS versions. The proposed public (non-internal) API uses a
different strategy. Rather than assigning explicitly anchor values to
the metadata properties, we create the metadata object without
consideration for anchors, for example with the "4326" code value. Only
at marshalling time, those values can be replaced on-the-fly by XLink.
This replacement can be done by overriding the
ReferenceResolver.anchor(...) method. The intend of this approach is to
separate the metadata content from the XML representation of that
metadata, if we consider XLink as a XML-specific thing. The XLink can
often be constructed dynamically from the content.
Below is a code example doing that.
final class Test extends ReferenceResolver {
public static void main(String[] args) throws Exception {
DefaultMetadata metadata = new DefaultMetadata();
NamedIdentifier namedIdentifier = new NamedIdentifier(null, "EPSG",
"4326", null, null);
ReferenceSystemMetadata rsm = new
ReferenceSystemMetadata(namedIdentifier);
metadata.setReferenceSystemInfo(Arrays.asList(rsm));
final Map<String,Object> properties = new HashMap<>();
properties.put(XML.RESOLVER, new Test());
XML.marshal(metadata, new StreamResult(System.out), properties);
}
@Override
public XLink anchor(MarshalContext context, Object value, CharSequence
text) {
if ("4326".equals(text)) {
final XLink srsAnchor = new XLink();
srsAnchor.setHRef(URI.create("http://www.opengis.net/def/crs/EPSG/0/4326"));
srsAnchor.setTitle(new SimpleInternationalString("WGS84"));
return srsAnchor;
}
return null;
}
}
Above example replaces all occurrences of
<gco:CharacterString>4326</gco:CharacterString> by <gmx:Anchor ...> at
marshalling time. This may be too aggressive, in which case it is
possible to filter by enclosing type (in this example: Identifier) -
that could be the subject of another email.
If nevertheless using directly the internal Anchor class is preferred,
then the only thing needed for fixing the IllegalArgumentException is to
replace:
Anchor srsAnchor = new Anchor();
srsAnchor.setHRef(new URI("http://www.opengis.net/def/crs/EPSG/0/4326"));
srsAnchor.setTitle(new SimpleInternationalString("WGS84"));
srsAnchor.setLabel("4326");
by:
Anchor srsAnchor = new Anchor(new
URI("http://www.opengis.net/def/crs/EPSG/0/4326"), "4326");
srsAnchor.setTitle(new SimpleInternationalString("WGS84"));
Martin