KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > apt > core > internal > env > EncodedFileOutputStream


1 /*******************************************************************************
2  * Copyright (c) 2006 BEA Systems, Inc.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * jgarms@bea.com - initial API and implementation
10  *
11  *******************************************************************************/

12 package org.eclipse.jdt.apt.core.internal.env;
13
14 import java.io.IOException JavaDoc;
15
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IWorkspace;
18 import org.eclipse.core.resources.ResourcesPlugin;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IStatus;
21
22 /**
23  * Sets the encoding for the IFile on close of the stream
24  */

25 public class EncodedFileOutputStream extends BinaryFileOutputStream {
26
27     private final String JavaDoc _charsetName;
28     
29     public EncodedFileOutputStream(IFile file, BuildEnv env, String JavaDoc charsetName) {
30         super(file, env);
31         _charsetName = charsetName;
32     }
33     
34     @Override JavaDoc
35     public void close() throws IOException JavaDoc {
36         super.close();
37         if (_charsetName != null) {
38             
39             // Need to check for source control on the resources encoding file
40
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=190268
41
IWorkspace ws = ResourcesPlugin.getWorkspace();
42             
43             // Yuck -- we need to hardcode the location of the prefs file for file encoding
44
IFile resourceFile = _file.getProject().getFile(".settings/org.eclipse.core.resources.prefs"); //$NON-NLS-1$
45
IStatus result = ws.validateEdit(new IFile[]{resourceFile}, IWorkspace.VALIDATE_PROMPT);
46             if (result.getSeverity() == IStatus.CANCEL) {
47                 // User cancelled the checkout. Don't try to edit the encoding for the file
48
return;
49             }
50             try {
51                 _file.setCharset(_charsetName, null);
52             }
53             catch (CoreException ce) {
54                 IOException JavaDoc ioe = new IOException JavaDoc("Could not set charset: " + _charsetName); //$NON-NLS-1$
55
ioe.initCause(ce);
56                 throw ioe;
57             }
58         }
59     }
60 }
61
Popular Tags