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 public class ModelBuildingException extends Exception {
36
37 private final ModelBuildingResult result;
38
39
40
41
42
43
44
45
46
47 @Deprecated
48 public ModelBuildingException(Model model, String modelId, List<ModelProblem> problems) {
49 super(toMessage(modelId, problems));
50
51 if (model != null) {
52 DefaultModelBuildingResult tmp = new DefaultModelBuildingResult();
53 if (modelId == null) {
54 modelId = "";
55 }
56 tmp.addModelId(modelId);
57 tmp.setRawModel(modelId, model);
58 tmp.setProblems(problems);
59 result = tmp;
60 } else {
61 result = null;
62 }
63 }
64
65
66
67
68
69
70 public ModelBuildingException(ModelBuildingResult result) {
71 super(toMessage(result));
72 this.result = result;
73 }
74
75
76
77
78
79
80 public ModelBuildingResult getResult() {
81 return result;
82 }
83
84
85
86
87
88
89 public Model getModel() {
90 if (result == null) {
91 return null;
92 }
93 if (result.getEffectiveModel() != null) {
94 return result.getEffectiveModel();
95 }
96 return result.getRawModel();
97 }
98
99
100
101
102
103
104
105
106 public String getModelId() {
107 if (result == null || result.getModelIds().isEmpty()) {
108 return "";
109 }
110 return result.getModelIds().get(0);
111 }
112
113
114
115
116
117
118 public List<ModelProblem> getProblems() {
119 if (result == null) {
120 return Collections.emptyList();
121 }
122 return Collections.unmodifiableList(result.getProblems());
123 }
124
125 private static String toMessage(ModelBuildingResult result) {
126 if (result != null && !result.getModelIds().isEmpty()) {
127 return toMessage(result.getModelIds().get(0), result.getProblems());
128 }
129 return null;
130 }
131
132 private static String toMessage(String modelId, List<ModelProblem> problems) {
133 StringWriter buffer = new StringWriter(1024);
134
135 PrintWriter writer = new PrintWriter(buffer);
136
137 writer.print(problems.size());
138 writer.print((problems.size() == 1) ? " problem was " : " problems were ");
139 writer.print("encountered while building the effective model");
140 if (modelId != null && modelId.length() > 0) {
141 writer.print(" for ");
142 writer.print(modelId);
143 }
144 writer.println();
145
146 for (ModelProblem problem : problems) {
147 writer.print("[");
148 writer.print(problem.getSeverity());
149 writer.print("] ");
150 writer.print(problem.getMessage());
151 writer.print(" @ ");
152 writer.println(ModelProblemUtils.formatLocation(problem, modelId));
153 }
154
155 return buffer.toString();
156 }
157 }