Search in sources :

Example 11 with XSDSimpleTypeDefinition

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

the class XSDAddComplexTypeElementAction method openDialog.

private int openDialog() {
    XSDSimpleTypeDefinition simpleTypeDefinition = schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(), defaultTypeName);
    List<XSDComplexTypeDefinition> types = Util.getComplexTypes(schema);
    dialogR = new ComplexTypeInputDialogR(page.getSite().getShell(), Messages._AddCType, modelGroup, schema, types, simpleTypeDefinition, false, false);
    dialogR.setBlockOnOpen(true);
    int ret = dialogR.open();
    return ret;
}
Also used : ComplexTypeInputDialogR(com.amalto.workbench.dialogs.ComplexTypeInputDialogR) XSDSimpleTypeDefinition(org.eclipse.xsd.XSDSimpleTypeDefinition) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition)

Example 12 with XSDSimpleTypeDefinition

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

the class XSDAddComplexTypeElementAction method transformToComplexType.

private boolean transformToComplexType(XSDParticle particle) {
    XSDElementDeclaration decl = (XSDElementDeclaration) particle.getContent();
    List<XSDComplexTypeDefinition> types = Util.getComplexTypes(schema);
    XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();
    // $NON-NLS-1$
    boolean anonymous = (typeName == null) || ("".equals(typeName));
    boolean alreadyExists = false;
    XSDComplexTypeDefinition complexType = null;
    // the sub element created if needed
    XSDParticle subParticle = null;
    XSDParticle groupParticle = null;
    XSDElementDeclaration subElement = null;
    XSDElementDeclaration parent = null;
    Object pObject = Util.getParent(decl);
    if (pObject instanceof XSDElementDeclaration) {
        parent = (XSDElementDeclaration) pObject;
    }
    if (!anonymous) {
        if (typeName.lastIndexOf(" : ") != -1) {
            // $NON-NLS-1$
            // $NON-NLS-1$
            typeName = typeName.substring(0, typeName.lastIndexOf(" : "));
        }
        for (XSDComplexTypeDefinition td : types) {
            if ((td.getName().equals(typeName))) {
                alreadyExists = true;
                complexType = td;
                break;
            }
        }
    } else {
        if (decl.getTypeDefinition() instanceof XSDSimpleTypeDefinition) {
            alreadyExists = false;
        }
    }
    if (alreadyExists) {
        XSDParticle partCnt = (XSDParticle) complexType.getContentType();
        partCnt.unsetMaxOccurs();
        partCnt.unsetMinOccurs();
        XSDTypeDefinition superType = null;
        for (XSDTypeDefinition type : types) {
            if (type.getName().equals(superTypeName)) {
                superType = type;
                break;
            }
        }
        if (superType != null) {
            XSDModelGroup mdlGrp = (XSDModelGroup) partCnt.getTerm();
            boolean status = updateCompositorType(superType, mdlGrp);
            if (!status) {
                return false;
            }
            complexType.setDerivationMethod(XSDDerivationMethod.EXTENSION_LITERAL);
            complexType.setBaseTypeDefinition(superType);
        }
        if (isAbstract) {
            complexType.setAbstract(isAbstract);
        } else {
            complexType.unsetAbstract();
        }
        if (parent != null) {
            parent.updateElement();
        }
        if (complexType != null) {
            complexType.updateElement();
        }
    } else {
        // Create if does not exist
        // add an element declaration
        subElement = factory.createXSDElementDeclaration();
        // $NON-NLS-1$
        subElement.setName("subelement");
        subElement.setTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(), defaultTypeName));
        subParticle = factory.createXSDParticle();
        subParticle.unsetMaxOccurs();
        subParticle.unsetMinOccurs();
        subParticle.setContent(subElement);
        subParticle.updateElement();
        // create group
        XSDModelGroup group = factory.createXSDModelGroup();
        if (isChoice) {
            group.setCompositor(XSDCompositor.CHOICE_LITERAL);
        } else if (isAll) {
            group.setCompositor(XSDCompositor.ALL_LITERAL);
        } else {
            group.setCompositor(XSDCompositor.SEQUENCE_LITERAL);
        }
        group.getContents().add(0, subParticle);
        group.updateElement();
        // create the complex type
        complexType = factory.createXSDComplexTypeDefinition();
        // complexType.setDerivationMethod(XSDDerivationMethod.EXTENSION_LITERAL);
        if (!anonymous) {
            // if (true) {
            XSDTypeDefinition superType = null;
            for (XSDTypeDefinition type : types) {
                if (type.getName().equals(superTypeName)) {
                    superType = type;
                    break;
                }
            }
            complexType.setName(typeName);
            if (superType != null) {
                boolean status = updateCompositorType(superType, group);
                if (!status) {
                    return false;
                }
                complexType.setDerivationMethod(XSDDerivationMethod.EXTENSION_LITERAL);
                complexType.setBaseTypeDefinition(superType);
            }
            if (isAbstract) {
                complexType.setAbstract(isAbstract);
            } else {
                complexType.unsetAbstract();
            }
            schema.getContents().add(complexType);
        }
        complexType.updateElement();
        // add the group
        groupParticle = factory.createXSDParticle();
        groupParticle.unsetMaxOccurs();
        groupParticle.unsetMinOccurs();
        groupParticle.setContent(group);
        groupParticle.updateElement();
        complexType.setContent(groupParticle);
        complexType.updateElement();
    }
    // set complex type to concept
    if (anonymous) {
        decl.setAnonymousTypeDefinition(complexType);
    } else {
        decl.setTypeDefinition(complexType);
    }
    decl.updateElement();
    schema.update();
    return true;
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDSimpleTypeDefinition(org.eclipse.xsd.XSDSimpleTypeDefinition) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDParticle(org.eclipse.xsd.XSDParticle) XSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition)

