Author: snjeza
Date: 2008-11-22 20:43:21 -0500 (Sat, 22 Nov 2008)
New Revision: 11964
Modified:
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/filesystems/impl/FolderImpl.java
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/plugin/ModelPlugin.java
Log:
JBIDE-2881 Incompatibility with UTF-8Y
Modified:
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/filesystems/impl/FolderImpl.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/filesystems/impl/FolderImpl.java 2008-11-22
10:51:55 UTC (rev 11963)
+++
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/filesystems/impl/FolderImpl.java 2008-11-23
01:43:21 UTC (rev 11964)
@@ -12,7 +12,13 @@
import java.io.File;
import java.io.IOException;
-import java.util.*;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
import org.eclipse.core.internal.resources.ResourceException;
import org.eclipse.core.resources.IContainer;
@@ -23,11 +29,10 @@
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
-import org.jboss.tools.common.model.markers.ResourceMarkers;
-import org.jboss.tools.common.model.plugin.ModelPlugin;
+import org.eclipse.core.runtime.Status;
import org.eclipse.swt.widgets.Display;
-
import org.jboss.tools.common.meta.action.XActionInvoker;
import org.jboss.tools.common.model.ServiceDialog;
import org.jboss.tools.common.model.XModelException;
@@ -42,6 +47,8 @@
import org.jboss.tools.common.model.loaders.Reloadable;
import org.jboss.tools.common.model.loaders.XObjectLoader;
import org.jboss.tools.common.model.loaders.impl.PropertiesLoader;
+import org.jboss.tools.common.model.markers.ResourceMarkers;
+import org.jboss.tools.common.model.plugin.ModelPlugin;
import org.jboss.tools.common.model.util.Paths;
import org.jboss.tools.common.model.util.XModelObjectLoaderUtil;
import org.jboss.tools.common.util.FileUtil;
@@ -1058,13 +1065,24 @@
public String get() {
String encoding = null;
- if(ef != null && ef.exists()) {
- encoding = FileUtil.getEncoding(ef);
+ if (ef != null && ef.exists()) {
+ encoding = FileUtil.getEncoding(ef);
+ }
+ if (encoding == null) {
+ encoding = ResourcesPlugin.getEncoding();
+ }
+ try {
+ boolean isUTF8BOM = ModelPlugin.isUTF8BOM(encoding, ef);
+ if (!isUTF8BOM) {
+ return FileUtil.readFileWithEncodingCheck(f, encoding);
+ } else {
+ return ModelPlugin.getContent(ef.getContents(), encoding, true);
}
- if(encoding == null) {
- encoding = ResourcesPlugin.getEncoding();
- }
- return FileUtil.readFileWithEncodingCheck(f, encoding);
+ } catch (CoreException e) {
+ IStatus status = new Status(IStatus.ERROR, ModelPlugin.PLUGIN_ID,
IStatus.OK,e.getLocalizedMessage(), e);
+ ModelPlugin.getDefault().getLog().log(status);
+ return null;
+ }
}
public boolean write(Object object) {
Modified:
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/plugin/ModelPlugin.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/plugin/ModelPlugin.java 2008-11-22
10:51:55 UTC (rev 11963)
+++
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/plugin/ModelPlugin.java 2008-11-23
01:43:21 UTC (rev 11964)
@@ -10,21 +10,25 @@
******************************************************************************/
package org.jboss.tools.common.model.plugin;
+import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
+import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.content.IContentDescription;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWindowListener;
import org.eclipse.ui.IWorkbenchWindow;
-import org.eclipse.ui.PlatformUI;
-import org.eclipse.ui.internal.Workbench;
import org.jboss.tools.common.log.BaseUIPlugin;
import org.jboss.tools.common.log.IPluginLog;
import org.jboss.tools.common.model.XModelConstants;
@@ -126,4 +130,59 @@
public static IPluginLog getPluginLog() {
return getDefault();
}
+
+ public static boolean isUTF8BOM(String encoding, IFile file) throws CoreException {
+ if ("UTF-8".equals(encoding) && file != null &&
file.exists()) { //$NON-NLS-1$
+ IContentDescription description= file.getContentDescription();
+ if (description != null) {
+ byte[] bom= (byte[]) description.getProperty(IContentDescription.BYTE_ORDER_MARK);
+ if (bom != null) {
+ if (bom != IContentDescription.BOM_UTF_8)
+ throw new CoreException(new Status(IStatus.ERROR, ModelPlugin.PLUGIN_ID,
IStatus.OK,"wrongByteOrderMark", null));
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ public static String getContent(InputStream contentStream, String encoding, boolean
skipUTF8BOM) throws CoreException {
+ Reader in= null;
+
+ try {
+ if (skipUTF8BOM) {
+ for (int i= 0; i < 3; i++)
+ if (contentStream.read() == -1) {
+ throw new IOException("notEnoughBytesForBOM");
+ }
+ }
+
+ final int DEFAULT_FILE_SIZE= 15 * 1024;
+ if (encoding == null)
+ in= new BufferedReader(new InputStreamReader(contentStream), DEFAULT_FILE_SIZE);
+ else
+ in= new BufferedReader(new InputStreamReader(contentStream, encoding),
DEFAULT_FILE_SIZE);
+ StringBuffer buffer= new StringBuffer(DEFAULT_FILE_SIZE);
+ char[] readBuffer= new char[2048];
+ int n= in.read(readBuffer);
+ while (n > 0) {
+ buffer.append(readBuffer, 0, n);
+ n= in.read(readBuffer);
+ }
+
+ return buffer.toString();
+
+ } catch (IOException x) {
+ throw new CoreException(new Status(IStatus.ERROR, ModelPlugin.PLUGIN_ID, IStatus.OK,
"Failed to access or read underlying storage", x)); //$NON-NLS-1$
+ } finally {
+ try {
+ if (in != null)
+ in.close();
+ else
+ contentStream.close();
+ } catch (IOException ignored) {
+ }
+ }
+ }
+
}
Show replies by date