01: /*
02: * Copyright Aduna (http://www.aduna-software.com/) (c) 2007.
03: *
04: * Licensed under the Aduna BSD-style license.
05: */
06: package org.openrdf.http.protocol.error;
07:
08: /**
09: *
10: * @author Herko ter Horst
11: * @author Arjohn Kampman
12: */
13: public class ErrorInfo {
14:
15: private ErrorType errorType;
16:
17: private String errMSg;
18:
19: public ErrorInfo(String errMsg) {
20: assert errMsg != null : "errMsg must not be null";
21: this .errMSg = errMsg;
22: }
23:
24: public ErrorInfo(ErrorType errorType, String errMsg) {
25: this (errMsg);
26: this .errorType = errorType;
27: }
28:
29: public ErrorType getErrorType() {
30: return errorType;
31: }
32:
33: public String getErrorMessage() {
34: return errMSg;
35: }
36:
37: @Override
38: public String toString() {
39: if (errorType != null) {
40: StringBuilder sb = new StringBuilder(64);
41: sb.append(errorType);
42: sb.append(": ");
43: sb.append(errMSg);
44: return sb.toString();
45: } else {
46: return errMSg;
47: }
48: }
49:
50: /**
51: * Parses the string output that is produced by {@link #toString()}.
52: */
53: public static ErrorInfo parse(String errInfoString) {
54: String message = errInfoString;
55: ErrorType errorType = null;
56:
57: int colonIdx = errInfoString.indexOf(':');
58: if (colonIdx >= 0) {
59: String label = errInfoString.substring(0, colonIdx).trim();
60: errorType = ErrorType.forLabel(label);
61:
62: if (errorType != null) {
63: message = errInfoString.substring(colonIdx + 1);
64: }
65: }
66:
67: return new ErrorInfo(errorType, message.trim());
68: }
69: }
|