Search in sources :

Example 6 with XSDInclude

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

the class XSDDirectivesManager method addToUnusedImports.

/**
 * From a list of used schemas, update the unusedImports list for the given schema
 *
 * @param schema
 * @param unusedImports
 * @param usedSchemas
 */
protected void addToUnusedImports(XSDSchema schema, List usedSchemas) {
    // now that we have the list of usedSchemas, get the list of unused
    // schemas by comparing this list to what is actually in the schema
    Iterator iter = schema.getContents().iterator();
    while (iter.hasNext()) {
        Object o = iter.next();
        if (o instanceof XSDSchemaDirective) {
            XSDSchemaDirective directive = (XSDSchemaDirective) o;
            boolean isUsed = false;
            Iterator iter2 = usedSchemas.iterator();
            while (iter2.hasNext()) {
                XSDSchema usedSchema = (XSDSchema) iter2.next();
                if (directive instanceof XSDImport && directive.getResolvedSchema() == usedSchema) {
                    isUsed = true;
                    break;
                }
                if (directive instanceof XSDInclude && ((XSDInclude) directive).getIncorporatedSchema() == usedSchema) {
                    isUsed = true;
                    usedDirectIncludes.add(usedSchema);
                    break;
                }
                // blindly accept redefines as used
                if (directive instanceof XSDRedefine) {
                    isUsed = true;
                    break;
                }
            }
            // If it is an include, we need to check if it is used indirectly
            if (directive instanceof XSDInclude && !isUsed) {
                XSDInclude inc = (XSDInclude) directive;
                XSDSchema incSchema = inc.getIncorporatedSchema();
                if (incSchema != null) {
                    XSDSchema usedSchema = getUsedIncludeSchema(incSchema, inc);
                    if (usedSchema != null) {
                        usedIndirectIncludes.add(directive);
                        usedIndirectIncludesMap.put(directive, usedSchema);
                        isUsed = true;
                    } else {
                        isUsed = false;
                    }
                }
            }
            // Also any redefines should be considered used
            if (!isUsed && !unusedDirectives.contains(directive) && !(directive instanceof XSDRedefine)) {
                unusedDirectives.add(directive);
            }
        }
    }
    Iterator iter3 = usedIndirectIncludes.iterator();
    while (iter3.hasNext()) {
        Object o = iter3.next();
        if (o instanceof XSDInclude) {
            XSDInclude inc = (XSDInclude) o;
            XSDSchema targetSchema = (XSDSchema) usedIndirectIncludesMap.get(inc);
            if (usedIncludeSchemas.contains(targetSchema) && usedDirectIncludes.contains(targetSchema)) {
                unusedDirectives.add(inc);
            } else {
                usedDirectIncludes.add(targetSchema);
            }
        }
    }
}
Also used : XSDSchemaDirective(org.eclipse.xsd.XSDSchemaDirective) XSDRedefine(org.eclipse.xsd.XSDRedefine) Iterator(java.util.Iterator) EObject(org.eclipse.emf.ecore.EObject) XSDImport(org.eclipse.xsd.XSDImport) XSDSchema(org.eclipse.xsd.XSDSchema) XSDInclude(org.eclipse.xsd.XSDInclude)

Example 7 with XSDInclude

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

the class XSDDirectivesManager method getUsedIncludeSchema.

/**
 * Includes can be used indirectly.   If the schema includes A which includes B, but the schema
 * references something in B, then A is indirectly used, and hence A cannot be removed.
 *
 * @param schema
 * @param xsdInclude
 * @return the referenced schema if used, null if not used
 */
