Skip to main content
summaryrefslogtreecommitdiffstats
blob: e7c336a27460c613f29261be8af666855c2ac5f4 (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
/*******************************************************************************
 * Copyright (c) 2007, 2015 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.data;

import java.util.List;
import java.util.StringTokenizer;

import org.eclipse.core.runtime.Platform;
import org.eclipse.help.internal.base.HelpBasePlugin;
import org.eclipse.help.internal.util.ProductPreferences;

/**
 * Utility class for parsing the CSS preferences
 */

public class CssUtil {

	private final static String cssLink1 = "<link rel=\"stylesheet\" href=\""; //$NON-NLS-1$
	private static final String cssLink2 = "\" type=\"text/css\"></link>\n"; //$NON-NLS-1$

	private static String replaceParameters(String input) {
		final String OS = "${os}"; //$NON-NLS-1$
		int index = input.indexOf(OS);
		if (index < 0) {
			return input;
		}
		String result = input.substring(0, index) + Platform.getOS() + input.substring(index + OS.length());
		return replaceParameters(result);
	}

	/**
	 * @param filenames
	 * @return
	 */
	public static String[] getCssFilenames(String filenames ) {
		if (filenames  == null) {
			return new String[0];
		}
		StringTokenizer tok = new StringTokenizer(filenames , ","); //$NON-NLS-1$
		String[] result = new String[tok.countTokens()];
		for (int i = 0; tok.hasMoreTokens(); i++) {
			result[i] = replaceParameters(tok.nextToken().trim());
		}
		return result;
	}

	public static void addCssFiles(final String preference, List<String> list) {
		String topicCssPath = Platform.getPreferencesService().getString(HelpBasePlugin.PLUGIN_ID, preference, "", null);  //$NON-NLS-1$
		String[] cssFiles = CssUtil.getCssFilenames(topicCssPath);
		for (String cssFile : cssFiles) {
			list.add(cssFile);
		}
	}

	public static String createCssIncludes(List<String> cssFiles, String backPath) {
		StringBuffer script = new StringBuffer();
		for (String cssPath : cssFiles) {
			script.append(cssLink1);
			script.append(fixCssPath(cssPath, backPath));
			script.append(cssLink2);
		}
		return script.toString();
	}

	/*
	 * Substitute for PLUGINS_ROOT and PRODUCT_PLUGIN
	 */
	private static String fixCssPath(String path, String prefix) {
		String newPath = ProductPreferences.resolveSpecialIdentifiers(path);
		return prefix + "content" + newPath; //$NON-NLS-1$
	}

}

Back to the top