Bug 325715 - [Backport] [content type] Platform doesn't recognize xml encoding attribute with additional whitespaces
diff --git a/bundles/org.eclipse.core.contenttype/src/org/eclipse/core/runtime/content/XMLContentDescriber.java b/bundles/org.eclipse.core.contenttype/src/org/eclipse/core/runtime/content/XMLContentDescriber.java index 5c00a90..523b578 100644 --- a/bundles/org.eclipse.core.contenttype/src/org/eclipse/core/runtime/content/XMLContentDescriber.java +++ b/bundles/org.eclipse.core.contenttype/src/org/eclipse/core/runtime/content/XMLContentDescriber.java
@@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2009 IBM Corporation and others. + * Copyright (c) 2004, 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 @@ -45,7 +45,6 @@ */ public class XMLContentDescriber extends TextContentDescriber implements ITextContentDescriber { private static final QualifiedName[] SUPPORTED_OPTIONS = new QualifiedName[] {IContentDescription.CHARSET, IContentDescription.BYTE_ORDER_MARK}; - private static final String ENCODING = "encoding="; //$NON-NLS-1$ private static final String XML_PREFIX = "<?xml "; //$NON-NLS-1$ private static final String XML_DECL_END = "?>"; //$NON-NLS-1$ @@ -138,7 +137,7 @@ } private String getCharset(String firstLine) { - int encodingPos = firstLine.indexOf(ENCODING); + int encodingPos = findEncodingPosition(firstLine); if (encodingPos == -1) return null; char quoteChar = '"'; @@ -147,13 +146,36 @@ quoteChar = '\''; firstQuote = firstLine.indexOf(quoteChar, encodingPos); } - if (firstQuote == -1 || firstLine.length() == firstQuote - 1) + if (firstQuote == -1 || firstLine.length() == firstQuote + 1) return null; int secondQuote = firstLine.indexOf(quoteChar, firstQuote + 1); if (secondQuote == -1) return isFullXMLDecl(firstLine) ? firstLine.substring(firstQuote + 1, firstLine.lastIndexOf(XML_DECL_END)).trim() : null; return firstLine.substring(firstQuote + 1, secondQuote); } + + private int findEncodingPosition(String line) { + String encoding = "encoding"; //$NON-NLS-1$ + int fromIndex = 0; + int position = 0; + while ((position = line.indexOf(encoding, fromIndex)) != -1) { + boolean equals = false; + fromIndex = position + encoding.length(); + for (int i = fromIndex; i < line.length(); i++) { + char c = line.charAt(i); + if (c == '=' && !equals) { + equals = true; + } else if (c == 0x20 || c == 0x09 || c == 0x0D || c == 0x0A) { + // white space characters to ignore + } else if ((c == '"' || c == '\'') && equals) { + return position; + } else { + break; + } + } + } + return -1; + } private boolean isCharsetValid(String charset) { if (charset.length() == 0)