Search in sources :

Example 1 with XSDImportImpl

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

the class Util method importSchema.

private static void importSchema(XSDSchema xsdSchema, List<XSDImport> imports, Map<String, Integer> schemaMonitor) {
    EList<XSDSchemaContent> list = xsdSchema.getContents();
    for (XSDSchemaContent schemaCnt : list) {
        if (schemaCnt instanceof XSDImport) {
            XSDImportImpl xsdImpl = (XSDImportImpl) schemaCnt;
            if (xsdImpl.getNamespace() == null || xsdImpl.getNamespace().trim().equals("")) {
                // $NON-NLS-1$
                URI fileURI = URI.createFileURI(xsdImpl.getSchemaLocation());
                // $NON-NLS-1$//$NON-NLS-2$
                xsdImpl.setNamespace(fileURI.toFileString().replaceAll("[\\W]", ""));
            }
            if (xsdImpl.getSchemaLocation() == null) {
                continue;
            }
            ((XSDImportImpl) schemaCnt).importSchema();
            imports.add(((XSDImportImpl) schemaCnt));
        } else if (schemaCnt instanceof XSDInclude) {
            XSDIncludeImpl xsdInclude = (XSDIncludeImpl) schemaCnt;
            ((XSDSchemaImpl) xsdSchema).included(xsdInclude);
        }
    }
}
Also used : XSDImportImpl(org.eclipse.xsd.impl.XSDImportImpl) XSDSchemaContent(org.eclipse.xsd.XSDSchemaContent) XSDImport(org.eclipse.xsd.XSDImport) XSDIncludeImpl(org.eclipse.xsd.impl.XSDIncludeImpl) URI(org.eclipse.emf.common.util.URI) XSDInclude(org.eclipse.xsd.XSDInclude)

Example 2 with XSDImportImpl

use of org.eclipse.xsd.impl.XSDImportImpl in project webtools.sourceediting by eclipse.

the class XSDDirectivesSchemaLocationUpdater method updateSchemaLocation.

/**
 * Modifies the schema location by opening the schema location dialog and
 * processing the results. This method refactors the code in
 * XSDImportSection$widgetSelected and SchemaLocationSection$widgetSelected
 * and the processing in handleSchemaLocationChange()
 */
