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