1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.model.building;
20
21 import java.io.PrintWriter;
22 import java.io.StringWriter;
23 import java.util.Collections;
24 import java.util.List;
25
26 import org.apache.maven.model.Model;
27
28
29
30
31
32
33
34
35 @Deprecated(since = "4.0.0")
36 public class ModelBuildingException extends Exception {
37
38 private final ModelBuildingResult result;
39
40
41
42
43
44
45
46
47
48 @Deprecated
49 public ModelBuildingException(Model model, String modelId, List<ModelProblem> problems) {
50 super(toMessage(modelId, problems));
51
52 if (model != null) {
53 DefaultModelBuildingResult tmp = new DefaultModelBuildingResult();
54 if (modelId == null) {
55 modelId = "";
56 }
57 tmp.addModelId(modelId);
58 tmp.setRawModel(modelId, model);
59 tmp.setProblems(problems);
60 result = tmp;
61 } else {
62 result = null;
63 }
64 }
65
66
67
68
69
70
71 public ModelBuildingException(ModelBuildingResult result) {
72 super(toMessage(result));
73 this.result = result;
74 }
75
76
77
78
79
80
81 public ModelBuildingResult getResult() {
82 return result;
83 }
84
85
86
87
88
89
90 public Model getModel() {
91 if (result == null) {
92 return null;
93 }
94 if (result.getEffectiveModel() != null) {
95 return result.getEffectiveModel();
96 }
97 return result.getRawModel();
98 }
99
100
101
102
103
104
105
106
107 public String getModelId() {
108 if (result == null || result.getModelIds().isEmpty()) {
109 return "";
110 }
111 return result.getModelIds().get(0);
112 }
113
114
115
116
117
118
119 public List<ModelProblem> getProblems() {
120 if (result == null) {
121 return Collections.emptyList();
122 }
123 return Collections.unmodifiableList(result.getProblems());
124 }
125
126 private static String toMessage(ModelBuildingResult result) {
127 if (result != null && !result.getModelIds().isEmpty()) {
128 return toMessage(result.getModelIds().get(0), result.getProblems());
129 }
130 return null;
131 }
132
133
134 static String toMessage(String modelId, List<ModelProblem> problems) {
135 StringWriter buffer = new StringWriter(1024);
136
137 PrintWriter writer = new PrintWriter(buffer);
138
139 writer.print(problems.size());
140 writer.print((problems.size() == 1) ? " problem was " : " problems were ");
141 writer.print("encountered while building the effective model");
142 if (modelId != null && !modelId.isEmpty()) {
143 writer.print(" for ");
144 writer.print(modelId);
145 }
146
147 for (ModelProblem problem : problems) {
148 writer.println();
149 writer.print(" - [");
150 writer.print(problem.getSeverity());
151 writer.print("] ");
152 writer.print(problem.getMessage());
153 String location = ModelProblemUtils.formatLocation(problem, modelId);
154 if (!location.isEmpty()) {
155 writer.print(" @ ");
156 writer.print(location);
157 }
158 }
159
160 return buffer.toString();
161 }
162 }