Search in sources :

Example 36 with XSDTypeDefinition

use of org.eclipse.xsd.XSDTypeDefinition in project tmdm-studio-se by Talend.

the class UtilMockTest method testUpdateReferenceToXSDTypeDefinition.

@Test
public void testUpdateReferenceToXSDTypeDefinition() {
    // $NON-NLS-1$
    String method_getall = "getAllObject";
    // $NON-NLS-1$
    String method_updateref = "updateReferenceToXSDTypeDefinition";
    PowerMockito.mockStatic(Util.class);
    try {
        PowerMockito.when(Util.class, method_updateref, any(), any(XSDTypeDefinition.class), any(IStructuredContentProvider.class)).thenCallRealMethod();
        Object[] allNodes = {};
        XSDFactory factory = XSDFactory.eINSTANCE;
        XSDTypeDefinition[] newTypes = { factory.createXSDComplexTypeDefinition(), factory.createXSDSimpleTypeDefinition() };
        for (XSDTypeDefinition newType : newTypes) {
            // 
            // 
            XSDElementDeclaration mockElementDecl = mock(XSDElementDeclaration.class);
            when(mockElementDecl.getTypeDefinition()).thenReturn(newType);
            // 
            // 
            XSDParticle xsdParticle1 = mock(XSDParticle.class);
            XSDElementDeclaration mockElementDec2 = mock(XSDElementDeclaration.class);
            when(mockElementDec2.getTypeDefinition()).thenReturn(newType);
            when(xsdParticle1.getTerm()).thenReturn(mockElementDec2);
            // 
            // 
            XSDParticle xsdParticle2 = mock(XSDParticle.class);
            XSDParticle particle_c1 = mock(XSDParticle.class);
            XSDElementDeclaration mockElementDec_c1 = mock(XSDElementDeclaration.class);
            when(mockElementDec_c1.getTypeDefinition()).thenReturn(newType);
            when(particle_c1.getContent()).thenReturn(mockElementDec_c1);
            XSDParticle particle_c2 = mock(XSDParticle.class);
            XSDElementDeclaration mockElementDec_c2 = mock(XSDElementDeclaration.class);
            when(mockElementDec_c2.getTypeDefinition()).thenReturn(newType);
            when(particle_c2.getContent()).thenReturn(mockElementDec_c2);
            XSDParticle particle_c3 = mock(XSDParticle.class);
            XSDElementDeclaration mockElementDec_c3 = mock(XSDElementDeclaration.class);
            when(mockElementDec_c3.getTypeDefinition()).thenReturn(factory.createXSDComplexTypeDefinition());
            when(particle_c3.getContent()).thenReturn(mockElementDec_c3);
            XSDModelGroup xsdModelGroup = mock(XSDModelGroup.class);
            EList<XSDParticle> elist = new BasicEList<XSDParticle>();
            elist.add(particle_c1);
            elist.add(particle_c2);
            elist.add(particle_c3);
            when(xsdModelGroup.getContents()).thenReturn(elist);
            when(xsdParticle2.getTerm()).thenReturn(xsdModelGroup);
            allNodes = new Object[] { mockElementDecl, xsdParticle1, xsdParticle2 };
            // 
            PowerMockito.when(Util.class, method_getall, any(), anyList(), any(IStructuredContentProvider.class)).thenReturn(allNodes);
            Util.updateReferenceToXSDTypeDefinition(new Object(), newType, mock(IStructuredContentProvider.class));
            Mockito.verify(mockElementDecl).setTypeDefinition(newType);
            Mockito.verify(mockElementDec2).setTypeDefinition(newType);
            Mockito.verify(mockElementDec_c1).setTypeDefinition(newType);
            Mockito.verify(mockElementDec_c2).setTypeDefinition(newType);
            if (newType instanceof XSDComplexTypeDefinition) {
                PowerMockito.verifyStatic();
                Util.updateChildrenReferenceToComplexType((XSDComplexTypeDefinition) newType);
            }
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) BasicEList(org.eclipse.emf.common.util.BasicEList) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) IStructuredContentProvider(org.eclipse.jface.viewers.IStructuredContentProvider) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDParticle(org.eclipse.xsd.XSDParticle) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 37 with XSDTypeDefinition

use of org.eclipse.xsd.XSDTypeDefinition in project tmdm-studio-se by Talend.

the class UtilMockTest method testFindOutAllSonElements.

@Test
public void testFindOutAllSonElements() {
    XSDFactory factory = XSDFactory.eINSTANCE;
    Set<XSDConcreteComponent> complexTypes = new HashSet<XSDConcreteComponent>();
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    String[] targetNameSpaces = { "http://www.w3.org/2001/XMLSchema", "", "" };
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    String[] names = { "p1", "p2", "p3" };
    XSDTypeDefinition[] types = { factory.createXSDSimpleTypeDefinition(), factory.createXSDSimpleTypeDefinition(), factory.createXSDSimpleTypeDefinition() };
    XSDElementDeclaration xsdElementDeclaration = factory.createXSDElementDeclaration();
    xsdElementDeclaration.setName(names[0]);
    xsdElementDeclaration.setTargetNamespace(targetNameSpaces[0]);
    XSDComplexTypeDefinition xsdComplexTypeDef = factory.createXSDComplexTypeDefinition();
    xsdElementDeclaration.setAnonymousTypeDefinition(xsdComplexTypeDef);
    XSDParticle typeParticle = factory.createXSDParticle();
    xsdComplexTypeDef.setContent(typeParticle);
    XSDModelGroup xsdModelGroup = factory.createXSDModelGroup();
    for (int i = 0; i < names.length; i++) {
        XSDElementDeclaration xsdEleDec = factory.createXSDElementDeclaration();
        xsdEleDec.setName(names[i]);
        xsdEleDec.setTargetNamespace(targetNameSpaces[i]);
        if (i == 0) {
            xsdEleDec.setTypeDefinition(xsdComplexTypeDef);
        } else {
            xsdEleDec.setTypeDefinition(types[i]);
        }
        XSDParticle xsdParticle = factory.createXSDParticle();
        xsdParticle.setContent(xsdEleDec);
        xsdModelGroup.getContents().add(xsdParticle);
    }
    typeParticle.setContent(xsdModelGroup);
    PowerMockito.mockStatic(Util.class);
    try {
        // $NON-NLS-1$
        String methodToExecute = "findOutAllSonElements";
        PowerMockito.when(Util.class, methodToExecute, any(XSDElementDeclaration.class), anySet()).thenCallRealMethod();
        List<XSDElementDeclaration> allson = Whitebox.invokeMethod(Util.class, methodToExecute, xsdElementDeclaration, complexTypes);
        assertNotNull(allson);
        assertTrue(allson.size() == 2);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) XSDConcreteComponent(org.eclipse.xsd.XSDConcreteComponent) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDParticle(org.eclipse.xsd.XSDParticle) HashSet(java.util.HashSet) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 38 with XSDTypeDefinition

use of org.eclipse.xsd.XSDTypeDefinition in project tmdm-studio-se by Talend.

the class UtilMockTest method testGetComplexTypes.

@Test
public void testGetComplexTypes() {
    XSDFactory factory = XSDFactory.eINSTANCE;
    XSDSchema xSchema = factory.createXSDSchema();
    List<XSDComplexTypeDefinition> complexTypes = Util.getComplexTypes(xSchema);
    assertNotNull(complexTypes);
    assertTrue(complexTypes.isEmpty());
    XSDSimpleTypeDefinition simpleTypeDefinition = factory.createXSDSimpleTypeDefinition();
    simpleTypeDefinition.setBaseTypeDefinition(// $NON-NLS-1$ );
    xSchema.resolveSimpleTypeDefinition(xSchema.getSchemaForSchemaNamespace(), "string"));
    xSchema.getContents().add(simpleTypeDefinition);
    complexTypes = Util.getComplexTypes(xSchema);
    assertNotNull(complexTypes);
    assertTrue(complexTypes.isEmpty());
    XSDComplexTypeDefinition baseComplexTypeDefinition = xSchema.resolveComplexTypeDefinition(xSchema.getSchemaForSchemaNamespace(), // $NON-NLS-1$
    "anyType");
    XSDComplexTypeDefinition complexType1 = factory.createXSDComplexTypeDefinition();
    complexType1.setBaseTypeDefinition(baseComplexTypeDefinition);
    complexType1.setTargetNamespace(null);
    // $NON-NLS-1$
    complexType1.setName("ctype1");
    XSDComplexTypeDefinition complexType2 = factory.createXSDComplexTypeDefinition();
    // $NON-NLS-1$
    complexType2.setTargetNamespace("targetNameSpace");
    // $NON-NLS-1$
    complexType2.setName("ctype2");
    complexType2.setBaseTypeDefinition(baseComplexTypeDefinition);
    XSDComplexTypeDefinition complexType3 = factory.createXSDComplexTypeDefinition();
    complexType3.setTargetNamespace(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001);
    // $NON-NLS-1$
    complexType3.setName("topLevelComplexType");
    complexType3.setBaseTypeDefinition(baseComplexTypeDefinition);
    EList<XSDTypeDefinition> contents = new BasicEList<XSDTypeDefinition>();
    contents.add(complexType1);
    contents.add(complexType2);
    contents.add(complexType3);
    XSDSchema spySchema = spy(xSchema);
    when(spySchema.getTypeDefinitions()).thenReturn(contents);
    complexTypes = Util.getComplexTypes(spySchema);
    assertNotNull(complexTypes);
    assertTrue(complexTypes.size() == 2);
    assertTrue(complexTypes.contains(complexType1));
    assertTrue(complexTypes.contains(complexType2));
    assertFalse(complexTypes.contains(complexType3));
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) XSDSimpleTypeDefinition(org.eclipse.xsd.XSDSimpleTypeDefinition) BasicEList(org.eclipse.emf.common.util.BasicEList) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDSchema(org.eclipse.xsd.XSDSchema) XSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 39 with XSDTypeDefinition

use of org.eclipse.xsd.XSDTypeDefinition in project webtools.sourceediting by eclipse.

the class XSDTypeReferenceEditManager method modifyComponentReference.

public void modifyComponentReference(Object referencingObject, ComponentSpecification component) {
    XSDConcreteComponent concreteComponent = null;
    if (referencingObject instanceof Adapter) {
        Adapter adpater = (Adapter) referencingObject;
        if (adpater.getTarget() instanceof XSDConcreteComponent) {
            concreteComponent = (XSDConcreteComponent) adpater.getTarget();
        }
    } else if (referencingObject instanceof XSDConcreteComponent) {
        concreteComponent = (XSDConcreteComponent) referencingObject;
    }
    if (concreteComponent != null) {
        if (component.isNew()) {
            XSDTypeDefinition td = null;
            if (// This means we set to anonymous type
            component.getName() == null && concreteComponent instanceof XSDElementDeclaration) {
                XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();
                XSDElementDeclaration elementDeclaration = (XSDElementDeclaration) concreteComponent;
                // with the proper undo descriptions
                if (component.getMetaName() == IXSDSearchConstants.COMPLEX_TYPE_META_NAME) {
                    XSDComplexTypeDefinition complexType = factory.createXSDComplexTypeDefinition();
                    elementDeclaration.setAnonymousTypeDefinition(complexType);
                } else {
                    XSDSimpleTypeDefinition simpleType = factory.createXSDSimpleTypeDefinition();
                    simpleType.setBaseTypeDefinition(// $NON-NLS-1$
                    elementDeclaration.getSchema().getSchemaForSchema().resolveSimpleTypeDefinition(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001, "string"));
                    elementDeclaration.setAnonymousTypeDefinition(simpleType);
                }
                // TODO use external literal string
                elementDeclaration.getElement().removeAttribute("type");
            } else if (component.getMetaName() == IXSDSearchConstants.COMPLEX_TYPE_META_NAME) {
                AddXSDComplexTypeDefinitionCommand command = new AddXSDComplexTypeDefinitionCommand(Messages._UI_ACTION_ADD_COMPLEX_TYPE, concreteComponent.getSchema());
                command.setNameToAdd(component.getName());
                command.execute();
                td = command.getCreatedComplexType();
            } else {
                AddXSDSimpleTypeDefinitionCommand command = new AddXSDSimpleTypeDefinitionCommand(Messages._UI_ACTION_ADD_SIMPLE_TYPE, concreteComponent.getSchema());
                command.setNameToAdd(component.getName());
                command.execute();
                td = command.getCreatedSimpleType();
            }
            if (td != null) {
                Command command = new UpdateTypeReferenceCommand(concreteComponent, td);
                command.setLabel(Messages._UI_ACTION_SET_TYPE);
                command.execute();
            }
            XSDDirectivesManager.removeUnusedXSDImports(concreteComponent.getSchema());
        } else {
            Command command = new UpdateTypeReferenceAndManageDirectivesCommand(concreteComponent, component.getName(), component.getQualifier(), component.getFile());
            command.setLabel(Messages._UI_ACTION_SET_TYPE);
            command.execute();
        }
    }
}
Also used : AddXSDComplexTypeDefinitionCommand(org.eclipse.wst.xsd.ui.internal.common.commands.AddXSDComplexTypeDefinitionCommand) UpdateTypeReferenceAndManageDirectivesCommand(org.eclipse.wst.xsd.ui.internal.common.commands.UpdateTypeReferenceAndManageDirectivesCommand) XSDFactory(org.eclipse.xsd.XSDFactory) XSDConcreteComponent(org.eclipse.xsd.XSDConcreteComponent) UpdateTypeReferenceCommand(org.eclipse.wst.xsd.ui.internal.common.commands.UpdateTypeReferenceCommand) AddXSDSimpleTypeDefinitionCommand(org.eclipse.wst.xsd.ui.internal.common.commands.AddXSDSimpleTypeDefinitionCommand) AddXSDComplexTypeDefinitionCommand(org.eclipse.wst.xsd.ui.internal.common.commands.AddXSDComplexTypeDefinitionCommand) UpdateTypeReferenceCommand(org.eclipse.wst.xsd.ui.internal.common.commands.UpdateTypeReferenceCommand) UpdateTypeReferenceAndManageDirectivesCommand(org.eclipse.wst.xsd.ui.internal.common.commands.UpdateTypeReferenceAndManageDirectivesCommand) Command(org.eclipse.gef.commands.Command) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDSimpleTypeDefinition(org.eclipse.xsd.XSDSimpleTypeDefinition) AddXSDSimpleTypeDefinitionCommand(org.eclipse.wst.xsd.ui.internal.common.commands.AddXSDSimpleTypeDefinitionCommand) Adapter(org.eclipse.emf.common.notify.Adapter) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition)

Example 40 with XSDTypeDefinition

use of org.eclipse.xsd.XSDTypeDefinition in project webtools.sourceediting by eclipse.

the class XSDComplexTypeDefinitionAdapter method getText.

public String getText() {
    XSDComplexTypeDefinition xsdComplexTypeDefinition = (XSDComplexTypeDefinition) target;
    StringBuffer result = new StringBuffer();
    // $NON-NLS-1$
    result.append(xsdComplexTypeDefinition.getName() == null ? "local type" : xsdComplexTypeDefinition.getName());
    XSDTypeDefinition baseTypeDefinition = xsdComplexTypeDefinition.getBaseTypeDefinition();
    if (baseTypeDefinition != null && baseTypeDefinition != xsdComplexTypeDefinition.getContent() && baseTypeDefinition.getName() != null && !XSDConstants.isURType(baseTypeDefinition)) {
        try {
            String qName = baseTypeDefinition.getQName(xsdComplexTypeDefinition);
            // $NON-NLS-1$
            result.append(" : ");
            result.append(qName);
        } catch (Exception e) {
            return result.toString();
        }
    }
    return result.toString();
}
Also used : XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition)

