Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7f28306a967a2c5627b65991dd64a976afa36016 (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
/*******************************************************************************
 * Copyright (c) 2004, 2020 IBM Corporation and others.
 * All rights reserved. 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.wst.html.ui.internal.contentassist.resources;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.content.IContentType;

/**
 * Cache for the filenames and filename extensions registered for a content
 * type and all of its descendants
 */
public class ContentTypeSpecs {
	/**
	 * @param contentTypeId - the registered content type's ID
	 * @return
	 */
	public static ContentTypeSpecs createFor(String contentTypeId) {
		return createFor(new IContentType[]{Platform.getContentTypeManager().getContentType(contentTypeId)});
	}

	public static ContentTypeSpecs createFor(IContentType[] contentTypes) {
//		long startTime = System.currentTimeMillis();
		List<String> filenameExtensions = new ArrayList<String>();
		Set<String> filenames = new HashSet<>();
		for (IContentType baseContentType: contentTypes) {
			String[] baseExtensions = baseContentType.getFileSpecs(IContentType.FILE_EXTENSION_SPEC);
			for (int i = 0; i < baseExtensions.length; i++) {
				filenameExtensions.add(baseExtensions[i]);
			}
			String[] names = baseContentType.getFileSpecs(IContentType.FILE_NAME_SPEC);
			for (int j = 0; j < names.length; j++) {
				filenames.add(names[j]);
			}
			Arrays.sort(baseExtensions);
			IContentType[] allContentTypes1 = Platform.getContentTypeManager().getAllContentTypes();
			for (int i = 0, length = allContentTypes1.length; i < length; i++) {
				if (allContentTypes1[i].isKindOf(baseContentType)) {
					String[] fileExtension = allContentTypes1[i].getFileSpecs(IContentType.FILE_EXTENSION_SPEC);
					for (int j = 0; j < fileExtension.length; j++) {
						if (!filenameExtensions.contains(fileExtension[j])) {
							filenameExtensions.add(fileExtension[j]);
						}
					}
					names = allContentTypes1[i].getFileSpecs(IContentType.FILE_NAME_SPEC);
					for (int j = 0; j < names.length; j++) {
						filenames.add(names[j]);
					}
				}
			}
		}
		String[] stringExtensions = filenameExtensions.toArray(new String[filenameExtensions.size()]);
//		System.out.println("Discovered content types for " + contentTypeId + " in " + (System.currentTimeMillis() - startTime) + "ms");
		return new ContentTypeSpecs(filenames.toArray(new String[filenames.size()]), stringExtensions);
	}

	String[] fFilenames = new String[0];
	String[] fExtensions = new String[0];

	private ContentTypeSpecs(String[] fileNames, String[] extensions) {
		super();
		fFilenames = fileNames;
		fExtensions = extensions;
		Arrays.sort(fileNames);
	}

	public boolean matches(String filename) {
		if (Arrays.binarySearch(fFilenames, filename) >= 0) {
			return true;
		}
		for (int i = 0; i < fExtensions.length; i++) {
			if (filename.length() > fExtensions[i].length() + 1
					&& filename.charAt(filename.length() - fExtensions[i].length() - 1) == '.'
					&& filename.endsWith(fExtensions[i])) {
				return true;
			}
		}
		return false;
	}

	public void addFilename(String filename) {
		List<String> combinedList = new ArrayList<String>(fFilenames.length + 1);
		for (int i = 0; i < fFilenames.length; i++) {
			combinedList.add(fFilenames[i]);
		}
		combinedList.add(filename);
		String[] combinedArray = combinedList.toArray(new String[combinedList.size()]);
		Arrays.sort(combinedArray);
		fFilenames = combinedArray;
	}

	public void addFilenameExtension(String extension) {
		List<String> combinedList = new ArrayList<String>(fExtensions.length + 1);
		for (int i = 0; i < fExtensions.length; i++) {
			combinedList.add(fExtensions[i]);
		}
		combinedList.add(extension);
		String[] combinedArray = combinedList.toArray(new String[combinedList.size()]);
		fExtensions = combinedArray;
	}
}

Back to the top