use of org.eclipse.xsd.XSDSchemaContent 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.XSDSchemaContent in project tmdm-studio-se by Talend.
the class XSDUtil method buildEntityUsedComplexTypeMap.
public static Map<XSDElementDeclaration, List<XSDComplexTypeDefinition>> buildEntityUsedComplexTypeMap(XSDSchema schema) {
Map<XSDElementDeclaration, List<XSDComplexTypeDefinition>> entityMapComplexType = new HashMap<XSDElementDeclaration, List<XSDComplexTypeDefinition>>();
EList<XSDSchemaContent> contents = schema.getContents();
for (XSDSchemaContent content : contents) {
if (content instanceof XSDElementDeclaration) {
XSDElementDeclaration concept = (XSDElementDeclaration) content;
XSDComplexTypeDefinition ctypeDef = (XSDComplexTypeDefinition) concept.getTypeDefinition();
List<XSDComplexTypeDefinition> ctypes = new ArrayList<XSDComplexTypeDefinition>();
ctypes.add(ctypeDef);
while (ctypeDef.getBaseTypeDefinition() != ((XSDComplexTypeDefinition) ctypeDef.getBaseTypeDefinition()).getBaseTypeDefinition()) {
ctypes.add((XSDComplexTypeDefinition) ctypeDef.getBaseTypeDefinition());
ctypeDef = (XSDComplexTypeDefinition) ctypeDef.getBaseTypeDefinition();
}
entityMapComplexType.put(concept, ctypes);
}
}
return entityMapComplexType;
}
use of org.eclipse.xsd.XSDSchemaContent in project tmdm-studio-se by Talend.
the class UtilMockTest method testAddImport.
@Test
public void testAddImport() {
// 2184
// $NON-NLS-1$
String namespace1 = "http://www.w3.org/XML/1998/namespace1";
// $NON-NLS-1$
String namespace2 = "http://www.w3.org/XML/1998/namespace2";
// $NON-NLS-1$
String namespace3 = "http://www.w3.org/XML/1998/namespace3";
String[] namespaces = { namespace1, namespace2, namespace3 };
// $NON-NLS-1$
String methodToExecute = "addImport";
XSDFactory factory = XSDFactory.eINSTANCE;
XSDSchema xsdschema = factory.createXSDSchema();
List<XSDImport> imports = new ArrayList<XSDImport>();
for (int i = 0; i < 3; i++) {
XSDImport importt = factory.createXSDImport();
importt.setNamespace(namespaces[i]);
imports.add(importt);
}
XSDImport importt = factory.createXSDImport();
// $NON-NLS-1$
importt.setNamespace("");
imports.add(importt);
xsdschema.getContents().add(imports.get(0));
PowerMockito.mockStatic(Util.class);
try {
PowerMockito.when(Util.class, methodToExecute, any(XSDSchema.class), anyListOf(XSDImport.class)).thenCallRealMethod();
Whitebox.invokeMethod(Util.class, methodToExecute, xsdschema, imports);
EList<XSDSchemaContent> contents = xsdschema.getContents();
assertTrue(contents.size() == 3);
assertTrue(contents.contains(imports.get(0)));
assertTrue(contents.contains(imports.get(1)));
assertTrue(contents.contains(imports.get(2)));
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
Aggregations