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);
}
}
}
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);
}
}
}
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());
}
}
}
}
}
}
}
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();
}
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);
}
}
}
Aggregations