Example 13 with XSDSimpleTypeDefinition

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

the class XSDChangeBaseTypeAction method widgetSelected.

public void widgetSelected(SelectionEvent e) {
    if (dialog.getReturnCode() == -1) {
        return;
    }
    typeName = dialog.getTypeName();
    builtIn = dialog.isBuiltIn();
    // if built in, check that the type actually exists
    if (builtIn) {
        boolean found = false;
        for (Object element : schema.getSchemaForSchema().getTypeDefinitions()) {
            XSDTypeDefinition type = (XSDTypeDefinition) element;
            if (type instanceof XSDSimpleTypeDefinition) {
                if (type.getName().equals(typeName)) {
                    found = true;
                    break;
                }
            }
        }
        if (!found) {
            MessageDialog.openError(page.getSite().getShell(), Messages._Error, Messages.bind(Messages.XSDChangeBaseTypeAction_ErrorMsg2, typeName));
            return;
        }
    }
    dialog.close();
}
Also used : XSDSimpleTypeDefinition(org.eclipse.xsd.XSDSimpleTypeDefinition) XSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition)

Example 14 with XSDSimpleTypeDefinition

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

the class XSDChangeBaseTypeAction method doAction.

@Override
public IStatus doAction() {
    try {
        IStructuredSelection selection = (IStructuredSelection) page.getTreeViewer().getSelection();
        XSDSimpleTypeDefinition typedef = (XSDSimpleTypeDefinition) selection.getFirstElement();
        // Cannot change the simple type definition of built in type
        // if (schema.getSchemaForSchemaNamespace().equals(typedef.getTargetNamespace())) return
        // Status.CANCEL_STATUS;
        // build list of custom types and built in types
        ArrayList customTypes = new ArrayList();
        for (Object element : schema.getTypeDefinitions()) {
            XSDTypeDefinition type = (XSDTypeDefinition) element;
            if (type instanceof XSDSimpleTypeDefinition) {
                if (type.getTargetNamespace() != null && !type.getTargetNamespace().equals(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001) || type.getTargetNamespace() == null) {
                    customTypes.add(type.getName());
                }
            }
        }
        List builtInTypes = XSDUtil.getBuiltInTypes();
        // can't change builtin's base type
        if (builtInTypes.contains(typedef.getName())) {
            return Status.CANCEL_STATUS;
        }
        dialog = new SimpleTypeInputDialog(this, page.getSite().getShell(), schema, Messages.XSDChangeBaseTypeAction_DialogTitle, customTypes, builtInTypes, typedef.getBaseTypeDefinition().getName());
        dialog.setBlockOnOpen(true);
        int ret = dialog.open();
        if (ret == Window.CANCEL) {
            return Status.CANCEL_STATUS;
        }
        // backup current Base Type
        XSDTypeDefinition current = typedef.getBaseTypeDefinition();
        // set new one
        if (builtIn) {
            typedef.setBaseTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(), typeName));
        } else {
            // check if simple type definition already exists
            XSDSimpleTypeDefinition std = schema.resolveSimpleTypeDefinition(typeName);
            if (!schema.getTypeDefinitions().contains(std)) {
                // $NON-NLS-1$
                std.setBaseTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(), "string"));
                schema.getContents().add(std);
            }
            typedef.setBaseTypeDefinition(std);
        }
        // remove current facets
        typedef.getFacetContents().removeAll(typedef.getFacetContents());
        typedef.updateElement();
        if (builtIn) {
            EList<XSDConstrainingFacet> constrainFacts = typedef.getBaseTypeDefinition().getFacetContents();
            for (XSDConstrainingFacet fact : constrainFacts) {
                if (fact instanceof XSDPatternFacet) {
                    XSDPatternFacet newFact = XSDSchemaBuildingTools.getXSDFactory().createXSDPatternFacet();
                    newFact.setLexicalValue(((XSDPatternFacet) fact).getLexicalValue());
                    typedef.getFacetContents().add(newFact);
                }
            }
            typedef.updateElement();
        }
        // remove current if no more in use
        // if ( (current.getName()!=null) && //anonymous type
        // (!schema.getSchemaForSchemaNamespace().equals(current.getTargetNamespace()))
        // ){
        // List eut =Util.findElementsUsingType(schema, current.getTargetNamespace(), current.getName());
        // if (eut.size()==0)
        // schema.getContents().remove(current);
        // }
        page.refresh();
        page.markDirty();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error, Messages.bind(Messages.XSDChangeBaseTypeAction_ErrorMsg1, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }
    return Status.OK_STATUS;
}
Also used : XSDPatternFacet(org.eclipse.xsd.XSDPatternFacet) XSDSimpleTypeDefinition(org.eclipse.xsd.XSDSimpleTypeDefinition) ArrayList(java.util.ArrayList) XSDConstrainingFacet(org.eclipse.xsd.XSDConstrainingFacet) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) List(java.util.List) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) SimpleTypeInputDialog(com.amalto.workbench.dialogs.SimpleTypeInputDialog) XSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition)

