Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.


#1 Bug encoding some multiline strings

Allan Crooks

There is a problem with yamlbeans when it tries to encode some multiline strings - here's an example:

private static String TEST_STRING = "<HIT>Test</HIT> Support 4\n" +
"Sub-total 6\n" +
"Optional - but - work, if it is test it could be by either ABC or DE staff\n" +
" Project Management 4 \"Engagement\" meetings \n" +
"Week Graphic\n" +
" ABC 1 mockup 1\n" +

If that is assigned as an attribute of an object, and then you attempt to YAML-encode it, you get a StringIndexOutOfBoundsException.

I've managed to pin down the cause - in EmitterWriter.writeDoubleQuoted, on line 131, it has the text:
data = text.substring(start, ending) + "\\";

This is very similar to the Ruby line of code which does the same thing - however, it can cope with "ending" being a value smaller than "start" - in the Ruby equivalent, it would return an empty string, whereas here, it throws an exception.

The code clearly expects to be able to cope with this situation, hence the presence of a "start >= ending" test in the if-block.

A suitable change would be to change the lines of code to this:
if (start >= ending) {
data = "";
else {
data = text.substring(start, ending) + "\\";

Other uses of substring in yamlbeans may need to be considered in a similar fashion.