Skip to content

Commit

Permalink
[cleanup] Remove dependency on javax.xml.bind.DatatypeConverter
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre-Charles David <[email protected]>
  • Loading branch information
pcdavid authored and sbegaudeau committed Jan 10, 2024
1 parent 707c106 commit 8894f0b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
3 changes: 1 addition & 2 deletions bundles/org.eclipse.sirius.emfjson/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -90,6 +88,11 @@ public class GsonEObjectSerializer implements JsonSerializer<List<EObject>> {
*/
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.
*/
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 8894f0b

Please sign in to comment.