Search in sources :

Example 31 with XSDSchemaDirective

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

the class OpenInNewEditor method run.

public void run() {
    Object selection = ((IStructuredSelection) getSelection()).getFirstElement();
    IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    IWorkbenchPage page = null;
    IEditorInput editorInput = null;
    if (workbenchWindow != null) {
        page = workbenchWindow.getActivePage();
        IEditorPart activeEditor = page.getActiveEditor();
        if (activeEditor != null) {
            editorInput = activeEditor.getEditorInput();
        }
    }
    if (selection instanceof XSDBaseAdapter) {
        XSDBaseAdapter xsdAdapter = (XSDBaseAdapter) selection;
        XSDConcreteComponent fComponent = (XSDConcreteComponent) xsdAdapter.getTarget();
        XSDSchema schema = fComponent.getSchema();
        boolean isReference = false;
        if (fComponent instanceof XSDFeature) {
            isReference = ((XSDFeature) fComponent).isFeatureReference();
            fComponent = ((XSDFeature) fComponent).getResolvedFeature();
        }
        String schemaLocation = null;
        IPath schemaPath = null;
        IFile schemaFile = null;
        // Special case any imports/includes
        if (selection instanceof XSDSchemaDirectiveAdapter) {
            XSDSchemaDirective dir = (XSDSchemaDirective) ((XSDSchemaDirectiveAdapter) selection).getTarget();
            // force load of imported schema
            if (dir instanceof XSDImportImpl) {
                ((XSDImportImpl) dir).importSchema();
            }
            if (dir.getResolvedSchema() != null) {
                schemaLocation = URIHelper.removePlatformResourceProtocol(dir.getResolvedSchema().getSchemaLocation());
                schemaPath = new Path(schemaLocation);
                schemaFile = ResourcesPlugin.getWorkspace().getRoot().getFile(schemaPath);
                schema = dir.getResolvedSchema();
                fComponent = dir.getResolvedSchema();
            }
        } else // Handle any other external components
        if (fComponent.getSchema() != null && fComponent.eContainer() instanceof XSDSchema || fComponent.eContainer() instanceof XSDRedefine || isReference) {
            schemaLocation = URIHelper.removePlatformResourceProtocol(fComponent.getSchema().getSchemaLocation());
            schemaPath = new Path(schemaLocation);
            schemaFile = ResourcesPlugin.getWorkspace().getRoot().getFile(schemaPath);
            try {
                XSDSchema xsdSchema = (XSDSchema) getWorkbenchPart().getAdapter(XSDSchema.class);
                if (fComponent.getSchema() == xsdSchema) {
                    IEditorPart editorPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
                    if (editorPart instanceof InternalXSDMultiPageEditor) {
                        ((InternalXSDMultiPageEditor) editorPart).openOnGlobalReference(fComponent);
                    }
                    return;
                }
            } catch (Exception e) {
            }
        }
        // If the schemaFile exists in the workspace
        if (page != null && schemaFile != null && schemaFile.exists()) {
            try {
                // Get the current editor's schema
                XSDSchema xsdSchema = (XSDSchema) getWorkbenchPart().getAdapter(XSDSchema.class);
                IEditorPart editorPart = null;
                // are in the same resource file....hence multiple schemas in the same file.
                if (xsdSchema != null && fComponent.getRootContainer().eResource() == xsdSchema.eResource() && xsdSchema != schema) {
                    String editorName = null;
                    XSDFileEditorInput xsdFileEditorInput = new XSDFileEditorInput(schemaFile, fComponent.getSchema());
                    // Try to use the same editor name as the current one
                    if (editorInput != null) {
                        editorName = editorInput.getName();
                        xsdFileEditorInput.setEditorName(editorName);
                    }
                    editorPart = getExistingEditorForInlineSchema(page, schemaFile, schema);
                    if (editorPart == null) {
                        editorPart = page.openEditor(xsdFileEditorInput, XSDEditorPlugin.EDITOR_ID, true, 0);
                    }
                } else {
                    editorPart = page.openEditor(new FileEditorInput(schemaFile), XSDEditorPlugin.EDITOR_ID);
                }
                if (editorPart instanceof InternalXSDMultiPageEditor) {
                    ((InternalXSDMultiPageEditor) editorPart).openOnGlobalReference(fComponent);
                }
            } catch (Exception e) {
            }
        } else {
            // open the xsd externally
            if (schemaLocation != null)
                openExternalFiles(page, schemaLocation, fComponent);
        }
    }
}
Also used : XSDSchemaDirectiveAdapter(org.eclipse.wst.xsd.ui.internal.adapters.XSDSchemaDirectiveAdapter) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IFile(org.eclipse.core.resources.IFile) XSDRedefine(org.eclipse.xsd.XSDRedefine) XSDConcreteComponent(org.eclipse.xsd.XSDConcreteComponent) IPath(org.eclipse.core.runtime.IPath) InternalXSDMultiPageEditor(org.eclipse.wst.xsd.ui.internal.editor.InternalXSDMultiPageEditor) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IEditorPart(org.eclipse.ui.IEditorPart) PartInitException(org.eclipse.ui.PartInitException) XSDSchemaDirective(org.eclipse.xsd.XSDSchemaDirective) XSDImportImpl(org.eclipse.xsd.impl.XSDImportImpl) XSDFeature(org.eclipse.xsd.XSDFeature) XSDFileEditorInput(org.eclipse.wst.xsd.ui.internal.editor.XSDFileEditorInput) XSDFileEditorInput(org.eclipse.wst.xsd.ui.internal.editor.XSDFileEditorInput) FileEditorInput(org.eclipse.ui.part.FileEditorInput) ADTReadOnlyFileEditorInput(org.eclipse.wst.xsd.ui.internal.adt.editor.ADTReadOnlyFileEditorInput) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) XSDBaseAdapter(org.eclipse.wst.xsd.ui.internal.adapters.XSDBaseAdapter) IEditorInput(org.eclipse.ui.IEditorInput) XSDSchema(org.eclipse.xsd.XSDSchema)

