Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c54544e7f2efcedbf7b75bd3babaa3ad30cce98a (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
/*******************************************************************************
 * Copyright (c) 2010 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 Rational Software - Initial API and implementation
 *******************************************************************************/

package org.eclipse.cdt.internal.core.indexer;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Map;
import java.util.TreeMap;

/**
 * this class is a registry which maps file name and file's encoding, the class
 * is used by standalone indexer
 * 
 * @author johnliu
 * 
 */
public class FileEncodingRegistry implements Serializable {

	private Map<String, String> fFilePathToEncodingMap = null;
	private String defaultEncoding;

	public FileEncodingRegistry(String defaultEncoding) {
		this.defaultEncoding = defaultEncoding;
		fFilePathToEncodingMap = new TreeMap<String, String>();
	}

	
	public void setDefaultEncoding(String newDefaultEncoding) {
		defaultEncoding = newDefaultEncoding;

	}

	public void registerFileEncoding(String filename, String encoding) {
		if (fFilePathToEncodingMap != null) {
			fFilePathToEncodingMap.put(filename, encoding);
		}

	}

	public void unregisterFile(String filename) {
		if (fFilePathToEncodingMap != null) {
			fFilePathToEncodingMap.remove(filename);
		}

	}

	public String getFileEncoding(String filename) {
		String fileEncoding = null;
		if (fFilePathToEncodingMap != null) {
			fileEncoding = fFilePathToEncodingMap.get(filename);
		}
		if (fileEncoding != null) {
			return fileEncoding;

		} else {
			return defaultEncoding;
		}
	}

	public void clear() {
		if (fFilePathToEncodingMap != null) {
			fFilePathToEncodingMap.clear();
		}
		fFilePathToEncodingMap = null;
		defaultEncoding = null;

	}

	// send as little over the wire as possible
	private void writeObject(ObjectOutputStream out) throws IOException {
		if (fFilePathToEncodingMap != null && fFilePathToEncodingMap.isEmpty()) {
			fFilePathToEncodingMap = null;
		}
		out.defaultWriteObject();
	}

}

Back to the top