Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ProblemType |
|
| 1.5;1.5 |
1 | /* | |
2 | * Copyright (c) 2009, Swiss Federal Department of Defence Civil Protection and Sport | |
3 | * (http://www.vbs.admin.ch) | |
4 | * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch) | |
5 | * All rights reserved. | |
6 | * | |
7 | * Licensed under the Open Permis License which accompanies this distribution, | |
8 | * and is available at http://www.openpermis.org/BSDlicenceKent.txt | |
9 | */ | |
10 | package org.openpermis.policy.io; | |
11 | ||
12 | import java.net.URI; | |
13 | import java.util.Arrays; | |
14 | import java.util.Formatter; | |
15 | import java.util.Locale; | |
16 | ||
17 | import org.slf4j.Logger; | |
18 | import org.slf4j.LoggerFactory; | |
19 | ||
20 | ||
21 | /** | |
22 | * Enumeration of all problem types that can occur during serialization. | |
23 | * @see ProblemReporter | |
24 | * @since 0.3.0 | |
25 | */ | |
26 | 1 | public enum ProblemType { |
27 | ||
28 | //---- Static | |
29 | ||
30 | /** | |
31 | * Indicates that portion of a policy have been skipped during import. | |
32 | * <p>Parameters include the location and the element skipped.</p> | |
33 | * @since 0.3.0 | |
34 | */ | |
35 | 1 | ELEMENT_SKIPPED( |
36 | "skipped", "%s: Element '%s' skipped.", | |
37 | String.class, String.class | |
38 | ), | |
39 | ||
40 | /** | |
41 | * Indicates that an identifier has been converted during import. | |
42 | * <p>Parameters include the location, the element and attribute for which the | |
43 | * conversion was performed followed by the original identifier and the resulting URI.</p> | |
44 | * @since 0.3.0 | |
45 | */ | |
46 | 1 | ID_CONVERTED( |
47 | "converted", "%s: ID of '%s.%s' converted from '%s' to URI '%s'.", | |
48 | String.class, String.class, String.class, String.class, URI.class | |
49 | ); | |
50 | ||
51 | /** | |
52 | * The logger object of this class. | |
53 | * @since 0.3.0 | |
54 | */ | |
55 | 1 | private static final Logger LOGGER = |
56 | LoggerFactory.getLogger(ProblemType.class); | |
57 | ||
58 | //---- State | |
59 | ||
60 | /** | |
61 | * The translation key of the problem type. | |
62 | * @since 0.3.0 | |
63 | */ | |
64 | private final String key; | |
65 | ||
66 | /** | |
67 | * The default message used by {@link #getMessage(Object...)}. | |
68 | * @since 0.3.0 | |
69 | */ | |
70 | private final String message; | |
71 | ||
72 | /** | |
73 | * The parameter types expected when problems of this type are issued. | |
74 | * @since 0.3.0 | |
75 | */ | |
76 | private final Class<?>[] parameterTypes; | |
77 | ||
78 | //---- Constructors | |
79 | ||
80 | /** | |
81 | * Creates a new problem type for the specified key. | |
82 | * <p>The message of the problem type describes a {@link Formatter} pattern that may use | |
83 | * the parameters described by the parameter types. The message is an english message.</p> | |
84 | * @param key the translation key of the problem type. | |
85 | * @param message the default message of the problem type. | |
86 | * @param parameterTypes the parameter types expected by this problem type. | |
87 | * @since 0.3.0 | |
88 | */ | |
89 | 2 | private ProblemType (String key, String message, Class<?>... parameterTypes) { |
90 | 2 | this.key = key; |
91 | 2 | this.message = message; |
92 | 2 | this.parameterTypes = parameterTypes == null ? new Class<?>[0] : parameterTypes; |
93 | 2 | } |
94 | ||
95 | //---- Methods | |
96 | ||
97 | /** | |
98 | * Returns translation key of the problem type. | |
99 | * @return translation key of the problem type. | |
100 | * @since 0.3.0 | |
101 | */ | |
102 | public String getKey () { | |
103 | 0 | return this.key; |
104 | } | |
105 | ||
106 | /** | |
107 | * Returns the parameter types expected by this problem type. | |
108 | * @return the parameter types expected by this problem type. | |
109 | * @since 0.3.0 | |
110 | */ | |
111 | public Class<?>[] getParameterTypes () { | |
112 | 0 | return this.parameterTypes.clone(); |
113 | } | |
114 | ||
115 | /** | |
116 | * Returns a default message for the problem type. | |
117 | * @param parameters the parameters of the problem type. | |
118 | * @return the message requested. | |
119 | * @since 0.3.0 | |
120 | */ | |
121 | public String getMessage (Object... parameters) { | |
122 | 2 | final StringBuilder sb = new StringBuilder(); |
123 | try { | |
124 | 2 | new Formatter(sb, Locale.US).format(this.message, parameters); |
125 | 0 | } catch (Exception e) { |
126 | 0 | LOGGER.debug("Default message could not be formatted.", e); |
127 | 0 | sb.append(getKey()).append(" ").append(Arrays.toString(parameters)); |
128 | 2 | } |
129 | 2 | return sb.toString(); |
130 | } | |
131 | ||
132 | } |