PageRenderTime 20ms CodeModel.GetById 15ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/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
25package org.hudsonci.utils.marshal;
26
27import org.hudsonci.utils.id.OID;
28import com.thoughtworks.xstream.XStream;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32/**
33 * XStream-related-ish utilities.
34 *
35 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
36 * @since 2.1.0
37 */
38public class XUtil
39{
40    private static final Logger log = LoggerFactory.getLogger(XUtil.class);
41
42    private static final XStream xstream = new XStream();
43
44    /**
45     * Clones an object via {@link XStream}.
46     *
47     * This provides the ability to take a source object and convert it into a target object. Unless
48     * there is some magic converter muck going on for in the source's marshaller this should be
49     * sufficient to create target copies.
50     */
51    public static Object clone(final Object source) {
52        if (source == null) {
53            return null;
54        }
55
56        if (log.isTraceEnabled()) {
57            log.trace("Cloning: {}", OID.get(source));
58        }
59
60        xstream.autodetectAnnotations(true);
61        String xml = xstream.toXML(source);
62        log.trace("Clone XML: {}", xml);
63
64        ClassLoader cl = Thread.currentThread().getContextClassLoader();
65        Thread.currentThread().setContextClassLoader(source.getClass().getClassLoader());
66        try {
67            Object target = xstream.fromXML(xml);
68
69            if (log.isTraceEnabled()) {
70                log.trace("Clone: {}", OID.get(target));
71            }
72
73            return target;
74        }
75        finally {
76            Thread.currentThread().setContextClassLoader(cl);
77        }
78    }
79
80    /**
81     * Render the given object as XML.
82     */
83    public static String render(final Object source) {
84        if (source == null) {
85            return null;
86        }
87
88        xstream.autodetectAnnotations(true);
89        String xml = xstream.toXML(source);
90        log.trace("Rendered XML: {}", xml);
91
92        return xml;
93    }
94}