Author: desruisseaux
Date: Tue Dec 25 17:30:13 2012
New Revision: 1425758
URL: http://svn.apache.org/viewvc?rev=1425758&view=rev
Log:
Make all adapters stateless (removed the anchor map which was in
CharSequenceAdapter).
The anchors are now specified though the ReferenceResolver.
Modified:
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/CharSequenceAdapter.java
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/GO_CharacterString.java
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/InternationalStringAdapter.java
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/StringAdapter.java
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/URIAdapter.java
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/Country.java
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/LanguageCode.java
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/LocaleAdapter.java
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/PT_FreeText.java
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmx/Anchor.java
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/Version.java
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/xml/ReferenceResolver.java
Modified:
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/CharSequenceAdapter.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/CharSequenceAdapter.java?rev=1425758&r1=1425757&r2=1425758&view=diff
==============================================================================
---
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/CharSequenceAdapter.java
(original)
+++
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/CharSequenceAdapter.java
Tue Dec 25 17:30:13 2012
@@ -16,12 +16,11 @@
*/
package org.apache.sis.internal.jaxb.gco;
-import java.net.URI;
-import java.util.Map;
-import java.util.HashMap;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import org.opengis.util.InternationalString;
-import org.apache.sis.util.resources.Errors;
+import org.apache.sis.util.CharSequences;
+import org.apache.sis.xml.XLink;
+import org.apache.sis.xml.ReferenceResolver;
import org.apache.sis.internal.jaxb.MarshalContext;
import org.apache.sis.internal.jaxb.gmx.Anchor;
import org.apache.sis.internal.jaxb.gmd.PT_FreeText;
@@ -29,21 +28,14 @@ import org.apache.sis.internal.jaxb.gmd.
/**
* JAXB adapter in order to wrap the string value with a {@code
<gco:CharacterString>} element,
- * for ISO-19139 compliance. A {@link CharSequenceAdapter} can also substitute
text by anchors.
- * At the difference of most adapters provided in {@code
org.apache.sis.internal.jaxb} packages,
- * this adapter is <em>configurable</em>. It must be created explicitly with a
map of bindings
- * between labels and URNs, and the configured adapter must be given to the
mashaller as below:
+ * for ISO-19139 compliance. A {@link CharSequenceAdapter} can handle the
following types:
*
- * {@preformat java
- * CharSequenceAdapter adapter = new CharSequenceAdapter();
- * adapter.addLinkage(...);
- * marshaller.setAdapter(adapter);
- * marshaller.setAdapter(new StringAdapter(adapter));
- * marshaller.setAdapter(new InternationalStringAdapter(adapter));
- * }
- *
- * This class can also handles {@link InternationalString}, which will be
mapped to
- * {@link PT_FreeText} elements.
+ * <ul>
+ * <li>{@link InternationalString}, which will be mapped to {@link
PT_FreeText} elements.</li>
+ * <li>{@link String} (actually any character sequences other than {@code
InternationalString}).</li>
+ * <li>{@link Anchor}, which can be substituted to any of the above if the
{@link ReferenceResolver}
+ * in the current marshalling context maps the given text to a {@code
xlink}.</li>
+ * </ul>
*
* @author Cédric Briançon (Geomatys)
* @author Guilhem Legal (Geomatys)
@@ -57,118 +49,45 @@ import org.apache.sis.internal.jaxb.gmd.
*/
public final class CharSequenceAdapter extends XmlAdapter<GO_CharacterString,
CharSequence> {
/**
- * Binds string labels with URNs or anchors. Values can be either {@link
URI} or
- * {@link Anchor} instances. The map is initially null and will be created
- * when first needed.
- *
- * @see #addLinkage(String, URI)
- * @see #addLinkage(Anchor)
+ * Constructor for JAXB only.
*/
- private Map<String,Object> anchors;
-
- /**
- * Creates a uninitialized adapter.
- */
- public CharSequenceAdapter() {
+ private CharSequenceAdapter() {
}
/**
- * Adds a label associated to the given URN.
- *
- * @param label The label associated to the URN.
- * @param linkage The URN.
- * @throws IllegalStateException If a URN is already associated to the
given label.
- */
- public void addLinkage(final String label, final URI linkage) throws
IllegalStateException {
- add(label, linkage);
- }
-
- /**
- * Adds an anchor (label associated to an URN).
- *
- * @param anchor The anchor to add.
- * @throws IllegalStateException If a URN is already associated to the
anchor value.
- */
- public void addLinkage(final Anchor anchor) throws IllegalStateException {
- add(anchor.toString(), anchor);
- }
-
- /**
- * Implementation of {@code addLinkage} methods.
- */
- private synchronized void add(final String label, final Object linkage)
throws IllegalStateException {
- if (anchors == null) {
- anchors = new HashMap<>();
- }
- final Object old = anchors.put(label, linkage);
- if (old != null) {
- anchors.put(label, old);
- if (!old.equals(linkage)) {
- throw new
IllegalStateException(Errors.format(Errors.Keys.ValueAlreadyDefined_1, label));
- }
- }
- }
-
- /**
- * Converts a string read from a XML stream to the object containing
- * the value. JAXB calls automatically this method at unmarshalling time.
+ * Converts a string read from a XML stream to the object containing the
value.
+ * JAXB calls automatically this method at unmarshalling time.
*
* @param value The adapter for this metadata value.
* @return A {@link CharSequence} which represents the metadata value.
*/
@Override
public CharSequence unmarshal(final GO_CharacterString value) {
- if (value != null) {
- if (value instanceof PT_FreeText) {
- final PT_FreeText freeText = (PT_FreeText) value;
- String defaultValue = freeText.toString(); // May be null.
- if (defaultValue != null && freeText.contains(defaultValue)) {
- /*
- * If the <gco:CharacterString> value is repeated in one
of the
- * <gmd:LocalisedCharacterString> elements, keep only the
localized
- * version (because it specifies the locale, while the
unlocalized
- * string saids nothing on that matter).
- */
- defaultValue = null;
- }
- /*
- * Create the international string with all locales found in
the <gml:textGroup>
- * element. If the <gml:textGroup> element is missing or
empty, then we will use
- * an instance of SimpleInternationalString instead than the
more heavy
- * DefaultInternationalString.
- */
- return freeText.toInternationalString(defaultValue);
- }
- /*
- * Case where the value is an ordinary GO_CharacterString (not a
PT_FreeText).
- */
- CharSequence text = value.text;
- if (text != null) {
- if (text instanceof String) {
- text = ((String) text).trim();
- }
- if (text.length() != 0 || text instanceof Anchor) { // Anchor
may contain attributes.
- return text;
- }
- }
- }
- return null;
+ return (value != null) ? value.toCharSequence() : null;
}
/**
* Converts a {@linkplain CharSequence character sequence} to the object
to be marshalled
* in a XML file or stream. JAXB calls automatically this method at
marshalling time.
*
- * @param value The string value.
- * @return The wrapper for the given string.
+ * @param value The string value.
+ * @return The wrapper for the given character sequence.
*/
@Override
- public GO_CharacterString marshal(CharSequence value) {
+ public GO_CharacterString marshal(final CharSequence value) {
+ return wrap(value);
+ }
+
+ /**
+ * 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.
+ */
+ public static GO_CharacterString wrap(CharSequence value) {
if (value instanceof String) {
- value = ((String) value).trim();
- }
- if (value == null || value.length() == 0) {
- return null;
+ return wrap(value, (String) value); // Slightly more efficient
variant of this method.
}
/*
* <gmd:someElement xsi:type="gmd:PT_FreeText_PropertyType">
@@ -185,24 +104,27 @@ public final class CharSequenceAdapter e
}
}
/*
+ * 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 <gmx:Anchor> if a linkage is
found.
*/
if (!(value instanceof Anchor)) {
- synchronized (this) {
- if (anchors != null) {
- String key = value.toString();
- if (key != null) {
- key = key.trim();
- if (!key.isEmpty()) {
- final Object linkage = anchors.get(key);
- if (linkage != null) {
- if (linkage instanceof URI) {
- value = new Anchor((URI) linkage, key);
- } else {
- value = (Anchor) linkage;
- }
- }
- }
+ final String key = CharSequences.trimWhitespaces(value.toString());
+ if (key != null && !key.isEmpty()) {
+ final MarshalContext context = MarshalContext.current();
+ final XLink linkage =
MarshalContext.resolver(context).anchor(context, value, key);
+ if (linkage != null) {
+ if (linkage instanceof Anchor) {
+ value = (Anchor) linkage;
+ } else {
+ value = new Anchor(linkage, key);
}
}
}
@@ -219,4 +141,31 @@ public final class CharSequenceAdapter e
*/
return new GO_CharacterString(value);
}
+
+ /**
+ * Converts the string representation of an object to be marshalled in a
XML file or stream.
+ * This method is a copy of {@link #wrap(CharSequence)} simplified for the
case when we know
+ * that the character sequence being marshalled is a string.
+ *
+ * @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 wrapper for the given character sequence.
+ */
+ public static GO_CharacterString wrap(final Object object, String string) {
+ string = CharSequences.trimWhitespaces(string);
+ if (string == null || string.isEmpty()) {
+ return null;
+ }
+ CharSequence value = string;
+ final MarshalContext context = MarshalContext.current();
+ final XLink linkage = MarshalContext.resolver(context).anchor(context,
object, string);
+ if (linkage != null) {
+ if (linkage instanceof Anchor) {
+ value = (Anchor) linkage;
+ } else {
+ value = new Anchor(linkage, string);
+ }
+ }
+ return new GO_CharacterString(value);
+ }
}
Modified:
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/GO_CharacterString.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/GO_CharacterString.java?rev=1425758&r1=1425757&r2=1425758&view=diff
==============================================================================
---
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/GO_CharacterString.java
(original)
+++
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/GO_CharacterString.java
Tue Dec 25 17:30:13 2012
@@ -46,7 +46,7 @@ public class GO_CharacterString {
* The text or anchor value, or {@code null} if none. May be an instance
* of {@link Anchor}, which needs to be handled in a special way.
*/
- CharSequence text;
+ private CharSequence text;
/**
* Empty constructor for JAXB and subclasses.
@@ -121,8 +121,23 @@ public class GO_CharacterString {
}
/**
+ * Returns the content of this {@code <gco:CharacterString>} as a {@code
String},
+ * an {@code InternationalString} or an {@code Anchor}. This method is
overridden
+ * by {@code PT_FreeText} in order to handle the international string case.
+ *
+ * @return The character sequence for this {@code <gco:CharacterString>}.
+ */
+ public CharSequence toCharSequence() {
+ final CharSequence text = CharSequences.trimWhitespaces(this.text);
+ if (text != null && (text.length() != 0 || text instanceof Anchor)) {
// Anchor may contain attributes.
+ return text;
+ }
+ return null;
+ }
+
+ /**
* Returns the text as a string, or {@code null} if none.
- * The null value is expected by various {@code unmarshal} methods.
+ * The null value is expected by various {@code PT_FreeText}.
*
* {@note Returning <code>null</code> is unusual and not a recommended
practice.
* But precedents exist (for example Swing
<code>DefaultMutableTreeNode</code>)
Modified:
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/InternationalStringAdapter.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/InternationalStringAdapter.java?rev=1425758&r1=1425757&r2=1425758&view=diff
==============================================================================
---
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/InternationalStringAdapter.java
(original)
+++
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/InternationalStringAdapter.java
Tue Dec 25 17:30:13 2012
@@ -36,41 +36,28 @@ import org.apache.sis.util.iso.SimpleInt
*/
public final class InternationalStringAdapter extends
XmlAdapter<GO_CharacterString, InternationalString> {
/**
- * The adapter on which to delegate the marshalling processes.
- */
- private final CharSequenceAdapter adapter;
-
- /**
* Empty constructor for JAXB.
*/
private InternationalStringAdapter() {
- adapter = new CharSequenceAdapter();
- }
-
- /**
- * Creates a new adapter which will use the anchor map from the given
adapter.
- *
- * @param adapter The adaptor on which to delegate the work.
- */
- public InternationalStringAdapter(final CharSequenceAdapter adapter) {
- this.adapter = adapter;
}
/**
* Converts an object read from a XML stream to an {@link
InternationalString} implementation.
* JAXB invokes automatically this method at unmarshalling time.
*
- * @param value The adapter for the string value.
- * @return An {@link InternationalString} for the string value.
+ * @param value The wrapper for the value, or {@code null}.
+ * @return The unwrapped {@link String} value, or {@code null}.
*/
@Override
public InternationalString unmarshal(final GO_CharacterString value) {
- final CharSequence text = adapter.unmarshal(value);
- if (text != null) {
- if (text instanceof InternationalString) {
- return (InternationalString) text;
+ if (value != null) {
+ final CharSequence text = value.toCharSequence();
+ if (text != null) {
+ if (text instanceof InternationalString) {
+ return (InternationalString) text;
+ }
+ return new SimpleInternationalString(text.toString());
}
- return new SimpleInternationalString(text.toString());
}
return null;
}
@@ -79,11 +66,11 @@ public final class InternationalStringAd
* Converts an {@link InternationalString} to an object to format into a
XML stream.
* JAXB invokes automatically this method at marshalling time.
*
- * @param value The string value.
- * @return The adapter for the string.
+ * @param value The string value, or {@code null}.
+ * @return The wrapper for the given string, or {@code null}.
*/
@Override
public GO_CharacterString marshal(final InternationalString value) {
- return adapter.marshal(value);
+ return CharSequenceAdapter.wrap(value);
}
}
Modified:
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/StringAdapter.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/StringAdapter.java?rev=1425758&r1=1425757&r2=1425758&view=diff
==============================================================================
---
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/StringAdapter.java
(original)
+++
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/StringAdapter.java
Tue Dec 25 17:30:13 2012
@@ -35,24 +35,9 @@ import org.apache.sis.internal.jaxb.Mars
*/
public final class StringAdapter extends XmlAdapter<GO_CharacterString,
String> {
/**
- * The adapter on which to delegate the marshalling processes.
- */
- private final CharSequenceAdapter adapter;
-
- /**
* Empty constructor for JAXB.
*/
private StringAdapter() {
- adapter = new CharSequenceAdapter();
- }
-
- /**
- * Creates a new adapter which will use the anchor map from the given
adapter.
- *
- * @param adapter The adaptor on which to delegate the work.
- */
- public StringAdapter(final CharSequenceAdapter adapter) {
- this.adapter = adapter;
}
/**
@@ -60,41 +45,46 @@ public final class StringAdapter extends
* sequence is an instance of {@link InternationalString}, then the locale
from
* the current unmashalling context is used in order to get a string.
*
- * @param text The text for which to get a string representation, or
{@code null}.
+ * @param value The wrapper for the value, or {@code null}.
* @return The string representation of the given text, or {@code null}.
*/
- public static String toString(final CharSequence text) {
- if (text != null) {
- if (text instanceof InternationalString) {
- final MarshalContext context = MarshalContext.current();
- return ((InternationalString) text).toString(context != null ?
context.getLocale() : null);
+ public static String toString(final GO_CharacterString value) {
+ if (value != null) {
+ final CharSequence text = value.toCharSequence();
+ if (text != null) {
+ if (text instanceof InternationalString) {
+ final MarshalContext context = MarshalContext.current();
+ return ((InternationalString) text).toString(context !=
null ? context.getLocale() : null);
+ }
+ return text.toString();
}
- return text.toString();
}
return null;
}
/**
* Converts a string read from a XML stream to the object containing the
value.
- * JAXB calls automatically this method at unmarshalling time.
+ * JAXB calls automatically this method at unmarshalling time. If the
character
+ * sequence is an instance of {@link InternationalString}, then the locale
from
+ * the current unmashalling context is used in order to get a string.
*
- * @param value The adapter for this metadata value.
- * @return A {@link String} which represents the metadata value.
+ * @param value The wrapper for the value, or {@code null}.
+ * @return The unwrapped {@link String} value, or {@code null}.
*/
@Override
public String unmarshal(final GO_CharacterString value) {
- return toString(adapter.unmarshal(value));
+ return toString(value);
}
/**
* Converts a {@linkplain String string} to the object to be marshalled in
a XML file or stream.
* JAXB calls automatically this method at marshalling time.
*
- * @param value The string value.
- * @return The adapter for this string.
+ * @param value The string value, or {@code null}.
+ * @return The wrapper for the given string, or {@code null}.
*/
@Override
public GO_CharacterString marshal(final String value) {
- return adapter.marshal(value);
+ return CharSequenceAdapter.wrap(value, value);
}
}
Modified:
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/URIAdapter.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/URIAdapter.java?rev=1425758&r1=1425757&r2=1425758&view=diff
==============================================================================
---
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/URIAdapter.java
(original)
+++
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/URIAdapter.java
Tue Dec 25 17:30:13 2012
@@ -34,50 +34,38 @@ import org.apache.sis.internal.jaxb.Mars
*/
public final class URIAdapter extends XmlAdapter<GO_CharacterString, URI> {
/**
- * The adapter on which to delegate the marshalling processes.
- */
- private final CharSequenceAdapter adapter;
-
- /**
* Empty constructor for JAXB.
*/
private URIAdapter() {
- adapter = new CharSequenceAdapter();
- }
-
- /**
- * Creates a new adapter which will use the anchor map from the given
adapter.
- *
- * @param adapter The adaptor on which to delegate the work.
- */
- public URIAdapter(final CharSequenceAdapter adapter) {
- this.adapter = adapter;
}
/**
* Converts a URI read from a XML stream to the object containing the
value.
* JAXB calls automatically this method at unmarshalling time.
*
- * @param value The adapter for this metadata value.
- * @return An {@link URI} which represents the metadata value.
+ * @param value The wrapper for the URI value, or {@code null}.
+ * @return An {@link URI} which represents the URI value, or {@code null}.
* @throws URISyntaxException If the string is not a valid URI.
*/
@Override
public URI unmarshal(final GO_CharacterString value) throws
URISyntaxException {
- final String text = StringAdapter.toString(adapter.unmarshal(value));
- final MarshalContext context = MarshalContext.current();
- return (text != null) ?
MarshalContext.converter(context).toURI(context, text) : null;
+ final String text = StringAdapter.toString(value);
+ if (text != null) {
+ final MarshalContext context = MarshalContext.current();
+ return MarshalContext.converter(context).toURI(context, text);
+ }
+ return null;
}
/**
* Converts a {@link URI} to the object to be marshalled in a XML file or
stream.
* JAXB calls automatically this method at marshalling time.
*
- * @param value The URI value.
- * @return The adapter for the given URI.
+ * @param value The URI value, or {@code null}.
+ * @return The wrapper for the given URI, or {@code null}.
*/
@Override
public GO_CharacterString marshal(final URI value) {
- return (value != null) ? adapter.marshal(value.toString()) : null;
+ return (value != null) ? CharSequenceAdapter.wrap(value,
value.toString()) : null;
}
}
Modified:
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/Country.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/Country.java?rev=1425758&r1=1425757&r2=1425758&view=diff
==============================================================================
---
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/Country.java
(original)
+++
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/Country.java
Tue Dec 25 17:30:13 2012
@@ -64,7 +64,7 @@ public final class Country extends GO_Ch
/**
* Builds a {@code <gco:CharacterString>} element.
- * For private use by {@link #create(MarshalContext, Locale,
CharSequenceAdapter)} only.
+ * For private use by {@link #create(MarshalContext, Locale)} only.
*/
private Country(final GO_CharacterString code) {
super(code);
@@ -88,16 +88,18 @@ public final class Country extends GO_Ch
*
* @param context The current (un)marshalling context, or {@code null} if
none.
* @param locale The value to marshal, or {@code null}.
- * @paral anchors If non-null, marshal the locale as a {@code
<gco:CharacterString>} instead
- * than {@code <Country>}, using the given anchors if any.
* @return The country to marshal, or {@code null} if the given locale was
null
* or if its {@link Locale#getCountry()} attribute is the empty
string.
*/
- static Country create(final MarshalContext context, final Locale locale,
final CharSequenceAdapter anchors) {
+ static Country create(final MarshalContext context, final Locale locale) {
if (locale != null) {
final String codeListValue =
MarshalContext.converter(context).toCountryCode(context, locale);
- if (anchors != null && !codeListValue.isEmpty()) {
- final GO_CharacterString string =
anchors.marshal(codeListValue);
+ if (!codeListValue.isEmpty() && MarshalContext.isFlagSet(context,
MarshalContext.SUBSTITUTE_COUNTRY)) {
+ /*
+ * Marshal the locale as a <gco:CharacterString> instead than
<Country>,
+ * using the user-supplied anchors if any.
+ */
+ final GO_CharacterString string =
CharSequenceAdapter.wrap(locale, codeListValue);
if (string != null) {
return new Country(string);
}
Modified:
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/LanguageCode.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/LanguageCode.java?rev=1425758&r1=1425757&r2=1425758&view=diff
==============================================================================
---
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/LanguageCode.java
(original)
+++
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/LanguageCode.java
Tue Dec 25 17:30:13 2012
@@ -62,7 +62,7 @@ public final class LanguageCode extends
/**
* Builds a {@code <gco:CharacterString>} element.
- * For private use by {@link #create(MarshalContext, Locale,
CharSequenceAdapter)} only.
+ * For private use by {@link #create(MarshalContext, Locale)} only.
*/
private LanguageCode(final GO_CharacterString code) {
super(code);
@@ -70,7 +70,7 @@ public final class LanguageCode extends
/**
* Builds a {@code <LanguageCode>} element.
- * For private use by {@link #create(MarshalContext, Locale,
CharSequenceAdapter)} only.
+ * For private use by {@link #create(MarshalContext, Locale)} only.
*
* @param context The current (un)marshalling context, or {@code
null} if none.
* @param codeListValue The {@code codeListValue} attribute in the XML
element.
@@ -86,16 +86,18 @@ public final class LanguageCode extends
*
* @param context The current (un)marshalling context, or {@code null} if
none.
* @param locale The value to marshal, or {@code null}.
- * @paral anchors If non-null, marshal the locale as a {@code
<gco:CharacterString>} instead
- * than {@code <LanguageCode>}, using the given anchors if
any.
* @return The language to marshal, or {@code null} if the given locale
was null
* or if its {@link Locale#getLanguage()} attribute is the empty
string.
*/
- static LanguageCode create(final MarshalContext context, final Locale
locale, final CharSequenceAdapter anchors) {
+ static LanguageCode create(final MarshalContext context, final Locale
locale) {
if (locale != null) {
final String codeListValue =
MarshalContext.converter(context).toLanguageCode(context, locale);
- if (anchors != null && !codeListValue.isEmpty()) {
- final GO_CharacterString string =
anchors.marshal(codeListValue);
+ if (!codeListValue.isEmpty() && MarshalContext.isFlagSet(context,
MarshalContext.SUBSTITUTE_LANGUAGE)) {
+ /*
+ * Marshal the locale as a <gco:CharacterString> instead than
<LanguageCode>,
+ * using the user-supplied anchors if any.
+ */
+ final GO_CharacterString string =
CharSequenceAdapter.wrap(locale, codeListValue);
if (string != null) {
return new LanguageCode(string);
}
Modified:
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/LocaleAdapter.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/LocaleAdapter.java?rev=1425758&r1=1425757&r2=1425758&view=diff
==============================================================================
---
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/LocaleAdapter.java
(original)
+++
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/LocaleAdapter.java
Tue Dec 25 17:30:13 2012
@@ -20,7 +20,6 @@ import java.util.Locale;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import org.apache.sis.internal.jaxb.MarshalContext;
import org.apache.sis.internal.jaxb.gco.StringAdapter;
-import org.apache.sis.internal.jaxb.gco.CharSequenceAdapter;
/**
@@ -44,24 +43,9 @@ import org.apache.sis.internal.jaxb.gco.
*/
public final class LocaleAdapter extends XmlAdapter<LanguageCode, Locale> {
/**
- * The adapter on which to delegate the marshalling processes.
- */
- private final CharSequenceAdapter adapter;
-
- /**
* Empty constructor for JAXB.
*/
private LocaleAdapter() {
- adapter = new CharSequenceAdapter();
- }
-
- /**
- * Creates a new adapter which will use the anchor map from the given
adapter.
- *
- * @param adapter The adaptor on which to delegate the work.
- */
- public LocaleAdapter(final CharSequenceAdapter adapter) {
- this.adapter = adapter;
}
/**
@@ -78,7 +62,7 @@ public final class LocaleAdapter extends
if (candidate != null) {
return candidate;
}
- final String text = StringAdapter.toString(adapter.unmarshal(value));
+ final String text = StringAdapter.toString(value);
return (text != null) ?
MarshalContext.converter(context).toLocale(context, text) : null;
}
@@ -91,8 +75,6 @@ public final class LocaleAdapter extends
*/
@Override
public LanguageCode marshal(final Locale value) {
- final MarshalContext context = MarshalContext.current();
- return LanguageCode.create(context, value,
- MarshalContext.isFlagSet(context,
MarshalContext.SUBSTITUTE_LANGUAGE) ? adapter : null);
+ return LanguageCode.create(MarshalContext.current(), value);
}
}
Modified:
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/PT_FreeText.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/PT_FreeText.java?rev=1425758&r1=1425757&r2=1425758&view=diff
==============================================================================
---
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/PT_FreeText.java
(original)
+++
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/PT_FreeText.java
Tue Dec 25 17:30:13 2012
@@ -76,7 +76,7 @@ public final class PT_FreeText extends G
/**
* Empty constructor used only by JAXB.
*/
- public PT_FreeText() {
+ private PT_FreeText() {
}
/**
@@ -147,7 +147,7 @@ public final class PT_FreeText extends G
* @param search The text to search (usually the {@link #text} value).
* @return {@code true} if the given text has been found.
*/
- public boolean contains(final String search) {
+ private boolean contains(final String search) {
final TextGroup[] textGroup = this.textGroup;
if (textGroup != null) {
for (final TextGroup group : textGroup) {
@@ -167,14 +167,26 @@ public final class PT_FreeText extends G
}
/**
- * Returns the international string for this {@code PT_FreeText}.
- *
- * @param defaultValue The unlocalized string to give to {@link
DefaultInternationalString},
- * or {@code null}Â if none.
- * @return The international string, or {@code null} if none.
- * This is usually the {@link #text} value.
+ * Returns the content of this {@code <gco:CharacterString>} as an {@code
InternationalString}.
*/
- public InternationalString toInternationalString(final String
defaultValue) {
+ @Override
+ public CharSequence toCharSequence() {
+ String defaultValue = toString(); // May be null.
+ if (defaultValue != null && contains(defaultValue)) {
+ /*
+ * If the <gco:CharacterString> value is repeated in one of the
+ * <gmd:LocalisedCharacterString> elements, keep only the localized
+ * version (because it specifies the locale, while the unlocalized
+ * string saids nothing on that matter).
+ */
+ defaultValue = null;
+ }
+ /*
+ * Create the international string with all locales found in the
<gml:textGroup>
+ * element. If the <gml:textGroup> element is missing or empty, then
we will use
+ * an instance of SimpleInternationalString instead than the more heavy
+ * DefaultInternationalString.
+ */
DefaultInternationalString i18n = null;
final TextGroup[] textGroup = this.textGroup;
if (textGroup != null) {
Modified:
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmx/Anchor.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmx/Anchor.java?rev=1425758&r1=1425757&r2=1425758&view=diff
==============================================================================
---
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmx/Anchor.java
(original)
+++
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmx/Anchor.java
Tue Dec 25 17:30:13 2012
@@ -29,19 +29,20 @@ import java.util.Objects;
/**
* The {@code Anchor} element, which is included in {@code CharacterString}
elements.
- * This class implements {@link InternationalString} in an opportunist way, in
order to allow
- * direct usage with public API expecting {@link CharSequence} or {@link
InternationalString}
- * object.
+ * In XML documents, anchors are values with {@code XLink} attributes used in
places
+ * where we would normally expect a character sequence. Since Java properties
of type
+ * {@code CharSequence} can not return {@code XLink}, we workaround that
restriction
+ * by providing this {@code Anchor} class as a {@code XLink} subtype
implementing the
+ * {@link InternationalString} interface, so it can be used with the
above-cited Java
+ * properties.
*
* @author Guilhem Legal (Geomatys)
* @author Martin Desruisseaux (Geomatys)
* @since 0.3 (derived from geotk-2.5)
* @version 0.3
* @module
- *
- * @see <a href="http://www.xml.com/pub/a/2000/09/xlink/part2.html">XLink
introduction</a>
*/
-@XmlType(name = "AnchorType")
+@XmlType(name = "Anchor_Type")
public final class Anchor extends XLink implements InternationalString {
/**
* Defined as a matter of principle (this class is not expected to be
serialized).
@@ -49,7 +50,7 @@ public final class Anchor extends XLink
private static final long serialVersionUID = -6101324942683322597L;
/**
- * Often a short textual description of the URN target.
+ * Often a short textual description of the URI target.
* This is the value returned by {@link #toString()}.
*/
@XmlValue
@@ -63,10 +64,21 @@ public final class Anchor extends XLink
}
/**
- * Creates an {@code Anchor} initialized to the given value.
+ * Creates an {@code Anchor} initialized to the given {@code xlink} value.
+ *
+ * @param xlink The {@code xlink} from which to copy the attributes.
+ * @param value Often a short textual description of the URI target.
+ */
+ public Anchor(final XLink xlink, final String value) {
+ super(xlink);
+ this.value = value;
+ }
+
+ /**
+ * Creates an {@code Anchor} initialized to the given {@code href} value.
*
- * @param href A URN to an external resources or an identifier.
- * @param value Often a short textual description of the URN target.
+ * @param href A URI to an external resources or an identifier.
+ * @param value Often a short textual description of the URI target.
*/
public Anchor(final URI href, final String value) {
setHRef(href);
@@ -74,8 +86,8 @@ public final class Anchor extends XLink
}
/**
- * Returns the text as a string, or {@code null} if none.
- * The null value is expected by {@link GO_CharacterString#toString()}.
+ * Returns the text as a string, or {@code null} if none. The null value
is needed for proper
+ * working of {@link
org.apache.sis.internal.jaxb.gco.GO_CharacterString#toString()} method.
*/
@Override
public String toString() {
@@ -108,10 +120,21 @@ public final class Anchor extends XLink
/**
* Returns the sequence of characters in the given range of index.
+ * The returned object still an anchor with the same attribute values.
+ * It is caller responsibility to determine if those attributes are still
+ * appropriate for the sub-sequence.
*/
@Override
public CharSequence subSequence(final int start, final int end) {
- return (value != null ? value : "").subSequence(start, end);
+ String original = value;
+ if (original == null) {
+ original = "";
+ }
+ final String substring = original.substring(start, end);
+ if (substring == original) {
+ return this;
+ }
+ return new Anchor(this, substring);
}
/**
Modified:
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/Version.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/Version.java?rev=1425758&r1=1425757&r2=1425758&view=diff
==============================================================================
---
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/Version.java
(original)
+++
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/Version.java
Tue Dec 25 17:30:13 2012
@@ -146,7 +146,7 @@ public class Version implements CharSequ
}
Comparable<?> candidate = parsed[index];
if (candidate == null) {
- final String value = components[index].trim();
+ final String value =
CharSequences.trimWhitespaces(components[index]);
try {
candidate = Integer.valueOf(value);
} catch (NumberFormatException e) {
Modified:
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/xml/ReferenceResolver.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/xml/ReferenceResolver.java?rev=1425758&r1=1425757&r2=1425758&view=diff
==============================================================================
---
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/xml/ReferenceResolver.java
(original)
+++
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/xml/ReferenceResolver.java
Tue Dec 25 17:30:13 2012
@@ -22,6 +22,7 @@ import org.opengis.metadata.Identifier;
import org.apache.sis.util.resources.Errors;
import org.apache.sis.util.LenientComparable;
import org.apache.sis.internal.jaxb.UUIDs;
+import org.apache.sis.internal.jaxb.gmx.Anchor;
import static org.apache.sis.util.ArgumentChecks.*;
@@ -43,9 +44,8 @@ import static org.apache.sis.util.Argume
*/
public class ReferenceResolver {
/**
- * The default and thread-safe instance. This instance is used at
unmarshalling time
- * when no {@code ReferenceResolver} was explicitly set by the {@link
XML#RESOLVER}
- * property.
+ * The default and thread-safe instance. This instance is used at
unmarshalling time when
+ * no {@code ReferenceResolver} was explicitly set by the {@link
XML#RESOLVER} property.
*/
public static final ReferenceResolver DEFAULT = new ReferenceResolver();
@@ -128,9 +128,9 @@ public class ReferenceResolver {
}
/**
- * Returns {@code true} if the marshaller can use a {@code xlink:href}
reference to the given
- * metadata instead than writing the full element. This method is invoked
when a metadata to be
- * marshalled has a {@link XLink} identifier. Because those metadata may
be defined externally,
+ * Returns {@code true} if the marshaller can use a reference to the given
metadata
+ * instead than writing the full element. This method is invoked when a
metadata to
+ * be marshalled has a UUID identifier. Because those metadata may be
defined externally,
* SIS can not know if the metadata shall be fully marshalled or not.
* Such information needs to be provided by the application.
*
@@ -143,18 +143,18 @@ public class ReferenceResolver {
* @param type The type of object to be marshalled as an
<strong>interface</strong>.
* This is usually a <a
href="http://www.geoapi.org">GeoAPI</a> interface.
* @param object The object to be marshalled.
- * @param link The reference of the object to be marshalled.
- * @return {@code true} if the marshaller can use the {@code xlink:href}
attribute
+ * @param uuid The unique identifier of the object to be marshalled.
+ * @return {@code true} if the marshaller can use the {@code uuidref}
attribute
* instead than marshalling the given metadata.
*/
- public <T> boolean canSubstituteByReference(final MarshalContext context,
final Class<T> type, final T object, final XLink link) {
+ public <T> boolean canSubstituteByReference(final MarshalContext context,
final Class<T> type, final T object, final UUID uuid) {
return false;
}
/**
- * Returns {@code true} if the marshaller can use a reference to the given
metadata
- * instead than writing the full element. This method is invoked when a
metadata to
- * be marshalled has a UUID identifier. Because those metadata may be
defined externally,
+ * Returns {@code true} if the marshaller can use a {@code xlink:href}
reference to the given
+ * metadata instead than writing the full element. This method is invoked
when a metadata to be
+ * marshalled has a {@link XLink} identifier. Because those metadata may
be defined externally,
* SIS can not know if the metadata shall be fully marshalled or not.
* Such information needs to be provided by the application.
*
@@ -167,11 +167,48 @@ public class ReferenceResolver {
* @param type The type of object to be marshalled as an
<strong>interface</strong>.
* This is usually a <a
href="http://www.geoapi.org">GeoAPI</a> interface.
* @param object The object to be marshalled.
- * @param uuid The unique identifier of the object to be marshalled.
- * @return {@code true} if the marshaller can use the {@code uuidref}
attribute
+ * @param link The reference of the object to be marshalled.
+ * @return {@code true} if the marshaller can use the {@code xlink:href}
attribute
* instead than marshalling the given metadata.
*/
- public <T> boolean canSubstituteByReference(final MarshalContext context,
final Class<T> type, final T object, final UUID uuid) {
+ public <T> boolean canSubstituteByReference(final MarshalContext context,
final Class<T> type, final T object, final XLink link) {
return false;
}
+
+ /**
+ * Returns the {@code <gmx:Anchor>} to use for the given text, or {@code
null} if none.
+ * Anchors can appear in ISO 19139 documents where we would normally
expect a character
+ * sequence. For example:
+ *
+ * <table class="sis">
+ * <tr>
+ * <th>As {@code <gco:CharacterString>}</th>
+ * <th>As {@code <gmx:Anchor>}</th>
+ * </tr><tr>
+ * <td>{@preformat xml
+ * <gmd:country>
+ * <gco:CharacterString>France</gco:CharacterString>
+ * </gmd:country>
+ * }</td>
+ * <td>{@preformat xml
+ * <gmd:country>
+ * <gmx:Anchor xlink:href="SDN:C320:2:FR">France</gmx:Anchor>
+ * </gmd:country>
+ * }</td>
+ * </tr>
+ * </table>
+ *
+ * Subclasses can override this method if they can provide a mapping from
some text
+ * values to anchors.
+ *
+ * @param context Context (GML version, locale, <i>etc.</i>) of the
(un)marshalling process.
+ * @param object The object for which an anchor is requested. Often same
than {@code text},
+ * but can also be the {@link java.net.URI} or {@link
java.util.Locale} instance
+ * for which {@code text} is a string representation.
+ * @param text The textual representation of the object for which to
get the anchor.
+ * @return The anchor for the given text, or {@code null} if none.
+ */
+ public XLink anchor(final MarshalContext context, final Object object,
final CharSequence text) {
+ return (text instanceof Anchor) ? (Anchor) text : null;
+ }
}