Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1f02877e3c1570d27fcb4a3076d29443168ff5c6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*******************************************************************************
 * Copyright (c) 2011 itemis AG (http://www.itemis.eu) and others.
 * 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
 *******************************************************************************/
package org.eclipse.emf.ecore.xcore.tests;

import java.util.List;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EFactory;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.util.EcoreUtil;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Iterables;

/**
 * @author Moritz Eysholdt - Initial contribution and API
 * 
 *         TODO: move this to xtext.util
 */
public class EObjectFormatter implements Function<EObject, String>
{

	protected boolean resolveCrossReferences = false;

	protected boolean showIndex = false;

	public String apply(EObject from)
	{
		return format(from);
	}

	protected String assignmentOperator(EObject object, EStructuralFeature feature)
	{
		if (feature instanceof EReference && !((EReference) feature).isContainment())
			return "->";
		else
			return "=";
	}

	public String format(EObject object)
	{
		if (object == null)
			return "null";
		StringBuilder result = new StringBuilder();
		result.append(object.eClass().getName());
		result.append(" {");
		for (EStructuralFeature feature : object.eClass().getEAllStructuralFeatures())
			if (shouldFormat(object, feature))
				result.append(indent("\n" + format(object, feature)));
		result.append("\n}");
		return result.toString();
	}

	protected String format(EObject object, EStructuralFeature feature)
	{
		StringBuilder result = new StringBuilder();
		result.append(feature.getName());
		result.append(" ");
		result.append(assignmentOperator(object, feature));
		result.append(" ");
		Object val = object.eGet(feature, resolveCrossReferences);
		if (feature.isMany())
		{
			result.append("[");
			List<?> vals = (List<?>) val;
			List<?> sortedVals = sort(object, feature, vals);
			if (vals == sortedVals)
			{
				for (int i = 0; i < vals.size(); i++)
					if (shouldFormat(object, feature, i, vals.get(i)))
						result.append(indent("\n" + format(object, feature, i, vals.get(i))));
			} else
			{
				for (int i = 0; i < sortedVals.size(); i++)
					if (shouldFormat(object, feature, sortedVals.get(i)))
						result.append(indent("\n" + format(object, feature, sortedVals.get(i))));
			}
			result.append("\n]");
		} else
			result.append(indent(format(object, feature, val)));
		return result.toString();
	}

	protected String format(EObject object, EStructuralFeature feature, int index, Object value)
	{
		if (showIndex)
			return index + ": " + format(object, feature, value);
		return format(object, feature, value);
	}

	protected String format(EObject object, EStructuralFeature feature, Object value)
	{
		if (feature instanceof EAttribute)
			return formatAttributeValue(object, (EAttribute) feature, value);
		else if (feature instanceof EReference)
		{
			EReference ref = (EReference) feature;
			if (ref.isContainment())
				return format((EObject) value);
			return formatCrossRefValue(object, ref, (EObject) value);
		}
		return "";
	}

	public String format(Iterable<? extends EObject> object)
	{
		return Joiner.on('\n').join(Iterables.transform(object, this));
	}

	protected String formatAttributeValue(EObject object, EAttribute feature, Object value)
	{
		if (value == null)
			return "null";
		EFactory factory = feature.getEAttributeType().getEPackage().getEFactoryInstance();
		String stringVal = factory.convertToString(feature.getEAttributeType(), value);
		return "'" + stringVal + "'";
	}

	protected String formatCrossRefValue(EObject object, EReference feature, EObject value)
	{
		if (value == null)
			return "null";
		if (value.eIsProxy())
			return "proxy (URI: " + ((InternalEObject) value).eProxyURI() + ")";
		if (value.eResource() == object.eResource())
			return value.eClass().getName() + " " + object.eResource().getURIFragment(value);
		URI uri = EcoreUtil.getURI(value);
		uri = uri.deresolve(object.eResource().getURI());
		return value.eClass().getName() + " " + uri.toString();
	}

	protected String indent(String string)
	{
		return string.replaceAll("\\n", "\n\t");
	}

	public EObjectFormatter resolveCrossReferences()
	{
		this.resolveCrossReferences = true;
		return this;
	}

	protected boolean shouldFormat(EObject object, EStructuralFeature feature)
	{
		return !feature.isDerived() && object.eIsSet(feature);
	}

	protected boolean shouldFormat(EObject object, EStructuralFeature feature, int index, Object value)
	{
		return true;
	}

	protected boolean shouldFormat(EObject object, EStructuralFeature feature, Object value)
	{
		return true;
	}

	public EObjectFormatter showIndex()
	{
		this.showIndex = true;
		return this;
	}

	protected List<?> sort(EObject obj, EStructuralFeature feature, List<?> values)
	{
		return values;
	}

}

Back to the top