Skip to content

Commit e3f7725

Browse files
NUTCH-2788 ParseData: improve presentation of Metadata in method toString()
- switch to multi-line presentation of Metadata in ParseData::toString - default implementation of Metadata::toString is still single-line - replace StringBuffer by StringBuilder in modified methods
1 parent 9139d6e commit e3f7725

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

src/java/org/apache/nutch/metadata/Metadata.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,28 @@ public boolean equals(Object o) {
232232
return true;
233233
}
234234

235+
@Override
235236
public String toString() {
236-
StringBuffer buf = new StringBuffer();
237+
return toString("=", " ");
238+
}
239+
240+
/**
241+
* @param separator
242+
* separator between Metadata's key-value pairs
243+
* @param keyValueSeparator
244+
* separator between key and value
245+
* @return list of all key-value pairs in Metadata using the provided
246+
* separators
247+
*/
248+
public String toString(String separator, String keyValueSeparator) {
249+
StringBuilder buf = new StringBuilder();
237250
String[] names = names();
238251
for (int i = 0; i < names.length; i++) {
239252
String[] values = _getValues(names[i]);
240253
for (int j = 0; j < values.length; j++) {
241-
buf.append(names[i]).append("=").append(values[j]).append(" ");
254+
if (buf.length() > 0)
255+
buf.append(separator);
256+
buf.append(names[i]).append(keyValueSeparator).append(values[j]);
242257
}
243258
}
244259
return buf.toString();
@@ -278,3 +293,4 @@ public final void readFields(DataInput in) throws IOException {
278293
}
279294

280295
}
296+

src/java/org/apache/nutch/parse/ParseData.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,22 +188,25 @@ public boolean equals(Object o) {
188188
&& this.parseMeta.equals(other.parseMeta);
189189
}
190190

191+
@Override
191192
public String toString() {
192-
StringBuffer buffer = new StringBuffer();
193+
StringBuilder buffer = new StringBuilder();
193194

194-
buffer.append("Version: " + version + "\n");
195-
buffer.append("Status: " + status + "\n");
196-
buffer.append("Title: " + title + "\n");
195+
buffer.append("Version: ").append(version).append("\n");
196+
buffer.append("Status: ").append(status).append("\n");
197+
buffer.append("Title: ").append(title ).append("\n");
197198

198199
if (outlinks != null) {
199-
buffer.append("Outlinks: " + outlinks.length + "\n");
200+
buffer.append("Outlinks: ").append(outlinks.length).append("\n");
200201
for (int i = 0; i < outlinks.length; i++) {
201-
buffer.append(" outlink: " + outlinks[i] + "\n");
202+
buffer.append(" outlink: ").append(outlinks[i]).append("\n");
202203
}
203204
}
204205

205-
buffer.append("Content Metadata: " + contentMeta + "\n");
206-
buffer.append("Parse Metadata: " + parseMeta + "\n");
206+
buffer.append("Content Metadata:\n ")
207+
.append(contentMeta.toString("\n ", " = ")).append("\n");
208+
buffer.append("Parse Metadata:\n ")
209+
.append(parseMeta.toString("\n ", " = ")).append("\n");
207210

208211
return buffer.toString();
209212
}

src/java/org/apache/nutch/parse/ParserChecker.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ protected int process(String url, StringBuilder output) throws Exception {
288288
}
289289
}
290290

291-
output.append(turl + "\n");
292-
output.append(parse.getData() + "\n");
291+
output.append(turl).append("\n");
292+
output.append(parse.getData()).append("\n");
293293
if (dumpText) {
294294
output.append(parse.getText());
295295
}

0 commit comments

Comments
 (0)