From bccec26d8d7217dfda325e43b01fefd3605d8e1b Mon Sep 17 00:00:00 2001 From: jding-tlnd Date: Wed, 17 Aug 2022 18:06:50 +0800 Subject: [PATCH] feat(TUP-35646):Research : tMap - possible payload to send to PTP https://jira.talendforge.org/browse/TUP-35646 --- .../runtime/model/emf/CustomXMIResource.java | 77 +++++++++++++++++++ .../commons/runtime/model/emf/EmfHelper.java | 18 ++--- 2 files changed, 86 insertions(+), 9 deletions(-) create mode 100644 main/plugins/org.talend.commons.runtime/src/org/talend/commons/runtime/model/emf/CustomXMIResource.java diff --git a/main/plugins/org.talend.commons.runtime/src/org/talend/commons/runtime/model/emf/CustomXMIResource.java b/main/plugins/org.talend.commons.runtime/src/org/talend/commons/runtime/model/emf/CustomXMIResource.java new file mode 100644 index 0000000000..e5d250673e --- /dev/null +++ b/main/plugins/org.talend.commons.runtime/src/org/talend/commons/runtime/model/emf/CustomXMIResource.java @@ -0,0 +1,77 @@ +// ============================================================================ +// +// Copyright (C) 2006-2022 Talend Inc. - www.talend.com +// +// This source code is available under agreement available at +// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt +// +// You should have received a copy of the agreement +// along with this program; if not, write to Talend SA +// 9 rue Pages 92150 Suresnes, France +// +// ============================================================================ +package org.talend.commons.runtime.model.emf; + +import java.util.Iterator; +import java.util.Map; +import java.util.StringJoiner; + +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.xmi.XMLHelper; +import org.eclipse.emf.ecore.xmi.XMLResource; +import org.eclipse.emf.ecore.xmi.XMLSave; +import org.eclipse.emf.ecore.xmi.impl.XMISaveImpl; +import org.eclipse.emf.ecore.xmi.impl.XMLString; + +/** + * DOC jding class global comment. Detailled comment + */ +public class CustomXMIResource extends TalendXMIResource { + + CustomXMISave xmiSave; + + public CustomXMIResource() { + super(); + } + + public CustomXMIResource(URI uri) { + super(uri); + } + + @Override + protected XMLSave createXMLSave() { + xmiSave = new CustomXMISave(createXMLHelper()); + return xmiSave; + } + + public String getResourceContent(Map options) { + if (xmiSave == null) { + createXMLSave(); + } + return xmiSave.getXMLContent(this, options); + } + +} + +class CustomXMISave extends XMISaveImpl { + + public CustomXMISave(XMLHelper helper) { + super(helper); + } + + public String getXMLContent(XMLResource resource, Map options) { + StringJoiner strJoin = new StringJoiner(""); + super.init(resource, options); + super.traverse(resource.getContents()); + XMLString xmlString = this.doc; + Iterator iterator = xmlString.stringIterator(); + while (iterator.hasNext()) { + String string = (String) iterator.next(); + if (string != null) { + strJoin.add(string); + } + } + return strJoin.toString(); + } + +} diff --git a/main/plugins/org.talend.commons.runtime/src/org/talend/commons/runtime/model/emf/EmfHelper.java b/main/plugins/org.talend.commons.runtime/src/org/talend/commons/runtime/model/emf/EmfHelper.java index 5d1c8eba38..4f2a421693 100644 --- a/main/plugins/org.talend.commons.runtime/src/org/talend/commons/runtime/model/emf/EmfHelper.java +++ b/main/plugins/org.talend.commons.runtime/src/org/talend/commons/runtime/model/emf/EmfHelper.java @@ -16,6 +16,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; +import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.List; @@ -177,15 +178,6 @@ public class EmfHelper { resource.save(null); } - public static Resource saveEmfModel(EPackage pkg, EObject model, String file) throws IOException { - ResourceSet resourceSet = getResourceSet(pkg); - URI uri = URI.createFileURI(file); - Resource resource = resourceSet.createResource(uri); - resource.getContents().add(model); - resource.save(null); - return resource; - } - private static ResourceSet getResourceSet(EPackage pkg) { ResourceSet resourceSet = new ResourceSetImpl(); @@ -272,4 +264,12 @@ public class EmfHelper { return result; } + public static String getEmfModelContent(EObject model) throws Exception { + String content = ""; + CustomXMIResource xmiResource = new CustomXMIResource(); + xmiResource.getContents().add(model); + content = xmiResource.getResourceContent(Collections.EMPTY_MAP); + return content; + } + }