public static void updateSchemaLocation(XSDSchema xsdSchema, Object selection, boolean isInclude) {
    Shell shell = Display.getCurrent().getActiveShell();
    IFile currentIFile = null;
    IEditorInput editorInput = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getEditorInput();
    ViewerFilter filter;
    if (editorInput instanceof IFileEditorInput) {
        currentIFile = ((IFileEditorInput) editorInput).getFile();
        filter = new // $NON-NLS-1$
        ResourceFilter(// $NON-NLS-1$
        new String[] { ".xsd" }, new IFile[] { currentIFile }, null);
    } else {
        filter = new // $NON-NLS-1$
        ResourceFilter(// $NON-NLS-1$
        new String[] { ".xsd" }, null, null);
    }
    XSDSelectIncludeFileWizard fileSelectWizard = new // $NON-NLS-1$
    XSDSelectIncludeFileWizard(// $NON-NLS-1$
    xsdSchema, // $NON-NLS-1$
    isInclude, // $NON-NLS-1$
    XSDEditorPlugin.getXSDString("_UI_FILEDIALOG_SELECT_XML_SCHEMA"), // $NON-NLS-1$
    XSDEditorPlugin.getXSDString("_UI_FILEDIALOG_SELECT_XML_DESC"), filter, new StructuredSelection(selection));
    WizardDialog wizardDialog = new WizardDialog(shell, fileSelectWizard);
    wizardDialog.create();
    wizardDialog.setBlockOnOpen(true);
    int result = wizardDialog.open();
    if (result == Window.OK) {
        IFile selectedIFile = fileSelectWizard.getResultFile();
        String schemaFileString;
        if (selectedIFile != null && currentIFile != null) {
            schemaFileString = URIHelper.getRelativeURI(selectedIFile.getLocation(), currentIFile.getLocation());
        } else if (selectedIFile != null && currentIFile == null) {
            schemaFileString = selectedIFile.getLocationURI().toString();
        } else {
            schemaFileString = fileSelectWizard.getURL();
        }
        // $NON-NLS-1$
        String attributeSchemaLocation = "schemaLocation";
        if (selection instanceof XSDImport) {
            XSDImport xsdImport = (XSDImport) selection;
            // $NON-NLS-1$
            xsdImport.getElement().setAttribute(attributeSchemaLocation, schemaFileString);
            String namespace = fileSelectWizard.getNamespace();
            if (namespace == null)
                // $NON-NLS-1$
                namespace = "";
            XSDSchema externalSchema = fileSelectWizard.getExternalSchema();
            java.util.Map map = xsdSchema.getQNamePrefixToNamespaceMap();
            Element schemaElement = xsdSchema.getElement();
            // update the xmlns in the schema element first, and then update the
            // import element next so that the last change will be in the import element. This keeps the
            // selection on the import element
            TypesHelper helper = new TypesHelper(externalSchema);
            String prefix = helper.getPrefix(namespace, false);
            if (map.containsKey(prefix)) {
                prefix = null;
            }
            if (prefix == null || (prefix != null && prefix.length() == 0)) {
                // $NON-NLS-1$
                StringBuffer newPrefix = new StringBuffer("pref");
                int prefixExtension = 1;
                while (map.containsKey(newPrefix.toString()) && prefixExtension < 100) {
                    // $NON-NLS-1$
                    newPrefix = new StringBuffer("pref" + String.valueOf(prefixExtension));
                    prefixExtension++;
                }
                prefix = newPrefix.toString();
            }
            // $NON-NLS-1$
            String attributeNamespace = "namespace";
            if (namespace.length() > 0) {
                // if ns already in map, use its corresponding prefix
                if (map.containsValue(namespace)) {
                    TypesHelper typesHelper = new TypesHelper(xsdSchema);
                    prefix = typesHelper.getPrefix(namespace, false);
                } else // otherwise add to the map
                {
                    // $NON-NLS-1$
                    schemaElement.setAttribute("xmlns:" + prefix, namespace);
                }
                // prefixText.setText(prefix);
                xsdImport.getElement().setAttribute(attributeNamespace, namespace);
            }
            // into the resource set
            if (// redundant
            selection instanceof XSDImportImpl) {
                XSDImportImpl xsdImportImpl = (XSDImportImpl) selection;
                xsdImportImpl.importSchema();
            }
        } else if (selection instanceof XSDInclude) {
            XSDInclude xsdInclude = (XSDInclude) selection;
            xsdInclude.getElement().setAttribute(attributeSchemaLocation, schemaFileString);
        } else if (selection instanceof XSDRedefine) {
            XSDRedefine xsdRedefine = (XSDRedefine) selection;
            xsdRedefine.getElement().setAttribute(attributeSchemaLocation, schemaFileString);
        }
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) XSDRedefine(org.eclipse.xsd.XSDRedefine) ViewerFilter(org.eclipse.jface.viewers.ViewerFilter) Element(org.w3c.dom.Element) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) Shell(org.eclipse.swt.widgets.Shell) XSDImportImpl(org.eclipse.xsd.impl.XSDImportImpl) XSDSelectIncludeFileWizard(org.eclipse.wst.xsd.ui.internal.wizards.XSDSelectIncludeFileWizard) TypesHelper(org.eclipse.wst.xsd.ui.internal.util.TypesHelper) IFileEditorInput(org.eclipse.ui.IFileEditorInput) XSDImport(org.eclipse.xsd.XSDImport) WizardDialog(org.eclipse.jface.wizard.WizardDialog) IEditorInput(org.eclipse.ui.IEditorInput) XSDSchema(org.eclipse.xsd.XSDSchema) XSDInclude(org.eclipse.xsd.XSDInclude)

Example 3 with XSDImportImpl

use of org.eclipse.xsd.impl.XSDImportImpl in project webtools.sourceediting by eclipse.

the class TopLevelComponentEditPart method performRequest.

