You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2013/06/27 12:54:05 UTC
svn commit: r1497293 - in /sis/branches/JDK7/core:
sis-metadata/src/main/java/org/apache/sis/metadata/iso/distribution/
sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/
sis-utility/src/main/java/org/apache/sis/internal/system/ sis-utility/sr...
Author: desruisseaux
Date: Thu Jun 27 10:54:04 2013
New Revision: 1497293
URL: http://svn.apache.org/r1497293
Log:
Make the LocalName adapter consistent with the GenericName one.
Added:
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/GO_LocalName.java
- copied, changed from r1497272, sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/LocalNameAdapter.java
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/NameAdapter.java
- copied, changed from r1497272, sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/GO_GenericName.java
Removed:
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/LocalNameAdapter.java
Modified:
sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/distribution/package-info.java
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/GO_GenericName.java
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/system/DefaultFactories.java
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/iso/AbstractName.java
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultLocalName.java
Modified: sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/distribution/package-info.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/distribution/package-info.java?rev=1497293&r1=1497292&r2=1497293&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/distribution/package-info.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/distribution/package-info.java [UTF-8] Thu Jun 27 10:54:04 2013
@@ -92,8 +92,8 @@
// Java types, primitive types and basic OGC types handling
@XmlJavaTypeAdapter(UnitAdapter.class),
- @XmlJavaTypeAdapter(LocalNameAdapter.class),
@XmlJavaTypeAdapter(InternationalStringAdapter.class),
+ @XmlJavaTypeAdapter(GO_LocalName.class),
@XmlJavaTypeAdapter(GO_DateTime.class),
@XmlJavaTypeAdapter(GO_Decimal.class), @XmlJavaTypeAdapter(type=double.class, value=GO_Decimal.class),
@XmlJavaTypeAdapter(GO_Integer.class), @XmlJavaTypeAdapter(type=int.class, value=GO_Integer.class)
Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/GO_GenericName.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/GO_GenericName.java?rev=1497293&r1=1497292&r2=1497293&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/GO_GenericName.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/GO_GenericName.java [UTF-8] Thu Jun 27 10:54:04 2013
@@ -16,22 +16,8 @@
*/
package org.apache.sis.internal.jaxb.gco;
-import java.util.ConcurrentModificationException;
-import java.util.List;
-import java.util.Locale;
-import javax.xml.bind.annotation.XmlElement;
-import javax.xml.bind.annotation.adapters.XmlAdapter;
-import org.opengis.util.TypeName;
-import org.opengis.util.LocalName;
-import org.opengis.util.ScopedName;
-import org.opengis.util.MemberName;
import org.opengis.util.GenericName;
import org.apache.sis.util.iso.AbstractName;
-import org.apache.sis.util.iso.DefaultTypeName;
-import org.apache.sis.util.iso.DefaultMemberName;
-import org.apache.sis.util.resources.Errors;
-
-import static org.apache.sis.internal.jaxb.gco.LocalNameAdapter.getNameFactory;
/**
@@ -53,12 +39,7 @@ import static org.apache.sis.internal.ja
* @version 0.3
* @module
*/
-public final class GO_GenericName extends XmlAdapter<GO_GenericName, GenericName> {
- /**
- * The generic name to be marshalled.
- */
- private AbstractName name;
-
+public final class GO_GenericName extends NameAdapter<GO_GenericName, GenericName> {
/**
* Empty constructor for JAXB only.
*/
@@ -68,135 +49,7 @@ public final class GO_GenericName extend
/**
* Wraps a name at marshalling-time.
*/
- private GO_GenericName(final AbstractName name) {
- this.name = name;
- }
-
- /**
- * Ensures that the {@linkplain #name} is not already defined.
- *
- * @throws IllegalStateException If a name is already defined.
- */
- private void ensureUndefined() throws IllegalStateException {
- if (name != null) {
- throw new IllegalStateException(Errors.format(Errors.Keys.ValueAlreadyDefined_1, "name"));
- }
- }
-
- /**
- * Returns the {@code LocalName} generated from the metadata value.
- * The local name is returned only if it is not a {@link TypeName} or a {@link MemberName}
- * (otherwise, the corresponding {@code getXXX()} method needs to be invoked instead.
- *
- * @return The current local name, or {@code null} if none.
- *
- * @see #getTypeName()
- * @see #getMemberName()
- */
- @XmlElement(name = "LocalName")
- public String getLocalName() {
- final AbstractName name = this.name;
- return (name instanceof LocalName) && !(name instanceof TypeName)
- && !(name instanceof MemberName) ? name.toString() : null;
- }
-
- /**
- * Sets the value for the {@code LocalName}.
- * This method is called at unmarshalling-time by JAXB.
- *
- * @param value The new name.
- * @throws IllegalStateException If a name is already defined.
- */
- public void setLocalName(final String value) throws IllegalStateException {
- ensureUndefined();
- if (value == null) {
- name = null;
- } else {
- /*
- * Following cast should be safe because the getNameFactory() method asked specifically
- * for a DefaultNameFactory instance, which is known to create AbstractName instances.
- */
- name = (AbstractName) getNameFactory().createLocalName(null, value);
- }
- }
-
- /**
- * Returns the {@code ScopedName} generated from the metadata value.
- * This method is called at marshalling-time by JAXB.
- *
- * @return The current name, or {@code null} if none.
- */
- @XmlElement(name = "ScopedName")
- public String getScopedName() {
- final AbstractName name = this.name;
- return (name instanceof ScopedName) ? name.toString() : null;
- }
-
- /**
- * Sets the value for the {@code ScopedName}.
- * This method is called at unmarshalling-time by JAXB.
- *
- * @param value The new name.
- * @throws IllegalStateException If a name is already defined.
- */
- public void setScopedName(final String value) throws IllegalStateException {
- ensureUndefined();
- if (value == null) {
- name = null;
- } else {
- /*
- * Following cast should be safe because the getNameFactory() method asked specifically
- * for a DefaultNameFactory instance, which is known to create AbstractName instances.
- */
- name = (AbstractName) getNameFactory().parseGenericName(null, value);
- }
- }
-
- /**
- * Returns the {@code TypeName} generated from the metadata value.
- * This method is called at marshalling-time by JAXB.
- *
- * @return The current name, or {@code null} if none.
- */
- @XmlElement(name = "TypeName")
- public DefaultTypeName getTypeName() {
- final AbstractName name = this.name;
- return (name instanceof DefaultTypeName) ? (DefaultTypeName) name : null;
- }
-
- /**
- * Sets the value for the {@code TypeName}.
- * This method is called at unmarshalling-time by JAXB.
- *
- * @param value The new name.
- * @throws IllegalStateException If a name is already defined.
- */
- public void setTypeName(final DefaultTypeName value) throws IllegalStateException {
- ensureUndefined();
- name = value;
- }
-
- /**
- * Returns the {@code MemberName} generated from the metadata value.
- * This method is called at marshalling-time by JAXB.
- *
- * @return The current name, or {@code null} if none.
- */
- @XmlElement(name = "MemberName")
- public DefaultMemberName getMemberName() {
- final AbstractName name = this.name;
- return (name instanceof MemberName) ? (DefaultMemberName) name : null;
- }
-
- /**
- * Sets the value for the {@code MemberName}.
- * This method is called at unmarshalling-time by JAXB.
- *
- * @param value The new name.
- * @throws IllegalStateException If a name is already defined.
- */
- public void setMemberName(final DefaultMemberName value) throws IllegalStateException {
- ensureUndefined();
+ private GO_GenericName(final AbstractName value) {
name = value;
}
@@ -209,34 +62,7 @@ public final class GO_GenericName extend
*/
@Override
public GO_GenericName marshal(final GenericName value) {
- if (value == null) {
- return null;
- }
- final AbstractName impl;
- if (value instanceof AbstractName) {
- impl = (AbstractName) value;
- } else {
- /*
- * Recreates a new name for the given name in order to get
- * a SIS implementation from an arbitrary implementation.
- */
- final List<? extends LocalName> parsedNames = value.getParsedNames();
- final CharSequence[] names = new CharSequence[parsedNames.size()];
- int i=0;
- for (final LocalName component : parsedNames) {
- // Asks for the unlocalized name, since we are going to marshal that.
- names[i++] = component.toInternationalString().toString(Locale.ROOT);
- }
- if (i != names.length) {
- throw new ConcurrentModificationException(Errors.format(Errors.Keys.UnexpectedChange_1, "parsedNames"));
- }
- /*
- * Following cast should be safe because the getNameFactory() method asked specifically
- * for a DefaultNameFactory instance, which is known to create AbstractName instances.
- */
- impl = (AbstractName) getNameFactory().createGenericName(value.scope(), names);
- }
- return new GO_GenericName(impl);
+ return (value != null) ? new GO_GenericName(AbstractName.castOrCopy(value)) : null;
}
/**
Copied: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/GO_LocalName.java (from r1497272, sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/LocalNameAdapter.java)
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/GO_LocalName.java?p2=sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/GO_LocalName.java&p1=sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/LocalNameAdapter.java&r1=1497272&r2=1497293&rev=1497293&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/LocalNameAdapter.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/GO_LocalName.java [UTF-8] Thu Jun 27 10:54:04 2013
@@ -16,82 +16,63 @@
*/
package org.apache.sis.internal.jaxb.gco;
-import java.util.Locale;
-import javax.xml.bind.annotation.adapters.XmlAdapter;
import org.opengis.util.LocalName;
-import org.opengis.util.NameFactory;
-import org.apache.sis.util.CharSequences;
+import org.apache.sis.util.iso.DefaultLocalName;
/**
- * JAXB adapter in order to map implementing class with the GeoAPI interface.
- * See package documentation for more information about JAXB and interface.
+ * JAXB wrapper in order to map implementing class with the GeoAPI interface.
+ * This adapter is used for all the following mutually exclusive properties
+ * (only one can be defined at time):
+ *
+ * <ul>
+ * <li>{@code LocalName}</li>
+ * <li>{@code TypeName}</li>
+ * <li>{@code MemberName}</li>
+ * </ul>
*
* @author Cédric Briançon (Geomatys)
* @author Martin Desruisseaux (Geomatys)
+ * @author Guilhem Legal (Geomatys)
* @since 0.3 (derived from geotk-2.5)
* @version 0.3
* @module
*/
-public final class LocalNameAdapter extends XmlAdapter<String,LocalName> {
- /**
- * The factory to use for creating names, fetched only if needed.
- *
- * {@section Restriction}
- * While this field type is the generic {@code NameFactory} interface in order to defer class
- * loading, {@code LocalNameAdapter} requires an instance of exactly {@link DefaultNameFactory}
- * class (not a subclass). See {@link #getNameFactory()} for more information.
- */
- private transient NameFactory factory;
-
+public final class GO_LocalName extends NameAdapter<GO_LocalName, LocalName> {
/**
* Empty constructor for JAXB only.
*/
- public LocalNameAdapter() {
+ public GO_LocalName() {
}
/**
- * Fetches the name factory. The returned factory shall be an instance
- * of {@code DefaultNameFactory}, not a subclass, because we are going
- * to cast the created {@code GenericName} to {@code AbstractName} for
- * XML marshalling and we know that {@code DefaultNameFactory} creates
- * the expected type. A subclass could create other types, so we are
- * better to avoid them.
+ * Wraps a name at marshalling-time.
*/
- static NameFactory getNameFactory() {
- return org.apache.sis.internal.system.DefaultFactories.NAMES;
+ private GO_LocalName(final DefaultLocalName value) {
+ name = value;
}
/**
- * Does the link between a {@link LocalName} and the string associated.
+ * Does the link between an {@link AbstractName} and the adapter associated.
* JAXB calls automatically this method at marshalling-time.
*
- * @param value The implementing class for this metadata value.
- * @return A string representation of the given local name.
+ * @param value The implementing class for this metadata value.
+ * @return An wrapper which contains the metadata value.
*/
@Override
- public String marshal(final LocalName value) {
- return (value == null) ? null : value.toInternationalString().toString(Locale.ROOT);
+ public GO_LocalName marshal(final LocalName value) {
+ return (value != null) ? new GO_LocalName(DefaultLocalName.castOrCopy(value)) : null;
}
/**
- * Does the link between {@linkplain String strings} and the way they will be unmarshalled.
+ * Does the link between adapters and the way they will be unmarshalled.
* JAXB calls automatically this method at unmarshalling-time.
*
- * @param value The string value to unmarshal.
- * @return The implementing class for the given string.
+ * @param value The wrapper, or {@code null} if none.
+ * @return The implementing class.
*/
@Override
- public LocalName unmarshal(String value) {
- value = CharSequences.trimWhitespaces(value);
- if (value == null || value.isEmpty()) {
- return null;
- }
- NameFactory factory = this.factory;
- if (factory == null) {
- // No need to synchronize. This is not a big deal if the factory is fetched twice.
- this.factory = factory = getNameFactory();
- }
- return factory.createLocalName(null, value);
+ public LocalName unmarshal(final GO_LocalName value) {
+ return (value != null) ? (LocalName) value.name : null;
}
}
Copied: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/NameAdapter.java (from r1497272, sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/GO_GenericName.java)
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/NameAdapter.java?p2=sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/NameAdapter.java&p1=sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/GO_GenericName.java&r1=1497272&r2=1497293&rev=1497293&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/GO_GenericName.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/NameAdapter.java [UTF-8] Thu Jun 27 10:54:04 2013
@@ -16,9 +16,6 @@
*/
package org.apache.sis.internal.jaxb.gco;
-import java.util.ConcurrentModificationException;
-import java.util.List;
-import java.util.Locale;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import org.opengis.util.TypeName;
@@ -26,13 +23,12 @@ import org.opengis.util.LocalName;
import org.opengis.util.ScopedName;
import org.opengis.util.MemberName;
import org.opengis.util.GenericName;
+import org.apache.sis.internal.system.DefaultFactories;
import org.apache.sis.util.iso.AbstractName;
import org.apache.sis.util.iso.DefaultTypeName;
import org.apache.sis.util.iso.DefaultMemberName;
import org.apache.sis.util.resources.Errors;
-import static org.apache.sis.internal.jaxb.gco.LocalNameAdapter.getNameFactory;
-
/**
* JAXB wrapper in order to map implementing class with the GeoAPI interface.
@@ -53,23 +49,18 @@ import static org.apache.sis.internal.ja
* @version 0.3
* @module
*/
-public final class GO_GenericName extends XmlAdapter<GO_GenericName, GenericName> {
+abstract class NameAdapter<ValueType extends NameAdapter<ValueType,BoundType>, BoundType extends GenericName>
+ extends XmlAdapter<ValueType, BoundType>
+{
/**
* The generic name to be marshalled.
*/
- private AbstractName name;
-
- /**
- * Empty constructor for JAXB only.
- */
- public GO_GenericName() {
- }
+ AbstractName name;
/**
- * Wraps a name at marshalling-time.
+ * Empty constructor for subclasses only.
*/
- private GO_GenericName(final AbstractName name) {
- this.name = name;
+ NameAdapter() {
}
/**
@@ -94,7 +85,7 @@ public final class GO_GenericName extend
* @see #getMemberName()
*/
@XmlElement(name = "LocalName")
- public String getLocalName() {
+ public final String getLocalName() {
final AbstractName name = this.name;
return (name instanceof LocalName) && !(name instanceof TypeName)
&& !(name instanceof MemberName) ? name.toString() : null;
@@ -107,16 +98,16 @@ public final class GO_GenericName extend
* @param value The new name.
* @throws IllegalStateException If a name is already defined.
*/
- public void setLocalName(final String value) throws IllegalStateException {
+ public final void setLocalName(final String value) throws IllegalStateException {
ensureUndefined();
if (value == null) {
name = null;
} else {
/*
- * Following cast should be safe because the getNameFactory() method asked specifically
- * for a DefaultNameFactory instance, which is known to create AbstractName instances.
+ * Following cast should be safe because the SIS_NAMES factory is fixed to a
+ * DefaultNameFactory instance, which is known to create AbstractName instances.
*/
- name = (AbstractName) getNameFactory().createLocalName(null, value);
+ name = (AbstractName) DefaultFactories.SIS_NAMES.createLocalName(null, value);
}
}
@@ -127,7 +118,7 @@ public final class GO_GenericName extend
* @return The current name, or {@code null} if none.
*/
@XmlElement(name = "ScopedName")
- public String getScopedName() {
+ public final String getScopedName() {
final AbstractName name = this.name;
return (name instanceof ScopedName) ? name.toString() : null;
}
@@ -139,16 +130,16 @@ public final class GO_GenericName extend
* @param value The new name.
* @throws IllegalStateException If a name is already defined.
*/
- public void setScopedName(final String value) throws IllegalStateException {
+ public final void setScopedName(final String value) throws IllegalStateException {
ensureUndefined();
if (value == null) {
name = null;
} else {
/*
- * Following cast should be safe because the getNameFactory() method asked specifically
- * for a DefaultNameFactory instance, which is known to create AbstractName instances.
+ * Following cast should be safe because the SIS_NAMES factory is fixed to a
+ * DefaultNameFactory instance, which is known to create AbstractName instances.
*/
- name = (AbstractName) getNameFactory().parseGenericName(null, value);
+ name = (AbstractName) DefaultFactories.SIS_NAMES.parseGenericName(null, value);
}
}
@@ -159,7 +150,7 @@ public final class GO_GenericName extend
* @return The current name, or {@code null} if none.
*/
@XmlElement(name = "TypeName")
- public DefaultTypeName getTypeName() {
+ public final DefaultTypeName getTypeName() {
final AbstractName name = this.name;
return (name instanceof DefaultTypeName) ? (DefaultTypeName) name : null;
}
@@ -171,7 +162,7 @@ public final class GO_GenericName extend
* @param value The new name.
* @throws IllegalStateException If a name is already defined.
*/
- public void setTypeName(final DefaultTypeName value) throws IllegalStateException {
+ public final void setTypeName(final DefaultTypeName value) throws IllegalStateException {
ensureUndefined();
name = value;
}
@@ -183,7 +174,7 @@ public final class GO_GenericName extend
* @return The current name, or {@code null} if none.
*/
@XmlElement(name = "MemberName")
- public DefaultMemberName getMemberName() {
+ public final DefaultMemberName getMemberName() {
final AbstractName name = this.name;
return (name instanceof MemberName) ? (DefaultMemberName) name : null;
}
@@ -195,59 +186,8 @@ public final class GO_GenericName extend
* @param value The new name.
* @throws IllegalStateException If a name is already defined.
*/
- public void setMemberName(final DefaultMemberName value) throws IllegalStateException {
+ public final void setMemberName(final DefaultMemberName value) throws IllegalStateException {
ensureUndefined();
name = value;
}
-
- /**
- * Does the link between an {@link AbstractName} and the adapter associated.
- * JAXB calls automatically this method at marshalling-time.
- *
- * @param value The implementing class for this metadata value.
- * @return An wrapper which contains the metadata value.
- */
- @Override
- public GO_GenericName marshal(final GenericName value) {
- if (value == null) {
- return null;
- }
- final AbstractName impl;
- if (value instanceof AbstractName) {
- impl = (AbstractName) value;
- } else {
- /*
- * Recreates a new name for the given name in order to get
- * a SIS implementation from an arbitrary implementation.
- */
- final List<? extends LocalName> parsedNames = value.getParsedNames();
- final CharSequence[] names = new CharSequence[parsedNames.size()];
- int i=0;
- for (final LocalName component : parsedNames) {
- // Asks for the unlocalized name, since we are going to marshal that.
- names[i++] = component.toInternationalString().toString(Locale.ROOT);
- }
- if (i != names.length) {
- throw new ConcurrentModificationException(Errors.format(Errors.Keys.UnexpectedChange_1, "parsedNames"));
- }
- /*
- * Following cast should be safe because the getNameFactory() method asked specifically
- * for a DefaultNameFactory instance, which is known to create AbstractName instances.
- */
- impl = (AbstractName) getNameFactory().createGenericName(value.scope(), names);
- }
- return new GO_GenericName(impl);
- }
-
- /**
- * Does the link between adapters and the way they will be unmarshalled.
- * JAXB calls automatically this method at unmarshalling-time.
- *
- * @param value The wrapper, or {@code null} if none.
- * @return The implementing class.
- */
- @Override
- public GenericName unmarshal(final GO_GenericName value) {
- return (value != null) ? value.name : null;
- }
}
Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/system/DefaultFactories.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/system/DefaultFactories.java?rev=1497293&r1=1497292&r2=1497293&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/system/DefaultFactories.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/system/DefaultFactories.java [UTF-8] Thu Jun 27 10:54:04 2013
@@ -34,9 +34,18 @@ import org.apache.sis.util.iso.DefaultNa
*/
public final class DefaultFactories extends SystemListener {
/**
- * The factory to use for creating names.
+ * A name factory which is guaranteed to be an instance of SIS {@link DefaultNameFactory}.
+ * We use this factory when we need to ensure that the created names are instances of the
+ * SIS {@link org.apache.sis.util.iso.AbstractName} implementation.
*/
- public static final NameFactory NAMES = new DefaultNameFactory();
+ public static final NameFactory SIS_NAMES = new DefaultNameFactory();
+
+ /**
+ * The factory to use for creating names, not necessarily SIS instances.
+ * This is fixed to {@link #SIS_NAMES} for now, but will probably be fetched in a more
+ * dynamic way later.
+ */
+ public static final NameFactory NAMES = SIS_NAMES;
/**
* Cache of factories which are found by {@code META-INF/services}.
Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/iso/AbstractName.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/iso/AbstractName.java?rev=1497293&r1=1497292&r2=1497293&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/iso/AbstractName.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/iso/AbstractName.java [UTF-8] Thu Jun 27 10:54:04 2013
@@ -19,6 +19,7 @@ package org.apache.sis.util.iso;
import java.util.List;
import java.util.Locale;
import java.util.Iterator;
+import java.util.ConcurrentModificationException;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlType;
import org.opengis.util.NameSpace;
@@ -26,6 +27,7 @@ import org.opengis.util.LocalName;
import org.opengis.util.ScopedName;
import org.opengis.util.GenericName;
import org.opengis.util.InternationalString;
+import org.apache.sis.internal.system.DefaultFactories;
import org.apache.sis.util.resources.Errors;
import org.apache.sis.util.Immutable;
@@ -89,6 +91,51 @@ public abstract class AbstractName imple
}
/**
+ * Returns a SIS name implementation with the values of the given arbitrary implementation.
+ * This method performs the first applicable actions in the following choices:
+ *
+ * <ul>
+ * <li>If the given object is {@code null}, then this method returns {@code null}.</li>
+ * <li>Otherwise if the given object is already an instance of {@code AbstractName},
+ * then it is returned unchanged.</li>
+ * <li>Otherwise if the given object is an instance of {@link LocalName}, then this
+ * method delegates to {@link DefaultLocalName#castOrCopy(LocalName)}.</li>
+ * <li>Otherwise a new instance of an {@code AbstractName} subclass is created using the
+ * {@link DefaultNameFactory#createGenericName(NameSpace, CharSequence[])} method.</li>
+ * </ul>
+ *
+ * @param object The object to get as a SIS implementation, or {@code null} if none.
+ * @return A SIS implementation containing the values of the given object (may be the
+ * given object itself), or {@code null} if the argument was null.
+ */
+ public static AbstractName castOrCopy(final GenericName object) {
+ if (object == null || object instanceof AbstractName) {
+ return (AbstractName) object;
+ }
+ if (object instanceof LocalName) {
+ return DefaultLocalName.castOrCopy((LocalName) object);
+ }
+ /*
+ * Recreates a new name for the given name in order to get
+ * a SIS implementation from an arbitrary implementation.
+ */
+ final List<? extends LocalName> parsedNames = object.getParsedNames();
+ final CharSequence[] names = new CharSequence[parsedNames.size()];
+ int i=0;
+ for (final LocalName component : parsedNames) {
+ names[i++] = component.toInternationalString();
+ }
+ if (i != names.length) {
+ throw new ConcurrentModificationException(Errors.format(Errors.Keys.UnexpectedChange_1, "parsedNames"));
+ }
+ /*
+ * Following cast should be safe because the SIS_NAMES factory is fixed to a
+ * DefaultNameFactory instance, which is known to create AbstractName instances.
+ */
+ return (AbstractName) DefaultFactories.SIS_NAMES.createGenericName(object.scope(), names);
+ }
+
+ /**
* Returns the scope (name space) in which this name is local. For example if a
* {@linkplain #toFullyQualifiedName() fully qualified name} is {@code "org.opengis.util.Record"}
* and if this instance is the {@code "util.Record"} part, then its scope is
Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultLocalName.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultLocalName.java?rev=1497293&r1=1497292&r2=1497293&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultLocalName.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultLocalName.java [UTF-8] Thu Jun 27 10:54:04 2013
@@ -25,11 +25,14 @@ import javax.xml.bind.annotation.XmlRoot
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import org.opengis.util.NameSpace;
import org.opengis.util.LocalName;
+import org.opengis.util.TypeName;
+import org.opengis.util.MemberName;
import org.opengis.util.GenericName;
import org.opengis.util.InternationalString;
import org.apache.sis.xml.Namespaces;
import org.apache.sis.util.Immutable;
import org.apache.sis.util.ArgumentChecks;
+import org.apache.sis.internal.system.DefaultFactories;
import org.apache.sis.internal.jaxb.gco.CharSequenceAdapter;
// Related to JDK7
@@ -138,6 +141,45 @@ public class DefaultLocalName extends Ab
}
/**
+ * Returns a SIS local name implementation with the values of the given arbitrary implementation.
+ * This method performs the first applicable actions in the following choices:
+ *
+ * <ul>
+ * <li>If the given object is {@code null}, then this method returns {@code null}.</li>
+ * <li>Otherwise if the given object is already an instance of {@code DefaultLocalName},
+ * then it is returned unchanged.</li>
+ * <li>Otherwise a new {@code DefaultLocalName} instance is created using the
+ * {@link DefaultNameFactory#createLocalName(NameSpace, CharSequence)} method,
+ * or the {@code createTypeName} or {@code createMemberName} variants if the
+ * given object implements the corresponding interface.</li>
+ * </ul>
+ *
+ * @param object The object to get as a SIS implementation, or {@code null} if none.
+ * @return A SIS implementation containing the values of the given object (may be the
+ * given object itself), or {@code null} if the argument was null.
+ */
+ public static DefaultLocalName castOrCopy(final LocalName object) {
+ if (object == null || object instanceof DefaultLocalName) {
+ return (DefaultLocalName) object;
+ }
+ final NameSpace scope = object.scope();
+ final InternationalString name = object.toInternationalString();
+ final LocalName result;
+ if (object instanceof MemberName) {
+ result = DefaultFactories.SIS_NAMES.createMemberName(scope, name, ((MemberName) object).getAttributeType());
+ } else if (object instanceof TypeName) {
+ result = DefaultFactories.SIS_NAMES.createTypeName(scope, name);
+ } else {
+ result = DefaultFactories.SIS_NAMES.createLocalName(scope, name);
+ }
+ /*
+ * Following cast should be safe because the SIS_NAMES factory is fixed to a
+ * DefaultNameFactory instance, which is known to create AbstractName instances.
+ */
+ return (DefaultLocalName) result;
+ }
+
+ /**
* Returns the scope (name space) in which this name is local. This method returns a
* non-null value in all cases, even when the scope given to the constructor was null.
*/