private XSDSchema getUsedIncludeSchema(XSDSchema schema, XSDInclude xsdInclude) {
    XSDSchema refSchema = null;
    boolean isUsed = false;
    Iterator iter = schema.getContents().iterator();
    while (iter.hasNext()) {
        Object o = iter.next();
        if (o instanceof XSDInclude) {
            XSDInclude inc = (XSDInclude) o;
            XSDSchema incSchema = inc.getIncorporatedSchema();
            if (incSchema != null) {
                Iterator iter2 = usedIncludeSchemas.iterator();
                while (iter2.hasNext()) {
                    XSDSchema xsdSch = (XSDSchema) iter2.next();
                    if (incSchema == xsdSch) {
                        isUsed = true;
                        refSchema = incSchema;
                        break;
                    }
                }
                if (!isUsed) {
                    if (// To prevent infinite cycle
                    !analyzedIncludeSchemas.contains(incSchema)) {
                        analyzedIncludeSchemas.add(incSchema);
                        refSchema = getUsedIncludeSchema(incSchema, inc);
                    }
                }
                if (isUsed || refSchema != null) {
                    return refSchema;
                }
            }
        } else {
            break;
        }
    }
    return refSchema;
}
Also used : Iterator(java.util.Iterator) EObject(org.eclipse.emf.ecore.EObject) XSDSchema(org.eclipse.xsd.XSDSchema) XSDInclude(org.eclipse.xsd.XSDInclude)

Example 8 with XSDInclude

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

the class DataModelMainPage method importSchema.

protected void importSchema(InputSource source, String uri) throws Exception {
    // $NON-NLS-1$
    String ns = "";
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    documentBuilderFactory.setValidating(false);
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(source);
    // $NON-NLS-1$
    ns = document.getDocumentElement().getAttribute("targetNamespace");
    if (xsdSchema == null) {
        xsdSchema = getXSDSchema(Util.nodeToString(document));
    } else {
        WSDataModel wsObject = (WSDataModel) (xobject.getWsObject());
        xsdSchema = Util.createXsdSchema(wsObject.getXsdSchema(), xobject);
    }
    boolean exist = false;
    for (int i = 0; i < xsdSchema.getContents().size(); i++) {
        XSDSchemaContent xsdComp = xsdSchema.getContents().get(i);
        if (ns != null && !ns.equals("")) {
            // import xsdschema
            if (xsdComp instanceof XSDImport && ((XSDImport) xsdComp).getNamespace().equals(ns)) {
                for (Map.Entry entry : xsdSchema.getQNamePrefixToNamespaceMap().entrySet()) {
                    if (entry.getValue().equals(((XSDImport) xsdComp).getNamespace())) {
                        exist = true;
                        break;
                    }
                }
                break;
            }
        } else {
            // include xsdschema
            if (xsdComp instanceof XSDInclude) {
                String xsdLocation = ((XSDInclude) xsdComp).getSchemaLocation();
                if (xsdLocation.equals(uri)) {
                    exist = true;
                    break;
                }
            }
        }
    }
    if (!exist) {
        if (ns != null && !ns.equals("")) {
            // $NON-NLS-1$
            // $NON-NLS-1$
            int last = ns.lastIndexOf("/");
            // $NON-NLS-1$//$NON-NLS-2$
            xsdSchema.getQNamePrefixToNamespaceMap().put(ns.substring(last + 1).replaceAll("[\\W]", ""), ns);
            XSDImport xsdImport = XSDFactory.eINSTANCE.createXSDImport();
            xsdImport.setNamespace(ns);
            xsdImport.setSchemaLocation(uri);
            xsdSchema.getContents().add(0, xsdImport);
        } else {
            XSDInclude xsdInclude = XSDFactory.eINSTANCE.createXSDInclude();
            xsdInclude.setSchemaLocation(uri);
            xsdSchema.getContents().add(0, xsdInclude);
        }
        String xsd = Util.nodeToString(xsdSchema.getDocument());
        setXsdSchema(xsdSchema);
        WSDataModel wsObject = (WSDataModel) (xobject.getWsObject());
        wsObject.setXsdSchema(xsd);
    }
}
Also used : WSDataModel(com.amalto.workbench.webservices.WSDataModel) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) XSDSchemaContent(org.eclipse.xsd.XSDSchemaContent) XSDImport(org.eclipse.xsd.XSDImport) Document(org.w3c.dom.Document) Map(java.util.Map) HashMap(java.util.HashMap) XSDInclude(org.eclipse.xsd.XSDInclude)

