Skip to main content
summaryrefslogtreecommitdiffstats
blob: 24fbd93f2812667647b0115d79e5bae6dea71b34 (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
/*******************************************************************************
 * Copyright (c) 2011, 2017 IBM Corporation and others.
 *
 * 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.help.internal.webapp.utils;

import java.util.Collection;

import org.eclipse.help.HelpSystem;
import org.eclipse.help.IHelpResource;
import org.eclipse.help.IToc;
import org.eclipse.help.ITopic;
import org.eclipse.help.internal.search.SearchHit;
import org.eclipse.help.internal.webapp.data.UrlUtil;
import org.eclipse.help.internal.webapp.servlet.XMLGenerator;

public class SearchXMLGenerator  {

	public static String serialize(Collection<SearchHit> results) {
		return serialize((results != null) ? results.toArray(new SearchHit[results.size()]) : null, false);
	}

	public static String serialize(SearchHit[] hits, boolean boolIsCategory) {
		StringBuilder buf = new StringBuilder();
		buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); //$NON-NLS-1$
		buf.append("<searchHits>\n"); //$NON-NLS-1$

		if (hits != null) {
			for (SearchHit hit : hits) {
				serialize(hit, buf, "   ", boolIsCategory); //$NON-NLS-1$
			}
		}

		buf.append("</searchHits>\n"); //$NON-NLS-1$

		return buf.toString();
	}

	private static void serialize(SearchHit hit, StringBuilder buf,
			String indent, boolean boolIsCategory) {
		buf.append(indent + "<hit"); //$NON-NLS-1$
		if (hit.getHref() != null) {
			buf.append('\n' + indent	+ "      href=\"" + XMLGenerator.xmlEscape(hit.getHref()) + '"'); //$NON-NLS-1$
		}
		if (hit.getLabel() != null) {
			buf.append('\n' + indent	+ "      label=\"" + XMLGenerator.xmlEscape(hit.getLabel()) + '"'); //$NON-NLS-1$
		}
		if (hit.isPotentialHit()) {
			buf.append('\n' + indent	+ "      isPotentialHit=\"true\""); //$NON-NLS-1$
		}
		buf.append('\n' + indent + "      score=\"" + hit.getScore() + '"'); //$NON-NLS-1$
		buf.append(">\n"); //$NON-NLS-1$

		// get Category
		if (boolIsCategory) {
			IHelpResource categoryResource = hit.getCategory();
			if (categoryResource != null) {
				serializeCategory(categoryResource, buf, indent + "  "); //$NON-NLS-1$
			}
		}

		// get Summary/Description
		String summary = hit.getSummary();
		if (summary != null) {
			serialize(summary, buf, indent + "   "); //$NON-NLS-1$
		}
		buf.append(indent + "</hit>\n"); //$NON-NLS-1$
	}

	private static void serialize(String summary, StringBuilder buf, String indent) {
		buf.append(indent + "<summary>"); //$NON-NLS-1$
		buf.append(XMLGenerator.xmlEscape(summary));
		buf.append("</summary>\n"); //$NON-NLS-1$
	}

	private static void serializeCategory(IHelpResource categoryResource,
			StringBuilder buf, String indent) {
		String category = categoryResource.getLabel();
		if (category == null) return;

		buf.append(indent + "<category"); //$NON-NLS-1$

		String catHref = getCategoryHref(categoryResource);
		if (catHref != null) {
			buf.append('\n' + indent	+ "      href=\""  //$NON-NLS-1$
					+ XMLGenerator.xmlEscape(catHref) + '"');
		}

		buf.append(">\n"); //$NON-NLS-1$
		buf.append(XMLGenerator.xmlEscape(category));

		buf.append("</category>\n"); //$NON-NLS-1$
	}

	private static String getCategoryHref(IHelpResource categoryResource) {
		String tocHref = categoryResource.getHref();
		IToc[] tocs = HelpSystem.getTocs();
		for (int j=0;j<tocs.length;++j) {
			if (tocHref.equals(tocs[j].getHref())) {
				ITopic topic = tocs[j].getTopic(null);
				String topicHref = topic.getHref();
				if (topicHref != null) {
					return UrlUtil.getHelpURL(topicHref);
				}
				return "../nav/" + j; //$NON-NLS-1$
			}
		}
		return null;
	}
}

Back to the top