Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e4e8bf5b00310fb6c61f2618506fb90b75f96d57 (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
/*******************************************************************************
 * Copyright (c) 2005, 2016 IBM Corporation 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
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.core.parser.tests;

import java.io.StringWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
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;

/**
 * This class implements a utility that will walk through an object
 * and it's children and create an XML file for it.
 */
public class XMLDumper {

	public static class Test {
		private String msg = "hi"; //$NON-NLS-1$
		
		public String getMsg() {
			return msg;
		}
		
		public Test self = this;
	}
	
	public static void main(String [] args) {
		Test test = new Test();
		try {
			XMLDumper dumper = new XMLDumper(test);
			Document document = dumper.getDocument();
			StringWriter writer = new StringWriter();
		
			Transformer transformer = TransformerFactory.newInstance().newTransformer();
			transformer.transform(new DOMSource(document), new StreamResult(writer));

			System.out.println( "STRXML = " + writer.toString() ); //Spit out DOM as a String //$NON-NLS-1$
		} catch (TransformerException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}

	}
	
	private int id = 0;
	private HashMap map = new HashMap();
	private Document document;
	
	public Document getDocument() {
		return document;
	}
	
	public XMLDumper(Object obj) throws ParserConfigurationException {
		document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
		document.appendChild(createObject(obj));
	}
	
	private Element createObject(Object obj) {
		Class cls = obj.getClass();
		String clsName = cls.getName();
		clsName = clsName.replace('$', '.');
		
		Element element = document.createElement(clsName);
		map.put(obj, Integer.valueOf(id));
		element.setAttribute("id",Integer.toString(id++)); //$NON-NLS-1$
		
		Field [] fields = cls.getDeclaredFields();
		for (int i = 0; i < fields.length; ++i) {
			Field field = fields[i];
			int modifiers = field.getModifiers();
			
			// Skip over static fields
			if (Modifier.isStatic(modifiers))
				continue;
			
			// Skip fields that start with an underscore
			if (field.getName().charAt(0) == '_')
				continue;
			
			Object value = null;
			
			String fieldName = field.getName();
			if (Modifier.isPublic(modifiers)) {
				try {
					value = field.get(obj);
				} catch (Exception e) {
					value = e;
				}
			} else {
				String methodName = "get" + //$NON-NLS-1$
					fieldName.substring(0, 1).toUpperCase() +
					fieldName.substring(1);
				
				Method method = null;
				try {
					method = cls.getMethod(methodName, (Class []) null);
				} catch (NoSuchMethodException e) {
					continue;
				}
				
				try {
					value = method.invoke(obj, (Object []) null);
				} catch (Exception e) {
					value = e;
				}
			}
			
			Element fieldElement = document.createElement(fieldName);
			element.appendChild(fieldElement);
			
			if (value == null)
				return element;
				
			Class type = field.getType();
			if (String.class.isAssignableFrom(type))
				fieldElement.appendChild(document.createTextNode((String)value));
			else if (Integer.class.isAssignableFrom(type))
				fieldElement.appendChild(document.createTextNode(((Integer)value).toString()));
			else if (Exception.class.isAssignableFrom(type))
				fieldElement.appendChild(document.createTextNode(value.toString()));
			else {
				Object v = map.get(value);
				if (v != null)
					fieldElement.setAttribute("refid", v.toString()); //$NON-NLS-1$
				else
					fieldElement.appendChild(createObject(value));
			}
		
		}
		
		return element;
	}
}

Back to the top