Example 15 with XSDSimpleTypeDefinition

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

the class XSDChangeToComplexTypeAction method doAction.

@Override
public IStatus doAction() {
    try {
        IStructuredSelection selection = (IStructuredSelection) page.getTreeViewer().getSelection();
        isConcept = false;
        TreePath tPath = null;
        if (((TreeSelection) selection).getPaths().length > 0) {
            tPath = ((TreeSelection) selection).getPaths()[0];
        }
        // declNew is the new created one not the selected one in tree vew
        if (declNew != null) {
            decl = declNew;
            checkConcept();
        } else if (selection.getFirstElement() instanceof XSDModelGroup) {
            for (int i = 0; i < tPath.getSegmentCount(); i++) {
                if (tPath.getSegment(i) instanceof XSDElementDeclaration) {
                    decl = (XSDElementDeclaration) tPath.getSegment(i);
                } else if (tPath.getSegment(i) instanceof XSDParticle) {
                    decl = (XSDElementDeclaration) ((XSDParticle) tPath.getSegment(i)).getTerm();
                }
            }
            checkConcept();
        } else if (selection.getFirstElement() instanceof XSDElementDeclaration) {
            decl = (XSDElementDeclaration) selection.getFirstElement();
            // check if concept or "just" element
            checkConcept();
        } else if (selection.getFirstElement() instanceof XSDParticle) {
            // if it's a particle,it should change the element of its
            // content
            decl = (XSDElementDeclaration) ((XSDParticle) selection.getFirstElement()).getContent();
        } else {
            // if(selection.getFirstElement() instanceof XSDParticle )
            if (selection.getFirstElement() != null) {
                // a sub element
                decl = (XSDElementDeclaration) ((XSDParticle) selection.getFirstElement()).getTerm();
            }
        }
        // /save current Type Definition
        // XSDTypeDefinition current = decl.getTypeDefinition();
        List<XSDComplexTypeDefinition> types = Util.getComplexTypes(decl.getSchema());
        if (showDlg) {
            if (decl.getTypeDefinition() instanceof XSDComplexTypeDefinition) {
                boolean confirm = MessageDialog.openConfirm(page.getSite().getShell(), Messages.Warning, Messages.XSDChangeToCXX_ChangeToAnotherTypeWarning);
                if (!confirm) {
                    return Status.CANCEL_STATUS;
                }
            }
            if (tPath != null) {
                for (int i = 0; i < tPath.getSegmentCount(); i++) {
                    if (tPath.getSegment(i) instanceof XSDElementDeclaration) {
                        XSDTypeDefinition type = (((XSDElementDeclaration) tPath.getSegment(i)).getTypeDefinition());
                        if (!type.equals(decl.getTypeDefinition())) {
                            types.remove(type);
                        }
                    }
                    if (tPath.getSegment(i) instanceof XSDParticle) {
                        XSDTypeDefinition type = ((XSDElementDeclaration) (((XSDParticle) tPath.getSegment(i)).getTerm())).getTypeDefinition();
                        if (!type.equals(decl.getTypeDefinition())) {
                            types.remove(type);
                        }
                    }
                }
            }
            dialog = new // $NON-NLS-1$
            ComplexTypeInputDialog(// $NON-NLS-1$
            this, // $NON-NLS-1$
            page.getSite().getShell(), // $NON-NLS-1$
            "", // $NON-NLS-1$
            schema, // $NON-NLS-1$
            decl.getTypeDefinition(), // $NON-NLS-1$
            types, isXSDModelGroup);
            dialog.setBlockOnOpen(true);
            int ret = dialog.open();
            if (ret == Dialog.CANCEL) {
                return Status.CANCEL_STATUS;
            }
        }
        if (!showDlg && !validateType()) {
            return Status.CANCEL_STATUS;
        }
        XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();
        // $NON-NLS-1$
        boolean anonymous = (typeName == null) || ("".equals(typeName));
        boolean alreadyExists = false;
        XSDComplexTypeDefinition complexType = null;
        // the sub element created if needed
        XSDParticle subParticle = null;
        XSDParticle groupParticle = null;
        XSDElementDeclaration subElement = null;
        // check if already exist
        // add by ymli; fix the bug:0012278;
        XSDElementDeclaration parent = null;
        Object pObject = Util.getParent(decl);
        if (pObject instanceof XSDElementDeclaration) {
            parent = (XSDElementDeclaration) pObject;
        }
        if (!anonymous) {
            List<XSDComplexTypeDefinition> list = Util.getComplexTypes(schema);
            if (typeName.lastIndexOf(" : ") != -1) {
                // $NON-NLS-1$
                // $NON-NLS-1$
                typeName = typeName.substring(0, typeName.lastIndexOf(" : "));
            }
            for (XSDComplexTypeDefinition td : list) {
                if ((td.getName().equals(typeName))) {
                    alreadyExists = true;
                    complexType = td;
                    break;
                }
            }
        } else {
            XSDComplexTypeDefinition declComplexType = null;
            if (parent != null && decl.getTypeDefinition() instanceof XSDComplexTypeDefinition) {
                declComplexType = (XSDComplexTypeDefinition) decl.getTypeDefinition();
            }
            if (declComplexType != null && declComplexType.getSchema() != null && declComplexType.getName() == null) {
                alreadyExists = true;
            }
            if (decl.getTypeDefinition() instanceof XSDSimpleTypeDefinition) {
                alreadyExists = false;
            }
        }
        if (alreadyExists) {
            XSDParticle partCnt = (XSDParticle) complexType.getContentType();
            partCnt.unsetMaxOccurs();
            partCnt.unsetMinOccurs();
            XSDTypeDefinition superType = null;
            for (XSDTypeDefinition type : types) {
                if (type.getName().equals(superTypeName)) {
                    superType = type;
                    break;
                }
            }
            if (superType != null) {
                XSDModelGroup mdlGrp = (XSDModelGroup) partCnt.getTerm();
                boolean status = updateCompositorType(superType, mdlGrp);
                if (!status) {
                    return Status.CANCEL_STATUS;
                }
                complexType.setDerivationMethod(XSDDerivationMethod.EXTENSION_LITERAL);
                complexType.setBaseTypeDefinition(superType);
            }
            if (isAbstract) {
                complexType.setAbstract(isAbstract);
            } else {
                complexType.unsetAbstract();
            }
            if (parent != null) {
                parent.updateElement();
            }
            if (complexType != null) {
                complexType.updateElement();
            }
        } else {
            // Create if does not exist
            // add an element declaration
            subElement = factory.createXSDElementDeclaration();
            if (declNew != null) {
                // crate a new entity
                if (declNew.getName() != null) {
                    // $NON-NLS-1$
                    subElement.setName(declNew.getName() + "Id");
                }
            } else {
                // create a complex element
                // $NON-NLS-1$
                subElement.setName("subelement");
            }
            // $NON-NLS-1$
            subElement.setTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(), "string"));
            subParticle = factory.createXSDParticle();
            subParticle.unsetMaxOccurs();
            subParticle.unsetMinOccurs();
            subParticle.setContent(subElement);
            subParticle.updateElement();
            // create group
            XSDModelGroup group = factory.createXSDModelGroup();
            if (isChoice) {
                group.setCompositor(XSDCompositor.CHOICE_LITERAL);
            } else if (isAll) {
                group.setCompositor(XSDCompositor.ALL_LITERAL);
            } else {
                group.setCompositor(XSDCompositor.SEQUENCE_LITERAL);
            }
            group.getContents().add(0, subParticle);
            group.updateElement();
            // create the complex type
            complexType = factory.createXSDComplexTypeDefinition();
            if (!anonymous) {
                XSDTypeDefinition superType = null;
                for (XSDTypeDefinition type : types) {
                    if (type.getName().equals(superTypeName)) {
                        superType = type;
                        break;
                    }
                }
                complexType.setName(typeName);
                if (superType != null) {
                    complexType.setDerivationMethod(XSDDerivationMethod.EXTENSION_LITERAL);
                    complexType.setBaseTypeDefinition(superType);
                    updateCompositorType(superType, group);
                }
                if (isAbstract) {
                    complexType.setAbstract(isAbstract);
                } else {
                    complexType.unsetAbstract();
                }
                schema.getContents().add(complexType);
            }
            complexType.updateElement();
            // add the group
            groupParticle = factory.createXSDParticle();
            groupParticle.unsetMaxOccurs();
            groupParticle.unsetMinOccurs();
            groupParticle.setContent(group);
            groupParticle.updateElement();
            complexType.setContent(groupParticle);
            complexType.updateElement();
        }
        // set complex type to concept
        if (anonymous) {
            decl.setAnonymousTypeDefinition(complexType);
        } else {
            decl.setTypeDefinition(complexType);
        }
        if (isConcept) {
            buildUniqueKey(factory, decl, complexType, anonymous, alreadyExists);
        }
        // if isConcept
        decl.updateElement();
        schema.update();
        page.refresh();
        declNew = null;
        page.markDirty();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error, Messages.bind(Messages.XSDChangeToCXX_ErrorMsg1, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }
    return Status.OK_STATUS;
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) XSDSimpleTypeDefinition(org.eclipse.xsd.XSDSimpleTypeDefinition) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) XSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition) TreePath(org.eclipse.jface.viewers.TreePath) TreeSelection(org.eclipse.jface.viewers.TreeSelection) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDParticle(org.eclipse.xsd.XSDParticle)

