1 | /* |
2 | * Copyright (c) 2009 The JOMC Project |
3 | * Copyright (c) 2005 Christian Schulte <cs@jomc.org> |
4 | * All rights reserved. |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions |
8 | * are met: |
9 | * |
10 | * o Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. |
12 | * |
13 | * o Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in |
15 | * the documentation and/or other materials provided with the |
16 | * distribution. |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS" |
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
20 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
21 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR |
22 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
23 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
24 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
25 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
26 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
27 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
28 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | * |
30 | * $Id: ModelValidationReport.java 1237 2010-01-09 20:22:54Z schulte2005 $ |
31 | * |
32 | */ |
33 | package org.jomc.model; |
34 | |
35 | import java.io.Serializable; |
36 | import java.util.ArrayList; |
37 | import java.util.Collections; |
38 | import java.util.List; |
39 | import java.util.logging.Level; |
40 | import javax.xml.bind.JAXBElement; |
41 | |
42 | /** |
43 | * Model validation report. |
44 | * |
45 | * @author <a href="mailto:cs@jomc.org">Christian Schulte</a> |
46 | * @version $Id: ModelValidationReport.java 1237 2010-01-09 20:22:54Z schulte2005 $ |
47 | */ |
48 | public class ModelValidationReport implements Serializable |
49 | { |
50 | |
51 | /** Report detail. */ |
52 | public static class Detail implements Serializable |
53 | { |
54 | |
55 | /** Serial version UID for backwards compatibility with 1.0.x object streams. */ |
56 | private static final long serialVersionUID = 7337752338576205625L; |
57 | |
58 | /** |
59 | * The detail identifier. |
60 | * @serial |
61 | */ |
62 | private String identifier; |
63 | |
64 | /** |
65 | * The detail level. |
66 | * @serial |
67 | */ |
68 | private Level level; |
69 | |
70 | /** |
71 | * The detail message. |
72 | * @serial |
73 | */ |
74 | private String message; |
75 | |
76 | /** |
77 | * The JAXB element this detail is associated with. |
78 | * @serial |
79 | */ |
80 | private JAXBElement element; |
81 | |
82 | /** |
83 | * Creates a new {@code Detail} taking an identifier, a level and a message. |
84 | * |
85 | * @param identifier The detail identifier. |
86 | * @param level The detail level. |
87 | * @param message The detail message. |
88 | * @param element The detail element. |
89 | */ |
90 | public Detail( final String identifier, final Level level, final String message, final JAXBElement element ) |
91 | { |
92 | this.identifier = identifier; |
93 | this.level = level; |
94 | this.message = message; |
95 | this.element = element; |
96 | } |
97 | |
98 | /** |
99 | * Gets the identifier of this detail. |
100 | * |
101 | * @return The identifier of this detail or {@code null}. |
102 | */ |
103 | public String getIdentifier() |
104 | { |
105 | return this.identifier; |
106 | } |
107 | |
108 | /** |
109 | * Gets the level of this detail. |
110 | * |
111 | * @return The level of this detail or {@code null}. |
112 | */ |
113 | public Level getLevel() |
114 | { |
115 | return this.level; |
116 | } |
117 | |
118 | /** |
119 | * Gets the message of this detail. |
120 | * |
121 | * @return The message of this detail or {@code null}. |
122 | */ |
123 | public String getMessage() |
124 | { |
125 | return this.message; |
126 | } |
127 | |
128 | /** |
129 | * Gets the JAXB element of this detail. |
130 | * |
131 | * @return The JAXB element of this detail or {@code null}. |
132 | */ |
133 | public JAXBElement getElement() |
134 | { |
135 | return this.element; |
136 | } |
137 | |
138 | /** |
139 | * Creates and returns a string representation of the object. |
140 | * |
141 | * @return A string representation of the object. |
142 | */ |
143 | private String toStringInternal() |
144 | { |
145 | return new StringBuilder().append( '{' ). |
146 | append( "identifier=" ).append( this.getIdentifier() ). |
147 | append( ", level=" ).append( this.getLevel().getLocalizedName() ). |
148 | append( ", message=" ).append( this.getMessage() ). |
149 | append( ", element=" ).append( this.getElement() ).append( '}' ).toString(); |
150 | |
151 | } |
152 | |
153 | /** |
154 | * Creates and returns a string representation of the object. |
155 | * |
156 | * @return A string representation of the object. |
157 | */ |
158 | @Override |
159 | public String toString() |
160 | { |
161 | return super.toString() + this.toStringInternal(); |
162 | } |
163 | |
164 | } |
165 | |
166 | /** Serial version UID for backwards compatibility with 1.0.x object streams. */ |
167 | private static final long serialVersionUID = -2364165537122367954L; |
168 | |
169 | /** |
170 | * Details of the instance. |
171 | * @serial |
172 | */ |
173 | private List<Detail> details; |
174 | |
175 | /** Creates a new {@code ModelValidationReport} instance. */ |
176 | public ModelValidationReport() |
177 | { |
178 | super(); |
179 | } |
180 | |
181 | /** |
182 | * Gets all details of the instance. |
183 | * <p>This accessor method returns a reference to the live list, not a snapshot. Therefore any modification you make |
184 | * to the returned list will be present inside the object. This is why there is no {@code set} method for the |
185 | * details property.</p> |
186 | * |
187 | * |
188 | * @return All details of the instance. |
189 | */ |
190 | public List<Detail> getDetails() |
191 | { |
192 | if ( this.details == null ) |
193 | { |
194 | this.details = new ArrayList<Detail>(); |
195 | } |
196 | |
197 | return this.details; |
198 | } |
199 | |
200 | /** |
201 | * Gets all details of the instance matching a given identifier. |
202 | * |
203 | * @param identifier The identifier of the details to return or {@code null}. |
204 | * |
205 | * @return An unmodifiable list containing all details of the instance matching {@code identifier}. |
206 | */ |
207 | public List<Detail> getDetails( final String identifier ) |
208 | { |
209 | final List<Detail> list = new ArrayList( this.getDetails().size() ); |
210 | |
211 | for ( Detail d : this.getDetails() ) |
212 | { |
213 | if ( identifier == null && d.getIdentifier() == null ) |
214 | { |
215 | list.add( d ); |
216 | } |
217 | if ( identifier != null && identifier.equals( d.getIdentifier() ) ) |
218 | { |
219 | list.add( d ); |
220 | } |
221 | } |
222 | |
223 | return Collections.unmodifiableList( list ); |
224 | } |
225 | |
226 | /** |
227 | * Gets a flag indicating model validity. |
228 | * |
229 | * @return {@code true} if the validated model is considered valid; {@code false} if the validated model is |
230 | * considered invalid. |
231 | */ |
232 | public boolean isModelValid() |
233 | { |
234 | for ( Detail d : this.getDetails() ) |
235 | { |
236 | if ( d.getLevel() != null && d.getLevel().intValue() > Level.WARNING.intValue() ) |
237 | { |
238 | return false; |
239 | } |
240 | } |
241 | |
242 | return true; |
243 | } |
244 | |
245 | } |