Author: desruisseaux
Date: Mon Dec 10 07:57:37 2012
New Revision: 1419253
URL: http://svn.apache.org/viewvc?rev=1419253&view=rev
Log:
Added JAXB adapters for ISO 19103 GenericName.
Added:
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/GO_GenericName.java
(with props)
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/LocalNameAdapter.java
(with props)
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/ScopedNameAdapter.java
(with props)
Modified:
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/type/package-info.java
Added:
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/GO_GenericName.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/GO_GenericName.java?rev=1419253&view=auto
==============================================================================
---
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/GO_GenericName.java
(added)
+++
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/GO_GenericName.java
Mon Dec 10 07:57:37 2012
@@ -0,0 +1,228 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.internal.jaxb.gco;
+
+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.MemberName;
+import org.opengis.util.GenericName;
+
+import org.apache.sis.util.type.AbstractName;
+import org.apache.sis.util.type.DefaultTypeName;
+import org.apache.sis.util.type.DefaultMemberName;
+import org.apache.sis.util.type.DefaultScopedName;
+import org.apache.sis.util.resources.Errors;
+
+
+/**
+ * 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 ScopedName}</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 GO_GenericName extends XmlAdapter<GO_GenericName,
GenericName> {
+ /**
+ * The generic name to be marshalled.
+ */
+ private AbstractName name;
+
+ /**
+ * Empty constructor for JAXB only.
+ */
+ public GO_GenericName() {
+ }
+
+ /**
+ * 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 Object 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 name The new name.
+ * @throws IllegalStateException If a name is already defined.
+ */
+ public void setLocalName(final String name) throws IllegalStateException {
+ ensureUndefined();
+ if (name == null) {
+ this.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.
+ */
+ this.name = (AbstractName)
LocalNameAdapter.getNameFactory().createLocalName(null, name);
+ }
+ }
+
+ /**
+ * 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 DefaultScopedName getScopedName() {
+ final Object name = this.name;
+ return (name instanceof DefaultScopedName) ? (DefaultScopedName) name
: null;
+ }
+
+ /**
+ * Sets the value for the {@code ScopedName}.
+ * This method is called at unmarshalling-time by JAXB.
+ *
+ * @param name The new name.
+ * @throws IllegalStateException If a name is already defined.
+ */
+ public void setScopedName(final DefaultScopedName name) throws
IllegalStateException {
+ ensureUndefined();
+ this.name = name;
+ }
+
+ /**
+ * 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 Object 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 name The new name.
+ * @throws IllegalStateException If a name is already defined.
+ */
+ public void setTypeName(final DefaultTypeName name) throws
IllegalStateException {
+ ensureUndefined();
+ this.name = name;
+ }
+
+ /**
+ * 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 Object 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 name The new name.
+ * @throws IllegalStateException If a name is already defined.
+ */
+ public void setMemberName(final DefaultMemberName name) throws
IllegalStateException {
+ ensureUndefined();
+ this.name = name;
+ }
+
+ /**
+ * 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 name;
+ if (value instanceof AbstractName) {
+ name = (AbstractName) value;
+ } 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) ScopedNameAdapter.wrap(value,
LocalNameAdapter.getNameFactory());
+ }
+ return new GO_GenericName(name);
+ }
+
+ /**
+ * 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;
+ }
+}
Propchange:
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/GO_GenericName.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/GO_GenericName.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/LocalNameAdapter.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/LocalNameAdapter.java?rev=1419253&view=auto
==============================================================================
---
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/LocalNameAdapter.java
(added)
+++
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/LocalNameAdapter.java
Mon Dec 10 07:57:37 2012
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.internal.jaxb.gco;
+
+import javax.xml.bind.annotation.adapters.XmlAdapter;
+import org.opengis.util.LocalName;
+import org.opengis.util.NameFactory;
+import org.apache.sis.util.CharSequences;
+
+
+/**
+ * JAXB adapter in order to map implementing class with the GeoAPI interface.
+ * See package documentation for more information about JAXB and interface.
+ *
+ * @author Cédric Briançon (Geomatys)
+ * @author Martin Desruisseaux (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;
+
+ /**
+ * Empty constructor for JAXB only.
+ */
+ public LocalNameAdapter() {
+ }
+
+ /**
+ * Fetches the name factory. The returned factory shall be an instance
+ * of {@link 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.
+ */
+ static NameFactory getNameFactory() {
+ throw new UnsupportedOperationException(); // TODO
+ }
+
+ /**
+ * Does the link between a {@link LocalName} and the string 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.
+ */
+ @Override
+ public String marshal(final LocalName value) {
+ return (value == null) ? null :
value.toInternationalString().toString(null);
+ }
+
+ /**
+ * Does the link between {@linkplain String strings} 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.
+ */
+ @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);
+ }
+}
Propchange:
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/LocalNameAdapter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/LocalNameAdapter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/ScopedNameAdapter.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/ScopedNameAdapter.java?rev=1419253&view=auto
==============================================================================
---
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/ScopedNameAdapter.java
(added)
+++
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/ScopedNameAdapter.java
Mon Dec 10 07:57:37 2012
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.internal.jaxb.gco;
+
+import java.util.List;
+import java.util.ConcurrentModificationException;
+import javax.xml.bind.annotation.adapters.XmlAdapter;
+import org.opengis.util.LocalName;
+import org.opengis.util.ScopedName;
+import org.opengis.util.GenericName;
+import org.opengis.util.NameFactory;
+import org.apache.sis.util.resources.Errors;
+import org.apache.sis.util.type.DefaultScopedName;
+
+
+/**
+ * JAXB adapter in order to map implementing class with the GeoAPI interface.
+ * See package documentation for more information about JAXB and interface.
+ *
+ * @author Guilhem Legal (Geomatys)
+ * @author Martin Desruisseaux (Geomatys)
+ * @since 0.3 (derived from geotk-3.00)
+ * @version 0.3
+ * @module
+ */
+public final class ScopedNameAdapter extends
XmlAdapter<DefaultScopedName,ScopedName> {
+ /**
+ * 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 ScopedNameAdapter} requires an instance of exactly
{@link DefaultNameFactory}
+ * class (not a subclass). See {@link LocalNameAdapter#getNameFactory()}
for more information.
+ */
+ private transient NameFactory factory;
+
+ /**
+ * Empty constructor for JAXB only.
+ */
+ public ScopedNameAdapter() {
+ }
+
+ /**
+ * Recreates a new name for the given name, using the given factory.
+ * This is used in order to get a SIS implementation from an arbitrary
implementation.
+ */
+ static GenericName wrap(final GenericName value, final NameFactory
factory) {
+ final List<? extends LocalName> parsedNames = value.getParsedNames();
+ final CharSequence[] names = new CharSequence[parsedNames.size()];
+ int i=0;
+ for (final LocalName name : parsedNames) {
+ // Asks for the unlocalized name, since we are going to marshal
that.
+ names[i++] = name.toInternationalString().toString(null);
+ }
+ if (i != names.length) {
+ throw new
ConcurrentModificationException(Errors.format(Errors.Keys.UnexpectedChange_1,
"parsedNames"));
+ }
+ return factory.createGenericName(value.scope(), names);
+ }
+
+ /**
+ * Does the link between a {@link ScopedName} and the associated string.
+ * JAXB calls automatically this method at marshalling-time.
+ *
+ * @param value The implementing class for this metadata value.
+ * @return A scoped name which represents the metadata value.
+ */
+ @Override
+ public DefaultScopedName marshal(final ScopedName value) {
+ if (value == null) {
+ return null;
+ }
+ if (value instanceof DefaultScopedName) {
+ return (DefaultScopedName) value;
+ }
+ 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 = LocalNameAdapter.getNameFactory();
+ }
+ /*
+ * The following cast should not fail because we asked specifically
for the
+ * DefaultNameFactory instance (which is known to create
DefaultLocalName or
+ * DefaultScopedName instances), and the names array should contains
two or
+ * more elements (otherwise the argument value should have been a
LocalName).
+ */
+ return (DefaultScopedName) wrap(value, factory);
+ }
+
+ /**
+ * Does the link between {@linkplain DefaultScopedName scoped names} and
the way they
+ * will be unmarshalled. JAXB calls automatically this method at
unmarshalling-time.
+ *
+ * @param value The {@linkplain DefaultScopedName scoped name} value.
+ * @return The implementing class for this string.
+ */
+ @Override
+ public ScopedName unmarshal(final DefaultScopedName value) {
+ return value;
+ }
+}
Propchange:
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/ScopedNameAdapter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/ScopedNameAdapter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified:
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/type/package-info.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/type/package-info.java?rev=1419253&r1=1419252&r2=1419253&view=diff
==============================================================================
---
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/type/package-info.java
(original)
+++
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/type/package-info.java
Mon Dec 10 07:57:37 2012
@@ -125,4 +125,23 @@
* @version 0.3
* @module
*/
+@XmlSchema(elementFormDefault = XmlNsForm.QUALIFIED, namespace =
Namespaces.GCO, xmlns = {
+ @XmlNs(prefix = "gco", namespaceURI = Namespaces.GCO)
+})
+@XmlAccessorType(XmlAccessType.NONE)
+@XmlJavaTypeAdapters({
+ @XmlJavaTypeAdapter(GO_GenericName.class),
+ @XmlJavaTypeAdapter(LocalNameAdapter.class),
+ @XmlJavaTypeAdapter(ScopedNameAdapter.class)
+})
package org.apache.sis.util.type;
+
+import javax.xml.bind.annotation.XmlNs;
+import javax.xml.bind.annotation.XmlNsForm;
+import javax.xml.bind.annotation.XmlSchema;
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
+import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters;
+import org.apache.sis.xml.Namespaces;
+import org.apache.sis.internal.jaxb.gco.*;