Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 82e0dbfc4848210c13e64adae11b7274a893ff5d (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*******************************************************************************
 * Copyright (c) 2008 BEA Systems, Inc. 
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    wharley@bea.com - initial API and implementation
 *    
 *******************************************************************************/

package org.eclipse.jdt.compiler.apt.tests.processors.base;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import java.util.Map;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.TypeParameterElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.ElementScanner6;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * Generate an XML representation of a language model.
 * Changes to this class will generally require changes to
 * the XMLComparer class (which compares documents generated
 * by this class) and possibly to the reference models of
 * various tests.
 * 
 * @since 3.4
 */
public class XMLConverter extends ElementScanner6<Void, Node> implements IXMLNames {
	
	private final Document _doc;
	@Deprecated
	private XMLConverter(Document doc) {
		_doc = doc;
	}
	
	/**
	 * Convert an XML DOM document to a canonical string representation
	 */
	public static String xmlToString(Document model) {
		StringWriter s = new StringWriter();
		DOMSource domSource = new DOMSource(model);
		StreamResult streamResult = new StreamResult(s);
		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer serializer;
		try {
			serializer = tf.newTransformer();
			serializer.setOutputProperty(OutputKeys.INDENT, "yes");
			serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "1");
			serializer.transform(domSource, streamResult); 
		} catch (Exception e) {
			e.printStackTrace(new PrintWriter(s));
		}
		return s.toString();
	}

	/**
	 * Convert an XML DOM document to a string representation suitable for paste
	 * into a processor written in Java.
	 */
	// derived from org.eclipse.jdt.core.tests.util.Util#displayString
	public static String xmlToCutAndPasteString(Document model, int indent, boolean shift) {
		String modelAsString = xmlToString(model);
	    int length = modelAsString.length();
	    StringBuffer buffer = new StringBuffer(length);
	    java.util.StringTokenizer tokenizer = new java.util.StringTokenizer(modelAsString, "\n\r", true);
	    for (int i = 0; i < indent; i++) buffer.append("\t");
	    if (shift) indent++;
	    buffer.append("\"");
	    while (tokenizer.hasMoreTokens()){
	        String token = tokenizer.nextToken();
	        if (token.equals("\n")) {
	            buffer.append("\\n");
	            if (tokenizer.hasMoreTokens()) {
	                buffer.append("\" + \n");
	                for (int i = 0; i < indent; i++) buffer.append("\t");
	                buffer.append("\"");
	            }
	            continue;
	        }
	        StringBuffer tokenBuffer = new StringBuffer();
	        for (int i = 0; i < token.length(); i++){
	            char c = token.charAt(i);
	            switch (c) {
	                case '\r' :
	                    break;
	                case '\n' :
	                    tokenBuffer.append("\\n");
	                    break;
	                case '\b' :
	                    tokenBuffer.append("\\b");
	                    break;
	                case '\t' :
	                    tokenBuffer.append("\t");
	                    break;
	                case '\f' :
	                    tokenBuffer.append("\\f");
	                    break;
	                case '\"' :
	                    tokenBuffer.append("\\\"");
	                    break;
	                case '\'' :
	                    tokenBuffer.append("\\'");
	                    break;
	                case '\\' :
	                    tokenBuffer.append("\\\\");
	                    break;
	                default :
	                    tokenBuffer.append(c);
	            }
	        }
	        buffer.append(tokenBuffer.toString());
	    }
	    buffer.append("\"");
	    return buffer.toString();
	}
	
	/**
	 * Recursively convert a collection of language elements (declarations) into an XML representation.
	 * @param declarations the collection of language elements to convert
	 * @return an XML document whose root node is named &lt;model&gt;.
	 * @throws ParserConfigurationException
	 */
	public static Document convertModel(Iterable<? extends javax.lang.model.element.Element> declarations) throws ParserConfigurationException {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		Document model = factory.newDocumentBuilder().newDocument();
		org.w3c.dom.Element modelNode = model.createElement(MODEL_TAG);
		
		XMLConverter converter = new XMLConverter(model);
		converter.scan(declarations, modelNode);
		model.appendChild(modelNode);
		return model;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.lang.model.util.ElementScanner6#visitExecutable(javax.lang.model.element.ExecutableElement,
	 *      java.lang.Object)
	 */
	@Override
	public Void visitExecutable(ExecutableElement e, Node target) {
		Element executableNode = _doc.createElement(EXECUTABLE_ELEMENT_TAG);
		executableNode.setAttribute(KIND_TAG, e.getKind().name());
		executableNode.setAttribute(SNAME_TAG, e.getSimpleName().toString());

		convertAnnotationMirrors(e, executableNode);

		target.appendChild(executableNode);

		// scan the method's parameters
		return super.visitExecutable(e, executableNode);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.lang.model.util.ElementScanner6#visitPackage(javax.lang.model.element.PackageElement,
	 *      java.lang.Object)
	 */
	@Override
	public Void visitPackage(PackageElement e, Node target) {
		// TODO not yet implemented
		return super.visitPackage(e, target);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.lang.model.util.ElementScanner6#visitType(javax.lang.model.element.TypeElement,
	 *      java.lang.Object)
	 */
	@Override
	public Void visitType(TypeElement e, Node target) {
		Element typeNode = _doc.createElement(TYPE_ELEMENT_TAG);
		typeNode.setAttribute(KIND_TAG, e.getKind().name());
		typeNode.setAttribute(SNAME_TAG, e.getSimpleName().toString());
		typeNode.setAttribute(QNAME_TAG, e.getQualifiedName().toString());
		
		convertSuperclass(e, typeNode);
		convertInterfaces(e, typeNode);
		convertAnnotationMirrors(e, typeNode);

		target.appendChild(typeNode);

		// Scan the type's subtypes, fields, and methods
		return super.visitType(e, typeNode);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.lang.model.util.ElementScanner6#visitTypeParameter(javax.lang.model.element.TypeParameterElement,
	 *      java.lang.Object)
	 */
	@Override
	public Void visitTypeParameter(TypeParameterElement e, Node target) {
		// TODO not yet implemented
		return super.visitTypeParameter(e, target);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.lang.model.util.ElementScanner6#visitVariable(javax.lang.model.element.VariableElement,
	 *      java.lang.Object)
	 */
	@Override
	public Void visitVariable(VariableElement e, Node target) {
		Element variableNode = _doc.createElement(VARIABLE_ELEMENT_TAG);
		variableNode.setAttribute(KIND_TAG, e.getKind().name());
		variableNode.setAttribute(SNAME_TAG, e.getSimpleName().toString());
		// TODO: the spec does not restrict the toString() implementation
		variableNode.setAttribute(TYPE_TAG, e.asType().toString());

		convertAnnotationMirrors(e, variableNode);

		target.appendChild(variableNode);

		// Variables do not enclose any elements, so no need to call super.
		return null;
	}
	
	private void convertAnnotationMirrors(javax.lang.model.element.Element e, Node target) {
		List<? extends AnnotationMirror> mirrors = e.getAnnotationMirrors();
		if (mirrors != null && !mirrors.isEmpty()) {
			Element annotationsNode = _doc.createElement(ANNOTATIONS_TAG);
			for (AnnotationMirror am : mirrors) {
				convertAnnotationMirror(am, annotationsNode);
			}
			target.appendChild(annotationsNode);
		}
	}

	/**
	 * Scan an annotation instance in the model and represent it in XML, including all its explicit
	 * values (but not any default values).
	 * 
	 * @param am
	 *            the annotation mirror to be converted
	 * @param target
	 *            the &lt;annotations&gt; XML node to which a new &lt;annotation&gt; node will be
	 *            added
	 */
	private void convertAnnotationMirror(AnnotationMirror am, Node target) {
		javax.lang.model.element.Element annoElement = am.getAnnotationType().asElement();
		if (annoElement == null) {
			return;
		}
		Element annoNode = _doc.createElement(ANNOTATION_TAG);
		String sname = am.getAnnotationType().asElement().getSimpleName().toString();
		annoNode.setAttribute(SNAME_TAG, sname);
		Map<? extends ExecutableElement, ? extends AnnotationValue> values = am.getElementValues();
		if (values.size() > 0) {
			Element valuesNode = _doc.createElement(ANNOTATION_VALUES_TAG);
			annoNode.appendChild(valuesNode);
			for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : values
					.entrySet()) {
				AnnotationValue valueHolder = entry.getValue();
				if (valueHolder != null) {
					Object value = valueHolder.getValue();
					Element valueNode = _doc.createElement(ANNOTATION_VALUE_TAG);
					valueNode.setAttribute(MEMBER_TAG, entry.getKey().getSimpleName().toString());
					valueNode.setAttribute(TYPE_TAG, value.getClass().getName());
					valueNode.setAttribute(VALUE_TAG, value.toString());
					valuesNode.appendChild(valueNode);
				}
			}
		}
		target.appendChild(annoNode);
	}

	/**
	 * Scan a type for its extended and implemented interfaces and represent them
	 * in XML.
	 * @param target the node representing the type; an &lt;interfaces&gt; node
	 * will be added as a child of this node, if any interfaces are found.
	 */
	private void convertInterfaces(TypeElement e, Node target) {
		List<? extends TypeMirror> interfaces = e.getInterfaces();
		if (interfaces != null && !interfaces.isEmpty()) {
			Element interfacesNode = _doc.createElement(INTERFACES_TAG);
			for (TypeMirror intfc : interfaces) {
				convertTypeMirror(intfc, interfacesNode);
			}
			target.appendChild(interfacesNode);
		}
	}

	/**
	 * Create a node representing a class declaration's superclass
	 * @param tmSuper
	 * @param target
	 */
	private void convertSuperclass(TypeElement e, Node target) {
		TypeMirror tmSuper = e.getSuperclass();
		if (null != tmSuper) {
			Element node = _doc.createElement(SUPERCLASS_TAG);
			convertTypeMirror(tmSuper, node);
			target.appendChild(node);
		}
	}

	/**
	 * Represent an arbitrary TypeMirror in XML, and append it as a child to
	 * the specified parent node.  
	 * 
	 * Note this is problematic, because TypeMirror has no well-specified ways 
	 * to canonicalize an arbitrary (and possibly erroneous) type.
	 * 
	 * @param tm must be non-null
	 * @param target the parent XML node, to which this new node will be appended
	 */
	private void convertTypeMirror(TypeMirror tm, Node target) {
		Element n = _doc.createElement(TYPE_MIRROR_TAG);
		n.setAttribute(KIND_TAG, tm.getKind().name());
		n.setAttribute(TO_STRING_TAG, tm.toString());
		// TODO: potentially walk type-variables here
		target.appendChild(n);
	}
}

Back to the top