From f0b2a16788a4d3f2e788e60eed62e563701bb7d1 Mon Sep 17 00:00:00 2001 From: pfullbright Date: Fri, 2 Dec 2011 21:45:14 +0000 Subject: added XmlSchemaType to XmlValueMapping, and added text content validation to XmlValue --- .../jpt/jaxb/core/context/XmlValueMapping.java | 11 +- .../context/java/AbstractJavaBasicMapping.java | 3 +- .../context/java/GenericJavaXmlValueMapping.java | 186 ++++++++++++++++++--- .../validation/JaxbValidationMessages.java | 1 + 4 files changed, 175 insertions(+), 26 deletions(-) (limited to 'jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt') diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/XmlValueMapping.java b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/XmlValueMapping.java index 0509dc42e5..c04335c848 100644 --- a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/XmlValueMapping.java +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/XmlValueMapping.java @@ -24,5 +24,14 @@ package org.eclipse.jpt.jaxb.core.context; */ public interface XmlValueMapping extends JaxbAttributeMapping, XmlAdaptableMapping, XmlListMapping { - + + // ***** XmlSchemaType ***** + + String XML_SCHEMA_TYPE_PROPERTY = "xmlSchemaType"; //$NON-NLS-1$ + + XmlSchemaType getXmlSchemaType(); + + XmlSchemaType addXmlSchemaType(); + + void removeXmlSchemaType(); } \ No newline at end of file diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/java/AbstractJavaBasicMapping.java b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/java/AbstractJavaBasicMapping.java index 11ef231267..1610730ae4 100644 --- a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/java/AbstractJavaBasicMapping.java +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/java/AbstractJavaBasicMapping.java @@ -157,7 +157,8 @@ public abstract class AbstractJavaBasicMapping implements XmlValueMapping { + protected XmlSchemaType xmlSchemaType; + protected boolean specifiedXmlList; protected boolean defaultXmlList; @@ -37,6 +46,7 @@ public class GenericJavaXmlValueMapping public GenericJavaXmlValueMapping(JaxbPersistentAttribute parent) { super(parent); + initializeXmlSchemaType(); initializeXmlList(); } @@ -56,9 +66,88 @@ public class GenericJavaXmlValueMapping @Override public void synchronizeWithResourceModel() { super.synchronizeWithResourceModel(); + syncXmlSchemaType(); syncXmlList(); } + @Override + public void update() { + super.update(); + updateXmlSchemaType(); + } + + + // ***** XmlSchemaType ***** + + public XmlSchemaType getXmlSchemaType() { + return this.xmlSchemaType; + } + + protected void setXmlSchemaType_(XmlSchemaType xmlSchemaType) { + XmlSchemaType oldXmlSchemaType = this.xmlSchemaType; + this.xmlSchemaType = xmlSchemaType; + this.firePropertyChanged(XML_SCHEMA_TYPE_PROPERTY, oldXmlSchemaType, xmlSchemaType); + } + + public boolean hasXmlSchemaType() { + return this.xmlSchemaType != null; + } + + public XmlSchemaType addXmlSchemaType() { + if (this.xmlSchemaType != null) { + throw new IllegalStateException(); + } + XmlSchemaTypeAnnotation annotation = (XmlSchemaTypeAnnotation) this.getJavaResourceAttribute().addAnnotation(0, JAXB.XML_SCHEMA_TYPE); + + XmlSchemaType xmlJavaTypeAdapter = this.buildXmlSchemaType(annotation); + this.setXmlSchemaType_(xmlJavaTypeAdapter); + return xmlJavaTypeAdapter; + } + + public void removeXmlSchemaType() { + if (this.xmlSchemaType == null) { + throw new IllegalStateException(); + } + this.getJavaResourceAttribute().removeAnnotation(JAXB.XML_SCHEMA_TYPE); + this.setXmlSchemaType_(null); + } + + protected XmlSchemaType buildXmlSchemaType(XmlSchemaTypeAnnotation annotation) { + return new GenericJavaAttributeMappingXmlSchemaType(this, annotation); + } + + protected XmlSchemaTypeAnnotation getXmlSchemaTypeAnnotation() { + return (XmlSchemaTypeAnnotation) this.getJavaResourceAttribute().getAnnotation(0, JAXB.XML_SCHEMA_TYPE); + } + + protected void initializeXmlSchemaType() { + XmlSchemaTypeAnnotation annotation = this.getXmlSchemaTypeAnnotation(); + if (annotation != null) { + this.xmlSchemaType = this.buildXmlSchemaType(annotation); + } + } + + protected void updateXmlSchemaType() { + if (this.xmlSchemaType != null) { + this.xmlSchemaType.update(); + } + } + + protected void syncXmlSchemaType() { + XmlSchemaTypeAnnotation annotation = this.getXmlSchemaTypeAnnotation(); + if (annotation != null) { + if (this.getXmlSchemaType() != null) { + this.getXmlSchemaType().synchronizeWithResourceModel(); + } + else { + this.setXmlSchemaType_(this.buildXmlSchemaType(annotation)); + } + } + else { + this.setXmlSchemaType_(null); + } + } + // ***** XmlList ***** @@ -116,45 +205,94 @@ public class GenericJavaXmlValueMapping } + // ***** misc ***** + + @Override + public XsdTypeDefinition getDataTypeXsdTypeDefinition() { + if (this.xmlSchemaType != null) { + return this.xmlSchemaType.getXsdTypeDefinition(); + } + return super.getDataTypeXsdTypeDefinition(); + } + + + // ***** content assist ***** + + @Override + public Iterable getJavaCompletionProposals(int pos, Filter filter, CompilationUnit astRoot) { + Iterable result = super.getJavaCompletionProposals(pos, filter, astRoot); + if (! CollectionTools.isEmpty(result)) { + return result; + } + + if (this.xmlSchemaType != null) { + result = this.xmlSchemaType.getJavaCompletionProposals(pos, filter, astRoot); + if (! CollectionTools.isEmpty(result)) { + return result; + } + } + + return EmptyIterable.instance(); + } + + // ***** validation ***** @Override public void validate(List messages, IReporter reporter, CompilationUnit astRoot) { super.validate(messages, reporter, astRoot); + if (this.xmlSchemaType != null) { + this.xmlSchemaType.validate(messages, reporter, astRoot); + } + if (isXmlList()) { - if (! getPersistentAttribute().isJavaResourceAttributeCollectionType()) { - messages.add( - DefaultValidationMessages.buildMessage( - IMessage.HIGH_SEVERITY, - JaxbValidationMessages.XML_LIST__ATTRIBUTE_NOT_COLLECTION_TYPE, - this, - getXmlListValidationTextRange(astRoot))); - } - else { - XsdTypeDefinition xsdType = getDataTypeXsdTypeDefinition(); - if (xsdType != null - && (xsdType.getKind() != XsdTypeDefinition.Kind.SIMPLE - || ((XsdSimpleTypeDefinition) xsdType).getXSDComponent().getVariety() == XSDVariety.LIST_LITERAL)) { - - messages.add( - DefaultValidationMessages.buildMessage( - IMessage.HIGH_SEVERITY, - JaxbValidationMessages.XML_LIST__ITEM_TYPE_NOT_MAPPED_TO_VALID_SCHEMA_TYPE, - new String[] { getValueTypeName() }, - this, - getValidationTextRange(astRoot))); - } - } + validateXmlList(messages, reporter, astRoot); } validateSchemaType(messages, reporter, astRoot); } - + protected void validateXmlList(List messages, IReporter reporter, CompilationUnit astRoot) { + if (! getPersistentAttribute().isJavaResourceAttributeCollectionType()) { + messages.add( + DefaultValidationMessages.buildMessage( + IMessage.HIGH_SEVERITY, + JaxbValidationMessages.XML_LIST__ATTRIBUTE_NOT_COLLECTION_TYPE, + this, + getXmlListValidationTextRange(astRoot))); + } + else { + XsdTypeDefinition xsdType = getDataTypeXsdTypeDefinition(); + if (xsdType != null + && (xsdType.getKind() != XsdTypeDefinition.Kind.SIMPLE + || ((XsdSimpleTypeDefinition) xsdType).getXSDComponent().getVariety() == XSDVariety.LIST_LITERAL)) { + + messages.add( + DefaultValidationMessages.buildMessage( + IMessage.HIGH_SEVERITY, + JaxbValidationMessages.XML_LIST__ITEM_TYPE_NOT_MAPPED_TO_VALID_SCHEMA_TYPE, + new String[] { getValueTypeName() }, + this, + getValidationTextRange(astRoot))); + } + } + } protected void validateSchemaType(List messages, IReporter reporter, CompilationUnit astRoot) { XsdTypeDefinition xsdClassType = getClassMapping().getXsdTypeDefinition(); + + if (! (xsdClassType.getKind() == XsdTypeDefinition.Kind.SIMPLE + || ((XsdComplexTypeDefinition) xsdClassType).getXSDComponent().getContentTypeCategory() == XSDContentTypeCategory.SIMPLE_LITERAL)) { + messages.add( + DefaultValidationMessages.buildMessage( + IMessage.HIGH_SEVERITY, + JaxbValidationMessages.XML_VALUE__NO_TEXT_CONTENT, + this, + getValidationTextRange(astRoot))); + return; + } + XsdTypeDefinition xsdType = (xsdClassType == null) ? null : xsdClassType.getBaseType(); XsdTypeDefinition expectedSchemaType = getDataTypeXsdTypeDefinition(); 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 73c2569c9a..4f687a96d1 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 @@ -102,6 +102,7 @@ public interface JaxbValidationMessages { String XML_REGISTRY__DUPLICATE_XML_ELEMENT_QNAME = "XML_REGISTRY__DUPLICATE_XML_ELEMENT_QNAME"; String XML_VALUE__MULTIPLE_MAPPINGS_DEFINED = "XML_VALUE__MULTIPLE_MAPPINGS_DEFINED"; + String XML_VALUE__NO_TEXT_CONTENT = "XML_VALUE__NO_TEXT_CONTENT"; String XML_VALUE__INVALID_SCHEMA_TYPE = "XML_VALUE__INVALID_SCHEMA_TYPE"; String XML_VALUE_MAPPING_WITH_NON_XML_ATTRIBUTE_MAPPING_DEFINED = "XML_VALUE_MAPPING_WITH_NON_XML_ATTRIBUTE_MAPPING_DEFINED"; -- cgit v1.2.3