KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > view > xslt > TransformerUtils


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
20 import javax.xml.transform.Transformer JavaDoc;
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     /**
41      * Enable indenting for the supplied {@link Transformer}.
42      * <p>If the underlying XSLT engine is Xalan, then the special output
43      * key <code>indent-amount</code> will be also be set to a value
44      * of {@link #DEFAULT_INDENT_AMOUNT} characters.
45      * @param transformer the target transformer
46      * @throws IllegalArgumentException if the supplied {@link Transformer} is <code>null</code>
47      * @see Transformer#setOutputProperty(String, String)
48      * @see OutputKeys#INDENT
49      */

50     public static void enableIndenting(Transformer JavaDoc transformer) {
51         enableIndenting(transformer, DEFAULT_INDENT_AMOUNT);
52     }
53
54     /**
55      * Enable indenting for the supplied {@link Transformer}.
56      * <p>If the underlying XSLT engine is Xalan, then the special output
57      * key <code>indent-amount</code> will be also be set to a value
58      * of {@link #DEFAULT_INDENT_AMOUNT} characters.
59      * @param transformer the target transformer
60      * @param indentAmount the size of the indent (2 characters, 3 characters, etc.)
61      * @throws IllegalArgumentException if the supplied {@link Transformer} is <code>null</code>
62      * or if the supplied indent amount is less than zero (that is, negative)
63      * @see Transformer#setOutputProperty(String, String)
64      * @see OutputKeys#INDENT
65      */

66     public static void enableIndenting(Transformer JavaDoc transformer, int indentAmount) {
67         Assert.notNull(transformer);
68         Assert.isTrue(indentAmount > -1, "The indent amount cannot be less than zero : got " + indentAmount);
69         transformer.setOutputProperty(OutputKeys.INDENT, "yes");
70         try {
71             // Xalan-specific, but this is the most common XSLT engine in any case
72
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indentAmount));
73         }
74         catch (IllegalArgumentException JavaDoc ignored) {
75         }
76     }
77
78     /**
79      * Disable indenting for the supplied {@link Transformer}.
80      * @param transformer the target transformer
81      * @throws IllegalArgumentException if the supplied {@link Transformer} is <code>null</code>
82      * @see OutputKeys#INDENT
83      */

84     public static void disableIndenting(Transformer JavaDoc transformer) {
85         Assert.notNull(transformer);
86         transformer.setOutputProperty(OutputKeys.INDENT, "no");
87     }
88
89 }
90
Popular Tags