Example 9 with XSDInclude

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

the class XSDSchemaAdapter method notifyChanged.

public void notifyChanged(final Notification msg) {
    class CategoryNotification extends NotificationImpl {

        protected Object category;

        public CategoryNotification(Object category) {
            super(msg.getEventType(), msg.getOldValue(), msg.getNewValue(), msg.getPosition());
            this.category = category;
        }

        public Object getNotifier() {
            return category;
        }

        public Object getFeature() {
            return msg.getFeature();
        }
    }
    if (children == null) {
        getChildren();
    }
    Object newValue = msg.getNewValue();
    Object oldValue = msg.getOldValue();
    // Bug 245480 - Deletion of Include, Import and Redefine is not reflected in the Outline view
    // We only want to refresh the Directives folder for any changes to XSDDirectives.  The first case covers
    // changes to one directive, whereas the missing case as reported in bug 245480 covers changes to a list
    // of directives.
    boolean updateDirectivesCategory = false;
    if (oldValue instanceof XSDSchemaDirective) {
        updateDirectivesCategory = true;
    } else if (oldValue instanceof Collection) {
        Iterator iterator = ((Collection) oldValue).iterator();
        while (iterator.hasNext()) {
            Object obj = iterator.next();
            if (obj instanceof XSDSchemaDirective) {
                // if we find at least one directive, then we should refresh the folder
                updateDirectivesCategory = true;
                break;
            }
        }
    }
    if (newValue instanceof XSDInclude || newValue instanceof XSDImport || newValue instanceof XSDRedefine || // handle the case for delete directive
    (msg.getFeature() == XSDPackage.eINSTANCE.getXSDSchema_Contents() && updateDirectivesCategory) || // updates to the imports/includes
    msg.getFeature() == XSDPackage.eINSTANCE.getXSDSchema_IncorporatedVersions()) {
        CategoryAdapter adapter = getCategory(CategoryAdapter.DIRECTIVES);
        Assert.isTrue(adapter != null);
        XSDSchema xsdSchema = adapter.getXSDSchema();
        adapter.setChildren(getDirectives(xsdSchema));
        adapter.setAllChildren(getDirectives(xsdSchema));
        notifyListeners(new CategoryNotification(adapter), adapter.getText());
        return;
    } else if (msg.getFeature() == XSDPackage.eINSTANCE.getXSDSchema_ElementDeclarations()) {
        CategoryAdapter adapter = getCategory(CategoryAdapter.ELEMENTS);
        Assert.isTrue(adapter != null);
        XSDSchema xsdSchema = adapter.getXSDSchema();
        adapter.setChildren(getGlobalElements(xsdSchema));
        adapter.setAllChildren(getGlobalElements(xsdSchema, true));
        notifyListeners(new CategoryNotification(adapter), adapter.getText());
        return;
    } else if (msg.getFeature() == XSDPackage.eINSTANCE.getXSDSchema_AttributeDeclarations() || msg.getFeature() == XSDPackage.eINSTANCE.getXSDSchema_AttributeGroupDefinitions()) {
        CategoryAdapter adapter = getCategory(CategoryAdapter.ATTRIBUTES);
        Assert.isTrue(adapter != null);
        XSDSchema xsdSchema = adapter.getXSDSchema();
        adapter.setChildren(getAttributeList(xsdSchema));
        adapter.setAllChildren(getAttributeList(xsdSchema, true));
        notifyListeners(new CategoryNotification(adapter), adapter.getText());
        return;
    } else if (msg.getFeature() == XSDPackage.eINSTANCE.getXSDSchema_TypeDefinitions()) {
        CategoryAdapter adapter = getCategory(CategoryAdapter.TYPES);
        Assert.isTrue(adapter != null);
        XSDSchema xsdSchema = adapter.getXSDSchema();
        List types = getComplexTypes(xsdSchema);
        types.addAll(getSimpleTypes(xsdSchema));
        adapter.setChildren(types);
        adapter.setAllChildren(getTypes(xsdSchema, true));
        notifyListeners(new CategoryNotification(adapter), adapter.getText());
        return;
    } else if (msg.getFeature() == XSDPackage.eINSTANCE.getXSDSchema_ModelGroupDefinitions()) {
        CategoryAdapter adapter = getCategory(CategoryAdapter.GROUPS);
        Assert.isTrue(adapter != null);
        XSDSchema xsdSchema = adapter.getXSDSchema();
        adapter.setChildren(getGroups(xsdSchema));
        adapter.setAllChildren(getGroups(xsdSchema, true));
        notifyListeners(new CategoryNotification(adapter), adapter.getText());
        return;
    } else if (msg.getFeature() == XSDPackage.eINSTANCE.getXSDSchema_Annotations()) {
        return;
    } else if (msg.getFeature() == XSDPackage.eINSTANCE.getXSDSchema_SchemaLocation()) {
        notifyListeners(msg, null);
        return;
    }
    types = null;
    getTypes();
    super.notifyChanged(msg);
}
Also used : XSDRedefine(org.eclipse.xsd.XSDRedefine) NotificationImpl(org.eclipse.emf.common.notify.impl.NotificationImpl) XSDSchemaDirective(org.eclipse.xsd.XSDSchemaDirective) Iterator(java.util.Iterator) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) XSDImport(org.eclipse.xsd.XSDImport) XSDInclude(org.eclipse.xsd.XSDInclude) XSDSchema(org.eclipse.xsd.XSDSchema)

