com.helger.jaxb.builder.JAXBBuilderDefaultSettings Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ph-jaxb Show documentation
Show all versions of ph-jaxb Show documentation
Special Java 1.8+ Library with extended JAXB support
/**
* Copyright (C) 2014-2016 Philip Helger (www.helger.com)
* philip[at]helger[dot]com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.helger.jaxb.builder;
import java.nio.charset.Charset;
import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe;
import javax.xml.bind.ValidationEventHandler;
import javax.xml.namespace.NamespaceContext;
import com.helger.commons.concurrent.SimpleReadWriteLock;
/**
* A class containing some default settings for the various JAXB builders. Each
* new instance of {@link JAXBReaderBuilder}, {@link JAXBValidationBuilder} and
* {@link JAXBWriterBuilder} use the settings in this class as the defaults.
* Changes made in this class only effects instances that are created
* afterwards. Existing instances are never changed.
*
* @author Philip Helger
*/
@ThreadSafe
public final class JAXBBuilderDefaultSettings
{
private static final SimpleReadWriteLock s_aRWLock = new SimpleReadWriteLock ();
@GuardedBy ("s_aRWLock")
private static boolean s_bUseContextCache = true;
@GuardedBy ("s_aRWLock")
private static ValidationEventHandler s_aEventHandler;
@GuardedBy ("s_aRWLock")
private static NamespaceContext s_aNamespaceContext;
@GuardedBy ("s_aRWLock")
private static boolean s_bFormattedOutput = false;
@GuardedBy ("s_aRWLock")
private static Charset s_aCharset;
@GuardedBy ("s_aRWLock")
private static String s_sIndentString;
private JAXBBuilderDefaultSettings ()
{}
/**
* Enable or disable the usage of the JAXBContext cache. For performance
* reasons it is recommended to enable it. By default it is enabled.
*
* @param bUseContextCache
* true
to enable it, false
to disable it.
*/
public static void setDefaultUseContextCache (final boolean bUseContextCache)
{
s_aRWLock.writeLocked ( () -> s_bUseContextCache = bUseContextCache);
}
/**
* @return true
if the JAXBContext cache should be used. Default
* is true
.
*/
public static boolean isDefaultUseContextCache ()
{
return s_aRWLock.readLocked ( () -> s_bUseContextCache);
}
/**
* Set a global event handler that should be passed to all read/write actions.
* If no global validation handler is defined, a default logging event handler
* is used.
*
* @param aEventHandler
* The new default event handler. May be null
to indicate,
* that the default handler should be used.
*/
public static void setDefaultValidationEventHandler (@Nullable final ValidationEventHandler aEventHandler)
{
s_aRWLock.writeLocked ( () -> s_aEventHandler = aEventHandler);
}
/**
* @return The current default validation event handler. May be
* null
to indicate that no global validation event
* handler is defined, and the default validation handler is used.
*/
@Nullable
public static ValidationEventHandler getDefaultValidationEventHandler ()
{
return s_aRWLock.readLocked ( () -> s_aEventHandler);
}
/**
* Set the default namespace context (prefix to namespace URL mapping) to be
* used.
*
* @param aNamespaceContext
* The namespace context to be used by default. May be
* null
.
*/
public static void setDefaultNamespaceContext (@Nullable final NamespaceContext aNamespaceContext)
{
s_aRWLock.writeLocked ( () -> s_aNamespaceContext = aNamespaceContext);
}
/**
* @return The special JAXB namespace context to be used. null
by
* default.
*/
@Nullable
public static NamespaceContext getDefaultNamespaceContext ()
{
return s_aRWLock.readLocked ( () -> s_aNamespaceContext);
}
/**
* Enable or disable the formatting of the output.
*
* @param bFormattedOutput
* true
to enable it, false
to disable it.
*/
public static void setDefaultFormattedOutput (final boolean bFormattedOutput)
{
s_aRWLock.writeLocked ( () -> s_bFormattedOutput = bFormattedOutput);
}
/**
* @return true
if the JAXB output should be formatted. Only for
* writers. Default is false
. The JDK implementation does
* not format by default.
*/
public static boolean isDefaultFormattedOutput ()
{
return s_aRWLock.readLocked ( () -> s_bFormattedOutput);
}
/**
* Set the default charset to be used for writing JAXB objects.
*
* @param aCharset
* The charset to be used by default. May be null
.
*/
public static void setDefaultCharset (@Nullable final Charset aCharset)
{
s_aRWLock.writeLocked ( () -> s_aCharset = aCharset);
}
/**
* @return The special JAXB Charset to be used for writing. null
* by default. The JDK implementation uses UTF-8 by default.
*/
@Nullable
public static Charset getDefaultCharset ()
{
return s_aRWLock.readLocked ( () -> s_aCharset);
}
/**
* Set the default indent string to be used for writing JAXB objects.
*
* @param sIndentString
* The indent string to be used by default. May be null
.
*/
public static void setDefaultIndentString (@Nullable final String sIndentString)
{
s_aRWLock.writeLocked ( () -> s_sIndentString = sIndentString);
}
/**
* @return The JAXB indentation string to be used for writing.
* null
by default. Only used when formatted output is
* used. The JDK implementation uses 4 spaces by default.
*/
@Nullable
public static String getDefaultIndentString ()
{
return s_aRWLock.readLocked ( () -> s_sIndentString);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy