use of org.eclipse.xsd.XSDImport 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.XSDImport 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);
}
}
}
}
use of org.eclipse.xsd.XSDImport in project tmdm-studio-se by Talend.
the class DataModelMainPage method save.
public int save(String xsd) {
try {
WSDataModel wsObject = (WSDataModel) (xobject.getWsObject());
wsObject.setDescription(desAntionComposite.getText());
String schema = xsd;
if (xsd == null) {
schema = ((SchemaTreeContentProvider) viewer.getContentProvider()).getXSDSchemaAsString();
xsdSchema = ((SchemaTreeContentProvider) viewer.getContentProvider()).getXsdSchema();
}
XSDImport xsdImport = XSDFactory.eINSTANCE.createXSDImport();
// $NON-NLS-1$
xsdImport.setNamespace("http://www.w3.org/2001/XMLSchema");
if (xsdSchema == null) {
xsdSchema = ((SchemaTreeContentProvider) viewer.getContentProvider()).getXsdSchema();
}
if (xsdSchema == null || (xsd != null && !xsd.equals(wsObject.getXsdSchema())) || (xsd != null && !xsd.equals(getXSDSchemaString()))) {
//
xsdSchema = Util.createXsdSchema(schema, xobject);
//
getSchemaContentProvider().setXsdSchema(xsdSchema);
}
wsObject.setXsdSchema(schema);
EList<XSDSchemaContent> elist = xsdSchema.getContents();
for (XSDSchemaContent cnt : elist) {
if (cnt instanceof XSDImport) {
XSDImport imp = (XSDImport) cnt;
if (imp.getNamespace().equals("http://www.w3.org/2001/XMLSchema")) {
// $NON-NLS-1$
xsdImport = null;
break;
}
}
}
if (xsdImport != null) {
xsdSchema.getContents().add(0, xsdImport);
wsObject.setXsdSchema(schema);
}
validateSchema();
// save to db
doSave(wsObject);
dirty = false;
} catch (Exception e) {
dirty = true;
log.error(e.getMessage(), e);
}
firePropertyChange(PROP_DIRTY);
return dirty ? 1 : 0;
}
use of org.eclipse.xsd.XSDImport 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);
}
}
use of org.eclipse.xsd.XSDImport 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();
}
Aggregations