Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 116405 Details for
Bug 251748
XMLContentDescriber doesn't set charset for XML declarations with line feeds
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read
this important communication.
[patch]
Fix v01
251748_20081029.txt (text/plain), 12.40 KB, created by
Szymon Brandys
on 2008-10-29 07:40:44 EDT
(
hide
)
Description:
Fix v01
Filename:
MIME Type:
Creator:
Szymon Brandys
Created:
2008-10-29 07:40:44 EDT
Size:
12.40 KB
patch
obsolete
>### Eclipse Workspace Patch 1.0 >#P org.eclipse.core.contenttype >Index: src/org/eclipse/core/internal/content/XMLContentDescriber.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.core.contenttype/src/org/eclipse/core/internal/content/XMLContentDescriber.java,v >retrieving revision 1.4 >diff -u -r1.4 XMLContentDescriber.java >--- src/org/eclipse/core/internal/content/XMLContentDescriber.java 10 Oct 2008 08:15:40 -0000 1.4 >+++ src/org/eclipse/core/internal/content/XMLContentDescriber.java 29 Oct 2008 11:40:12 -0000 >@@ -30,6 +30,7 @@ > 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$ > > public int describe(InputStream input, IContentDescription description) throws IOException { > byte[] bom = getByteOrderMark(input); >@@ -37,60 +38,25 @@ > input.reset(); > if (bom != null) { > if (bom == IContentDescription.BOM_UTF_16BE) >- xmlDeclEncoding = "UTF-16BE"; //$NON-NLS-1$ >+ xmlDeclEncoding = "UTF-16";//BE"; //$NON-NLS-1$ > else if (bom == IContentDescription.BOM_UTF_16LE) >- xmlDeclEncoding = "UTF-16LE"; //$NON-NLS-1$ >+ xmlDeclEncoding = "UTF-16";//LE"; //$NON-NLS-1$ > // skip BOM to make comparison simpler > input.skip(bom.length); > // set the BOM in the description if requested > if (description != null && description.isRequested(IContentDescription.BYTE_ORDER_MARK)) > description.setProperty(IContentDescription.BYTE_ORDER_MARK, bom); > } >- byte[] xmlPrefixBytes = XML_PREFIX.getBytes(xmlDeclEncoding); >- byte[] prefix = new byte[xmlPrefixBytes.length]; >- if (input.read(prefix) < prefix.length) >- // there is not enough info to say anything >- return INDETERMINATE; >- for (int i = 0; i < prefix.length; i++) >- if (prefix[i] != xmlPrefixBytes[i]) >- // we don't have a XMLDecl... there is not enough info to say anything >- return INDETERMINATE; >- if (description == null) >- return VALID; >- // describe charset if requested >- if (description.isRequested(IContentDescription.CHARSET)) { >- String fullXMLDecl = readFullXMLDecl(input, xmlDeclEncoding); >- if (fullXMLDecl != null) { >- String charset = getCharset(fullXMLDecl); >- if (charset != null && !isCharsetValid(charset)) >- return INVALID; >- if (charset != null && !charset.matches("[uU][tT][fF](-)?8")) //$NON-NLS-1$ >- // only set property if value is not default (avoid using a non-default content description) >- description.setProperty(IContentDescription.CHARSET, charset); >- if (charset == null || !charset.matches("[uU][tT][fF](-)?(8|16)")) >- description.setProperty(IContentDescription.BYTE_ORDER_MARK, null); >- } >- } >- return VALID; >+ return internalDescribe(new InputStreamReader(input, xmlDeclEncoding), description); > } > >- private boolean isCharsetValid(String charset){ >- return charset.matches("[A-Za-z]([A-Za-z0-9._\\-])*"); >- } >- >- private String readFullXMLDecl(InputStream input, String unicodeEncoding) throws IOException { >- byte[] xmlDecl = new byte[100]; >- int c = 0; >- // looks for XMLDecl ending char (?) >- int read = 0; >- while (read < xmlDecl.length && (c = input.read()) != -1 && c !='\r' && c !='\n') >- xmlDecl[read++] = (byte) c; >- return new String(xmlDecl, 0, read, unicodeEncoding); >+ public int describe(Reader input, IContentDescription description) throws IOException { >+ return internalDescribe(input, description); > } >- >- public int describe(Reader input, IContentDescription description) throws IOException { >- BufferedReader reader = new BufferedReader(input); >- String line = reader.readLine(); >+ >+ private int internalDescribe(Reader input, IContentDescription description) throws IOException { >+ String line = readXMLDecl(input); >+ > // end of stream > if (line == null) > return INDETERMINATE; >@@ -109,6 +75,24 @@ > description.setProperty(IContentDescription.CHARSET, charset); > } > return VALID; >+ } >+ >+ private boolean isFullXMLDecl(String xmlDecl) { >+ return xmlDecl.endsWith(XML_DECL_END); >+ } >+ >+ private String readXMLDecl(Reader input) throws IOException { >+ BufferedReader reader = new BufferedReader(input); >+ StringBuffer buffer = new StringBuffer(); >+ String line = null; >+ >+ while (buffer.length() < 100 && ((line = reader.readLine()) != null)) { >+ buffer.append(line); >+ if (line.indexOf(XML_DECL_END) != -1) { >+ return buffer.substring(0, buffer.indexOf(XML_DECL_END) + XML_DECL_END.length()); >+ } >+ } >+ return buffer.toString(); > } > > private String getCharset(String firstLine) { >@@ -124,11 +108,17 @@ > if (firstQuote == -1 || firstLine.length() == firstQuote - 1) > return null; > int secondQuote = firstLine.indexOf(quoteChar, firstQuote + 1); >- if (secondQuote == -1) >+ if (secondQuote == -1 && !isFullXMLDecl(firstLine)) > return null; >+ else if (secondQuote == -1 && isFullXMLDecl(firstLine)) >+ return firstLine.substring(firstQuote + 1); > return firstLine.substring(firstQuote + 1, secondQuote); > } > >+ private boolean isCharsetValid(String charset) { >+ return charset.matches("[A-Za-z]([A-Za-z0-9._\\-])*"); >+ } >+ > public QualifiedName[] getSupportedOptions() { > return SUPPORTED_OPTIONS; > } >#P org.eclipse.core.tests.runtime >Index: src/org/eclipse/core/tests/runtime/content/XMLContentDescriberTest.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.core.tests.runtime/src/org/eclipse/core/tests/runtime/content/XMLContentDescriberTest.java,v >retrieving revision 1.11 >diff -u -r1.11 XMLContentDescriberTest.java >--- src/org/eclipse/core/tests/runtime/content/XMLContentDescriberTest.java 10 Oct 2008 08:15:39 -0000 1.11 >+++ src/org/eclipse/core/tests/runtime/content/XMLContentDescriberTest.java 29 Oct 2008 11:40:13 -0000 >@@ -45,22 +45,44 @@ > > checkEncodedContents(ENCODING_NOTSUPPORTED, ENCODING_NOTSUPPORTED, ENCODING_UTF8); > checkEncodedContents(ENCODING_NOTSUPPORTED, ENCODING_NOTSUPPORTED, ENCODING_UTF16); >+ >+ checkEncodedContents(ENCODING_UTF16, ENCODING_UTF16, ENCODING_UTF16, true); >+ checkEncodedContents(ENCODING_UTF8, ENCODING_UTF8, ENCODING_UTF8, true); >+ >+ checkEncodedContents(ENCODING_UTF16, ENCODING_UTF16, ENCODING_UTF8, true); >+ checkEncodedContents(ENCODING_UTF8, ENCODING_UTF8, ENCODING_UTF16, true); >+ >+ checkEncodedContents(ENCODING_NOTSUPPORTED, ENCODING_NOTSUPPORTED, ENCODING_UTF8, true); >+ checkEncodedContents(ENCODING_NOTSUPPORTED, ENCODING_NOTSUPPORTED, ENCODING_UTF16, true); > } > > public void testEncodedContents2() throws Exception { > checkEncodedContents2(ENCODING_UTF16, ENCODING_UTF16); > checkEncodedContents2(ENCODING_UTF8, ENCODING_UTF8); > checkEncodedContents2(ENCODING_NOTSUPPORTED, ENCODING_NOTSUPPORTED); >+ >+ checkEncodedContents2(ENCODING_UTF16, ENCODING_UTF16, true); >+ checkEncodedContents2(ENCODING_UTF8, ENCODING_UTF8, true); >+ checkEncodedContents2(ENCODING_NOTSUPPORTED, ENCODING_NOTSUPPORTED, true); > } > > public void testEncodedContents3() throws Exception { >- IContentDescription description = Platform.getContentTypeManager().getDescriptionFor(getInputStream(ENCODING_INCORRECT, ENCODING_UTF8), "fake.xml", new QualifiedName[] {IContentDescription.CHARSET}); >+ IContentDescription description = Platform.getContentTypeManager().getDescriptionFor(getInputStream(ENCODING_INCORRECT, ENCODING_UTF8, false), "fake.xml", new QualifiedName[] {IContentDescription.CHARSET}); >+ assertNull("1.0", description); >+ >+ description = Platform.getContentTypeManager().getDescriptionFor(getInputStream(ENCODING_INCORRECT, ENCODING_UTF16, false), "fake.xml", new QualifiedName[] {IContentDescription.CHARSET}); >+ assertNull("2.0", description); >+ >+ description = Platform.getContentTypeManager().getDescriptionFor(getReader(ENCODING_INCORRECT, false), "fake.xml", new QualifiedName[] {IContentDescription.CHARSET}); >+ assertNull("3.0", description); >+ >+ description = Platform.getContentTypeManager().getDescriptionFor(getInputStream(ENCODING_INCORRECT, ENCODING_UTF8, true), "fake.xml", new QualifiedName[] {IContentDescription.CHARSET}); > assertNull("1.0", description); > >- description = Platform.getContentTypeManager().getDescriptionFor(getInputStream(ENCODING_INCORRECT, ENCODING_UTF16), "fake.xml", new QualifiedName[] {IContentDescription.CHARSET}); >+ description = Platform.getContentTypeManager().getDescriptionFor(getInputStream(ENCODING_INCORRECT, ENCODING_UTF16, true), "fake.xml", new QualifiedName[] {IContentDescription.CHARSET}); > assertNull("2.0", description); > >- description = Platform.getContentTypeManager().getDescriptionFor(getReader(ENCODING_INCORRECT), "fake.xml", new QualifiedName[] {IContentDescription.CHARSET}); >+ description = Platform.getContentTypeManager().getDescriptionFor(getReader(ENCODING_INCORRECT, true), "fake.xml", new QualifiedName[] {IContentDescription.CHARSET}); > assertNull("3.0", description); > } > >@@ -68,27 +90,35 @@ > return new TestSuite(XMLContentDescriberTest.class); > } > >- private void checkEncodedContents(String resultEncoding, String encodingInContent, String encoding) throws Exception { >- IContentDescription description = Platform.getContentTypeManager().getDescriptionFor(getInputStream(encodingInContent, encoding), "fake.xml", new QualifiedName[] {IContentDescription.CHARSET}); >+ private void checkEncodedContents(String resultEncoding, String encodingInContent, String encoding, boolean encInNewLine) throws Exception { >+ IContentDescription description = Platform.getContentTypeManager().getDescriptionFor(getInputStream(encodingInContent, encoding, encInNewLine), "fake.xml", new QualifiedName[] {IContentDescription.CHARSET}); > assertNotNull("1.0", description); > assertEquals("1.1", Platform.PI_RUNTIME + ".xml", description.getContentType().getId()); > assertEquals("1.2", resultEncoding, description.getProperty(IContentDescription.CHARSET)); > } > >- private void checkEncodedContents2(String resultEncoding, String encodingInContent) throws Exception { >- IContentDescription description = Platform.getContentTypeManager().getDescriptionFor(getReader(encodingInContent), "fake.xml", new QualifiedName[] {IContentDescription.CHARSET}); >+ private void checkEncodedContents(String resultEncoding, String encodingInContent, String encoding) throws Exception { >+ checkEncodedContents(resultEncoding, encodingInContent, encoding, false); >+ } >+ >+ private void checkEncodedContents2(String resultEncoding, String encodingInContent, boolean encInNewLine) throws Exception { >+ IContentDescription description = Platform.getContentTypeManager().getDescriptionFor(getReader(encodingInContent, encInNewLine), "fake.xml", new QualifiedName[] {IContentDescription.CHARSET}); > assertNotNull("2.0", description); > assertEquals("2.1", Platform.PI_RUNTIME + ".xml", description.getContentType().getId()); > assertEquals("2.2", resultEncoding, description.getProperty(IContentDescription.CHARSET)); > } > >- private InputStream getInputStream(String encodingInContent, String encoding) throws UnsupportedEncodingException { >- String content = "<?xml version=\"1.0\" encoding=\"" + encodingInContent + "\"?><root attribute=\"" + ENCODED_TEXT + "\">"; >+ private void checkEncodedContents2(String resultEncoding, String encodingInContent) throws Exception { >+ checkEncodedContents2(resultEncoding, encodingInContent, false); >+ } >+ >+ private InputStream getInputStream(String encodingInContent, String encoding, boolean encInNewLine) throws UnsupportedEncodingException { >+ String content = "<?xml version=\"1.0\"" + (encInNewLine ? "\n" : "") + "encoding=\"" + encodingInContent + "\"?><root attribute=\"" + ENCODED_TEXT + "\">"; > return new ByteArrayInputStream(content.getBytes(encoding)); > } > >- private Reader getReader(String encodingInContent) { >- String content = "<?xml version=\"1.0\" encoding=\"" + encodingInContent + "\"?><root attribute=\"" + ENCODED_TEXT + "\">"; >+ private Reader getReader(String encodingInContent, boolean encInNewLine) { >+ String content = "<?xml version=\"1.0\"" + (encInNewLine ? "\n" : "") + " encoding=\"" + encodingInContent + "\"?><root attribute=\"" + ENCODED_TEXT + "\">"; > return new InputStreamReader(new ByteArrayInputStream(content.getBytes())); > } > }
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 251748
: 116405