Example 32 with XSDSchemaDirective

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

the class UpdateComponentReferenceAndManageDirectivesCommand method computeComponent.

protected XSDComponent computeComponent() {
    XSDComponent result = null;
    XSDSchema schema = concreteComponent.getSchema();
    XSDSchemaDirective directive = null;
    // lets see if the element is already visible to our schema
    result = getDefinedComponent(schema, componentName, componentNamespace);
    if (result == null) {
        // includes/imports to get to it
        if (componentNamespace != null && componentNamespace.equals(schema.getTargetNamespace())) {
            // we need to add an include
            // if the component's namespace is not null and matches the schema's
            // target namespace
            directive = XSDFactory.eINSTANCE.createXSDInclude();
        } else if (componentNamespace == null) {
            // we need to add an include
            // if the component's namespace is null, then we can just add it
            // only if the current namespace is not null
            directive = XSDFactory.eINSTANCE.createXSDInclude();
            // we have to ensure the schema for schema prefix is NOT null
            if (schema.getSchemaForSchemaQNamePrefix() == null) {
                String targetNS = schema.getTargetNamespace();
                if (targetNS == null)
                    targetNS = "";
                // this will just update the schema for schema prefix to be, say, xsd
                UpdateNamespaceInformationCommand command = new UpdateNamespaceInformationCommand("", schema, "", targetNS);
                command.execute();
            }
        } else {
            // we need to add an import
            XSDImport xsdImport = XSDFactory.eINSTANCE.createXSDImport();
            xsdImport.setNamespace(componentNamespace);
            directive = xsdImport;
        }
        String location = computeNiceLocation(schema.getSchemaLocation(), file);
        directive.setSchemaLocation(location);
        // TODO (cs) we should at the directive 'next' in the list of directives
        // for now I'm just adding as the first thing in the schema :-(
        // 
        schema.getContents().add(0, directive);
        XSDSchema resolvedSchema = directive.getResolvedSchema();
        if (resolvedSchema == null) {
            String platformLocation = "platform:/resource" + file.getFullPath();
            Resource resource = concreteComponent.eResource().getResourceSet().createResource(URI.createURI(platformLocation));
            if (resource instanceof XSDResourceImpl) {
                try {
                    resource.load(null);
                    XSDResourceImpl resourceImpl = (XSDResourceImpl) resource;
                    resolvedSchema = resourceImpl.getSchema();
                    if (resolvedSchema != null) {
                        directive.setResolvedSchema(resolvedSchema);
                    }
                } catch (Exception e) {
                }
            }
        }
        if (resolvedSchema != null) {
            result = getDefinedComponent(resolvedSchema, componentName, componentNamespace);
        } else {
        // TODO (cs) consider setting some error state so that the client can
        // provide a pop-dialog error
        // we should also remove the import/include so save from cluttering
        // the file with bogus directives
        }
    }
    return result;
}
Also used : XSDSchemaDirective(org.eclipse.xsd.XSDSchemaDirective) Resource(org.eclipse.emf.ecore.resource.Resource) XSDResourceImpl(org.eclipse.xsd.util.XSDResourceImpl) XSDImport(org.eclipse.xsd.XSDImport) XSDComponent(org.eclipse.xsd.XSDComponent) XSDSchema(org.eclipse.xsd.XSDSchema)

Example 33 with XSDSchemaDirective

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

the class XSDAdapterFactory method createAdapter.

