| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.util; |
| 34 | | | |
| 35 | | | import java.io.ByteArrayOutputStream; |
| 36 | | | import java.util.ArrayList; |
| 37 | | | import java.util.Collections; |
| 38 | | | import java.util.HashMap; |
| 39 | | | import java.util.List; |
| 40 | | | import java.util.Map; |
| 41 | | | import java.util.logging.Level; |
| 42 | | | import java.util.logging.Logger; |
| 43 | | | |
| 44 | | | import javax.xml.bind.JAXBContext; |
| 45 | | | import javax.xml.bind.JAXBException; |
| 46 | | | import javax.xml.bind.Marshaller; |
| 47 | | | import javax.xml.bind.Unmarshaller; |
| 48 | | | import javax.xml.bind.ValidationEvent; |
| 49 | | | import javax.xml.bind.ValidationEventHandler; |
| 50 | | | import javax.xml.bind.ValidationEventLocator; |
| 51 | | | |
| 52 | | | import org.jcoderz.commons.ArgumentMalformedException; |
| 53 | | | import org.jcoderz.commons.RteLogMessage; |
| 54 | | | import org.xml.sax.InputSource; |
| 55 | | | |
| 56 | | | |
| 57 | | | |
| 58 | | | |
| 59 | | | @author |
| 60 | | | |
| 61 | | | public final class JaxbUtil |
| 62 | | | { |
| 63 | 0 | | private static final Map JAXB_CONTEXT_MAP = new HashMap(); |
| 64 | | | |
| 65 | | | |
| 66 | | | private JaxbUtil () |
| 67 | 0 | | { |
| 68 | | | |
| 69 | 0 | | } |
| 70 | | | |
| 71 | | | |
| 72 | | | |
| 73 | | | |
| 74 | | | @param |
| 75 | | | @return |
| 76 | | | @throws |
| 77 | | | |
| 78 | | | public static synchronized JAXBContext getJaxbContext (String contextPath) |
| 79 | | | throws JAXBException |
| 80 | | | { |
| 81 | 0 | | JAXBContext ctx = (JAXBContext) JAXB_CONTEXT_MAP.get(contextPath); |
| 82 | 0 | | if (ctx == null) |
| 83 | | | { |
| 84 | 0 | | ctx = JAXBContext.newInstance(contextPath); |
| 85 | 0 | (1) | JAXB_CONTEXT_MAP.put(contextPath, ctx); |
| 86 | | | } |
| 87 | 0 | | return ctx; |
| 88 | | | } |
| 89 | | | |
| 90 | | | |
| 91 | | | |
| 92 | | | |
| 93 | | | @param |
| 94 | | | @param |
| 95 | | | |
| 96 | | | @return |
| 97 | | | @throws |
| 98 | | | |
| 99 | | | public static UnmarshalResult unmarshal (InputSource data, String ctxPath) |
| 100 | | | throws JAXBException |
| 101 | | | { |
| 102 | 0 | | final JAXBContext ctx = getJaxbContext(ctxPath); |
| 103 | 0 | | return unmarshal(data, ctx); |
| 104 | | | } |
| 105 | | | |
| 106 | | | |
| 107 | | | |
| 108 | | | |
| 109 | | | @param |
| 110 | | | @param |
| 111 | | | |
| 112 | | | @return |
| 113 | | | @throws |
| 114 | | | |
| 115 | | | public static UnmarshalResult unmarshal (InputSource data, JAXBContext ctx) |
| 116 | | | throws JAXBException |
| 117 | | | { |
| 118 | 0 | | final Unmarshaller unmarsh = ctx.createUnmarshaller(); |
| 119 | 0 | (2) | unmarsh.setValidating(true); |
| 120 | 0 | | final ValidationEventCollector evtHandler |
| 121 | | | = new ValidationEventCollector(); |
| 122 | 0 | | unmarsh.setEventHandler(evtHandler); |
| 123 | 0 | | final Object parsedData = unmarsh.unmarshal(data); |
| 124 | 0 | | return new UnmarshalResult(parsedData, evtHandler); |
| 125 | | | } |
| 126 | | | |
| 127 | | | |
| 128 | | | |
| 129 | | | |
| 130 | | | |
| 131 | | | @param |
| 132 | | | @param |
| 133 | | | |
| 134 | | | @return |
| 135 | | | @throws |
| 136 | | | |
| 137 | | | public static MarshalResult marshal (Object data, String contextPath) |
| 138 | | | throws JAXBException |
| 139 | | | { |
| 140 | 0 | | final JAXBContext ctx = getJaxbContext(contextPath); |
| 141 | 0 | | return marshal(data, ctx); |
| 142 | | | } |
| 143 | | | |
| 144 | | | |
| 145 | | | |
| 146 | | | |
| 147 | | | |
| 148 | | | @param |
| 149 | | | @param |
| 150 | | | @return |
| 151 | | | @throws |
| 152 | | | |
| 153 | | | public static MarshalResult marshal (Object data, JAXBContext ctx) |
| 154 | | | throws JAXBException |
| 155 | | | { |
| 156 | 0 | | final Marshaller marsh = ctx.createMarshaller(); |
| 157 | 0 | | final ValidationEventCollector evtHandler |
| 158 | | | = new ValidationEventCollector(); |
| 159 | 0 | | marsh.setEventHandler(evtHandler); |
| 160 | 0 | | final ByteArrayOutputStream outStream = new ByteArrayOutputStream(); |
| 161 | 0 | | marsh.marshal(data, outStream); |
| 162 | 0 | | final MarshalResult result |
| 163 | | | = new MarshalResult(outStream.toByteArray(), evtHandler); |
| 164 | 0 | (3) | return result; |
| 165 | | | } |
| 166 | | | |
| 167 | | | |
| 168 | | | |
| 169 | | | @author |
| 170 | | | |
| 171 | | | public static class UnmarshalResult |
| 172 | | | { |
| 173 | | | private final Object mParsedData; |
| 174 | | | private final ValidationEventCollector mValidationEvents; |
| 175 | | | |
| 176 | | | UnmarshalResult (Object parsedData, ValidationEventCollector evtHandler) |
| 177 | 0 | | { |
| 178 | 0 | | mParsedData = parsedData; |
| 179 | 0 | | mValidationEvents = evtHandler; |
| 180 | 0 | | } |
| 181 | | | |
| 182 | | | |
| 183 | | | |
| 184 | | | @return |
| 185 | | | |
| 186 | | | public Object getParsedData () |
| 187 | | | { |
| 188 | 0 | | return mParsedData; |
| 189 | | | } |
| 190 | | | |
| 191 | | | |
| 192 | | | |
| 193 | | | @return |
| 194 | | | |
| 195 | | | public ValidationEventCollector getValidationEvents () |
| 196 | | | { |
| 197 | 0 | | return mValidationEvents; |
| 198 | | | } |
| 199 | | | } |
| 200 | | | |
| 201 | | | |
| 202 | | | |
| 203 | | | @author |
| 204 | | | |
| 205 | | | public static class MarshalResult |
| 206 | | | { |
| 207 | | | private final byte[] mMarshalledData; |
| 208 | | | private final ValidationEventCollector mValidationEvents; |
| 209 | | | |
| 210 | | | MarshalResult (byte[] marshalledData, ValidationEventCollector evtHandler) |
| 211 | 0 | | { |
| 212 | 0 | | mMarshalledData = marshalledData; |
| 213 | 0 | | mValidationEvents = evtHandler; |
| 214 | 0 | | } |
| 215 | | | |
| 216 | | | |
| 217 | | | |
| 218 | | | @return |
| 219 | | | |
| 220 | | | public byte[] getMarshalledData () |
| 221 | | | { |
| 222 | 0 | (4)(5) | return mMarshalledData; |
| 223 | | | } |
| 224 | | | |
| 225 | | | |
| 226 | | | |
| 227 | | | @return |
| 228 | | | |
| 229 | | | public ValidationEventCollector getValidationEvents () |
| 230 | | | { |
| 231 | 0 | | return mValidationEvents; |
| 232 | | | } |
| 233 | | | } |
| 234 | | | |
| 235 | | | |
| 236 | | | |
| 237 | | | |
| 238 | | | @author |
| 239 | | | |
| 240 | 0 | | public static class ValidationEventCollector |
| 241 | | | implements ValidationEventHandler |
| 242 | | | { |
| 243 | 0 | | private static final String CLASSNAME |
| 244 | | | = ValidationEventCollector.class.getName(); |
| 245 | | | |
| 246 | 0 | | private static final Logger logger = Logger.getLogger(CLASSNAME); |
| 247 | | | |
| 248 | 0 | | private final List mEvents = new ArrayList(); |
| 249 | | | |
| 250 | | | |
| 251 | | | |
| 252 | | | |
| 253 | | | |
| 254 | | | @return |
| 255 | | | |
| 256 | | | |
| 257 | | | public List getEvents () |
| 258 | | | { |
| 259 | 0 | | return Collections.unmodifiableList(mEvents); |
| 260 | | | } |
| 261 | | | |
| 262 | | | |
| 263 | | | |
| 264 | | | |
| 265 | | | public void reset () |
| 266 | | | { |
| 267 | 0 | | mEvents.clear(); |
| 268 | 0 | | } |
| 269 | | | |
| 270 | | | |
| 271 | | | |
| 272 | | | |
| 273 | | | |
| 274 | | | @return |
| 275 | | | |
| 276 | | | |
| 277 | | | public boolean hasEvents () |
| 278 | | | { |
| 279 | 0 | (6) | return mEvents.size() != 0; |
| 280 | | | } |
| 281 | | | |
| 282 | | | {@inheritDoc} |
| 283 | | | public boolean handleEvent (ValidationEvent event) |
| 284 | | | { |
| 285 | 0 | | final String methodName = "handleEvent"; |
| 286 | 0 | | if (logger.isLoggable(Level.FINER)) |
| 287 | | | { |
| 288 | 0 | | logger.entering(CLASSNAME, methodName, event); |
| 289 | 0 | | logger.finer("Event details: " |
| 290 | | | + eventToString(new StringBuffer(), event)); |
| 291 | | | } |
| 292 | | | |
| 293 | 0 | | mEvents.add(event); |
| 294 | | | |
| 295 | 0 | | final boolean doContinue |
| 296 | | | = event.getSeverity() != ValidationEvent.FATAL_ERROR; |
| 297 | | | |
| 298 | 0 | | if (logger.isLoggable(Level.FINER)) |
| 299 | | | { |
| 300 | 0 | | logger.exiting(CLASSNAME, methodName, String.valueOf(doContinue)); |
| 301 | | | } |
| 302 | 0 | | return doContinue; |
| 303 | | | } |
| 304 | | | |
| 305 | | | |
| 306 | | | |
| 307 | | | @return |
| 308 | | | |
| 309 | | | public String toString () |
| 310 | | | { |
| 311 | 0 | | final StringBuffer sb = new StringBuffer(); |
| 312 | 0 | | for (int i = 0; i < mEvents.size(); ++i) |
| 313 | | | { |
| 314 | 0 | | sb.append('['); |
| 315 | 0 | | sb.append(i + 1); |
| 316 | 0 | | sb.append('/'); |
| 317 | 0 | | sb.append(mEvents.size()); |
| 318 | 0 | | sb.append("] "); |
| 319 | 0 | | final ValidationEvent e = (ValidationEvent) mEvents.get(i); |
| 320 | 0 | | eventToString(sb, e); |
| 321 | | | } |
| 322 | 0 | | return sb.toString().trim(); |
| 323 | | | } |
| 324 | | | |
| 325 | | | private StringBuffer eventToString ( |
| 326 | | | final StringBuffer sb, final ValidationEvent e) |
| 327 | | | { |
| 328 | 0 | | appendLocator(sb, e.getLocator()); |
| 329 | | | |
| 330 | 0 | | if (e.getLinkedException() != null) |
| 331 | | | { |
| 332 | 0 | | appendLinkedException(sb, e); |
| 333 | | | } |
| 334 | | | else |
| 335 | | | { |
| 336 | 0 | | sb.append(e.getMessage()); |
| 337 | | | } |
| 338 | 0 | | appendSpace(sb); |
| 339 | | | |
| 340 | 0 | | return sb; |
| 341 | | | } |
| 342 | | | |
| 343 | | | private void appendLinkedException (final StringBuffer sb, |
| 344 | | | final ValidationEvent e) |
| 345 | | | { |
| 346 | 0 | | final String causeMessage = e.getLinkedException().getMessage(); |
| 347 | 0 | | if (!e.getMessage().equals(causeMessage)) |
| 348 | | | { |
| 349 | 0 | | if (e.getLinkedException() instanceof ArgumentMalformedException) |
| 350 | | | { |
| 351 | 0 | | final ArgumentMalformedException ame |
| 352 | | | = (ArgumentMalformedException) e.getLinkedException(); |
| 353 | 0 | | sb.append("The Argument "); |
| 354 | 0 | | sb.append(getParameter( |
| 355 | | | ame, RteLogMessage.ArgumentMalformed.PARAM_ARGUMENT_NAME)); |
| 356 | 0 | | sb.append(" with the value '"); |
| 357 | 0 | | sb.append(getParameter(ame, |
| 358 | | | RteLogMessage.ArgumentMalformed.PARAM_ARGUMENT_VALUE)); |
| 359 | 0 | | sb.append("' is malformed. "); |
| 360 | 0 | | sb.append(getParameter( |
| 361 | | | ame, RteLogMessage.ArgumentMalformed.PARAM_HINT)); |
| 362 | 0 | | } |
| 363 | | | else |
| 364 | | | { |
| 365 | 0 | | sb.append(e.getMessage()); |
| 366 | 0 | | if (causeMessage != null) |
| 367 | | | { |
| 368 | 0 | | sb.append(" Cause: "); |
| 369 | 0 | | sb.append(causeMessage); |
| 370 | | | } |
| 371 | | | } |
| 372 | | | } |
| 373 | | | else |
| 374 | | | { |
| 375 | 0 | | sb.append(e.getMessage()); |
| 376 | | | } |
| 377 | 0 | | } |
| 378 | | | |
| 379 | | | private void appendLocator (final StringBuffer sb, |
| 380 | | | ValidationEventLocator locator) |
| 381 | | | { |
| 382 | 0 | | if (locator != null) |
| 383 | | | { |
| 384 | 0 | | if (locator.getObject() != null) |
| 385 | | | { |
| 386 | 0 | | sb.append("Object: "); |
| 387 | 0 | | sb.append(locator.getObject()); |
| 388 | 0 | | appendSpace(sb); |
| 389 | | | } |
| 390 | 0 | | if (locator.getNode() != null) |
| 391 | | | { |
| 392 | 0 | | sb.append("Node: "); |
| 393 | 0 | | sb.append(locator.getObject()); |
| 394 | 0 | | appendSpace(sb); |
| 395 | | | } |
| 396 | 0 | | if (locator.getOffset() >= 0) |
| 397 | | | { |
| 398 | 0 | | sb.append("Offset: "); |
| 399 | 0 | | sb.append(locator.getOffset()); |
| 400 | 0 | | appendSpace(sb); |
| 401 | | | } |
| 402 | | | } |
| 403 | 0 | | } |
| 404 | | | |
| 405 | | | |
| 406 | | | private final StringBuffer appendSpace (final StringBuffer sb) |
| 407 | | | { |
| 408 | 0 | | return sb.append(' '); |
| 409 | | | } |
| 410 | | | |
| 411 | | | private String getParameter (ArgumentMalformedException ex, String name) |
| 412 | | | { |
| 413 | 0 | (7) | final List parameters = ex.getParameter(name); |
| 414 | | | final String result; |
| 415 | 0 | | if (parameters != null) |
| 416 | | | { |
| 417 | 0 | | result = (String) parameters.get(0); |
| 418 | | | } |
| 419 | | | else |
| 420 | | | { |
| 421 | 0 | | result = ""; |
| 422 | | | } |
| 423 | 0 | | return result; |
| 424 | | | } |
| 425 | | | } |
| 426 | | | |
| 427 | | | } |