/hudson-utils/src/main/java/org/hudsonci/utils/marshal/XUtil.java

http://github.com/hudson/hudson · Java · 94 lines · 42 code · 13 blank · 39 comment · 6 complexity · 8182aff5ff432cf590582464d07d804c MD5 · raw file

  1. /**
  2. * The MIT License
  3. *
  4. * Copyright (c) 2010-2011 Sonatype, Inc. All rights reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package org.hudsonci.utils.marshal;
  25. import org.hudsonci.utils.id.OID;
  26. import com.thoughtworks.xstream.XStream;
  27. import org.slf4j.Logger;
  28. import org.slf4j.LoggerFactory;
  29. /**
  30. * XStream-related-ish utilities.
  31. *
  32. * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
  33. * @since 2.1.0
  34. */
  35. public class XUtil
  36. {
  37. private static final Logger log = LoggerFactory.getLogger(XUtil.class);
  38. private static final XStream xstream = new XStream();
  39. /**
  40. * Clones an object via {@link XStream}.
  41. *
  42. * This provides the ability to take a source object and convert it into a target object. Unless
  43. * there is some magic converter muck going on for in the source's marshaller this should be
  44. * sufficient to create target copies.
  45. */
  46. public static Object clone(final Object source) {
  47. if (source == null) {
  48. return null;
  49. }
  50. if (log.isTraceEnabled()) {
  51. log.trace("Cloning: {}", OID.get(source));
  52. }
  53. xstream.autodetectAnnotations(true);
  54. String xml = xstream.toXML(source);
  55. log.trace("Clone XML: {}", xml);
  56. ClassLoader cl = Thread.currentThread().getContextClassLoader();
  57. Thread.currentThread().setContextClassLoader(source.getClass().getClassLoader());
  58. try {
  59. Object target = xstream.fromXML(xml);
  60. if (log.isTraceEnabled()) {
  61. log.trace("Clone: {}", OID.get(target));
  62. }
  63. return target;
  64. }
  65. finally {
  66. Thread.currentThread().setContextClassLoader(cl);
  67. }
  68. }
  69. /**
  70. * Render the given object as XML.
  71. */
  72. public static String render(final Object source) {
  73. if (source == null) {
  74. return null;
  75. }
  76. xstream.autodetectAnnotations(true);
  77. String xml = xstream.toXML(source);
  78. log.trace("Rendered XML: {}", xml);
  79. return xml;
  80. }
  81. }