use of org.eclipse.xsd.XSDIdentityConstraintDefinition in project tmdm-studio-se by Talend.
the class NewXSDIndentityConstraintValidatorTest method testIsValid.
@Test
public void testIsValid() {
// $NON-NLS-1$
String name = "Product";
XSDIdentityConstraintCategory type = XSDIdentityConstraintCategory.UNIQUE_LITERAL;
XSDElementDeclaration mockEntity = mock(XSDElementDeclaration.class);
EList<XSDIdentityConstraintDefinition> constraintDefs = new BasicEList<XSDIdentityConstraintDefinition>();
XSDIdentityConstraintDefinition mockConstraintDef = mock(XSDIdentityConstraintDefinition.class);
// $NON-NLS-1$
when(mockConstraintDef.getName()).thenReturn("Product");
when(mockConstraintDef.getContainer()).thenReturn(mockEntity);
when(mockConstraintDef.getIdentityConstraintCategory()).thenReturn(XSDIdentityConstraintCategory.UNIQUE_LITERAL);
constraintDefs.add(mockConstraintDef);
XSDSchema mockSchema = mock(XSDSchema.class);
when(mockSchema.getIdentityConstraintDefinitions()).thenReturn(constraintDefs);
NewXSDIndentityConstraintValidator validator = new NewXSDIndentityConstraintValidator(mockSchema);
// ///
String keyName = null;
String msg = validator.isValid(keyName, type, mockEntity);
assertEquals(Messages.NewXSDIndentityConstraintValidator_KeyNameCannotbeEmpty, msg);
// $NON-NLS-1$
keyName = "";
msg = validator.isValid(keyName, type, mockEntity);
assertEquals(Messages.NewXSDIndentityConstraintValidator_KeyNameCannotbeEmpty, msg);
// $NON-NLS-1$
keyName = " ";
msg = validator.isValid(keyName, type, mockEntity);
assertEquals(Messages.NewXSDIndentityConstraintValidator_KeyNameCannotbeEmpty, msg);
// $NON-NLS-1$ UNIQUE_LITERAL
keyName = "Family";
msg = validator.isValid(keyName, type, mockEntity);
assertEquals(Messages.NewXSDIndentityConstraintValidator_BElemntAlreadyHasUniqueKey, msg);
keyName = name;
when(mockConstraintDef.getIdentityConstraintCategory()).thenReturn(XSDIdentityConstraintCategory.KEY_LITERAL);
msg = validator.isValid(keyName, type, mockEntity);
assertEquals(Messages.bind(Messages.NewXSDIndentityConstraintValidator_KeyInfo, keyName), msg);
// $NON-NLS-1$ not UNIQUE_LITERAL
keyName = "Family";
msg = validator.isValid(keyName, type, mockEntity);
assertNull(msg);
}
use of org.eclipse.xsd.XSDIdentityConstraintDefinition in project webtools.sourceediting by eclipse.
the class XSDVisitor method visitSchema.
public void visitSchema(XSDSchema schema) {
this.schema = schema;
for (Iterator iterator = schema.getAttributeDeclarations().iterator(); iterator.hasNext(); ) {
XSDAttributeDeclaration attr = (XSDAttributeDeclaration) iterator.next();
visitAttributeDeclaration(attr);
}
for (Iterator iterator = schema.getTypeDefinitions().iterator(); iterator.hasNext(); ) {
XSDTypeDefinition type = (XSDTypeDefinition) iterator.next();
visitTypeDefinition(type);
}
for (Iterator iterator = schema.getElementDeclarations().iterator(); iterator.hasNext(); ) {
XSDElementDeclaration element = (XSDElementDeclaration) iterator.next();
visitElementDeclaration(element);
}
for (Iterator iterator = schema.getIdentityConstraintDefinitions().iterator(); iterator.hasNext(); ) {
XSDIdentityConstraintDefinition identityConstraint = (XSDIdentityConstraintDefinition) iterator.next();
visitIdentityConstraintDefinition(identityConstraint);
}
for (Iterator iterator = schema.getModelGroupDefinitions().iterator(); iterator.hasNext(); ) {
XSDModelGroupDefinition modelGroup = (XSDModelGroupDefinition) iterator.next();
visitModelGroupDefinition(modelGroup);
}
for (Iterator iterator = schema.getAttributeGroupDefinitions().iterator(); iterator.hasNext(); ) {
XSDAttributeGroupDefinition attributeGroup = (XSDAttributeGroupDefinition) iterator.next();
visitAttributeGroupDefinition(attributeGroup);
}
for (Iterator iterator = schema.getNotationDeclarations().iterator(); iterator.hasNext(); ) {
XSDNotationDeclaration element = (XSDNotationDeclaration) iterator.next();
visitNotationDeclaration(element);
}
}
use of org.eclipse.xsd.XSDIdentityConstraintDefinition in project webtools.sourceediting by eclipse.
the class XSDVisitor method visitSchema.
public void visitSchema(XSDSchema schema) {
this.schema = schema;
for (Iterator iterator = schema.getAttributeDeclarations().iterator(); iterator.hasNext(); ) {
XSDAttributeDeclaration attr = (XSDAttributeDeclaration) iterator.next();
visitAttributeDeclaration(attr);
}
for (Iterator iterator = schema.getTypeDefinitions().iterator(); iterator.hasNext(); ) {
XSDTypeDefinition type = (XSDTypeDefinition) iterator.next();
visitTypeDefinition(type);
}
for (Iterator iterator = schema.getElementDeclarations().iterator(); iterator.hasNext(); ) {
XSDElementDeclaration element = (XSDElementDeclaration) iterator.next();
visitElementDeclaration(element);
}
for (Iterator iterator = schema.getIdentityConstraintDefinitions().iterator(); iterator.hasNext(); ) {
XSDIdentityConstraintDefinition identityConstraint = (XSDIdentityConstraintDefinition) iterator.next();
visitIdentityConstraintDefinition(identityConstraint);
}
for (Iterator iterator = schema.getModelGroupDefinitions().iterator(); iterator.hasNext(); ) {
XSDModelGroupDefinition modelGroup = (XSDModelGroupDefinition) iterator.next();
visitModelGroupDefinition(modelGroup);
}
for (Iterator iterator = schema.getAttributeGroupDefinitions().iterator(); iterator.hasNext(); ) {
XSDAttributeGroupDefinition attributeGroup = (XSDAttributeGroupDefinition) iterator.next();
visitAttributeGroupDefinition(attributeGroup);
}
for (Iterator iterator = schema.getNotationDeclarations().iterator(); iterator.hasNext(); ) {
XSDNotationDeclaration element = (XSDNotationDeclaration) iterator.next();
visitNotationDeclaration(element);
}
}
use of org.eclipse.xsd.XSDIdentityConstraintDefinition in project webtools.sourceediting by eclipse.
the class XSDVisitor method visitSchema.
public void visitSchema(XSDSchema schema) {
this.schema = schema;
for (Iterator iterator = schema.getAttributeDeclarations().iterator(); iterator.hasNext(); ) {
XSDAttributeDeclaration attr = (XSDAttributeDeclaration) iterator.next();
visitAttributeDeclaration(attr);
}
for (Iterator iterator = schema.getTypeDefinitions().iterator(); iterator.hasNext(); ) {
XSDTypeDefinition type = (XSDTypeDefinition) iterator.next();
visitTypeDefinition(type);
}
for (Iterator iterator = schema.getElementDeclarations().iterator(); iterator.hasNext(); ) {
XSDElementDeclaration element = (XSDElementDeclaration) iterator.next();
visitElementDeclaration(element);
}
for (Iterator iterator = schema.getIdentityConstraintDefinitions().iterator(); iterator.hasNext(); ) {
XSDIdentityConstraintDefinition identityConstraint = (XSDIdentityConstraintDefinition) iterator.next();
visitIdentityConstraintDefinition(identityConstraint);
}
for (Iterator iterator = schema.getModelGroupDefinitions().iterator(); iterator.hasNext(); ) {
XSDModelGroupDefinition modelGroup = (XSDModelGroupDefinition) iterator.next();
visitModelGroupDefinition(modelGroup);
}
for (Iterator iterator = schema.getAttributeGroupDefinitions().iterator(); iterator.hasNext(); ) {
XSDAttributeGroupDefinition attributeGroup = (XSDAttributeGroupDefinition) iterator.next();
visitAttributeGroupDefinition(attributeGroup);
}
for (Iterator iterator = schema.getNotationDeclarations().iterator(); iterator.hasNext(); ) {
XSDNotationDeclaration element = (XSDNotationDeclaration) iterator.next();
visitNotationDeclaration(element);
}
}
use of org.eclipse.xsd.XSDIdentityConstraintDefinition in project tmdm-common by Talend.
the class MetadataRepository method visitElement.
@Override
public void visitElement(XSDElementDeclaration element) {
if (currentTypeStack.isEmpty()) {
// "top level" elements means new MDM entity type
String typeName = element.getName();
if (getComplexType(typeName) != null) {
// Don't process twice type
return;
}
// If entity's type is abstract
boolean isAbstract = false;
XSDTypeDefinition typeDefinition = element.getTypeDefinition();
if (typeDefinition != null && typeDefinition instanceof XSDComplexTypeDefinition) {
isAbstract = ((XSDComplexTypeDefinition) typeDefinition).isAbstract();
}
// Id fields
Map<String, XSDXPathDefinition> idFields = new LinkedHashMap<String, XSDXPathDefinition>();
EList<XSDIdentityConstraintDefinition> constraints = element.getIdentityConstraintDefinitions();
for (XSDIdentityConstraintDefinition constraint : constraints) {
EList<XSDXPathDefinition> fields = constraint.getFields();
for (XSDXPathDefinition field : fields) {
idFields.put(field.getValue(), field);
}
}
XmlSchemaAnnotationProcessorState state;
try {
XSDAnnotation annotation = element.getAnnotation();
state = new XmlSchemaAnnotationProcessorState();
for (XmlSchemaAnnotationProcessor processor : XML_ANNOTATIONS_PROCESSORS) {
processor.process(this, null, annotation, state);
}
} catch (Exception e) {
throw new RuntimeException("Annotation processing exception while parsing info for type '" + typeName + "'.", e);
}
// If write is not allowed for everyone, at least add "administration".
if (state.getAllowWrite().isEmpty()) {
state.getAllowWrite().add(ICoreConstants.ADMIN_PERMISSION);
}
ComplexTypeMetadata type = new ComplexTypeMetadataImpl(targetNamespace, typeName, state.getAllowWrite(), state.getDenyCreate(), state.getHide(), state.getDenyPhysicalDelete(), state.getDenyLogicalDelete(), state.getSchematron(), state.getPrimaryKeyInfo(), state.getLookupFields(), true, isAbstract, state.getWorkflowAccessRights());
// Register parsed localized labels
Map<Locale, String> localeToLabel = state.getLocaleToLabel();
for (Map.Entry<Locale, String> entry : localeToLabel.entrySet()) {
type.registerName(entry.getKey(), entry.getValue());
}
// Register parsed localized descriptions
Map<Locale, String> localeToDescription = state.getLocaleToDescription();
for (Map.Entry<Locale, String> entry : localeToDescription.entrySet()) {
type.registerDescription(entry.getKey(), entry.getValue());
}
// Keep line and column of definition
type.setData(XSD_LINE_NUMBER, XSDParser.getStartLine(element.getElement()));
type.setData(XSD_COLUMN_NUMBER, XSDParser.getStartColumn(element.getElement()));
type.setData(XSD_ELEMENT, element);
type.setData(XSD_DOM_ELEMENT, element.getElement());
addTypeMetadata(type);
// Keep usage information
entityTypeUsage.get(element.getType()).add(type);
// Walk the fields
currentTypeStack.push(type);
{
XmlSchemaWalker.walk(element.getType(), this);
}
currentTypeStack.pop();
// Super types
XSDElementDeclaration substitutionGroup = element.getSubstitutionGroupAffiliation();
if (substitutionGroup != null && !XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(substitutionGroup.getTargetNamespace()) && !Types.ANY_TYPE.equals(substitutionGroup.getName())) {
if (!substitutionGroup.getResolvedElementDeclaration().equals(element)) {
SoftTypeRef superType = new SoftTypeRef(this, substitutionGroup.getTargetNamespace(), substitutionGroup.getName(), true);
type.addSuperType(superType);
}
}
// Register keys (TMDM-4470).
for (Map.Entry<String, XSDXPathDefinition> unresolvedId : idFields.entrySet()) {
SoftIdFieldRef keyField = new SoftIdFieldRef(this, type.getName(), unresolvedId.getKey());
// Keep line and column of definition
keyField.setData(XSD_LINE_NUMBER, XSDParser.getStartLine(unresolvedId.getValue().getElement()));
keyField.setData(XSD_COLUMN_NUMBER, XSDParser.getStartColumn(unresolvedId.getValue().getElement()));
keyField.setData(XSD_DOM_ELEMENT, unresolvedId.getValue().getElement());
type.registerKey(keyField);
}
// entity.
if (type.getKeyFields().isEmpty() && type.getSuperTypes().isEmpty()) {
Map<String, TypeMetadata> userEntityTypes = entityTypes.get(getUserNamespace());
if (userEntityTypes != null) {
userEntityTypes.remove(type.getName());
}
}
} else {
// Non "top level" elements means fields for the MDM entity type being parsed
FieldMetadata fieldMetadata;
int minOccurs = ((XSDParticle) element.getContainer()).getMinOccurs();
int maxOccurs = ((XSDParticle) element.getContainer()).getMaxOccurs();
if (element.isElementDeclarationReference() && currentTypeStack.peek().getName().equals(element.getResolvedElementDeclaration().getName())) {
return;
}
if (element.getResolvedElementDeclaration() != null && element.getResolvedElementDeclaration().getTargetNamespace() == null) {
fieldMetadata = createFieldMetadata(element.getResolvedElementDeclaration(), currentTypeStack.peek(), minOccurs, maxOccurs);
} else {
fieldMetadata = createFieldMetadata(element, currentTypeStack.peek(), minOccurs, maxOccurs);
}
currentTypeStack.peek().addField(fieldMetadata);
}
}
Aggregations