Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: b7c9ae67ceca4290797cf294fd1d6b3c34d14d3c (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
/*******************************************************************************
 * Copyright (c) 2001, 2004 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
 *     Jens Lukowski/Innoopract - initial renaming/restructuring
 *     
 *******************************************************************************/
package org.eclipse.wst.sse.core.internal.encoding;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.content.IContentType;

public class NonContentBasedEncodingRules {

	private static final String getJavaPlatformDefaultEncoding() {
		String enc = System.getProperty("file.encoding"); //$NON-NLS-1$
		// return blank as null
		if (enc != null && enc.trim().length() == 0) {
			enc = null;
		}
		return enc;
	}


	public static String getUserSpecifiedDefaultForContentType(IFile iFile) {
		String enc = null;
		
		IContentType contentType = null;
		try {
			contentType = iFile.getContentDescription().getContentType();
			
			// first try to get base's default encoding for content type 
			if (contentType != null) {
				enc = contentType.getDefaultCharset();
			}

			// next try to get sse's default encoding for content type
			if (enc == null || enc.trim().length() == 0) {
				enc = ContentBasedPreferenceGateway.getPreferencesString(contentType, CommonEncodingPreferenceNames.INPUT_CODESET);
			}
			
			// return blank as null
			if (enc != null && enc.trim().length() == 0) {
				enc = null;
			}
		} catch (CoreException e) {
			// if core exception occurs, assume no preference!
			enc = null;
		}
		return enc;
	}

	public static String getUserSpecifiedDefaultForContentType(String contentTypeId) {
		String enc = null;
		
		// first try to get base's default encoding for content type
		IContentType contentType = Platform.getContentTypeManager().getContentType(contentTypeId);
		if (contentType != null) {
			enc = contentType.getDefaultCharset();
		}
		
		// next try to get sse's default encoding for content type
		if (enc == null || enc.trim().length() == 0) {
			enc = ContentBasedPreferenceGateway.getPreferencesString(contentTypeId, CommonEncodingPreferenceNames.INPUT_CODESET);
		}
		
		// return blank as null
		if (enc != null && enc.trim().length() == 0) {
			enc = null;
		}
		return enc;
	}

	private static final String getWorkbenchSpecifiedDefaultEncoding() {
		ResourcesPlugin resourcePlugin = ResourcesPlugin.getPlugin();
		String enc = resourcePlugin.getPluginPreferences().getString(ResourcesPlugin.PREF_ENCODING);
		// return blank as null
		if (enc != null && enc.trim().length() == 0) {
			enc = null;
		}
		return enc;
	}

	/**
	 * @param specDefault
	 *            This is the default charset name that would ordinarily be
	 *            used for a particular type of content. Null may be
	 *            specififed for those types with no spec default. If the spec
	 *            default is known (and passed in), then it will be returned
	 *            after being checked to see if there's be any user specified
	 *            "override" for that charset (which would be rare). In other
	 *            words, if the spec is known, there's little reason to use
	 *            this method.
	 * @return the charset that should be used according to the rules
	 *         established by this class.
	 */
	public static final String useDefaultNameRules(String specDefault) {
		String enc = null;
		String result = null;
		enc = specDefault;
		if (enc != null) {
			result = enc;
		} else {
			enc = ContentTypeEncodingPreferences.getUserSpecifiedDefaultEncodingPreference();
			if (enc != null && enc.trim().length() > 0) {
				result = enc.trim();
			} else {
				if (enc == null || enc.trim().length() == 0) {
					enc = getWorkbenchSpecifiedDefaultEncoding();
					if (enc != null) {
						result = enc.trim();
					}
				}
				if (enc == null || enc.trim().length() == 0) {
					enc = getJavaPlatformDefaultEncoding();
					// enc should never be null (but we'll
					// check anyway)
					if (enc != null) {
						result = enc;
					}
				}
			}
		}
		result = CodedIO.checkMappingOverrides(result);
		return result;
	}

	private NonContentBasedEncodingRules() {
		super();
	}
}

Back to the top