public void performRequest(Request request) {
    // Do not open on or set focus on direct edit type
    if (request.getType() == RequestConstants.REQ_OPEN) {
        Object model = getModel();
        if (model instanceof IGraphElement) {
            if (((IGraphElement) model).isFocusAllowed()) {
                if (request instanceof LocationRequest) {
                    LocationRequest locationRequest = (LocationRequest) request;
                    Point p = locationRequest.getLocation();
                    if (hitTest(labelHolder, p)) {
                        performDrillDownAction();
                    }
                }
            }
        } else if (model instanceof XSDSchemaDirectiveAdapter) {
            if (request instanceof LocationRequest) {
                LocationRequest locationRequest = (LocationRequest) request;
                Point p = locationRequest.getLocation();
                if (hitTest(labelHolder, p)) {
                    XSDSchemaDirective dir = (XSDSchemaDirective) ((XSDSchemaDirectiveAdapter) model).getTarget();
                    String schemaLocation = "";
                    // force load of imported schema
                    if (dir instanceof XSDImportImpl) {
                        ((XSDImportImpl) dir).importSchema();
                    }
                    if (dir.getResolvedSchema() != null) {
                        schemaLocation = URIHelper.removePlatformResourceProtocol(dir.getResolvedSchema().getSchemaLocation());
                        if (schemaLocation != null) {
                            OpenOnSelectionHelper.openXSDEditor(dir.getResolvedSchema());
                        }
                    }
                }
            }
        }
    }
}
Also used : XSDSchemaDirectiveAdapter(org.eclipse.wst.xsd.ui.internal.adapters.XSDSchemaDirectiveAdapter) XSDSchemaDirective(org.eclipse.xsd.XSDSchemaDirective) XSDImportImpl(org.eclipse.xsd.impl.XSDImportImpl) IGraphElement(org.eclipse.wst.xsd.ui.internal.adt.design.editparts.model.IGraphElement) LocationRequest(org.eclipse.gef.requests.LocationRequest) Point(org.eclipse.draw2d.geometry.Point)

Example 4 with XSDImportImpl

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

the class Util method addImport.

private static void addImport(XSDSchema xsdSchema, List<XSDImport> imports) {
    Map<String, String> nsMap = xsdSchema.getQNamePrefixToNamespaceMap();
    int imp = 0;
    for (XSDImport xsdImport : imports) {
        String ns = xsdImport.getNamespace();
        if (ns.equals("")) {
            // $NON-NLS-1$
            continue;
        }
        // $NON-NLS-1$
        int last = ns.lastIndexOf("/");
        if (!nsMap.containsValue(ns)) {
            if (ns.equals("http://www.w3.org/XML/1998/namespace")) {
                // $NON-NLS-1$
                // $NON-NLS-1$
                nsMap.put("xml", ns);
            } else {
                // $NON-NLS-1$//$NON-NLS-2$
                nsMap.put(ns.substring(last + 1).replaceAll("[\\W]", ""), ns);
            }
        }
        boolean exist = false;
        for (XSDSchemaContent cnt : xsdSchema.getContents()) {
            if (cnt instanceof XSDImportImpl) {
                XSDImportImpl cntImpl = (XSDImportImpl) cnt;
                if (cntImpl.getNamespace().equals(ns)) {
                    exist = true;
                    break;
                }
            }
        }
        if (!exist) {
            xsdSchema.getContents().add(imp++, xsdImport);
        }
    }
    xsdSchema.updateElement();
}
Also used : XSDImportImpl(org.eclipse.xsd.impl.XSDImportImpl) XSDSchemaContent(org.eclipse.xsd.XSDSchemaContent) XSDImport(org.eclipse.xsd.XSDImport)

Example 5 with XSDImportImpl

use of org.eclipse.xsd.impl.XSDImportImpl 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)

Aggregations

XSDImportImpl (org.eclipse.xsd.impl.XSDImportImpl)5 XSDImport (org.eclipse.xsd.XSDImport)3 IFile (org.eclipse.core.resources.IFile)2 IEditorInput (org.eclipse.ui.IEditorInput)2 XSDSchemaDirectiveAdapter (org.eclipse.wst.xsd.ui.internal.adapters.XSDSchemaDirectiveAdapter)2 XSDInclude (org.eclipse.xsd.XSDInclude)2 XSDRedefine (org.eclipse.xsd.XSDRedefine)2 XSDSchema (org.eclipse.xsd.XSDSchema)2 XSDSchemaContent (org.eclipse.xsd.XSDSchemaContent)2 XSDSchemaDirective (org.eclipse.xsd.XSDSchemaDirective)2 IPath (org.eclipse.core.runtime.IPath)1 Path (org.eclipse.core.runtime.Path)1 Point (org.eclipse.draw2d.geometry.Point)1 URI (org.eclipse.emf.common.util.URI)1 LocationRequest (org.eclipse.gef.requests.LocationRequest)1 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)1 ViewerFilter (org.eclipse.jface.viewers.ViewerFilter)1 WizardDialog (org.eclipse.jface.wizard.WizardDialog)1 Shell (org.eclipse.swt.widgets.Shell)1