use of com.amalto.workbench.models.TreeObject in project tmdm-studio-se by Talend.
the class Util method getXSDSchema.
private static XSDSchema getXSDSchema(String namespaceURI, String rawData, final List<XSDImport> imports, final TreeObject treeObj, boolean uri, final List<Exception> exceptions, final Map<String, Integer> schemaMonitor) throws Exception {
FileInputStream fin = null;
try {
// $NON-NLS-1$//$NON-NLS-2$
final String xsdFileName = System.getProperty("user.dir") + "/.xsdModel.xml";
URI fileURI = URI.createFileURI(xsdFileName);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
documentBuilderFactory.setValidating(false);
DocumentBuilder documentBuilder;
XSDSchema schema = null;
InputSource source = null;
Document document = null;
String schemaLocation = rawData;
documentBuilder = documentBuilderFactory.newDocumentBuilder();
if (rawData == null) {
// $NON-NLS-1$
return XSDSchemaImpl.getSchemaForSchema("http://www.w3.org/2001/XMLSchema");
}
if (namespaceURI == null && rawData.endsWith(".xsd") && rawData.indexOf(File.separator) > 0) {
// $NON-NLS-1$
File rawFile = new File(rawData);
if (!rawFile.exists()) {
throw new IllegalArgumentException(rawData);
}
}
// import namespace="http://xxx" schemaLocation="xxxx"
if (namespaceURI != null && schemaLocation.endsWith(".xsd")) {
// $NON-NLS-1$
// $NON-NLS-1$
URL url = new java.net.URI(namespaceURI + "/" + rawData).toURL();
uri = false;
rawData = IOUtils.toString(url.openConnection().getInputStream());
// $NON-NLS-1$//$NON-NLS-2$
rawData = rawData.replaceAll("<!DOCTYPE(.*?)>", "");
}
if (rawData.equals("http://www.w3.org/2001/03/xml.xsd")) {
// $NON-NLS-1$
// $NON-NLS-1$
URL url = new java.net.URI("http://www.w3.org/2001/03/xml.xsd").toURL();
uri = false;
rawData = IOUtils.toString(url.openConnection().getInputStream());
// $NON-NLS-1$//$NON-NLS-2$
rawData = rawData.replaceAll("<!DOCTYPE(.*?)>", "");
}
if (uri) {
File file = new File(rawData);
if (file.exists()) {
fin = new FileInputStream(file);
source = new InputSource(fin);
} else {
source = new InputSource(new StringReader(Util.getResponseFromURL(rawData, treeObj)));
}
} else {
source = new InputSource(new StringReader(rawData));
}
try {
document = documentBuilder.parse(source);
} catch (SAXParseException ex) {
exceptions.add(ex);
return null;
}
schema = XSDSchemaImpl.createSchema(document.getDocumentElement());
ResourceSet resourceSet = new ResourceSetImpl();
Resource resource = resourceSet.createResource(fileURI);
resourceSet.getAdapterFactories().add(new AdapterFactoryImpl() {
class SchemaLocator extends AdapterImpl implements XSDSchemaLocator {
public XSDSchema locateSchema(XSDSchema xsdSchema, String namespaceURI, String rawSchemaLocationURI, String resolvedSchemaLocation) {
XSDSchema schema;
Integer rawCnt = schemaMonitor.get(rawSchemaLocationURI);
if (rawCnt == null) {
rawCnt = 0;
} else {
rawCnt++;
}
schemaMonitor.put(rawSchemaLocationURI, rawCnt);
if (rawCnt >= 10) {
schemaMonitor.put(rawSchemaLocationURI, -1);
return null;
}
try {
schema = Util.getXSDSchema(namespaceURI, rawSchemaLocationURI, imports, treeObj, true, exceptions, schemaMonitor);
} catch (Exception e) {
return XSDSchemaImpl.getSchemaForSchema(namespaceURI);
}
schema.setTargetNamespace(namespaceURI);
schema.setElement(schema.getDocument().getDocumentElement());
return schema;
}
public boolean isAdatperForType(Object type) {
return type == XSDSchemaLocator.class;
}
}
protected SchemaLocator schemaLocator = new SchemaLocator();
@Override
public boolean isFactoryForType(Object type) {
return type == XSDSchemaLocator.class;
}
@Override
public Adapter adaptNew(Notifier target, Object type) {
return schemaLocator;
}
});
// import namespace="http://xxx" schemaLocation="xxxx"
if (namespaceURI != null && schemaLocation.endsWith(".xsd")) {
// $NON-NLS-1$
schema.setSchemaLocation(schemaLocation);
} else {
schema.setSchemaLocation(fileURI.toString());
// set the schema for schema QName prefix to "xsd"
// $NON-NLS-1$
schema.setSchemaForSchemaQNamePrefix("xsd");
}
// catch up the NPE to make sure data model can still run in case of unknown conflict
try {
resource.getContents().add(schema);
} catch (Exception ex) {
log.error(ex.getMessage(), ex);
}
// Add the root schema to the resource that was created above
Iterator<Integer> iter = schemaMonitor.values().iterator();
while (iter.hasNext()) {
Integer it = iter.next();
if (it.intValue() == -1) {
return schema;
}
}
importSchema(schema, imports, schemaMonitor);
schema.setElement(document.getDocumentElement());
return schema;
} finally {
if (fin != null) {
fin.close();
}
}
}
use of com.amalto.workbench.models.TreeObject in project tmdm-studio-se by Talend.
the class Bean2EObjUtilTest method testWrapEObjWithTreeObject1Args.
/**
* test wrapEObjWithTreeObject with one input parameter
*/
@Test
public void testWrapEObjWithTreeObject1Args() {
EObject eobj = null;
Bean2EObjUtil util = Bean2EObjUtil.getInstance();
TreeObject treeObject = util.wrapEObjWithTreeObject(eobj);
assertNull(treeObject);
MDMServerObject mdmServerObject = PowerMockito.mock(MDMServerObject.class);
when(mdmServerObject.getName()).thenReturn("name");
when(mdmServerObject.getType()).thenReturn(2);
eobj = mdmServerObject;
treeObject = util.wrapEObjWithTreeObject(eobj);
assertNotNull(treeObject);
Mockito.verify(mdmServerObject, Mockito.atLeastOnce()).getName();
Mockito.verify(mdmServerObject, Mockito.atLeastOnce()).getType();
assertEquals("name", treeObject.getName());
assertEquals(2, treeObject.getType());
}
use of com.amalto.workbench.models.TreeObject in project tmdm-studio-se by Talend.
the class ResourcesUtilTest method testGetResourcesNameListFromURI.
@Test
public void testGetResourcesNameListFromURI() {
// $NON-NLS-1$
String method_private_getXMLString = "getXMLString";
// $NON-NLS-1$
String method_private_parsXMLString = "parsXMLString";
// $NON-NLS-1$
String responseBody = "responseBody";
// $NON-NLS-1$
String uri = "uri";
TreeObject treeObj = new TreeObject();
try {
PowerMockito.mockStatic(ResourcesUtil.class);
PowerMockito.when(ResourcesUtil.class, method_private_getXMLString, anyString(), any(TreeObject.class)).thenReturn(responseBody);
Document doc = null;
PowerMockito.when(ResourcesUtil.class, method_private_parsXMLString, anyString()).thenReturn(doc);
PowerMockito.when(ResourcesUtil.getResourcesNameListFromURI(anyString(), any(TreeObject.class))).thenCallRealMethod();
List<String> resourcesNameList = ResourcesUtil.getResourcesNameListFromURI(uri, treeObj);
assertTrue(resourcesNameList.isEmpty());
doc = DocumentHelper.createDocument();
// $NON-NLS-1$
Element rootElement = DocumentHelper.createElement("root");
doc.setRootElement(rootElement);
PowerMockito.when(ResourcesUtil.class, method_private_parsXMLString, anyString()).thenReturn(doc);
boolean[][] ownNameURIElement = { { true, false }, { false, false } };
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
String[][] localNameUris = { { "name0", "uri0" }, { "name1", "uri1" } };
// $NON-NLS-1$
String entry = "entry";
for (int i = 0; i < ownNameURIElement.length; i++) {
createChildElement(rootElement, entry + i, localNameUris[i], ownNameURIElement[i][0], ownNameURIElement[i][1]);
}
resourcesNameList = ResourcesUtil.getResourcesNameListFromURI(uri, treeObj);
assertNotNull(resourcesNameList);
assertEquals(2, resourcesNameList.size());
List<String> resourcesNamesList = new ArrayList<String>();
// $NON-NLS-1$
resourcesNamesList.add("name0");
// $NON-NLS-1$
resourcesNamesList.add("entry1");
assertEquals(resourcesNamesList, resourcesNameList);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
use of com.amalto.workbench.models.TreeObject in project tmdm-studio-se by Talend.
the class XObjectBrowserInput2 method getToolTipText.
@Override
public String getToolTipText() {
TreeObject xobject = (TreeObject) getModel();
// $NON-NLS-1$
String sep = " - ";
String serverName = null;
if (serverDef != null) {
serverName = serverDef.getName();
} else {
serverName = xobject.getServerRoot().getName();
}
if (xobject.getType() == TreeObject.SUBSCRIPTION_ENGINE) {
return serverName + sep + Messages.XObjectBrowserInput2_eventmanager;
} else if (xobject.getType() == TreeObject.DATA_CLUSTER) {
return serverName + sep + TreeObject.getTypeName(xobject.getType()) + sep + xobject.getDisplayName();
} else {
return TreeObject.getTypeName(xobject.getType()) + sep + xobject.getDisplayName();
}
}
use of com.amalto.workbench.models.TreeObject in project tmdm-studio-se by Talend.
the class XObjectEditor2 method saveResourceToRepository.
private boolean saveResourceToRepository() {
XObjectEditorInput2 editorInput = (XObjectEditorInput2) this.getEditorInput();
TreeObject xobject = (TreeObject) editorInput.getModel();
IRepositoryViewObject viewObject = editorInput.getViewObject();
IRepositoryViewObject refreshViewObject = RepositoryResourceUtil.assertViewObject(viewObject);
if (viewObject != refreshViewObject) {
editorInput.updateViewObject(refreshViewObject);
}
MDMServerObjectItem serverObjectItem = (MDMServerObjectItem) editorInput.getInputItem();
MDMServerObject serverObject = serverObjectItem.getMDMServerObject();
EObject eObj = Bean2EObjUtil.getInstance().convertFromBean2EObj(xobject.getWsObject(), serverObject);
if (eObj != null) {
IProxyRepositoryFactory factory = CoreRuntimePlugin.getInstance().getProxyRepositoryFactory();
try {
factory.save(serverObjectItem);
// TODO should call the following,but the page in editor has many call to remote webService ,it will
// search ServerRoot which cause a NPE
// xobject.fireEvent(IXObjectModelListener.SAVE, xobject.getParent(), xobject);
editorDirtyStateChanged();
refreshDirtyCue();
DeployService deployService = DeployService.getInstance();
if (deployService.isAutoDeploy()) {
autoDeployProcess(deployService);
} else {
MDMServerDef lastServerDef = RepositoryResourceUtil.getLastServerDef(serverObjectItem);
if (lastServerDef != null) {
CommandManager.getInstance().pushCommand(ICommand.CMD_MODIFY, editorInput.getViewObject());
}
}
return true;
} catch (PersistenceException e) {
log.error(e.getMessage(), e);
}
}
return false;
}
Aggregations