Bump odlparent to 6.0.0
[netconf.git] / netconf / netconf-netty-util / src / main / java / org / opendaylight / netconf / nettyutil / handler / ThreadLocalTransformers.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.netconf.nettyutil.handler;
9
10 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
11 import javax.xml.transform.OutputKeys;
12 import javax.xml.transform.Transformer;
13 import javax.xml.transform.TransformerConfigurationException;
14 import javax.xml.transform.TransformerFactory;
15 import javax.xml.transform.TransformerFactoryConfigurationError;
16
17 /**
18  * Utility class for cached thread-local transformers. This class exists mostly for use by handlers.
19  */
20 final class ThreadLocalTransformers {
21     private static final TransformerFactory FACTORY = TransformerFactory.newInstance();
22
23     private static final ThreadLocal<Transformer> DEFAULT_TRANSFORMER = new ThreadLocal<Transformer>() {
24         @Override
25         protected Transformer initialValue() {
26             return createTransformer();
27         }
28
29         @Override
30         public void set(final Transformer value) {
31             throw new UnsupportedOperationException();
32         }
33     };
34
35     private static final ThreadLocal<Transformer> PRETTY_TRANSFORMER = new ThreadLocal<Transformer>() {
36         @Override
37         protected Transformer initialValue() {
38             final Transformer ret = createTransformer();
39             ret.setOutputProperty(OutputKeys.INDENT, "yes");
40             ret.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
41             return ret;
42         }
43
44         @Override
45         public void set(final Transformer value) {
46             throw new UnsupportedOperationException();
47         }
48     };
49
50     private ThreadLocalTransformers() {
51         throw new UnsupportedOperationException("Utility class");
52     }
53
54     /**
55      * Get the transformer with default configuration.
56      *
57      * @return A transformer with default configuration based on the default implementation.
58      */
59     static Transformer getDefaultTransformer() {
60         return DEFAULT_TRANSFORMER.get();
61     }
62
63     /**
64      * Get the transformer with default configuration, but with automatic indentation
65      * and the XML declaration removed.
66      *
67      * @return A transformer with human-friendly configuration.
68      */
69     static Transformer getPrettyTransformer() {
70         return PRETTY_TRANSFORMER.get();
71     }
72
73     @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
74             justification = "https://github.com/spotbugs/spotbugs/issues/811")
75     private static Transformer createTransformer() {
76         try {
77             return FACTORY.newTransformer();
78         } catch (TransformerConfigurationException | TransformerFactoryConfigurationError e) {
79             throw new IllegalStateException("Unexpected error while instantiating a Transformer", e);
80         }
81     }
82 }