Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 260700
Collapse All | Expand All

(-)src/org/eclipse/gmf/internal/xpand/inactive/ContentDescriber.java (-18 / +34 lines)
Lines 11-50 Link Here
11
 */
11
 */
12
package org.eclipse.gmf.internal.xpand.inactive;
12
package org.eclipse.gmf.internal.xpand.inactive;
13
13
14
import java.io.BufferedReader;
14
import java.io.IOException;
15
import java.io.IOException;
15
import java.io.InputStream;
16
import java.io.InputStream;
17
import java.io.InputStreamReader;
16
import java.io.Reader;
18
import java.io.Reader;
19
import java.util.regex.Pattern;
17
20
18
import org.eclipse.core.runtime.QualifiedName;
21
import org.eclipse.core.runtime.QualifiedName;
19
import org.eclipse.core.runtime.content.IContentDescription;
22
import org.eclipse.core.runtime.content.IContentDescription;
20
import org.eclipse.core.runtime.content.ITextContentDescriber;
23
import org.eclipse.core.runtime.content.ITextContentDescriber;
21
24
22
/**
25
/**
23
 * Major difference from platform's default content describer (which uses BOM) is that we
26
 * Major difference from platform's default content describer (which uses BOM)
24
 * supply ISO-8859-1 encoding for legacy template files (those with 0xAB and 0xBB), but keep
27
 * is that we supply ISO-8859-1 encoding for legacy template files (those with
25
 * exposing UTF-8 as our default encoding. Besides, we do recognize UTF-8 encoding even if
28
 * 0xAB and 0xBB), but keep exposing UTF-8 as our default encoding. Besides, we
26
 * there's no BOM in the file (looking for 0xC2 0xAB and 0xC2 0xBB sequences)
29
 * do recognize UTF-8 encoding even if there's no BOM in the file (looking for
27
 *   
30
 * 0xC2 0xAB and 0xC2 0xBB sequences)
31
 * 
28
 * @author artem
32
 * @author artem
29
 */
33
 */
