@Override public XLink anchor(final MarshalContext context, final Object value, final CharSequence text) { final URI linkage; synchronized (anchors) { linkage = anchors.get(value); } if (linkage != null) { final XLink xlink = new XLink(); xlink.setHRef(linkage); return xlink; } return super.anchor(context, value, text); } });
/** * Compares this {@code Anchor} with the given object for equality. * * @param object the object to compare with this anchor type. */ @Override public boolean equals(final Object object) { if (object == this) { return true; } if (super.equals(object)) { final Anchor that = (Anchor) object; return Objects.equals(this.value, that.value); } return false; }
/** * Returns a string representation of this object. The default implementation returns the * simple class name followed by non-null attributes, as in the example below: * * {@preformat text * XLink[type="locator", href="urn:ogc:def:method:EPSG::4326"] * } */ @Override public String toString() { final StringBuilder buffer = new StringBuilder(64); buffer.append(Classes.getShortClassName(this)).append('['); append(buffer, "type", getType()); append(buffer, "href", getHRef()); append(buffer, "role", getRole()); append(buffer, "arcrole", getArcRole()); append(buffer, "title", getTitle()); append(buffer, "show", getShow()); append(buffer, "actuate", getActuate()); append(buffer, "label", getLabel()); append(buffer, "from", getFrom()); append(buffer, "to", getTo()); return buffer.append(']').toString(); }
/** * 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(); xlink.setType(XLink.Type.SIMPLE); // The "simple" type is fixed in the "gco" schema. } return xlink; }
final XLink link = new XLink(); int hashCode = link.hashCode(); assertFalse(hashCode == 0); assertNull(link.getType()); link.setType(XLink.Type.AUTO); assertEquals(XLink.Type.TITLE, link.getType()); assertEquals("XLink[type=\"title\"]", link.toString()); assertFalse("Hash code should have changed.", hashCode == (hashCode = link.hashCode())); assertFalse("Hash code can not be zero.", hashCode == 0); link.setRole(new URI("org:apache:sis:role")); assertEquals(XLink.Type.EXTENDED, link.getType()); assertEquals("XLink[type=\"extended\", role=\"org:apache:sis:role\"]", link.toString()); assertFalse("Hash code should have changed.", hashCode == (hashCode = link.hashCode())); assertFalse("Hash code can not be zero.", hashCode == 0); link.setTitle(new SimpleInternationalString("Some title")); assertEquals(XLink.Type.EXTENDED, link.getType()); assertEquals("XLink[type=\"extended\", role=\"org:apache:sis:role\", title=\"Some title\"]", link.toString()); assertFalse("Hash code should have changed.", hashCode == (hashCode = link.hashCode())); assertFalse("Hash code can not be zero.", hashCode == 0); link.setLabel("SomeLabel"); assertEquals(XLink.Type.RESOURCE, link.getType()); assertEquals("XLink[type=\"resource\", role=\"org:apache:sis:role\", title=\"Some title\", label=\"SomeLabel\"]", link.toString()); assertFalse("Hash code should have changed.", hashCode == (hashCode = link.hashCode())); assertFalse("Hash code can not be zero.", hashCode == 0);
final XLink link = new XLink(); link.setType(XLink.Type.AUTO); link.setRole(new URI("org:apache:sis:role")); link.setTitle(new SimpleInternationalString("Some title")); link.freeze(); final XLink other = new XLink(); assertFalse(link.equals(other)); assertFalse(link.hashCode() == other.hashCode()); other.setType(XLink.Type.AUTO); assertFalse(link.equals(other)); assertFalse(link.hashCode() == other.hashCode()); other.setRole(new URI("org:apache:sis:role")); assertFalse(link.equals(other)); assertFalse(link.hashCode() == other.hashCode()); other.setTitle(new SimpleInternationalString("Some title")); assertEquals(link, other); assertEquals(link.hashCode(), other.hashCode()); other.freeze(); assertEquals(link, other); assertEquals(link.hashCode(), other.hashCode());
final XLink link = new XLink(); link.setType(XLink.Type.SIMPLE); link.setHRef(new URI("org:apache:sis:href")); assertEquals("XLink[type=\"simple\", href=\"org:apache:sis:href\"]", link.toString()); try { link.setLabel("SomeLabel"); fail("Should not be allowed to set the label."); } catch (IllegalStateException e) { assertTrue(e.getMessage().contains("simple")); assertEquals("XLink[type=\"simple\", href=\"org:apache:sis:href\"]", link.toString()); try { link.setType(XLink.Type.EXTENDED); fail("Should not be allowed to set a type that does not include HREF."); } catch (IllegalStateException e) { assertEquals("XLink[type=\"simple\", href=\"org:apache:sis:href\"]", link.toString()); link.setType(XLink.Type.LOCATOR); assertEquals("XLink[type=\"locator\", href=\"org:apache:sis:href\"]", link.toString()); link.freeze(); try { link.setHRef(null); fail("The XLink should be unmodifiable."); } catch (UnsupportedOperationException e) {
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) {
/** * Tests explicitly 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)); }
/** * Extracts the {@code xlink:href} value from the {@link XLink} if presents. * This method does not test if an explicit {@code xlink:href} identifier exists; * this check must be done by the caller <strong>before</strong> to invoke this method. * * @see ModifiableIdentifierMap#setHRef(URI) */ private URI getHRef() { final Identifier identifier = getIdentifier(IdentifierSpace.XLINK); if (identifier instanceof SpecializedIdentifier<?>) { final Object link = ((SpecializedIdentifier<?>) identifier).value; if (link instanceof XLink) { return ((XLink) link).getHRef(); } } return null; }
/** * Sets the {@code href} attribute value. * * @param href the new attribute value. * @throws URISyntaxException if the given string can not be parsed as a URI. * @category xlink */ public final void setHRef(final String href) throws URISyntaxException { xlink(true).setHRef(toURI(href)); }
/** * Sets the {@code role} attribute value. * * @param role the new attribute value. * @throws URISyntaxException if the given string can not be parsed as a URI. * @category xlink */ public final void setRole(final String role) throws URISyntaxException { xlink(true).setRole(toURI(role)); }
/** * Sets the {@code title} attribute value. * * @param title the new attribute value. * @category xlink */ public final void setTitle(String title) { if (title != null && !(title = title.trim()).isEmpty()) { xlink(true).setTitle(new SimpleInternationalString(title)); } }
/** * Sets the {@code show} attribute value. * * @param show the new attribute value. * @category xlink */ public final void setShow(final XLink.Show show) { xlink(true).setShow(show); }
/** * Returns a hash code value for this anchor type. */ @Override public int hashCode() { return super.hashCode()*31 + Objects.hashCode(value); } }
/** * Sets the {@code actuate} attribute value. * * @param actuate the new attribute value. * @category xlink */ public final void setActuate(final XLink.Actuate actuate) { xlink(true).setActuate(actuate); }
/** * 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(); xlink.setType(XLink.Type.SIMPLE); // The "simple" type is fixed in the "gco" schema. } return xlink; }