Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7d3d165f0c24ecca5dac097d8a93c73b23cd69c6 (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) 2015 Frank Becker and others.
 * 
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * https://www.eclipse.org/legal/epl-2.0
 * 
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Frank Becker - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.bugzilla.rest.core;

import static com.google.common.collect.Sets.newHashSet;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.mylyn.tasks.core.data.TaskAttribute;

import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import com.google.gson.Gson;
import com.google.gson.stream.JsonWriter;

public class BugzillaRestGsonUtil {

	private class RemoveAddStringHelper {
		Set<String> add;

		Set<String> remove;

		public RemoveAddStringHelper(Set<String> removeSet, Set<String> addSet) {
			add = new HashSet<String>(addSet);
			remove = new HashSet<String>(removeSet);
			if (remove.contains("")) { //$NON-NLS-1$
				remove.remove(""); //$NON-NLS-1$
			}
			if (add.contains("")) { //$NON-NLS-1$
				add.remove(""); //$NON-NLS-1$
			}
			Set<String> intersection = Sets.intersection(addSet, removeSet);
			remove.removeAll(intersection);
			add.removeAll(intersection);
			if (remove.isEmpty()) {
				remove = null;
			}
			if (add.isEmpty()) {
				add = null;
			}
		}
	}

	private class RemoveAddIntegerHelper {
		Set<Integer> add;

		Set<Integer> remove;

		public RemoveAddIntegerHelper(Set<Integer> removeSet, Set<Integer> addSet) {
			add = new HashSet<Integer>(addSet);
			remove = new HashSet<Integer>(removeSet);
			if (remove.contains(null)) {
				remove.remove(null);
			}
			if (add.contains(null)) {
				add.remove(null);
			}
			Set<Integer> intersection = Sets.intersection(addSet, removeSet);
			remove.removeAll(intersection);
			add.removeAll(intersection);
			if (remove.isEmpty()) {
				remove = null;
			}
			if (add.isEmpty()) {
				add = null;
			}
		}
	}

	private static Gson gson = new Gson();

	public static String convertString2GSonString(String str) {
		str = str.replace("\"", "\\\"").replace("\n", "\\\n"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$//$NON-NLS-4$
		StringBuffer ostr = new StringBuffer();
		for (int i = 0; i < str.length(); i++) {
			char ch = str.charAt(i);
			if ((ch >= 0x0020) && (ch <= 0x007e)) {
				ostr.append(ch);
			} else {
				ostr.append("\\u"); //$NON-NLS-1$
				String hex = Integer.toHexString(str.charAt(i) & 0xFFFF);
				for (int j = 0; j < 4 - hex.length(); j++) {
					ostr.append("0"); //$NON-NLS-1$
				}
				ostr.append(hex.toLowerCase());
			}
		}
		return (new String(ostr));
	}

	private static BugzillaRestGsonUtil instance;

	public static BugzillaRestGsonUtil getDefault() {
		if (instance == null) {
			instance = new BugzillaRestGsonUtil();
		}
		return instance;
	}

	public static void buildArrayFromHash(JsonWriter out, String id, Set<String> setNew, boolean asInteger)
			throws IOException {
		if (!setNew.isEmpty()) {
			out.name(id).beginArray();
			for (String string : setNew) {
				if (!"".equals(string)) {
					if (asInteger) {
						out.value(Integer.parseInt(string));
					} else {
						out.value(string);
					}
				}
			}
			out.endArray();
		}
	}

	public void buildAddRemoveHash(JsonWriter out, String id, Set<String> setOld, Set<String> setNew)
			throws IOException {
		RemoveAddStringHelper test = new RemoveAddStringHelper(setOld, setNew);
		out.name(id);
		gson.toJson(test, RemoveAddStringHelper.class, out);
	}

	private final Function<String, Integer> convert2Integer = new Function<String, Integer>() {

		@Override
		public Integer apply(String input) {
			if ("".equals(input)) {
				return null;
			}
			return Integer.parseInt(input);
		}
	};

	public void buildAddRemoveIntegerHash(JsonWriter out, String id, Set<String> setOld, Set<String> setNew)
			throws IOException {
		RemoveAddIntegerHelper test = new RemoveAddIntegerHelper(
				newHashSet(Iterables.transform(setOld, convert2Integer)),
				newHashSet(Iterables.transform(setNew, convert2Integer)));
		out.name(id);
		gson.toJson(test, RemoveAddIntegerHelper.class, out);
	}

	public static void buildFlags(JsonWriter out, Set<TaskAttribute> oldAttributes, TaskAttribute rootTaskData)
			throws IOException {
		boolean flagFound = false;
		for (TaskAttribute element : oldAttributes) {
			TaskAttribute taskAttribute = rootTaskData.getAttribute(element.getId());
			String id = taskAttribute.getId();
			if (id.startsWith(IBugzillaRestConstants.KIND_FLAG)
					|| id.startsWith(IBugzillaRestConstants.KIND_FLAG_TYPE)) {
				if (!flagFound) {
					out.name("flags").beginArray(); //$NON-NLS-1$
				}
				BugzillaRestFlagMapper flagMapper = BugzillaRestFlagMapper.createFrom(taskAttribute);
				flagMapper.applyTo(out);
				flagFound = true;
			}
		}
		if (flagFound) {
			out.endArray();
		}
	}

}

Back to the top