Example 10 with XSDInclude

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

the class AddXSDIncludeCommand method execute.

public void execute() {
    try {
        super.execute();
        // Add this after if we don't have a DOM Node yet
        beginRecording(xsdSchema.getElement());
        XSDInclude xsdInclude = XSDFactory.eINSTANCE.createXSDInclude();
        // $NON-NLS-1$
        xsdInclude.setSchemaLocation("");
        xsdSchema.getContents().add(findNextPositionToInsert(), xsdInclude);
        addedXSDConcreteComponent = xsdInclude;
        formatChild(xsdSchema.getElement());
    } finally {
        endRecording();
    }
}
Also used : XSDInclude(org.eclipse.xsd.XSDInclude)

Aggregations

XSDInclude (org.eclipse.xsd.XSDInclude)11 XSDSchema (org.eclipse.xsd.XSDSchema)7 XSDImport (org.eclipse.xsd.XSDImport)6 XSDRedefine (org.eclipse.xsd.XSDRedefine)6 ArrayList (java.util.ArrayList)3 Iterator (java.util.Iterator)3 EObject (org.eclipse.emf.ecore.EObject)3 XSDSchemaContent (org.eclipse.xsd.XSDSchemaContent)3 HashMap (java.util.HashMap)2 List (java.util.List)2 IFile (org.eclipse.core.resources.IFile)2 URI (org.eclipse.emf.common.util.URI)2 IEditorInput (org.eclipse.ui.IEditorInput)2 IFileEditorInput (org.eclipse.ui.IFileEditorInput)2 XSDAttributeDeclaration (org.eclipse.xsd.XSDAttributeDeclaration)2 XSDElementDeclaration (org.eclipse.xsd.XSDElementDeclaration)2 XSDSchemaDirective (org.eclipse.xsd.XSDSchemaDirective)2 XSDImportImpl (org.eclipse.xsd.impl.XSDImportImpl)2 TreeObject (com.amalto.workbench.models.TreeObject)1 WSDataModel (com.amalto.workbench.webservices.WSDataModel)1