61337 -- try to convert assertions to exceptions. I left in the assertions for the binary search components.

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1803092 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Tim Allison 2017-07-26 18:43:27 +00:00
parent 21cbf101bc
commit 16f331054f
3 changed files with 90 additions and 23 deletions

View File

@ -0,0 +1,53 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.util;
/**
* This is similar to {@link RecordFormatException}, except this is thrown
* when there's a higher order problem with parsing a document beyond individual records.
*/
public class DocumentFormatException extends RuntimeException {
public DocumentFormatException(String exception) {
super(exception);
}
public DocumentFormatException(String exception, Throwable thr) {
super(exception, thr);
}
public DocumentFormatException(Throwable thr) {
super(thr);
}
/**
* Syntactic sugar to check whether a DocumentFormatException should
* be thrown. If assertTrue is <code>false</code>, this will throw this
* exception with the message.
*
* @param assertTrue
* @param message
*/
public static void check(boolean assertTrue, String message) {
if (!assertTrue) {
throw new DocumentFormatException(message);
}
}
}

View File

@ -39,4 +39,18 @@ public class RecordFormatException
public RecordFormatException(Throwable thr) {
super(thr);
}
/**
* Syntactic sugar to check whether a RecordFormatException should
* be thrown. If assertTrue is <code>false</code>, this will throw this
* exception with the message.
*
* @param assertTrue
* @param message
*/
public static void check(boolean assertTrue, String message) {
if (! assertTrue) {
throw new RecordFormatException(message);
}
}
}

View File

@ -32,6 +32,7 @@ import org.apache.poi.hwpf.model.SubdocumentType;
import org.apache.poi.hwpf.sprm.CharacterSprmCompressor;
import org.apache.poi.hwpf.sprm.ParagraphSprmCompressor;
import org.apache.poi.hwpf.sprm.SprmBuffer;
import org.apache.poi.util.DocumentFormatException;
import org.apache.poi.util.Internal;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.POILogFactory;
@ -192,7 +193,7 @@ public class Range { // TODO -instantiable superclass
_parent = new WeakReference<Range>(parent);
sanityCheckStartEnd();
assert sanityCheck();
sanityCheck();
}
@ -328,7 +329,7 @@ public class Range { // TODO -instantiable superclass
// update the FIB.CCPText + friends fields
adjustFIB( text.length() );
assert sanityCheck();
sanityCheck();
return getCharacterRun( 0 );
}
@ -356,7 +357,7 @@ public class Range { // TODO -instantiable superclass
}
adjustForInsert( text.length() );
assert sanityCheck();
sanityCheck();
return getCharacterRun( numCharacterRuns() - 1 );
}
@ -965,8 +966,9 @@ public class Range { // TODO -instantiable superclass
if ( startIndex < 0 || startIndex >= rpl.size()
|| startIndex > endIndex || endIndex < 0
|| endIndex >= rpl.size() )
throw new AssertionError();
|| endIndex >= rpl.size() ) {
throw new DocumentFormatException("problem finding range");
}
return new int[] { startIndex, endIndex + 1 };
}
@ -1050,7 +1052,9 @@ public class Range { // TODO -instantiable superclass
*/
protected void adjustFIB( int adjustment )
{
assert ( _doc instanceof HWPFDocument );
if (!( _doc instanceof HWPFDocument)) {
throw new IllegalArgumentException("doc must be instance of HWPFDocument");
}
// update the FIB.CCPText field (this should happen once per adjustment,
// so we don't want it in
@ -1148,20 +1152,19 @@ public class Range { // TODO -instantiable superclass
/**
* Method for debug purposes. Checks that all resolved elements are inside
* of current range.
* of current range. Throws {@link IllegalArgumentException} if checks fail.
*/
public boolean sanityCheck()
{
if ( _start < 0 )
throw new AssertionError();
if ( _start > _text.length() )
throw new AssertionError();
if ( _end < 0 )
throw new AssertionError();
if ( _end > _text.length() )
throw new AssertionError();
if ( _start > _end )
throw new AssertionError();
DocumentFormatException.check(_start >= 0,
"start can't be < 0");
DocumentFormatException.check( _start <= _text.length(),
"start can't be > text length");
DocumentFormatException.check( _end >= 0,
"end can't be < 0");
DocumentFormatException.check( _end <= _text.length(),
"end can't be > text length");
DocumentFormatException.check( _start <= _end,"start can't be > end");
if ( _charRangeFound )
{
@ -1171,9 +1174,7 @@ public class Range { // TODO -instantiable superclass
int left = Math.max( this._start, chpx.getStart() );
int right = Math.min( this._end, chpx.getEnd() );
if ( left >= right )
throw new AssertionError();
DocumentFormatException.check(left < right, "left must be < right");
}
}
if ( _parRangeFound )
@ -1185,11 +1186,10 @@ public class Range { // TODO -instantiable superclass
int left = Math.max( this._start, papx.getStart() );
int right = Math.min( this._end, papx.getEnd() );
if ( left >= right )
throw new AssertionError();
DocumentFormatException.check( left < right,
"left must be < right");
}
}
return true;
}
}