From 16f331054ff07d42e479edcae361ea708321d3f0 Mon Sep 17 00:00:00 2001 From: Tim Allison Date: Wed, 26 Jul 2017 18:43:27 +0000 Subject: [PATCH] 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 --- .../poi/util/DocumentFormatException.java | 53 +++++++++++++++++++ .../poi/util/RecordFormatException.java | 14 +++++ .../org/apache/poi/hwpf/usermodel/Range.java | 46 ++++++++-------- 3 files changed, 90 insertions(+), 23 deletions(-) create mode 100644 src/java/org/apache/poi/util/DocumentFormatException.java diff --git a/src/java/org/apache/poi/util/DocumentFormatException.java b/src/java/org/apache/poi/util/DocumentFormatException.java new file mode 100644 index 0000000000..e53202235c --- /dev/null +++ b/src/java/org/apache/poi/util/DocumentFormatException.java @@ -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 false, 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); + } + } +} diff --git a/src/java/org/apache/poi/util/RecordFormatException.java b/src/java/org/apache/poi/util/RecordFormatException.java index d1643b8be0..2bc4ba3bcb 100644 --- a/src/java/org/apache/poi/util/RecordFormatException.java +++ b/src/java/org/apache/poi/util/RecordFormatException.java @@ -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 false, 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); + } + } } diff --git a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java index 10b3f963c1..425e8036e8 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java @@ -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(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; } } -- 2.39.5