Author: tallison
Date: Wed Jul 26 18:43:27 2017
New Revision: 1803092
URL: http://svn.apache.org/viewvc?rev=1803092&view=rev
Log:
61337 -- try to convert assertions to exceptions. I left in the assertions for
the binary search components.
Added:
poi/trunk/src/java/org/apache/poi/util/DocumentFormatException.java (with
props)
Modified:
poi/trunk/src/java/org/apache/poi/util/RecordFormatException.java
poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java
Added: poi/trunk/src/java/org/apache/poi/util/DocumentFormatException.java
URL:
http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/util/DocumentFormatException.java?rev=1803092&view=auto
==============================================================================
--- poi/trunk/src/java/org/apache/poi/util/DocumentFormatException.java (added)
+++ poi/trunk/src/java/org/apache/poi/util/DocumentFormatException.java Wed Jul
26 18:43:27 2017
@@ -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);
+ }
+ }
+}
Propchange: poi/trunk/src/java/org/apache/poi/util/DocumentFormatException.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: poi/trunk/src/java/org/apache/poi/util/RecordFormatException.java
URL:
http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/util/RecordFormatException.java?rev=1803092&r1=1803091&r2=1803092&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/util/RecordFormatException.java (original)
+++ poi/trunk/src/java/org/apache/poi/util/RecordFormatException.java Wed Jul
26 18:43:27 2017
@@ -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);
+ }
+ }
}
Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java
URL:
http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java?rev=1803092&r1=1803091&r2=1803092&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java
(original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java Wed
Jul 26 18:43:27 2017
@@ -32,6 +32,7 @@ import org.apache.poi.hwpf.model.Subdocu
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 -instantiab
_parent = new WeakReference<Range>(parent);
sanityCheckStartEnd();
- assert sanityCheck();
+ sanityCheck();
}
@@ -328,7 +329,7 @@ public class Range { // TODO -instantiab
// update the FIB.CCPText + friends fields
adjustFIB( text.length() );
- assert sanityCheck();
+ sanityCheck();
return getCharacterRun( 0 );
}
@@ -356,7 +357,7 @@ public class Range { // TODO -instantiab
}
adjustForInsert( text.length() );
- assert sanityCheck();
+ sanityCheck();
return getCharacterRun( numCharacterRuns() - 1 );
}
@@ -965,8 +966,9 @@ public class Range { // TODO -instantiab
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 -instantiab
*/
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 -instantiab
/**
* 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 -instantiab
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 -instantiab
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;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]