Aggregations

XSDTypeDefinition (org.eclipse.xsd.XSDTypeDefinition)119 XSDComplexTypeDefinition (org.eclipse.xsd.XSDComplexTypeDefinition)66 XSDElementDeclaration (org.eclipse.xsd.XSDElementDeclaration)57 XSDSimpleTypeDefinition (org.eclipse.xsd.XSDSimpleTypeDefinition)46 ArrayList (java.util.ArrayList)39 XSDParticle (org.eclipse.xsd.XSDParticle)36 XSDModelGroup (org.eclipse.xsd.XSDModelGroup)32 Iterator (java.util.Iterator)26 XSDSchema (org.eclipse.xsd.XSDSchema)26 List (java.util.List)22 XSDAttributeDeclaration (org.eclipse.xsd.XSDAttributeDeclaration)17 EList (org.eclipse.emf.common.util.EList)16 TreeObject (com.amalto.workbench.models.TreeObject)14 XSDIdentityConstraintDefinition (org.eclipse.xsd.XSDIdentityConstraintDefinition)14 XSDAttributeGroupDefinition (org.eclipse.xsd.XSDAttributeGroupDefinition)13 XSDTerm (org.eclipse.xsd.XSDTerm)13 XSDFactory (org.eclipse.xsd.XSDFactory)12 XSDConcreteComponent (org.eclipse.xsd.XSDConcreteComponent)11 XSDAnnotation (org.eclipse.xsd.XSDAnnotation)10 XSDComplexTypeContent (org.eclipse.xsd.XSDComplexTypeContent)10