Java tutorial
/******************************************************************************* * Copyright (c) 2013 Guillaume Hillairet. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Guillaume Hillairet - initial API and implementation *******************************************************************************/ package org.eclipselabs.emfjson.gwt.map; import static org.eclipse.emf.ecore.util.EcoreUtil.convertToString; import static org.eclipselabs.emfjson.gwt.common.ModelUtil.getElementName; import java.util.Date; import java.util.Iterator; import org.eclipse.emf.common.util.EList; import org.eclipse.emf.ecore.EAttribute; import org.eclipse.emf.ecore.EEnum; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EStructuralFeature; import org.eclipse.emf.ecore.util.FeatureMap; import org.eclipse.emf.ecore.util.FeatureMapUtil; import com.google.gwt.i18n.client.DateTimeFormat; import com.google.gwt.json.client.JSONArray; import com.google.gwt.json.client.JSONBoolean; import com.google.gwt.json.client.JSONNumber; import com.google.gwt.json.client.JSONObject; import com.google.gwt.json.client.JSONString; import com.google.gwt.json.client.JSONValue; /** * * @author ghillairet * @since 0.6.0 */ class EAttributeSerializer { EAttributeSerializer() { } void serialize(EObject eObject, JSONObject node) { for (EAttribute eAttribute : eObject.eClass().getEAllAttributes()) { if (isCandidate(eObject, eAttribute)) { if (isFeatureMap(eAttribute)) { serializeFeatureMap(eObject, eAttribute, node); } else if (eAttribute.isMany()) { serializeMany(eObject, eAttribute, node); } else { serializeValue(node, eAttribute, eObject.eGet(eAttribute)); } } } } void serializeMany(EObject eObject, EAttribute eAttribute, JSONObject node) { final EList<?> rawValues = (EList<?>) eObject.eGet(eAttribute); if (!rawValues.isEmpty()) { JSONArray arrayNode = new JSONArray(); node.put(getElementName(eAttribute), arrayNode); for (Object val : rawValues) { serializeValue(arrayNode, eAttribute, val); } } } void serializeFeatureMap(EObject eObject, EAttribute eAttribute, JSONObject node) { final FeatureMap.Internal featureMap = (FeatureMap.Internal) eObject.eGet(eAttribute); final Iterator<FeatureMap.Entry> iterator = featureMap.basicIterator(); while (iterator.hasNext()) { FeatureMap.Entry entry = iterator.next(); EStructuralFeature feature = entry.getEStructuralFeature(); if (feature instanceof EAttribute) { serializeValue(node, (EAttribute) feature, entry.getValue()); } } } static boolean isCandidate(EObject eObject, EAttribute eAttribute) { return (eObject.eIsSet(eAttribute) || eAttribute.getEType() instanceof EEnum) && !eAttribute.isDerived() && !eAttribute.isTransient() && !eAttribute.isUnsettable(); } boolean isFeatureMap(EAttribute eAttribute) { return FeatureMapUtil.isFeatureMap(eAttribute); } void serializeValue(JSONValue node, EAttribute attribute, Object value) { if (value == null) return; String key = getElementName(attribute); if (value instanceof Integer) { serializeInteger(node, key, (Integer) value); } else if (value instanceof Boolean) { serializeBoolean(node, key, (Boolean) value); } else if (value instanceof Date) { serializeDate(node, key, (Date) value); } else if (value instanceof Double) { serializeDouble(node, key, (Double) value); } else if (value instanceof Long) { serializeLong(node, key, (Long) value); } else if (value instanceof Short) { serializeShort(node, key, (Short) value); } else if (value instanceof Float) { serializeFloat(node, key, (Float) value); } else { serializeString(node, key, convertToString(attribute.getEAttributeType(), value)); } } void serializeString(JSONValue node, String key, String value) { if (node.isObject() != null) { node.isObject().put(key, new JSONString(value)); } else { node.isArray().set(node.isArray().size(), new JSONString(value)); } } void serializeBoolean(JSONValue node, String key, Boolean value) { if (node.isObject() != null) { node.isObject().put(key, JSONBoolean.getInstance(value)); } else { node.isArray().set(node.isArray().size(), JSONBoolean.getInstance(value)); } } void serializeInteger(JSONValue node, String key, Integer value) { if (node.isObject() != null) { node.isObject().put(key, new JSONNumber(value)); } else { node.isArray().set(node.isArray().size(), new JSONNumber(value)); } } void serializeDouble(JSONValue node, String key, Double value) { if (node.isObject() != null) { node.isObject().put(key, new JSONNumber(value)); } else { node.isArray().set(node.isArray().size(), new JSONNumber(value)); } } void serializeLong(JSONValue node, String key, Long value) { if (node.isObject() != null) { node.isObject().put(key, new JSONNumber(value)); } else { node.isArray().set(node.isArray().size(), new JSONNumber(value)); } } void serializeShort(JSONValue node, String key, Short value) { if (node.isObject() != null) { node.isObject().put(key, new JSONNumber(value)); } else { node.isArray().set(node.isArray().size(), new JSONNumber(value)); } } void serializeFloat(JSONValue node, String key, Float value) { if (node.isObject() != null) { node.isObject().put(key, new JSONNumber(value)); } else { node.isArray().set(node.isArray().size(), new JSONNumber(value)); } } void serializeDate(JSONValue node, String key, Date value) { DateTimeFormat formatter = DateTimeFormat.getFormat("yyyy-MM-dd'T'HH:mm:ss"); String dateValue = formatter.format(value); if (node.isObject() != null) { node.isObject().put(key, new JSONString(dateValue)); } else { node.isArray().set(node.isArray().size(), new JSONString(dateValue)); } } }