From e0b6a624b02096217d08785eb6ca562f5af01fb2 Mon Sep 17 00:00:00 2001 From: pfullbright Date: Tue, 11 Jan 2011 22:50:47 +0000 Subject: added validation for XmlType --- .../eclipse/jpt/jaxb/core/context/JaxbPackage.java | 11 ++ .../jpt/jaxb/core/context/JaxbPersistentType.java | 30 ++++-- .../eclipse/jpt/jaxb/core/context/JaxbType.java | 26 ++++- .../core/internal/context/GenericContextRoot.java | 5 +- .../jaxb/core/internal/context/GenericPackage.java | 8 ++ .../context/java/AbstractJavaPersistentType.java | 115 +++++++++++++++++---- .../validation/JaxbValidationMessages.java | 4 + 7 files changed, 165 insertions(+), 34 deletions(-) (limited to 'jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb') diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/JaxbPackage.java b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/JaxbPackage.java index a11a56f87e..e2cb4d80f1 100644 --- a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/JaxbPackage.java +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/JaxbPackage.java @@ -10,6 +10,7 @@ package org.eclipse.jpt.jaxb.core.context; import java.util.List; +import org.eclipse.jpt.jaxb.core.xsd.XsdSchema; import org.eclipse.wst.validation.internal.provisional.core.IMessage; import org.eclipse.wst.validation.internal.provisional.core.IReporter; @@ -40,6 +41,11 @@ public interface JaxbPackage JaxbPackageInfo getPackageInfo(); public final static String PACKAGE_INFO_PROPERTY = "package-info"; //$NON-NLS-1$ + /** + * Return the namespace associated with this package, default or specified + */ + String getNamespace(); + /** * Return whether this package has no useful information. * Useful information includes: @@ -49,6 +55,11 @@ public interface JaxbPackage */ boolean isEmpty(); + /** + * Return the {@link XsdSchema} associated with this package, if there is one, null otherwise + */ + XsdSchema getXsdSchema(); + // **************** validation ******************************************** diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/JaxbPersistentType.java b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/JaxbPersistentType.java index 4b6d58d6e4..277a23f81e 100644 --- a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/JaxbPersistentType.java +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/JaxbPersistentType.java @@ -50,13 +50,31 @@ public interface JaxbPersistentType /**************** name *****************/ /** - * Schema type name corresponds to the XmlType annotation name element + * Return the specified xml type name or the default type name if it is not specified */ - String getSchemaTypeName(); - void setSchemaTypeName(String schemaTypeName); - String SCHEMA_TYPE_NAME_PROPERTY = "schemaTypeName"; //$NON-NLS-1$ - - + String getXmlTypeName(); + + /** + * Return the specified xml type name or null if it is not specified + */ + String getSpecifiedXmlTypeName(); + + /** + * Set the xml type name, null to unspecify (use the default) + */ + void setSpecifiedXmlTypeName(String xmlTypeName); + + /** + * String constant associated with changes to the specified xml type name + */ + String SPECIFIED_XML_TYPE_NAME_PROPERTY = "specifiedXmlTypeName"; //$NON-NLS-1$ + + /** + * Return the default xml type name + */ + String getDefaultXmlTypeName(); + + /**************** namespace *****************/ /** diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/JaxbType.java b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/JaxbType.java index 2271bb04c4..603e87969e 100644 --- a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/JaxbType.java +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/JaxbType.java @@ -9,7 +9,10 @@ *******************************************************************************/ package org.eclipse.jpt.jaxb.core.context; +import java.util.List; import org.eclipse.jpt.jaxb.core.resource.java.AbstractJavaResourceType; +import org.eclipse.wst.validation.internal.provisional.core.IMessage; +import org.eclipse.wst.validation.internal.provisional.core.IReporter; /** * Represents a java class (or enum or interface) with JAXB metadata (specified or implied). @@ -42,11 +45,6 @@ public interface JaxbType */ String getFullyQualifiedName(); - /** - * Return the name of the type's package. Empty string if none. - */ - String getPackageName(); - /** * Returns the type-qualified name of this type, including qualification for any * enclosing types, but not including package qualification. @@ -58,6 +56,24 @@ public interface JaxbType */ String getSimpleName(); + /** + * Return the name of the type's package. Empty string if none. + */ + String getPackageName(); + + /** + * Return the {@link JaxbPackage} associated with this type + */ + JaxbPackage getJaxbPackage(); + + + // **************** validation ******************************************** + + /** + * Add to the list of current validation messages + */ + void validate(List messages, IReporter reporter); + /** * The kind of metadata specified on the java type. diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/GenericContextRoot.java b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/GenericContextRoot.java index 8fbfe071cf..b05bddcec5 100644 --- a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/GenericContextRoot.java +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/GenericContextRoot.java @@ -24,8 +24,8 @@ import org.eclipse.jpt.jaxb.core.context.JaxbPersistentEnum; import org.eclipse.jpt.jaxb.core.context.JaxbRegistry; import org.eclipse.jpt.jaxb.core.context.JaxbType; import org.eclipse.jpt.jaxb.core.context.JaxbType.Kind; -import org.eclipse.jpt.jaxb.core.resource.java.JAXB; import org.eclipse.jpt.jaxb.core.resource.java.AbstractJavaResourceType; +import org.eclipse.jpt.jaxb.core.resource.java.JAXB; import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceEnum; import org.eclipse.jpt.jaxb.core.resource.java.JavaResourcePackage; import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceType; @@ -505,5 +505,8 @@ public class GenericContextRoot for (JaxbPackage pkg : this.packages.values()) { pkg.validate(messages, reporter); } + for (JaxbType type : this.types.values()) { + type.validate(messages, reporter); + } } } diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/GenericPackage.java b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/GenericPackage.java index 6b3312af0d..1ac1d13623 100644 --- a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/GenericPackage.java +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/GenericPackage.java @@ -16,8 +16,11 @@ import org.eclipse.jpt.jaxb.core.context.JaxbPackageInfo; import org.eclipse.jpt.jaxb.core.internal.validation.DefaultValidationMessages; import org.eclipse.jpt.jaxb.core.internal.validation.JaxbValidationMessages; import org.eclipse.jpt.jaxb.core.resource.java.JavaResourcePackage; +import org.eclipse.jpt.jaxb.core.xsd.XsdSchema; +import org.eclipse.jpt.jaxb.core.xsd.XsdUtil; import org.eclipse.wst.validation.internal.provisional.core.IMessage; import org.eclipse.wst.validation.internal.provisional.core.IReporter; +import org.eclipse.xsd.XSDSchema; public class GenericPackage extends AbstractJaxbContextNode @@ -97,6 +100,11 @@ public class GenericPackage return (getPackageInfo() == null) ? "" : getPackageInfo().getXmlSchema().getNamespace(); } + public XsdSchema getXsdSchema() { + XSDSchema emfSchema = getJaxbProject().getSchemaLibrary().getSchema(getNamespace()); + return (emfSchema == null) ? null : (XsdSchema) XsdUtil.getAdapter(emfSchema); + } + // **************** validation ******************************************** diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/java/AbstractJavaPersistentType.java b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/java/AbstractJavaPersistentType.java index 900867d5b7..bf98058332 100644 --- a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/java/AbstractJavaPersistentType.java +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/java/AbstractJavaPersistentType.java @@ -9,15 +9,26 @@ ******************************************************************************/ package org.eclipse.jpt.jaxb.core.internal.context.java; +import java.beans.Introspector; +import java.util.List; +import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jpt.core.utility.TextRange; import org.eclipse.jpt.jaxb.core.context.JaxbContextRoot; import org.eclipse.jpt.jaxb.core.context.JaxbPackage; import org.eclipse.jpt.jaxb.core.context.JaxbPackageInfo; import org.eclipse.jpt.jaxb.core.context.JaxbPersistentType; import org.eclipse.jpt.jaxb.core.context.XmlRootElement; +import org.eclipse.jpt.jaxb.core.internal.validation.DefaultValidationMessages; +import org.eclipse.jpt.jaxb.core.internal.validation.JaxbValidationMessages; import org.eclipse.jpt.jaxb.core.resource.java.AbstractJavaResourceType; import org.eclipse.jpt.jaxb.core.resource.java.XmlRootElementAnnotation; import org.eclipse.jpt.jaxb.core.resource.java.XmlTypeAnnotation; +import org.eclipse.jpt.jaxb.core.xsd.XsdSchema; +import org.eclipse.jpt.jaxb.core.xsd.XsdTypeDefinition; +import org.eclipse.jpt.utility.internal.StringTools; import org.eclipse.jpt.utility.internal.iterables.ListIterable; +import org.eclipse.wst.validation.internal.provisional.core.IMessage; +import org.eclipse.wst.validation.internal.provisional.core.IReporter; public abstract class AbstractJavaPersistentType extends AbstractJavaType @@ -25,7 +36,7 @@ public abstract class AbstractJavaPersistentType protected String factoryClass; protected String factoryMethod; - protected String schemaTypeName; + protected String specifiedXmlTypeName; protected String namespace; protected final PropOrderContainer propOrderContainer; @@ -35,7 +46,7 @@ public abstract class AbstractJavaPersistentType super(parent, resourceType); this.factoryClass = this.getResourceFactoryClass(); this.factoryMethod = this.getResourceFactoryMethod(); - this.schemaTypeName = this.getResourceSchemaTypeName(); + this.specifiedXmlTypeName = this.getResourceXmlTypeName(); this.namespace = this.getResourceNamespace(); this.propOrderContainer = new PropOrderContainer(); this.rootElement = this.buildRootElement(); @@ -54,10 +65,10 @@ public abstract class AbstractJavaPersistentType // ********** synchronize/update ********** public void synchronizeWithResourceModel() { - this.setFactoryClass_(this.getResourceFactoryClass()); - this.setFactoryMethod_(this.getResourceFactoryMethod()); - this.setSchemaTypeName_(this.getResourceSchemaTypeName()); - this.setNamespace_(this.getResourceNamespace()); + this.setFactoryClass_(getResourceFactoryClass()); + this.setFactoryMethod_(getResourceFactoryMethod()); + this.setSpecifiedXmlTypeName_(getResourceXmlTypeName()); + this.setNamespace_(getResourceNamespace()); this.syncPropOrder(); this.syncRootElement(); } @@ -116,26 +127,35 @@ public abstract class AbstractJavaPersistentType } // ********** name ********** - - public String getSchemaTypeName() { - return this.schemaTypeName; + + public String getXmlTypeName() { + return (this.specifiedXmlTypeName != null) ? this.specifiedXmlTypeName : getDefaultXmlTypeName(); } - - public void setSchemaTypeName(String schemaTypeName) { - this.getXmlTypeAnnotation().setName(schemaTypeName); - this.setSchemaTypeName_(schemaTypeName); + + public String getSpecifiedXmlTypeName() { + return this.specifiedXmlTypeName; } - - protected void setSchemaTypeName_(String schemaTypeName) { - String old = this.schemaTypeName; - this.schemaTypeName = schemaTypeName; - this.firePropertyChanged(SCHEMA_TYPE_NAME_PROPERTY, old, schemaTypeName); + + public void setSpecifiedXmlTypeName(String xmlTypeName) { + this.getXmlTypeAnnotation().setName(xmlTypeName); + this.setSpecifiedXmlTypeName_(xmlTypeName); } - - protected String getResourceSchemaTypeName() { + + protected void setSpecifiedXmlTypeName_(String xmlTypeName) { + String old = this.specifiedXmlTypeName; + this.specifiedXmlTypeName = xmlTypeName; + this.firePropertyChanged(SPECIFIED_XML_TYPE_NAME_PROPERTY, old, xmlTypeName); + } + + public String getDefaultXmlTypeName() { + return Introspector.decapitalize(getSimpleName()); + } + + protected String getResourceXmlTypeName() { return this.getXmlTypeAnnotation().getName(); } - + + // ********** namespace ********** public String getNamespace() { @@ -251,7 +271,58 @@ public abstract class AbstractJavaPersistentType protected XmlRootElementAnnotation getRootElementAnnotation() { return (XmlRootElementAnnotation) this.getJavaResourceType().getAnnotation(XmlRootElementAnnotation.ANNOTATION_NAME); } - + + + // **************** validation ******************************************** + + @Override + public TextRange getValidationTextRange(CompilationUnit astRoot) { + TextRange textRange = getXmlTypeAnnotation().getTextRange(astRoot); + return (textRange != null) ? textRange : super.getValidationTextRange(astRoot); + } + + @Override + public void validate(List messages, IReporter reporter, CompilationUnit astRoot) { + super.validate(messages, reporter, astRoot); + + // 1. if name is absent (""), namespace cannot be different from package namespace + // 2. if name is not absent (""), type must be from schema associated with this package + + String name = getXmlTypeName(); + String namespace = getNamespaceForValidation(); + if ("".equals(name)) { + if (! StringTools.stringsAreEqual(namespace, getJaxbPackage().getNamespace())) { + messages.add( + DefaultValidationMessages.buildMessage( + IMessage.HIGH_SEVERITY, + JaxbValidationMessages.XML_TYPE_UNMATCHING_NAMESPACE_FOR_ANONYMOUS_TYPE, + this, + getXmlTypeAnnotation().getNamespaceTextRange(astRoot))); + } + } + else { + XsdSchema schema = getJaxbPackage().getXsdSchema(); + + if (schema != null) { + XsdTypeDefinition schemaType = schema.getTypeDefinition(namespace, name); + if (schemaType == null) { + messages.add( + DefaultValidationMessages.buildMessage( + IMessage.HIGH_SEVERITY, + JaxbValidationMessages.XML_TYPE_UNRESOLVED_SCHEMA_TYPE, + new String[] {name, namespace}, + this, + getXmlTypeAnnotation().getTextRange(astRoot))); + } + } + } + } + + protected String getNamespaceForValidation() { + return (this.namespace != null) ? this.namespace : getJaxbPackage().getNamespace(); + } + + @Override public void toString(StringBuilder sb) { super.toString(sb); diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/validation/JaxbValidationMessages.java b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/validation/JaxbValidationMessages.java index 975ec152bc..8f55ac09dc 100644 --- a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/validation/JaxbValidationMessages.java +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/validation/JaxbValidationMessages.java @@ -20,4 +20,8 @@ public interface JaxbValidationMessages { // validation on package public static final String PACKAGE_NO_SCHEMA_FOR_NAMESPACE = "PACKAGE_NO_SCHEMA_FOR_NAMESPACE"; + + // validation on type + public static final String XML_TYPE_UNMATCHING_NAMESPACE_FOR_ANONYMOUS_TYPE = "XML_TYPE_UNMATCHING_NAMESPACE_FOR_ANONYMOUS_TYPE"; + public static final String XML_TYPE_UNRESOLVED_SCHEMA_TYPE = "XML_TYPE_UNRESOLVED_SCHEMA_TYPE"; } -- cgit v1.2.3