001/*
002 * Copyright 2002-2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.util.xml;
018
019import javax.xml.transform.OutputKeys;
020import javax.xml.transform.Transformer;
021
022import org.springframework.util.Assert;
023
024/**
025 * Contains common behavior relating to {@link javax.xml.transform.Transformer Transformers}
026 * and the {@code javax.xml.transform} package in general.
027 *
028 * @author Rick Evans
029 * @author Juergen Hoeller
030 * @since 2.5.5
031 */
032public abstract class TransformerUtils {
033
034        /**
035         * The indent amount of characters if {@link #enableIndenting indenting is enabled}.
036         * <p>Defaults to "2".
037         */
038        public static final int DEFAULT_INDENT_AMOUNT = 2;
039
040
041        /**
042         * Enable indenting for the supplied {@link javax.xml.transform.Transformer}.
043         * <p>If the underlying XSLT engine is Xalan, then the special output key {@code indent-amount}
044         * will be also be set to a value of {@link #DEFAULT_INDENT_AMOUNT} characters.
045         * @param transformer the target transformer
046         * @see javax.xml.transform.Transformer#setOutputProperty(String, String)
047         * @see javax.xml.transform.OutputKeys#INDENT
048         */
049        public static void enableIndenting(Transformer transformer) {
050                enableIndenting(transformer, DEFAULT_INDENT_AMOUNT);
051        }
052
053        /**
054         * Enable indenting for the supplied {@link javax.xml.transform.Transformer}.
055         * <p>If the underlying XSLT engine is Xalan, then the special output key {@code indent-amount}
056         * will be also be set to a value of {@link #DEFAULT_INDENT_AMOUNT} characters.
057         * @param transformer the target transformer
058         * @param indentAmount the size of the indent (2 characters, 3 characters, etc)
059         * @see javax.xml.transform.Transformer#setOutputProperty(String, String)
060         * @see javax.xml.transform.OutputKeys#INDENT
061         */
062        public static void enableIndenting(Transformer transformer, int indentAmount) {
063                Assert.notNull(transformer, "Transformer must not be null");
064                if (indentAmount < 0) {
065                        throw new IllegalArgumentException("Invalid indent amount (must not be less than zero): " + indentAmount);
066                }
067                transformer.setOutputProperty(OutputKeys.INDENT, "yes");
068                try {
069                        // Xalan-specific, but this is the most common XSLT engine in any case
070                        transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", String.valueOf(indentAmount));
071                }
072                catch (IllegalArgumentException ignored) {
073                }
074        }
075
076        /**
077         * Disable indenting for the supplied {@link javax.xml.transform.Transformer}.
078         * @param transformer the target transformer
079         * @see javax.xml.transform.OutputKeys#INDENT
080         */
081        public static void disableIndenting(Transformer transformer) {
082                Assert.notNull(transformer, "Transformer must not be null");
083                transformer.setOutputProperty(OutputKeys.INDENT, "no");
084        }
085
086}