Aggregations

XSDSimpleTypeDefinition (org.eclipse.xsd.XSDSimpleTypeDefinition)106 XSDComplexTypeDefinition (org.eclipse.xsd.XSDComplexTypeDefinition)53 XSDElementDeclaration (org.eclipse.xsd.XSDElementDeclaration)47 XSDTypeDefinition (org.eclipse.xsd.XSDTypeDefinition)46 XSDParticle (org.eclipse.xsd.XSDParticle)34 ArrayList (java.util.ArrayList)33 XSDModelGroup (org.eclipse.xsd.XSDModelGroup)26 Iterator (java.util.Iterator)24 List (java.util.List)18 XSDIdentityConstraintDefinition (org.eclipse.xsd.XSDIdentityConstraintDefinition)18 XSDSchema (org.eclipse.xsd.XSDSchema)17 XSDAttributeDeclaration (org.eclipse.xsd.XSDAttributeDeclaration)16 XSDAttributeGroupDefinition (org.eclipse.xsd.XSDAttributeGroupDefinition)15 XSDConcreteComponent (org.eclipse.xsd.XSDConcreteComponent)15 XSDFactory (org.eclipse.xsd.XSDFactory)15 EList (org.eclipse.emf.common.util.EList)14 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)13 XSDAnnotation (org.eclipse.xsd.XSDAnnotation)12 XSDXPathDefinition (org.eclipse.xsd.XSDXPathDefinition)12 XSDAttributeUse (org.eclipse.xsd.XSDAttributeUse)11