01: /*
02: * Copyright 2002-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.web.servlet.view.xslt;
18:
19: import javax.xml.transform.OutputKeys;
20: import javax.xml.transform.Transformer;
21:
22: import org.springframework.util.Assert;
23:
24: /**
25: * Contains common behavior relating to {@link Transformer Transformers}.
26: *
27: * @author Rick Evans
28: * @since 2.0
29: */
30: public abstract class TransformerUtils {
31:
32: /**
33: * The indent amount of characters if
34: * {@link #enableIndenting(javax.xml.transform.Transformer) indenting is enabled}.
35: * <p>Defaults to "2".
36: */
37: public static final int DEFAULT_INDENT_AMOUNT = 2;
38:
39: /**
40: * Enable indenting for the supplied {@link Transformer}.
41: * <p>If the underlying XSLT engine is Xalan, then the special output
42: * key <code>indent-amount</code> will be also be set to a value
43: * of {@link #DEFAULT_INDENT_AMOUNT} characters.
44: * @param transformer the target transformer
45: * @throws IllegalArgumentException if the supplied {@link Transformer} is <code>null</code>
46: * @see Transformer#setOutputProperty(String, String)
47: * @see OutputKeys#INDENT
48: */
49: public static void enableIndenting(Transformer transformer) {
50: enableIndenting(transformer, DEFAULT_INDENT_AMOUNT);
51: }
52:
53: /**
54: * Enable indenting for the supplied {@link Transformer}.
55: * <p>If the underlying XSLT engine is Xalan, then the special output
56: * key <code>indent-amount</code> will be also be set to a value
57: * of {@link #DEFAULT_INDENT_AMOUNT} characters.
58: * @param transformer the target transformer
59: * @param indentAmount the size of the indent (2 characters, 3 characters, etc.)
60: * @throws IllegalArgumentException if the supplied {@link Transformer} is <code>null</code>
61: * or if the supplied indent amount is less than zero (that is, negative)
62: * @see Transformer#setOutputProperty(String, String)
63: * @see OutputKeys#INDENT
64: */
65: public static void enableIndenting(Transformer transformer,
66: int indentAmount) {
67: Assert.notNull(transformer);
68: Assert.isTrue(indentAmount > -1,
69: "The indent amount cannot be less than zero : got "
70: + indentAmount);
71: transformer.setOutputProperty(OutputKeys.INDENT, "yes");
72: try {
73: // Xalan-specific, but this is the most common XSLT engine in any case
74: transformer.setOutputProperty(
75: "{http://xml.apache.org/xslt}indent-amount", String
76: .valueOf(indentAmount));
77: } catch (IllegalArgumentException ignored) {
78: }
79: }
80:
81: /**
82: * Disable indenting for the supplied {@link Transformer}.
83: * @param transformer the target transformer
84: * @throws IllegalArgumentException if the supplied {@link Transformer} is <code>null</code>
85: * @see OutputKeys#INDENT
86: */
87: public static void disableIndenting(Transformer transformer) {
88: Assert.notNull(transformer);
89: transformer.setOutputProperty(OutputKeys.INDENT, "no");
90: }
91:
92: }
|