Skip to main content
summaryrefslogtreecommitdiffstats
blob: 152b714a476bc807d04d26fa312bc4dc30c845cd (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
/*******************************************************************************
 * Copyright (c) 2011 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.help.internal.webapp.parser;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Properties;

import org.eclipse.help.internal.webapp.utils.JSonHelper;

public class ParseElement {

	private Properties props;
	private ArrayList children = new ArrayList();
	private ParseElement parent;

	public ParseElement(Properties props, ParseElement parent) {
		this.props = props;
		this.parent = parent;
	}

	public ParseElement(Properties props) {
		this(props, null);
	}
	
	public void updateParseElement(Properties props) {
		this.props = props;
	}
	
	public Properties getProps() {
		return props;
	}
	
	public ParseElement getParent() {
		return parent;
	}
	
	public String getProperty(String key) {
		return (props != null) ? props.getProperty(key) : ""; //$NON-NLS-1$
	}
	
	public String toString() {
		return (props != null) ? props.toString() : ""; //$NON-NLS-1$
	}
	
	public void addChild(ParseElement elem) {
		children.add(elem);
	}
	
	public int getChildrenCount() {
		return children.size();
	}
	
	public String toJSON(int level) {
		
		StringBuffer buff = new StringBuffer();
		
		String space = JSonHelper.SPACE;
		for (int s = 0; s < level; s++) {
			space += JSonHelper.SPACE;
		}
		
		buff.append(JSonHelper.NEWLINE + space);
		buff.append(JSonHelper.BEGIN_BRACE);
		
		if (props != null) {
			Enumeration enumObj = props.keys();
			while (enumObj.hasMoreElements()) {
				
				String key = (String) enumObj.nextElement();
				String val = props.getProperty(key);
				
				buff.append(JSonHelper.NEWLINE + space + JSonHelper.SPACE);
				buff.append(key);
				buff.append(JSonHelper.COLON);
				try {
					val = URLEncoder.encode(val, "UTF-8"); //$NON-NLS-1$
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				buff.append(JSonHelper.getQuotes(val));
				buff.append(JSonHelper.COMMA);
			}
		}
		
		if (children.size() <= 0) {
			int len = buff.length();
			char ch = buff.charAt(len - 1);
			if (ch == ',') {
				buff.deleteCharAt(len - 1);
				buff.append(JSonHelper.NEWLINE + space);
			}
			
		} else {
			
			buff.append(JSonHelper.NEWLINE + space + JSonHelper.SPACE);
			buff.append(JSonHelper.CHILDREN);
			buff.append(JSonHelper.COLON);
			buff.append(JSonHelper.BEGIN_BRACKET);
			
			for (int i = 0; i < children.size(); i++) {
				
				if (i > 0)
					buff.append(JSonHelper.COMMA);
				
				ParseElement element = (ParseElement) children.get(i);
				buff.append(element.toJSON(level + 2));
			}
			
			buff.append(JSonHelper.NEWLINE + space + JSonHelper.SPACE);
			
			buff.append(JSonHelper.END_BRACKET);
			buff.append(JSonHelper.NEWLINE + space);
		}
		
		buff.append(JSonHelper.END_BRACE);
		
		return buff.toString();
	}
}

Back to the top