use of org.eclipse.xsd.XSDAnnotation in project tmdm-studio-se by Talend.
the class UtilMockTest method testGetSimpleTypeDefinitionChildren.
@Test
public void testGetSimpleTypeDefinitionChildren() {
// $NON-NLS-1$
String methodToExpect = "isBuildInType";
// $NON-NLS-1$
String methodToExecute = "getSimpleTypeDefinitionChildren";
XSDFactory factory = XSDFactory.eINSTANCE;
XSDSimpleTypeDefinition simpleTypeDefinition = factory.createXSDSimpleTypeDefinition();
PowerMockito.mockStatic(Util.class);
try {
PowerMockito.when(Util.class, methodToExecute, any(XSDSimpleTypeDefinition.class)).thenCallRealMethod();
//
PowerMockito.when(Util.class, methodToExpect, any(XSDSimpleTypeDefinition.class)).thenReturn(true);
List<Object> children = Util.getSimpleTypeDefinitionChildren(simpleTypeDefinition);
assertNotNull(children);
assertTrue(children.size() == 1);
assertTrue(children.contains(simpleTypeDefinition));
//
PowerMockito.when(Util.class, methodToExpect, any(XSDSimpleTypeDefinition.class)).thenReturn(false);
children = Util.getSimpleTypeDefinitionChildren(simpleTypeDefinition);
assertNotNull(children);
assertTrue(children.size() == 1);
assertTrue(children.contains(simpleTypeDefinition));
//
XSDAnnotation annotation1 = factory.createXSDAnnotation();
XSDAnnotation annotation2 = factory.createXSDAnnotation();
simpleTypeDefinition.getAnnotations().add(annotation1);
simpleTypeDefinition.getAnnotations().add(annotation2);
children = Util.getSimpleTypeDefinitionChildren(simpleTypeDefinition);
assertNotNull(children);
assertTrue(children.size() == 3);
assertTrue(children.contains(simpleTypeDefinition));
assertTrue(children.contains(annotation1));
assertTrue(children.contains(annotation2));
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
use of org.eclipse.xsd.XSDAnnotation in project webtools.sourceediting by eclipse.
the class AnnotationSection method refresh.
/*
* @see org.eclipse.wst.common.ui.properties.internal.provisional.view.ITabbedPropertySection#refresh()
*/
public void refresh() {
super.refresh();
if (simpleText.isFocusControl())
return;
setListenerEnabled(false);
if (input instanceof XSDConcreteComponent) {
XSDAnnotation xsdAnnotation = XSDCommonUIUtils.getInputXSDAnnotation((XSDConcreteComponent) input, false);
setInitialText(xsdAnnotation);
}
setListenerEnabled(true);
}
use of org.eclipse.xsd.XSDAnnotation in project webtools.sourceediting by eclipse.
the class XSDCommonUIUtils method getInputXSDAnnotation.
public static XSDAnnotation getInputXSDAnnotation(XSDConcreteComponent input, boolean createIfNotExist) {
XSDAnnotation xsdAnnotation = null;
XSDFactory factory = XSDFactory.eINSTANCE;
if (input instanceof XSDAttributeDeclaration) {
XSDAttributeDeclaration xsdComp = (XSDAttributeDeclaration) input;
xsdAnnotation = xsdComp.getAnnotation();
if (createIfNotExist && xsdAnnotation == null) {
xsdAnnotation = factory.createXSDAnnotation();
xsdComp.setAnnotation(xsdAnnotation);
}
} else if (input instanceof XSDAttributeGroupDefinition) {
XSDAttributeGroupDefinition xsdComp = (XSDAttributeGroupDefinition) input;
xsdAnnotation = xsdComp.getAnnotation();
if (createIfNotExist && xsdAnnotation == null) {
xsdAnnotation = factory.createXSDAnnotation();
xsdComp.setAnnotation(xsdAnnotation);
}
} else if (input instanceof XSDElementDeclaration) {
XSDElementDeclaration xsdComp = (XSDElementDeclaration) input;
xsdAnnotation = xsdComp.getAnnotation();
if (createIfNotExist && xsdAnnotation == null) {
xsdAnnotation = factory.createXSDAnnotation();
xsdComp.setAnnotation(xsdAnnotation);
}
} else if (input instanceof XSDNotationDeclaration) {
XSDNotationDeclaration xsdComp = (XSDNotationDeclaration) input;
xsdAnnotation = xsdComp.getAnnotation();
if (createIfNotExist && xsdAnnotation == null) {
xsdAnnotation = factory.createXSDAnnotation();
xsdComp.setAnnotation(xsdAnnotation);
}
} else if (input instanceof XSDXPathDefinition) {
XSDXPathDefinition xsdComp = (XSDXPathDefinition) input;
xsdAnnotation = xsdComp.getAnnotation();
if (createIfNotExist && xsdAnnotation == null) {
xsdAnnotation = factory.createXSDAnnotation();
xsdComp.setAnnotation(xsdAnnotation);
}
} else if (input instanceof XSDModelGroup) {
XSDModelGroup xsdComp = (XSDModelGroup) input;
xsdAnnotation = xsdComp.getAnnotation();
if (createIfNotExist && xsdAnnotation == null) {
xsdAnnotation = factory.createXSDAnnotation();
xsdComp.setAnnotation(xsdAnnotation);
}
} else if (input instanceof XSDModelGroupDefinition) {
XSDModelGroupDefinition xsdComp = (XSDModelGroupDefinition) input;
xsdAnnotation = xsdComp.getAnnotation();
if (createIfNotExist && xsdAnnotation == null) {
xsdAnnotation = factory.createXSDAnnotation();
xsdComp.setAnnotation(xsdAnnotation);
}
} else if (input instanceof XSDIdentityConstraintDefinition) {
XSDIdentityConstraintDefinition xsdComp = (XSDIdentityConstraintDefinition) input;
xsdAnnotation = xsdComp.getAnnotation();
if (createIfNotExist && xsdAnnotation == null) {
xsdAnnotation = factory.createXSDAnnotation();
xsdComp.setAnnotation(xsdAnnotation);
}
} else if (input instanceof XSDWildcard) {
XSDWildcard xsdComp = (XSDWildcard) input;
xsdAnnotation = xsdComp.getAnnotation();
if (createIfNotExist && xsdAnnotation == null) {
xsdAnnotation = factory.createXSDAnnotation();
xsdComp.setAnnotation(xsdAnnotation);
}
} else if (input instanceof XSDSchema) {
XSDSchema xsdComp = (XSDSchema) input;
List list = xsdComp.getAnnotations();
if (list.size() > 0) {
xsdAnnotation = (XSDAnnotation) list.get(0);
} else {
if (createIfNotExist && xsdAnnotation == null) {
xsdAnnotation = factory.createXSDAnnotation();
if (xsdComp.getContents() != null) {
xsdComp.getContents().add(0, xsdAnnotation);
}
}
}
return xsdAnnotation;
} else if (input instanceof XSDFacet) {
XSDFacet xsdComp = (XSDFacet) input;
xsdAnnotation = xsdComp.getAnnotation();
if (createIfNotExist && xsdAnnotation == null) {
xsdAnnotation = factory.createXSDAnnotation();
xsdComp.setAnnotation(xsdAnnotation);
}
} else if (input instanceof XSDTypeDefinition) {
XSDTypeDefinition xsdComp = (XSDTypeDefinition) input;
xsdAnnotation = xsdComp.getAnnotation();
if (createIfNotExist && xsdAnnotation == null) {
xsdAnnotation = factory.createXSDAnnotation();
xsdComp.setAnnotation(xsdAnnotation);
}
} else if (input instanceof XSDInclude) {
XSDInclude xsdComp = (XSDInclude) input;
xsdAnnotation = xsdComp.getAnnotation();
if (createIfNotExist && xsdAnnotation == null) {
xsdAnnotation = factory.createXSDAnnotation();
xsdComp.setAnnotation(xsdAnnotation);
}
} else if (input instanceof XSDImport) {
XSDImport xsdComp = (XSDImport) input;
xsdAnnotation = xsdComp.getAnnotation();
if (createIfNotExist && xsdAnnotation == null) {
xsdAnnotation = factory.createXSDAnnotation();
xsdComp.setAnnotation(xsdAnnotation);
}
} else if (input instanceof XSDRedefine) {
XSDRedefine xsdComp = (XSDRedefine) input;
List contents = xsdComp.getContents();
for (int i = 0; i < contents.size(); i++) {
Object content = contents.get(i);
if (content instanceof XSDAnnotation) {
xsdAnnotation = (XSDAnnotation) content;
break;
}
}
if (createIfNotExist && xsdAnnotation == null) {
xsdAnnotation = factory.createXSDAnnotation();
contents.add(0, xsdAnnotation);
}
return xsdAnnotation;
} else if (input instanceof XSDAnnotation) {
xsdAnnotation = (XSDAnnotation) input;
}
if (createIfNotExist) {
formatAnnotation(xsdAnnotation);
}
return xsdAnnotation;
}
use of org.eclipse.xsd.XSDAnnotation in project webtools.sourceediting by eclipse.
the class ExtensibleAddExtensionCommand method addExtensionNode.
protected void addExtensionNode() {
XSDAnnotation xsdAnnotation = XSDCommonUIUtils.getInputXSDAnnotation(component, true);
XSDSchema schema = xsdAnnotation.getSchema();
Element schemaElement = schema.getElement();
if (xsdAnnotation.getApplicationInformation().size() == 0) {
appInfo = xsdAnnotation.createApplicationInformation(null);
xsdAnnotation.getElement().appendChild(appInfo);
List appInfos = xsdAnnotation.getApplicationInformation();
appInfos.add(appInfo);
} else {
// use the first appInfo
appInfo = (Element) xsdAnnotation.getApplicationInformation().get(0);
}
if (appInfo != null) {
Document doc = appInfo.getOwnerDocument();
String prefix = addToNamespaceTable(schemaElement);
newElement = doc.createElementNS(extensionsSchemaSpec.getNamespaceURI(), element.getName());
newElement.setPrefix(prefix);
appInfo.appendChild(newElement);
xsdAnnotation.updateElement();
}
}
use of org.eclipse.xsd.XSDAnnotation in project tmdm-common by Talend.
the class MetadataRepository method createFieldMetadata.
// TODO Refactor!
private FieldMetadata createFieldMetadata(XSDElementDeclaration element, ComplexTypeMetadata containingType, int minOccurs, int maxOccurs) {
String fieldName = element.getName();
if (maxOccurs > 0 && minOccurs > maxOccurs) {
// Eclipse XSD does not check this
throw new IllegalArgumentException("Can not parse information on field '" + element.getQName() + "' of type '" + containingType + "' (maxOccurs > minOccurs)");
}
boolean isMany = maxOccurs == -1 || maxOccurs > 1;
XmlSchemaAnnotationProcessorState state = new XmlSchemaAnnotationProcessorState();
try {
XSDAnnotation annotation = element.getAnnotation();
for (XmlSchemaAnnotationProcessor processor : XML_ANNOTATIONS_PROCESSORS) {
processor.process(this, containingType, annotation, state);
}
} catch (Exception e) {
throw new RuntimeException("Annotation processing exception while parsing info for field '" + fieldName + "' in type '" + containingType.getName() + "'", e);
}
boolean isMandatory = minOccurs > 0;
boolean isContained = false;
boolean isReference = state.isReference();
boolean fkIntegrity = state.isFkIntegrity();
boolean fkIntegrityOverride = state.isFkIntegrityOverride();
List<FieldMetadata> foreignKeyInfo = state.getForeignKeyInfo();
String foreignKeyInfoFormat = state.getForeignKeyInfoFormat();
TypeMetadata fieldType = state.getFieldType();
FieldMetadata referencedField = state.getReferencedField();
TypeMetadata referencedType = state.getReferencedType();
List<String> hideUsers = state.getHide();
List<String> allowWriteUsers = state.getAllowWrite();
List<String> workflowAccessRights = state.getWorkflowAccessRights();
String visibilityRule = state.getVisibilityRule();
XSDTypeDefinition schemaType = element.getType();
if (schemaType instanceof XSDSimpleTypeDefinition) {
XSDSimpleTypeDefinition simpleSchemaType = (XSDSimpleTypeDefinition) schemaType;
XSDSimpleTypeDefinition content = simpleSchemaType.getBaseTypeDefinition();
if (schemaType.getQName() != null) {
fieldType = new SoftTypeRef(this, schemaType.getTargetNamespace(), schemaType.getName(), false);
} else {
// Null QNames may happen for anonymous types extending other types.
fieldType = new SimpleTypeMetadata(targetNamespace, ANONYMOUS_PREFIX + String.valueOf(anonymousCounter++));
if (content != null) {
fieldType.addSuperType(new SoftTypeRef(this, content.getTargetNamespace(), content.getName(), false));
}
}
setFieldData(simpleSchemaType, fieldType);
fieldType.setData(XSD_LINE_NUMBER, XSDParser.getStartLine(element.getElement()));
fieldType.setData(XSD_COLUMN_NUMBER, XSDParser.getStartColumn(element.getElement()));
fieldType.setData(XSD_DOM_ELEMENT, element.getElement());
fieldType.setData(MIN_OCCURS, minOccurs);
fieldType.setData(MAX_OCCURS, maxOccurs);
if (isReference) {
ReferenceFieldMetadata referenceField = new ReferenceFieldMetadata(containingType, false, isMany, isMandatory, fieldName, (ComplexTypeMetadata) referencedType, referencedField, foreignKeyInfo, foreignKeyInfoFormat, fkIntegrity, fkIntegrityOverride, fieldType, allowWriteUsers, hideUsers, workflowAccessRights, state.getForeignKeyFilter(), visibilityRule);
referenceField.setData(XSD_LINE_NUMBER, XSDParser.getStartLine(element.getElement()));
referenceField.setData(XSD_COLUMN_NUMBER, XSDParser.getStartColumn(element.getElement()));
referenceField.setData(XSD_ELEMENT, element);
referenceField.setData(XSD_DOM_ELEMENT, element.getElement());
referenceField.setData(MIN_OCCURS, minOccurs);
referenceField.setData(MAX_OCCURS, maxOccurs);
setLocalizedNames(referenceField, state.getLocaleToLabel());
setLocalizedDescriptions(referenceField, state.getLocaleToDescription());
setDefaultValueRule(referenceField, state.getDefaultValueRule());
setFieldData(simpleSchemaType, referenceField);
return referenceField;
}
if (content != null) {
if (content.getFacets().size() > 0) {
boolean isEnumeration = false;
for (int i = 0; i < content.getFacets().size(); i++) {
XSDConstrainingFacet item = content.getFacets().get(i);
if (item instanceof XSDEnumerationFacet) {
isEnumeration = true;
}
}
if (isEnumeration) {
EnumerationFieldMetadata enumField = new EnumerationFieldMetadata(containingType, false, isMany, isMandatory, fieldName, fieldType, allowWriteUsers, hideUsers, workflowAccessRights, visibilityRule);
enumField.setData(XSD_LINE_NUMBER, XSDParser.getStartLine(element.getElement()));
enumField.setData(XSD_COLUMN_NUMBER, XSDParser.getStartColumn(element.getElement()));
enumField.setData(XSD_ELEMENT, element);
enumField.setData(XSD_DOM_ELEMENT, element.getElement());
enumField.setData(MIN_OCCURS, minOccurs);
enumField.setData(MAX_OCCURS, maxOccurs);
setLocalizedNames(enumField, state.getLocaleToLabel());
setLocalizedDescriptions(enumField, state.getLocaleToDescription());
setDefaultValueRule(enumField, state.getDefaultValueRule());
setFieldData(simpleSchemaType, enumField);
return enumField;
} else {
FieldMetadata field = new SimpleTypeFieldMetadata(containingType, false, isMany, isMandatory, fieldName, fieldType, allowWriteUsers, hideUsers, workflowAccessRights, visibilityRule);
field.setData(XSD_LINE_NUMBER, XSDParser.getStartLine(element.getElement()));
field.setData(XSD_COLUMN_NUMBER, XSDParser.getStartColumn(element.getElement()));
field.setData(XSD_ELEMENT, element);
field.setData(XSD_DOM_ELEMENT, element.getElement());
field.setData(MIN_OCCURS, minOccurs);
field.setData(MAX_OCCURS, maxOccurs);
setLocalizedNames(field, state.getLocaleToLabel());
setLocalizedDescriptions(field, state.getLocaleToDescription());
setDefaultValueRule(field, state.getDefaultValueRule());
setFieldData(simpleSchemaType, field);
return field;
}
} else {
FieldMetadata field = new SimpleTypeFieldMetadata(containingType, false, isMany, isMandatory, fieldName, fieldType, allowWriteUsers, hideUsers, workflowAccessRights, visibilityRule);
field.setData(XSD_LINE_NUMBER, XSDParser.getStartLine(element.getElement()));
field.setData(XSD_COLUMN_NUMBER, XSDParser.getStartColumn(element.getElement()));
field.setData(XSD_ELEMENT, element);
field.setData(XSD_DOM_ELEMENT, element.getElement());
field.setData(MIN_OCCURS, minOccurs);
field.setData(MAX_OCCURS, maxOccurs);
setLocalizedNames(field, state.getLocaleToLabel());
setLocalizedDescriptions(field, state.getLocaleToDescription());
setDefaultValueRule(field, state.getDefaultValueRule());
setFieldData(simpleSchemaType, field);
return field;
}
}
}
if (fieldType == null) {
String qName = element.getType() == null ? null : element.getType().getQName();
if (qName != null) {
TypeMetadata metadata = getType(element.getType().getTargetNamespace(), element.getType().getName());
if (metadata != null) {
fieldType = new SoftTypeRef(this, targetNamespace, schemaType.getName(), false);
isContained = true;
} else {
if (schemaType instanceof XSDComplexTypeDefinition) {
fieldType = new SoftTypeRef(this, schemaType.getTargetNamespace(), schemaType.getName(), false);
isContained = true;
} else {
throw new NotImplementedException("Support for '" + schemaType.getClass() + "'.");
}
}
} else {
// Ref & anonymous complex type
isContained = true;
XSDElementDeclaration refName = element.getResolvedElementDeclaration();
if (schemaType != null) {
fieldType = new ComplexTypeMetadataImpl(targetNamespace, ANONYMOUS_PREFIX + String.valueOf(anonymousCounter++), false);
isContained = true;
} else if (refName != null) {
// Reference being an element, consider references as references to entity type.
fieldType = new SoftTypeRef(this, refName.getTargetNamespace(), refName.getName(), true);
} else {
throw new NotImplementedException();
}
}
}
if (isContained) {
ContainedTypeFieldMetadata containedField = new ContainedTypeFieldMetadata(containingType, isMany, isMandatory, fieldName, (ComplexTypeMetadata) fieldType, isReference, allowWriteUsers, hideUsers, workflowAccessRights, visibilityRule);
containedField.setData(XSD_LINE_NUMBER, XSDParser.getStartLine(element.getElement()));
containedField.setData(XSD_COLUMN_NUMBER, XSDParser.getStartColumn(element.getElement()));
containedField.setData(XSD_ELEMENT, element);
containedField.setData(XSD_DOM_ELEMENT, element.getElement());
containedField.setData(MIN_OCCURS, minOccurs);
containedField.setData(MAX_OCCURS, maxOccurs);
if (fieldType.getName().startsWith(ANONYMOUS_PREFIX)) {
currentTypeStack.push((ComplexTypeMetadata) containedField.getType());
{
XmlSchemaWalker.walk(schemaType, this);
}
currentTypeStack.pop();
}
setLocalizedNames(containedField, state.getLocaleToLabel());
setLocalizedDescriptions(containedField, state.getLocaleToDescription());
return containedField;
} else {
FieldMetadata field = new SimpleTypeFieldMetadata(containingType, false, isMany, isMandatory, fieldName, fieldType, allowWriteUsers, hideUsers, workflowAccessRights, visibilityRule);
field.setData(XSD_LINE_NUMBER, XSDParser.getStartLine(element.getElement()));
field.setData(XSD_COLUMN_NUMBER, XSDParser.getStartColumn(element.getElement()));
field.setData(XSD_ELEMENT, element);
field.setData(XSD_DOM_ELEMENT, element.getElement());
field.setData(MIN_OCCURS, minOccurs);
field.setData(MAX_OCCURS, maxOccurs);
setLocalizedNames(field, state.getLocaleToLabel());
setLocalizedDescriptions(field, state.getLocaleToDescription());
return field;
}
}
Aggregations