HoneyNode Java 11 support for 221 devices
[transportpce.git] / tests / honeynode / 2.1 / 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 javax.xml.transform.OutputKeys;
11 import javax.xml.transform.Transformer;
12 import javax.xml.transform.TransformerConfigurationException;
13 import javax.xml.transform.TransformerFactory;
14 import javax.xml.transform.TransformerFactoryConfigurationError;
15
16 /**
17  * Utility class for cached thread-local transformers. This class exists mostly for use by handlers.
18  */
19 final class ThreadLocalTransformers {
20     private static final TransformerFactory FACTORY = TransformerFactory.newInstance();
21
22     private static final ThreadLocal<Transformer> DEFAULT_TRANSFORMER = new ThreadLocal<Transformer>() {
23         @Override
24         protected Transformer initialValue() {
25             return createTransformer();
26         }
27
28         @Override
29         public void set(final Transformer value) {
30             throw new UnsupportedOperationException();
31         }
32     };
33
34     private static final ThreadLocal<Transformer> PRETTY_TRANSFORMER = new ThreadLocal<Transformer>() {
35         @Override
36         protected Transformer initialValue() {
37             final Transformer ret = createTransformer();
38             ret.setOutputProperty(OutputKeys.INDENT, "yes");
39             ret.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
40             return ret;
41         }
42
43         @Override
44         public void set(final Transformer value) {
45             throw new UnsupportedOperationException();
46         }
47     };
48
49     private ThreadLocalTransformers() {
50         throw new UnsupportedOperationException("Utility class");
51     }
52
53     /**
54      * Get the transformer with default configuration.
55      *
56      * @return A transformer with default configuration based on the default implementation.
57      */
58     static Transformer getDefaultTransformer() {
59         return DEFAULT_TRANSFORMER.get();
60     }
61
62     /**
63      * Get the transformer with default configuration, but with automatic indentation
64      * and the XML declaration removed.
65      *
66      * @return A transformer with human-friendly configuration.
67      */
68     static Transformer getPrettyTransformer() {
69         return PRETTY_TRANSFORMER.get();
70     }
71
72     private static Transformer createTransformer() {
73         try {
74             return FACTORY.newTransformer();
75         } catch (TransformerConfigurationException | TransformerFactoryConfigurationError e) {
76             throw new IllegalStateException("Unexpected error while instantiating a Transformer", e);
77         }
78     }
79 }