summaryrefslogtreecommitdiffstats
blob: 7f6e34680053274c0795374bef5bcf713e87a5ba (plain)
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
/*
 * Copyright (c) 2011 Tobias Markmann
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */
/*
 * Copyright (c) 2014-2015 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */
/*
 * Copyright (c) 2015 Tarun Gupta.
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

package com.isode.stroke.client;

import java.util.Stack;
import com.isode.stroke.parser.XMLParserClient;
import com.isode.stroke.parser.XMLParser;
import com.isode.stroke.parser.AttributeMap;
import com.isode.stroke.parser.PlatformXMLParserFactory;

public class XMLBeautifier implements XMLParserClient {

	private boolean doIndention;
	private boolean doColoring;

	private int intLevel;
	private String inputBuffer = "";
	private StringBuffer buffer = new StringBuffer();
	private XMLParser parser;

	private boolean lastWasStepDown;
	private Stack<String> parentNSs = new Stack<String>();

	// all bold but reset
	public static final String colorReset = "\u001B[0m";
	public static final String ANSI_BLACK = "\u001B[30m";
	public static final String colorRed = "\u001B[31m";
	public static final String colorGreen = "\u001B[32m";
	public static final String colorYellow = "\u001B[33m";
	public static final String colorBlue = "\u001B[34m";
	public static final String ANSI_PURPLE = "\u001B[35m";
	public static final String colorCyan = "\u001B[36m";
	public static final String ANSI_WHITE = "\u001B[37m";

	public XMLBeautifier(boolean indention, boolean coloring) {
		this.doIndention = indention;
		this.doColoring = coloring;
		intLevel = 0;
		parser = null;
		lastWasStepDown = false;
	}

	public String beautify(final String text) {
		parser = PlatformXMLParserFactory.createXMLParser(this);
		intLevel = 0;
		buffer.append("");
		parser.parse(text);
		parser = null;
		return buffer.toString();
	}

	public void handleStartElement(final String element, final String ns, final AttributeMap attributes) {
		if (doIndention) {
			if (intLevel != 0) buffer.append("\n");
		}
		indent();
		buffer.append("<").append(doColoring ? styleTag(element) : element);
		if (!ns.isEmpty() && (!parentNSs.isEmpty() && !parentNSs.peek().equals(ns))) {
			buffer.append(" ");
			buffer.append((doColoring ? styleAttribute("xmlns") : "xmlns"));
			buffer.append("=");
			buffer.append("\"").append((doColoring ? styleNamespace(ns) : ns)).append("\"");
		}
		if (!attributes.getEntries().isEmpty()) {
			for(AttributeMap.Entry entry : attributes.getEntries()) {
				buffer.append(" ");
				buffer.append((doColoring ? styleAttribute(entry.getAttribute().getName()) : entry.getAttribute().getName()));
				buffer.append("=");
				buffer.append("\"").append((doColoring ? styleValue(entry.getValue()) : entry.getValue())).append("\"");
			}
		}
		buffer.append(">");
		++intLevel;
		lastWasStepDown = false;
		parentNSs.push(ns);
	}

	public void handleEndElement(final String element, final String ns) {
		--intLevel;
		parentNSs.pop();
		if (/*hadCDATA.top() ||*/ lastWasStepDown) {
			if (doIndention) {
				buffer.append("\n");
			}
			indent();
		}
		buffer.append("</").append((doColoring ? styleTag(element) : element)).append(">");
		lastWasStepDown = true;
	}

	public void handleCharacterData(final String data) {
		buffer.append(data);
		lastWasStepDown = false;
	}

	private void indent() {
	for (int i = 0; i < intLevel; ++i) {
			buffer.append(" ");
		}
	}

	private String styleTag(final String text) {
		String result = "";
		result += colorYellow;
		result += text;
		result += colorReset;
		return result;
	}

	private String styleNamespace(final String text) {
		String result = "";
		result += colorRed;
		result += text;
		result += colorReset;
		return result;
	}

	private String styleAttribute(final String text) {
		String result = "";
		result += colorGreen;
		result += text;
		result += colorReset;
		return result;
	}

	private String styleValue(final String text) {
		String result = "";
		result += colorCyan;
		result += text;
		result += colorReset;
		return result;
	}
}