Author: mattmann Date: Thu Nov 23 09:15:55 2006 New Revision: 478619 URL: http://svn.apache.org/viewvc?view=rev&rev=478619 Log: - fix for NUTCH-406 Metadata tries to write null values
Modified: lucene/nutch/trunk/CHANGES.txt lucene/nutch/trunk/src/java/org/apache/nutch/metadata/Metadata.java Modified: lucene/nutch/trunk/CHANGES.txt URL: http://svn.apache.org/viewvc/lucene/nutch/trunk/CHANGES.txt?view=diff&rev=478619&r1=478618&r2=478619 ============================================================================== --- lucene/nutch/trunk/CHANGES.txt (original) +++ lucene/nutch/trunk/CHANGES.txt Thu Nov 23 09:15:55 2006 @@ -89,6 +89,8 @@ 29. NUTCH-305 - Update crawl and url filter lists to exclude jpeg|JPEG|bmp|BMP, suffix-urlfilter.txt (contributed by Stefan Neufeind) is also updated (siren) + +30. NUTCH-406 - Metadata tries to write null values (mattmann) Release 0.8 - 2006-07-25 Modified: lucene/nutch/trunk/src/java/org/apache/nutch/metadata/Metadata.java URL: http://svn.apache.org/viewvc/lucene/nutch/trunk/src/java/org/apache/nutch/metadata/Metadata.java?view=diff&rev=478619&r1=478618&r2=478619 ============================================================================== --- lucene/nutch/trunk/src/java/org/apache/nutch/metadata/Metadata.java (original) +++ lucene/nutch/trunk/src/java/org/apache/nutch/metadata/Metadata.java Thu Nov 23 09:15:55 2006 @@ -19,10 +19,14 @@ import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; +import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Properties; +import java.util.Vector; + import org.apache.hadoop.io.Text; import org.apache.hadoop.io.Writable; @@ -209,9 +213,13 @@ for (int i = 0; i < names.length; i++) { Text.writeString(out, names[i]); values = getValues(names[i]); - out.writeInt(values.length); - for (int j = 0; j < values.length; j++) { - Text.writeString(out, values[j]); + // @since NUTCH-406: get the set + // of non-null values, and work with + // them to avoid NPE exception + String [] nonNullVals = getNonNullValues(values); + out.writeInt(nonNullVals.length); + for (int j = 0; j < nonNullVals.length; j++) { + Text.writeString(out, nonNullVals[j]); } } } @@ -227,5 +235,20 @@ } } } + + private String[] getNonNullValues(String[] values) { + if (values == null) { + return new String[0]; + } + + List<String> nonNull = new Vector<String>(); + for (int i = 0; i < values.length; i++) { + if (values[i] != null && !values[i].equals("")) { + nonNull.add(values[i]); + } + } + + return nonNull.toArray(new String[nonNull.size()]); + } }