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