1 |
ef934f44
|
unknown
|
/**
|
2 |
|
|
* Copyright (C) 2017 EDIT
|
3 |
|
|
* European Distributed Institute of Taxonomy
|
4 |
|
|
* http://www.e-taxonomy.eu
|
5 |
|
|
*
|
6 |
|
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7 |
|
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8 |
|
|
*/
|
9 |
|
|
package eu.etaxonomy.cdm.io.common;
|
10 |
|
|
|
11 |
|
|
import java.util.ArrayList;
|
12 |
|
|
import java.util.List;
|
13 |
|
|
|
14 |
c3d8375b
|
Andreas Müller
|
import org.apache.commons.lang3.StringUtils;
|
15 |
|
|
|
16 |
ef934f44
|
unknown
|
/**
|
17 |
|
|
* @author a.mueller
|
18 |
|
|
* @date 24.03.2017
|
19 |
|
|
*
|
20 |
|
|
*/
|
21 |
|
|
public abstract class IoResultBase {
|
22 |
|
|
|
23 |
b93248b0
|
Andreas Müller
|
private List<IoInfo> errors = new ArrayList<>();
|
24 |
cd602b47
|
Andreas Müller
|
private List<IoInfo> warnings = new ArrayList<>();
|
25 |
|
|
private List<IoInfo> exceptions = new ArrayList<>();
|
26 |
c3d8375b
|
Andreas Müller
|
|
27 |
cd602b47
|
Andreas Müller
|
public class IoInfo{
|
28 |
c3d8375b
|
Andreas Müller
|
String message;
|
29 |
|
|
Exception exception;
|
30 |
cd602b47
|
Andreas Müller
|
String location;
|
31 |
|
|
private IoInfo(String msg, Exception e){
|
32 |
|
|
this.message = msg;
|
33 |
|
|
this.exception = e;
|
34 |
|
|
}
|
35 |
|
|
private IoInfo(String msg, Exception e, String location){
|
36 |
|
|
this.message = msg;
|
37 |
|
|
this.exception = e;
|
38 |
|
|
this.location = location;
|
39 |
|
|
}
|
40 |
71691b29
|
Katja Luther
|
|
41 |
|
|
public String getMessage(){
|
42 |
|
|
return message;
|
43 |
|
|
}
|
44 |
|
|
|
45 |
|
|
public Exception getException(){
|
46 |
|
|
return exception;
|
47 |
|
|
}
|
48 |
c3d8375b
|
Andreas Müller
|
}
|
49 |
ef934f44
|
unknown
|
|
50 |
|
|
// ************* GETTERS / SETTERS / ADDERS ***********************/
|
51 |
|
|
|
52 |
b93248b0
|
Andreas Müller
|
public List<IoInfo> getErrors() {return errors;}
|
53 |
|
|
public void setErrors(List<IoInfo> ioInfos) {this.errors = ioInfos;}
|
54 |
ef934f44
|
unknown
|
public void addError(String error) {
|
55 |
b93248b0
|
Andreas Müller
|
errors.add(new IoInfo(error, null));
|
56 |
c3d8375b
|
Andreas Müller
|
}
|
57 |
|
|
public void addError(String error, Exception e) {
|
58 |
b93248b0
|
Andreas Müller
|
errors.add(new IoInfo(error, e));
|
59 |
cd602b47
|
Andreas Müller
|
}
|
60 |
|
|
public void addError(String message, int location) {
|
61 |
b93248b0
|
Andreas Müller
|
errors.add(new IoInfo(message, null, String.valueOf(location)));
|
62 |
ef934f44
|
unknown
|
}
|
63 |
|
|
|
64 |
cd602b47
|
Andreas Müller
|
public List<IoInfo> getWarnings() {return warnings;}
|
65 |
|
|
public void setWarnings(List<IoInfo> warnings) {this.warnings = warnings;}
|
66 |
ef934f44
|
unknown
|
public void addWarning(String warning) {
|
67 |
c3d8375b
|
Andreas Müller
|
// warnings.add(warning.getBytes(StandardCharsets.UTF_8));
|
68 |
cd602b47
|
Andreas Müller
|
warnings.add(new IoInfo(warning, null));
|
69 |
|
|
}
|
70 |
|
|
public void addWarning(String message, int location) {
|
71 |
|
|
warnings.add(new IoInfo(message, null, String.valueOf(location)));
|
72 |
ef934f44
|
unknown
|
}
|
73 |
b93248b0
|
Andreas Müller
|
public void addWarning(String message, String location) {
|
74 |
|
|
warnings.add(new IoInfo(message, null, location));
|
75 |
|
|
}
|
76 |
ef934f44
|
unknown
|
|
77 |
cd602b47
|
Andreas Müller
|
public List<IoInfo> getExceptions() {return exceptions;}
|
78 |
|
|
public void setExceptions(List<IoInfo> exceptions) {this.exceptions = exceptions;}
|
79 |
ef934f44
|
unknown
|
public void addException(Exception e) {
|
80 |
cd602b47
|
Andreas Müller
|
exceptions.add(new IoInfo(null, e));
|
81 |
c3d8375b
|
Andreas Müller
|
setExceptionState();
|
82 |
|
|
}
|
83 |
|
|
public void addException(Exception e, String message) {
|
84 |
cd602b47
|
Andreas Müller
|
exceptions.add(new IoInfo(message, e));
|
85 |
ef934f44
|
unknown
|
setExceptionState();
|
86 |
|
|
}
|
87 |
98ad8fb7
|
Andreas Müller
|
public void addException(Exception e, String message, String location) {
|
88 |
|
|
exceptions.add(new IoInfo(message, e, location));
|
89 |
|
|
setExceptionState();
|
90 |
|
|
}
|
91 |
ef934f44
|
unknown
|
|
92 |
|
|
protected abstract void setExceptionState();
|
93 |
|
|
|
94 |
|
|
/**
|
95 |
|
|
* Adds an error and aborts the import.
|
96 |
|
|
* @param string
|
97 |
|
|
*/
|
98 |
|
|
public void setAborted(String error) {
|
99 |
|
|
this.addError(error);
|
100 |
|
|
this.setAborted();
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
public abstract void setAborted();
|
104 |
c3d8375b
|
Andreas Müller
|
|
105 |
|
|
/**
|
106 |
|
|
* @return
|
107 |
|
|
*/
|
108 |
|
|
public StringBuffer createReport() {
|
109 |
|
|
StringBuffer report = new StringBuffer("");
|
110 |
b93248b0
|
Andreas Müller
|
addErrorReport(report, "Errors", errors);
|
111 |
c3d8375b
|
Andreas Müller
|
addErrorReport(report, "Exceptions", exceptions);
|
112 |
cd602b47
|
Andreas Müller
|
addErrorReport(report, "Warnings", warnings);
|
113 |
c3d8375b
|
Andreas Müller
|
return report;
|
114 |
|
|
}
|
115 |
cd602b47
|
Andreas Müller
|
// /**
|
116 |
|
|
// * @param report
|
117 |
|
|
// * @param string
|
118 |
|
|
// * @param warnings2
|
119 |
|
|
// */
|
120 |
|
|
// private void addWarnings(StringBuffer report, String label, List<String> list) {
|
121 |
|
|
// if (!list.isEmpty()){
|
122 |
|
|
// report.append("\n\n" + label + ":\n" + StringUtils.leftPad("", label.length()+1, "="));
|
123 |
|
|
// for (String warning : list){
|
124 |
|
|
// String str = String.valueOf(warning);
|
125 |
|
|
// report.append("\n" + str);
|
126 |
|
|
// }
|
127 |
|
|
// }
|
128 |
|
|
// }
|
129 |
|
|
|
130 |
c3d8375b
|
Andreas Müller
|
/**
|
131 |
|
|
* @param report
|
132 |
cd602b47
|
Andreas Müller
|
* @param label
|
133 |
|
|
* @param list
|
134 |
c3d8375b
|
Andreas Müller
|
*/
|
135 |
cd602b47
|
Andreas Müller
|
private void addErrorReport(StringBuffer report, String label, List<IoInfo> list) {
|
136 |
b93248b0
|
Andreas Müller
|
if (!list.isEmpty()){
|
137 |
c3d8375b
|
Andreas Müller
|
report.append("\n\n" + label + ":\n" + StringUtils.leftPad("", label.length()+1, "="));
|
138 |
cd602b47
|
Andreas Müller
|
for (IoInfo ioInfo : list){
|
139 |
|
|
String location = ioInfo.location == null ? "" : (ioInfo.location + ": ");
|
140 |
|
|
String message = ioInfo.message != null ? ioInfo.message : ioInfo.exception != null ? ioInfo.exception.getMessage() : "";
|
141 |
c3d8375b
|
Andreas Müller
|
message = StringUtils.isBlank(message)? "no message" : message;
|
142 |
b93248b0
|
Andreas Müller
|
Object stacktrace = ioInfo.exception == null? null : ioInfo.exception.getStackTrace();
|
143 |
98ad8fb7
|
Andreas Müller
|
String available = (stacktrace != null ? " (stacktrace available)" : "");
|
144 |
|
|
report.append("\n" + location + message + available);
|
145 |
c3d8375b
|
Andreas Müller
|
}
|
146 |
|
|
}
|
147 |
|
|
}
|
148 |
ef934f44
|
unknown
|
}
|