some files has strange property values... try to handle them.

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1187644 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sergey Vladimirov 2011-10-22 02:23:19 +00:00
parent ad10ff8187
commit 24f32eec41
2 changed files with 48 additions and 3 deletions

View File

@ -22,11 +22,16 @@ import java.io.UnsupportedEncodingException;
import org.apache.poi.util.Internal; import org.apache.poi.util.Internal;
import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
@Internal @Internal
class CodePageString class CodePageString
{ {
private final static POILogger logger = POILogFactory
.getLogger( CodePageString.class );
private static String codepageToEncoding( final int codepage ) private static String codepageToEncoding( final int codepage )
throws UnsupportedEncodingException throws UnsupportedEncodingException
{ {
@ -172,7 +177,23 @@ class CodePageString
result = new String( _value ); result = new String( _value );
else else
result = new String( _value, codepageToEncoding( codepage ) ); result = new String( _value, codepageToEncoding( codepage ) );
return result.substring( 0, result.length() - 1 ); final int terminator = result.indexOf( '\0' );
if ( terminator == -1 )
{
logger.log(
POILogger.WARN,
"String terminator (\\0) for CodePageString property value not found."
+ "Continue without trimming and hope for the best." );
return result;
}
if ( terminator != result.length() - 1 )
{
logger.log(
POILogger.WARN,
"String terminator (\\0) for CodePageString property value occured before the end of string. "
+ "Trimming and hope for the best." );
}
return result.substring( 0, terminator );
} }
int getSize() int getSize()

View File

@ -18,11 +18,17 @@ package org.apache.poi.hpsf;
import org.apache.poi.util.Internal; import org.apache.poi.util.Internal;
import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
import org.apache.poi.util.StringUtil; import org.apache.poi.util.StringUtil;
@Internal @Internal
class UnicodeString class UnicodeString
{ {
private final static POILogger logger = POILogFactory
.getLogger( UnicodeString.class );
private byte[] _value; private byte[] _value;
UnicodeString( byte[] data, int offset ) UnicodeString( byte[] data, int offset )
@ -59,7 +65,25 @@ class UnicodeString
if ( _value.length == 0 ) if ( _value.length == 0 )
return null; return null;
return StringUtil.getFromUnicodeLE( _value, 0, String result = StringUtil.getFromUnicodeLE( _value, 0,
( _value.length - 2 ) >> 1 ); _value.length >> 1 );
final int terminator = result.indexOf( '\0' );
if ( terminator == -1 )
{
logger.log(
POILogger.WARN,
"String terminator (\\0) for UnicodeString property value not found."
+ "Continue without trimming and hope for the best." );
return result;
}
if ( terminator != result.length() - 1 )
{
logger.log(
POILogger.WARN,
"String terminator (\\0) for UnicodeString property value occured before the end of string. "
+ "Trimming and hope for the best." );
}
return result.substring( 0, terminator );
} }
} }