30
public class ContentDescriber implements ITextContentDescriber {
34
public class ContentDescriber implements ITextContentDescriber {
31
35
32
	public int describe(Reader contents, IContentDescription description) throws IOException {
36
	private final static String PATTERN_IMPORT = "IMPORT\\s+(\"|\')";
33
		// no idea what I can tell here, but if I do not implement ITextContentDescriber, attempt to save
37
	private final static Pattern PATTERN = Pattern.compile(PATTERN_IMPORT);
34
		// existing! UTF-8 xpt file under template folder with ISO encoding results in error "can't convert UTF to ISO"
38
35
		// - for some stupid reason Eclipse TextEditor tries to come up with new encoding for existing file
39
	public int describe(Reader contents, IContentDescription description) {
36
		return VALID;
40
		BufferedReader reader = new BufferedReader(contents);
41
		if (isValid(reader))
42
			return VALID;
43
44
		return INVALID;
37
	}
45
	}
38
46
39
	public int describe(InputStream contents, IContentDescription description) throws IOException {
47
	public int describe(InputStream contents, IContentDescription description) {
40
		StreamDecoder sd = new StreamDecoder(contents, null);
48
		BufferedReader reader = new BufferedReader(new InputStreamReader(
41
		if (sd.getEncoding() == null) {
49
				contents));
42
			return INDETERMINATE;
50
		return describe(reader, description);
51
	}
52
53
	private boolean isValid(BufferedReader reader) {
54
		String line;
55
		try {
56
			for (int i = 0; (line = reader.readLine()) != null; i++)
57
				if (PATTERN.matcher(line).find())
58
					return true;
59
		} catch (IOException e) {
60
			e.printStackTrace();
43
		}
61
		}
44
		if (description != null) {
62
45
			description.setProperty(IContentDescription.CHARSET, sd.getEncoding().name());
63
		return false;
46
		} // otherwise, rely on Eclipse to detect encoding
47
		return VALID;
48
	}
64
	}
49
65
50
	public QualifiedName[] getSupportedOptions() {
66
	public QualifiedName[] getSupportedOptions() {
(-).settings/org.eclipse.core.resources.prefs (+3 lines)
Added Link Here
1
#Tue Mar 31 11:39:52 CEST 2009
2
eclipse.preferences.version=1
3
encoding//src/org/eclipse/gmf/internal/xpand/inactive/ContentDescriber.java=UTF-8
(-)META-INF/MANIFEST.MF (+1 lines)
Lines 9-11 Link Here
9
 org.junit,
9
 org.junit,
10
 org.eclipse.ocl.ecore;bundle-version="[1.2.0,2.0.0)",
10
 org.eclipse.ocl.ecore;bundle-version="[1.2.0,2.0.0)",
11
 org.eclipse.m2m.qvt.oml.runtime;bundle-version="[2.0.0,3.0.0)"
11
 org.eclipse.m2m.qvt.oml.runtime;bundle-version="[2.0.0,3.0.0)"
12
Import-Package: org.eclipse.core.runtime.content
(-).settings/org.eclipse.core.resources.prefs (+3 lines)
Added Link Here
1
#Tue Mar 31 11:42:49 CEST 2009
2
eclipse.preferences.version=1
3
encoding//src/org/eclipse/gmf/internal/xpand/inactive/ContentDescriberTest.java=UTF-8
(-)src/org/eclipse/gmf/internal/xpand/inactive/ContentDescriberTest.java (+151 lines)
Added Link Here
1
package org.eclipse.gmf.internal.xpand.inactive;
2
3
import java.io.BufferedReader;
4
import java.io.ByteArrayInputStream;
5
import java.io.IOException;
6
import java.io.InputStream;
7
import java.io.Reader;
8
import java.io.StringReader;
9
10
import junit.framework.TestCase;
11
12
import org.eclipse.core.runtime.content.IContentDescriber;
13
import org.eclipse.core.runtime.content.ITextContentDescriber;
14
import org.eclipse.gmf.internal.xpand.inactive.ContentDescriber;
15
import org.xml.sax.InputSource;
16
17
/**
18
 * 
19
 * @author Torsten Krohn
20
 * 
21
 */
22
23
@SuppressWarnings("restriction")
24
public class ContentDescriberTest extends TestCase {
25
26
	public static final String ENCODING_UTF8 = "UTF-8";
27
	public static final String ENCODING_ISO = "ISO-8859-1";
28
	public static final String ENCODING_MACROMAN = "MacRoman";
29
30
	public Reader readerGmfXptUtf8;
31
	public Reader readerGmfXptIso88591;
32
	public Reader readerXtextXptUtf8;
33
	public Reader readerXtextXptIso88591;
34
	public Reader readerOther;
35
36
	public InputStream inputStreamGmfXptUtf8;
37
	public InputStream inputStreamGmfXptIso;
38
	public InputStream inputStreamXtextXptUtf8;
39
	public InputStream inputStreamXtextXptIso;
40
	public InputStream inputStreamOther;
41
42
	@Override
43
	protected void setUp() throws Exception {
44
		super.setUp();
45
46
		String xtextxpt = "/*\n";
47
		xtextxpt += " * some comments with the word import included\n";
48
		xtextxpt += " */\n";
49
		xtextxpt += "\n";
50
		xtextxpt += "\u00ABIMPORT 'http://www.eclipse.org/gmf/2009/GenModel'\00BB\n";
51
		xtextxpt += "\u00ABEXTENSION xpt::diagram::parts::Common\u00BB\n";
52
53
		byte[] utf8bytes = xtextxpt.getBytes(ENCODING_UTF8);
54
		byte[] isobytes = xtextxpt.getBytes(ENCODING_ISO);
55
		inputStreamXtextXptUtf8 = new ByteArrayInputStream(utf8bytes);
56
		inputStreamXtextXptIso = new ByteArrayInputStream(isobytes);
57
58
		InputSource inputSourceXtextXptUtf8 = new InputSource(new StringReader(
59
				xtextxpt));
60
		inputSourceXtextXptUtf8.setEncoding(ENCODING_UTF8);
61
		readerXtextXptUtf8 = inputSourceXtextXptUtf8.getCharacterStream();
62
63
		InputSource inputSourceXtextXptIso = new InputSource(new StringReader(
64
				xtextxpt));
65
		inputSourceXtextXptIso.setEncoding(ENCODING_ISO);
66
		readerXtextXptIso88591 = inputSourceXtextXptIso.getCharacterStream();
67
68
		String gmfxpt = "/*\n";
69
		gmfxpt += " * some comments with the word import included\n";
70
		gmfxpt += " */\n";
71
		gmfxpt += "package org.eclipse.gmf.internal.xpand.inactive;\n";
72
		gmfxpt += "\n";
73
		gmfxpt += "import java.io.IOException;\n";
74
		gmfxpt += "public class MagicClass implements IMagic {\n";
75
76
		utf8bytes = gmfxpt.getBytes(ENCODING_UTF8);
77
		isobytes = gmfxpt.getBytes(ENCODING_ISO);
78
		inputStreamGmfXptUtf8 = new ByteArrayInputStream(utf8bytes);
79
		inputStreamGmfXptIso = new ByteArrayInputStream(isobytes);
80
81
		InputSource inputSourceGmfXptUtf8 = new InputSource(new StringReader(
82
				gmfxpt));
83
		inputSourceGmfXptUtf8.setEncoding(ENCODING_UTF8);
84
		readerGmfXptUtf8 = inputSourceGmfXptUtf8.getCharacterStream();
85
		InputSource inputStreamGmfXptIso = new InputSource(new StringReader(
86
				gmfxpt));
87
		inputStreamGmfXptIso.setEncoding(ENCODING_ISO);
88
		readerGmfXptIso88591 = inputStreamGmfXptIso.getCharacterStream();
89
90
		String other = "/*";
91
		other += " * some comments with the word import included\n";
92
		other += " */\n";
93
		other += "package org.eclipse.gmf.internal.xpand.inactive;\n";
94
95
		byte[] otherBytes = other.getBytes(ENCODING_MACROMAN);
96
		inputStreamOther = new ByteArrayInputStream(otherBytes);
97
98
		InputSource otherInputSource = new InputSource(new StringReader(other));
99
		readerOther = otherInputSource.getCharacterStream();
100
	}
101
102
	public final void testDescribeReader() throws IOException {
103
		BufferedReader contents = new BufferedReader(readerXtextXptUtf8);
104
		ITextContentDescriber describer = new ContentDescriber();
105
		assertEquals(IContentDescriber.VALID, describer
106
				.describe(contents, null));
107
108
		contents = new BufferedReader(readerXtextXptIso88591);
109
		assertEquals(IContentDescriber.VALID, describer
110
				.describe(contents, null));
111
112
		contents = new BufferedReader(readerGmfXptUtf8);
113
		assertEquals(IContentDescriber.INVALID, describer.describe(contents,
114
				null));
115
116
		contents = new BufferedReader(readerGmfXptIso88591);
117
		assertEquals(IContentDescriber.INVALID, describer.describe(contents,
118
				null));
119
		
120
		contents = new BufferedReader(readerOther);
121
		assertEquals(IContentDescriber.INVALID, describer.describe(contents,
122
				null));
123
	}
124
125
	public final void testDescribeInputStream() throws IOException {
126
		ITextContentDescriber describer = new ContentDescriber();
127
		assertEquals(IContentDescriber.VALID, describer.describe(
128
				inputStreamXtextXptUtf8, null));
129
130
		assertEquals(IContentDescriber.VALID, describer.describe(
131
				inputStreamXtextXptIso, null));
132
133
		assertEquals(IContentDescriber.INVALID, describer.describe(
134
				inputStreamGmfXptUtf8, null));
135
136
		assertEquals(IContentDescriber.INVALID, describer.describe(
137
				inputStreamGmfXptIso, null));
138
		
139
		assertEquals(IContentDescriber.INVALID, describer.describe(
140
				inputStreamOther, null));
141
	}
142
143
	@Override
144
	protected void tearDown() throws Exception {
145
		super.tearDown();
146
		readerGmfXptUtf8 = null;
147
		readerGmfXptIso88591 = null;
148
		readerXtextXptUtf8 = null;
149
		readerXtextXptIso88591 = null;
150
	}
151
}

Return to bug 260700