use of org.apache.sis.xml.XLink in project sis by apache.
the class CharSequenceAdapter method value.
/**
* Same as {@link #wrap(Context, Object, String)}, but returns directly the {@link GO_CharacterString#text}
* value without wrapping in a {@code GO_CharacterString} instance.
*
* @param context the current (un)marshalling context, or {@code null} if none.
* @param object the object being marshalled (e.g. {@code URI} or {@code Locale}).
* @param string the string representation of the object being marshalled.
* @return the text value for the given character sequence, or {@code null}.
*/
public static CharSequence value(final Context context, final Object object, String string) {
string = CharSequences.trimWhitespaces(string);
if (string == null || string.isEmpty()) {
return null;
}
final XLink linkage = Context.resolver(context).anchor(context, object, string);
if (linkage != null) {
if (linkage instanceof Anchor) {
return (Anchor) linkage;
} else {
return new Anchor(linkage, string);
}
}
return string;
}
use of org.apache.sis.xml.XLink in project sis by apache.
the class CharSequenceAdapter method wrap.
/**
* Converts a {@linkplain CharSequence character sequence} to the object to be marshalled
* in a XML file or stream.
*
* @param value the character representation of the object being marshalled.
* @return the wrapper for the given character sequence, or {@code null}.
*/
static GO_CharacterString wrap(CharSequence value) {
if (value instanceof String) {
// Slightly more efficient variant of this method.
return wrap(Context.current(), value, (String) value);
}
/*
* <mdb:someElement xsi:type="lan:PT_FreeText_PropertyType">
* <gco:CharacterString>...</gco:CharacterString>
* <lan:PT_FreeText>
* ... see PT_FreeText ...
* </lan:PT_FreeText>
* </mdb:someElement>
*/
if (value instanceof InternationalString) {
final PT_FreeText ft = PT_FreeText.create((InternationalString) value);
if (ft != null) {
return ft;
}
}
/*
* Invoking (indirectly) CharSequence.subSequence(…) may change the kind of object.
* We know that Anchor is safe, and that most InternationalString implementations
* lost the localized strings. This is why we trim the white spaces only here.
*/
value = CharSequences.trimWhitespaces(value);
if (value == null || value.length() == 0) {
return null;
}
/*
* Substitute <gco:CharacterString> by <gcx:Anchor> if a linkage is found.
*/
if (!(value instanceof Anchor)) {
final String key = CharSequences.trimWhitespaces(value.toString());
if (key != null && !key.isEmpty()) {
final Context context = Context.current();
final XLink linkage = Context.resolver(context).anchor(context, value, key);
if (linkage != null) {
if (linkage instanceof Anchor) {
value = (Anchor) linkage;
} else {
value = new Anchor(linkage, key);
}
}
}
}
/*
* At this stage, the value (typically a String or InternationalString) may
* have been replaced by an Anchor. The output will be one of the following:
*
* ┌──────────────────────────────────────────────────┬────────────────────────────────┐
* │ <mdb:someElement> │ <mdb:someElement> │
* │ <gco:CharacterString>...</gco:CharacterString> │ <gcx:Anchor>...</gcx:Anchor> │
* │ </mdb:someElement> │ </mdb:someElement> │
* └──────────────────────────────────────────────────┴────────────────────────────────┘
*/
return new GO_CharacterString(value);
}
use of org.apache.sis.xml.XLink in project sis by apache.
the class PropertyType method xlink.
/**
* Returns the {@code xlink}, or {@code null} if none and {@code create} is {@code false}.
* If the {@code create} argument is {@code true}, then this method will create the XLink
* when first needed. In the later case, any previous {@code gco:nilReason} will be
* overwritten since the object is not nil.
*/
private XLink xlink(final boolean create) {
final ObjectReference ref = reference(create);
if (ref == null) {
return null;
}
XLink xlink = ref.xlink;
if (create && xlink == null) {
ref.xlink = xlink = new XLink();
// The "simple" type is fixed in the "gco" schema.
xlink.setType(XLink.Type.SIMPLE);
}
return xlink;
}
use of org.apache.sis.xml.XLink in project sis by apache.
the class ModifiableIdentifierMapTest method testHRefSubstitution.
/**
* Tests explicitely the special handling of {@code href} values.
*/
@Test
public void testHRefSubstitution() {
final List<Identifier> identifiers = new ArrayList<>();
final IdentifierMap map = new ModifiableIdentifierMap(identifiers);
assertNull(map.put(HREF, "myHREF"));
assertEquals("Shall contain the entry we added.", "myHREF", map.get(HREF));
// Check the XLink object
final XLink link = map.getSpecialized(XLINK);
assertEquals("Added href shall be stored as XLink attribute.", "myHREF", String.valueOf(link.getHRef()));
assertEquals("Identifier list shall contain the XLink.", link.toString(), getSingleton(identifiers).getCode());
// Modidfy the XLink object directly
link.setHRef(URI.create("myNewHREF"));
assertEquals("Change in XLink shall be reflected in href.", "myNewHREF", map.get(HREF));
}
use of org.apache.sis.xml.XLink in project sis by apache.
the class PropertyTypeTest method testWithXLink.
/**
* Implementation of the public {@code test*XLink()} methods.
*
* <ul>
* <li>If {@code useReferenceResolverMock} is {@code false}, then this test uses the default SIS behavior,
* which is to not omit the metadata and still write the XLink for informative purpose.</li>
*
* <li>If {@code useReferenceResolverMock} is {@code true}, then the metadata object shall be replaced
* by the XLink because the {@link ReferenceResolverMock#canSubstituteByReference(MarshalContext,
* Class, Object, XLink)} method returns {@code true}.</li>
* </ul>
*/
private void testWithXLink(final boolean useReferenceResolverMock) {
final XLink link = new XLink();
link.setShow(XLink.Show.REPLACE);
link.setActuate(XLink.Actuate.ON_LOAD);
link.setTitle(new SimpleInternationalString("myLinkTitle"));
metadata.getIdentifierMap().putSpecialized(IdentifierSpace.XLINK, link);
if (useReferenceResolverMock) {
context = ReferenceResolverMock.begin(true);
// XMLTestCase.clearContext() will dispose the context.
}
final PropertyTypeMock property = marshal();
if (!useReferenceResolverMock) {
assertSame(metadata, property.metadata);
} else {
assertNull("metadata", property.metadata);
}
assertNull("nilReason", property.getNilReason());
assertNull("uuidref", property.getUUIDREF());
assertNull("href", property.getHRef());
assertNull("role", property.getRole());
assertNull("arcrole", property.getArcRole());
assertEquals("title", "myLinkTitle", property.getTitle());
assertEquals("show", XLink.Show.REPLACE, property.getShow());
assertEquals("actuate", XLink.Actuate.ON_LOAD, property.getActuate());
}
Aggregations