From 8894f0b3c2015e677478b922a581063ed7d5c985 Mon Sep 17 00:00:00 2001 From: Pierre-Charles David Date: Wed, 16 Nov 2022 21:59:24 +0100 Subject: [PATCH] [cleanup] Remove dependency on javax.xml.bind.DatatypeConverter Signed-off-by: Pierre-Charles David --- .../META-INF/MANIFEST.MF | 3 +- .../emfjson/utils/GsonEObjectSerializer.java | 28 ++++++++++++++++--- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/bundles/org.eclipse.sirius.emfjson/META-INF/MANIFEST.MF b/bundles/org.eclipse.sirius.emfjson/META-INF/MANIFEST.MF index a72facb..f562b93 100644 --- a/bundles/org.eclipse.sirius.emfjson/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.sirius.emfjson/META-INF/MANIFEST.MF @@ -9,8 +9,7 @@ Bundle-Localization: bundle Export-Package: org.eclipse.sirius.emfjson.resource;version="1.1.0", org.eclipse.sirius.emfjson.resource.exception;version="1.0.0", org.eclipse.sirius.emfjson.utils;version="2.1.0" -Import-Package: javax.xml.bind;version="0.0.0", - com.google.gson;version="2.2.4", +Import-Package: com.google.gson;version="2.2.4", com.google.gson.reflect;version="2.2.4", com.google.gson.stream;version="2.2.4", org.eclipse.emf.common.util;version="0.0.0", diff --git a/bundles/org.eclipse.sirius.emfjson/src/main/java/org/eclipse/sirius/emfjson/utils/GsonEObjectSerializer.java b/bundles/org.eclipse.sirius.emfjson/src/main/java/org/eclipse/sirius/emfjson/utils/GsonEObjectSerializer.java index 278a325..8fbb7fb 100644 --- a/bundles/org.eclipse.sirius.emfjson/src/main/java/org/eclipse/sirius/emfjson/utils/GsonEObjectSerializer.java +++ b/bundles/org.eclipse.sirius.emfjson/src/main/java/org/eclipse/sirius/emfjson/utils/GsonEObjectSerializer.java @@ -31,8 +31,6 @@ import java.util.Map; import java.util.Map.Entry; -import javax.xml.bind.DatatypeConverter; - import org.eclipse.emf.common.util.EList; import org.eclipse.emf.common.util.EMap; import org.eclipse.emf.common.util.URI; @@ -90,6 +88,11 @@ public class GsonEObjectSerializer implements JsonSerializer> { */ private static final int CROSS_DOC = 2; + /** + * Hexadecimal digits. + */ + private static final char[] DIGITS = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; + /** * the helper. */ @@ -1057,20 +1060,37 @@ private JsonElement serializeEByteArrayEAttribute(EObject eObject, EAttribute eA Collection collection = (Collection) value; for (Object object : collection) { if (object instanceof byte[]) { - jsonArray.add(new JsonPrimitive(DatatypeConverter.printHexBinary((byte[]) object))); + jsonArray.add(new JsonPrimitive(this.toHexString((byte[]) object))); } } } jsonElement = jsonArray; } else { if (value instanceof byte[]) { - jsonElement = new JsonPrimitive(DatatypeConverter.printHexBinary((byte[]) value)); + jsonElement = new JsonPrimitive(this.toHexString((byte[]) value)); } } return jsonElement; } + /** + * Converts an array of bytes into an hexadecimal string. + * + * @param source + * the source bytes. + * @return an hexadecimal representation of the source bytes. + */ + private String toHexString(byte[] source) { + char[] target = new char[source.length * 2]; + for (int i = 0; i < source.length; i++) { + byte b = source[i]; + target[2 * i] = DIGITS[(b >> 4) & 0xf]; + target[2 * i + 1] = DIGITS[b & 0x0f]; + } + return new String(target); + } + /** * Returns the JsonElement representing an EDate. *