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