Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 369784

Summary: XMLTypeUtil normalize is unable to handle strings containing only whitespaces
Product: [Modeling] EMF Reporter: Paul Idstein <idstein>
Component: XML/XMIAssignee: Ed Merks <Ed.Merks>
Status: VERIFIED FIXED QA Contact:
Severity: normal    
Priority: P3    
Version: unspecified   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:

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!