public Adapter createAdapter(Notifier target) {
    XSDSwitch xsdSwitch = new XSDSwitch() {

        public Object caseXSDSchemaDirective(XSDSchemaDirective object) {
            return new XSDSchemaDirectiveAdapter();
        }

        public Object caseXSDWildcard(XSDWildcard object) {
            return new XSDWildcardAdapter();
        }

        public Object caseXSDAttributeGroupDefinition(XSDAttributeGroupDefinition object) {
            return new XSDAttributeGroupDefinitionAdapter();
        }

        public Object caseXSDModelGroupDefinition(XSDModelGroupDefinition object) {
            return new XSDModelGroupDefinitionAdapter();
        }

        public Object caseXSDAttributeDeclaration(XSDAttributeDeclaration object) {
            return new XSDAttributeDeclarationAdapter();
        }

        public Object caseXSDAttributeUse(XSDAttributeUse object) {
            return new XSDAttributeUseAdapter();
        }

        public Object caseXSDParticle(XSDParticle object) {
            return new XSDParticleAdapter();
        }

        public Object caseXSDElementDeclaration(XSDElementDeclaration object) {
            return new XSDElementDeclarationAdapter();
        }

        public Object caseXSDSimpleTypeDefinition(XSDSimpleTypeDefinition object) {
            return new XSDSimpleTypeDefinitionAdapter();
        }

        public Object caseXSDComplexTypeDefinition(XSDComplexTypeDefinition object) {
            // 
            if (// $NON-NLS-1$
            "anyType".equals(object.getName())) {
                return new XSDAnyTypeDefinitionAdapter();
            } else {
                return new XSDComplexTypeDefinitionAdapter();
            }
        }

        public Object caseXSDModelGroup(XSDModelGroup object) {
            return new XSDModelGroupAdapter();
        }

        public Object caseXSDSchema(XSDSchema object) {
            return new XSDSchemaAdapter();
        }

        public Object caseXSDEnumerationFacet(XSDEnumerationFacet object) {
            return new XSDEnumerationFacetAdapter();
        }

        public Object caseXSDRedefine(XSDRedefine object) {
            return new XSDRedefineAdapter();
        }
    };
    Object o = xsdSwitch.doSwitch((EObject) target);
    Adapter result = null;
    if (o instanceof Adapter) {
        result = (Adapter) o;
    } else {
    // Thread.dumpStack();
    }
    return result;
}
Also used : XSDAttributeUse(org.eclipse.xsd.XSDAttributeUse) XSDRedefine(org.eclipse.xsd.XSDRedefine) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) Adapter(org.eclipse.emf.common.notify.Adapter) XSDEnumerationFacet(org.eclipse.xsd.XSDEnumerationFacet) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDParticle(org.eclipse.xsd.XSDParticle) XSDSchema(org.eclipse.xsd.XSDSchema) XSDSimpleTypeDefinition(org.eclipse.xsd.XSDSimpleTypeDefinition) XSDWildcard(org.eclipse.xsd.XSDWildcard) XSDSwitch(org.eclipse.xsd.util.XSDSwitch) XSDAttributeGroupDefinition(org.eclipse.xsd.XSDAttributeGroupDefinition) XSDSchemaDirective(org.eclipse.xsd.XSDSchemaDirective) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) EObject(org.eclipse.emf.ecore.EObject) XSDModelGroupDefinition(org.eclipse.xsd.XSDModelGroupDefinition) XSDAttributeDeclaration(org.eclipse.xsd.XSDAttributeDeclaration)

Aggregations

XSDSchemaDirective (org.eclipse.xsd.XSDSchemaDirective)33 XSDSchema (org.eclipse.xsd.XSDSchema)28 List (java.util.List)24 Iterator (java.util.Iterator)5 XSDImport (org.eclipse.xsd.XSDImport)4 XSDRedefine (org.eclipse.xsd.XSDRedefine)4 EObject (org.eclipse.emf.ecore.EObject)3 ArrayList (java.util.ArrayList)2 Adapter (org.eclipse.emf.common.notify.Adapter)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 XSDSchemaDirectiveAdapter (org.eclipse.wst.xsd.ui.internal.adapters.XSDSchemaDirectiveAdapter)2 XSDAttributeDeclaration (org.eclipse.xsd.XSDAttributeDeclaration)2 XSDAttributeGroupDefinition (org.eclipse.xsd.XSDAttributeGroupDefinition)2 XSDConcreteComponent (org.eclipse.xsd.XSDConcreteComponent)2 XSDElementDeclaration (org.eclipse.xsd.XSDElementDeclaration)2 XSDInclude (org.eclipse.xsd.XSDInclude)2 XSDModelGroupDefinition (org.eclipse.xsd.XSDModelGroupDefinition)2 MissingJarsException (com.amalto.workbench.service.MissingJarsException)1 InaccessibleWSDLException (com.sun.xml.internal.ws.wsdl.parser.InaccessibleWSDLException)1 IOException (java.io.IOException)1