Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 369784 - XMLTypeUtil normalize is unable to handle strings containing only whitespaces
Summary: XMLTypeUtil normalize is unable to handle strings containing only whitespaces
Status: VERIFIED FIXED
Alias: None
Product: EMF
Classification: Modeling
Component: XML/XMI (show other bugs)
Version: unspecified   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Ed Merks CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-01-26 05:46 EST by Paul Idstein CLA
Modified: 2012-01-27 10:44 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Paul Idstein CLA 2012-01-26 05:46:00 EST
Build Identifier: Version: 3.6.2.v201102251823 Build id: M20110210-1200

A call normalize(String value, boolean collapse) will result in a java.lang.StringIndexOutOfBoundsException: String index out of range: -1 when value contains only whitespaces.

Line 179: buffer.deleteCharAt(i - offset++); //unsafe for String containing only whitespaces as it results in a boundary exception as stated earlier.

Fix:
Call to String.trim() before length check at line 158.

Reproducible: Always

Steps to Reproduce:
1. Create a XML document with a element enclosing whitespaces: e.g.
<?xml version="1.0" encoding="UTF-8"?>
<empty>    </empty>
2. Call org.eclipse.emf.ecore.xml.XMLTypeUtil.normalize on element empty
Comment 1 Ed Merks CLA 2012-01-26 06:48:02 EST
Can you show me a string value I can pass that will display this problem.  I can't reproduce it with any value I've tried, including null, an empty string, a string with one space, a string with two spaces, a string with three spaces, or anything else I tried.


    String[] EXAMPLES = { null, "", " ", "   ", "    ", "  a  ", "\n\ta\t\n"};
    for (int i = 0; i < EXAMPLES.length; ++i)
    {
      for (int j = 0; j < 2; ++j)
      {
        System.out.println("'" + EXAMPLES[i] + "' -> '" + XMLTypeUtil.normalize(EXAMPLES[i], j !=0) + "'");
      }
    }

I don't know how to "call normalize on element empty".

Please provide an actual test case I can run locally and then reopen.
Comment 2 Paul Idstein CLA 2012-01-27 10:44:08 EST
Hi there,

indeed your sample works pretty well for me, too! In fact the bug occurred when we were assign a value to an element of a DataObject (IBM WebSphere ESB Implementation specifc).

The value is propagated down to XMLTypeUtil.normalize and is binary a string of spaces as printed via String.format("%x", new BigInteger( value )) -> 20 20 20 20

So the error might be somewhere in between as we have a valid String before and within calling XMLTypeUtil.norrmalize we'll end up with this StringIndexOutOfBoundsException.

I'll have to catch what's propagated in the final call.

Thanks so far!