KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > output > XMLIndenter


1 package com.icl.saxon.output;
2 import com.icl.saxon.*;
3 import com.icl.saxon.om.Namespace;
4 import java.util.*;
5 import java.io.*;
6 import org.xml.sax.Attributes JavaDoc;
7 import javax.xml.transform.OutputKeys JavaDoc;
8 import javax.xml.transform.TransformerException JavaDoc;
9
10 /**
11 * XMLIndenter: This ProxyEmitter indents elements, by adding character data where appropriate.
12 * The character data is always added as "ignorable white space", that is, it is never added
13 * adjacent to existing character data.
14 *
15 * Author Michael Kay (mhkay@iclway.co.uk)
16 */

17
18
19 public class XMLIndenter extends ProxyEmitter {
20
21     private int level = 0;
22     private int indentSpaces = 3;
23     private String JavaDoc indentChars = " ";
24     private boolean sameline = false;
25     private boolean afterTag = true;
26     private boolean allWhite = true;
27     private int suppressedAtLevel = -1;
28         // records level at which xml:space="preserve" was found
29

30     /**
31     * Start of document
32     */

33
34     public void startDocument() throws TransformerException JavaDoc {
35         super.startDocument();
36         
37         String JavaDoc s = outputProperties.getProperty(SaxonOutputKeys.INDENT_SPACES);
38         if (s!=null) {
39             try {
40                 indentSpaces = Integer.parseInt(s);
41             } catch (Exception JavaDoc err) {
42                 indentSpaces = 3;
43             }
44         }
45
46         String JavaDoc omit = outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION);
47         afterTag = omit==null || !omit.equals("yes") ||
48                     outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM)!=null ;
49     }
50
51     /**
52     * Output element start tag
53     */

54
55     public void startElement(int tag, Attributes JavaDoc atts,
56                              int[] namespaces, int nscount) throws TransformerException JavaDoc {
57         if (afterTag) {
58             indent();
59         }
60         super.startElement(tag, atts, namespaces, nscount);
61         if ("preserve".equals(atts.getValue(Namespace.XML, "space")) && suppressedAtLevel < 0) {
62             suppressedAtLevel = level;
63         }
64         level++;
65         sameline = true;
66         afterTag = true;
67         allWhite = true;
68     }
69
70     /**
71     * Output element end tag
72     */

73     
74     public void endElement(int tag) throws TransformerException JavaDoc {
75         level--;
76         if (afterTag && !sameline) indent();
77         super.endElement(tag);
78         sameline = false;
79         afterTag = true;
80         allWhite = true;
81         if (level == (suppressedAtLevel - 1)) {
82             suppressedAtLevel = -1;
83             // remove the suppression of indentation
84
}
85     }
86
87     /**
88     * Output a processing instruction
89     */

90
91     public void processingInstruction(String JavaDoc target, String JavaDoc data) throws TransformerException JavaDoc {
92         super.processingInstruction(target, data);
93         afterTag = true;
94     }
95
96     /**
97     * Output character data
98     */

99
100     public void characters(char[] chars, int start, int len) throws TransformerException JavaDoc {
101         for (int i=start; i<start+len; i++) {
102             if (chars[i]=='\n') {
103                 sameline = false;
104             }
105             if (!Character.isWhitespace(chars[i])) {
106                 allWhite = false;
107             }
108         }
109         super.characters(chars, start, len);
110         if (!allWhite) {
111             afterTag = false;
112         }
113     }
114
115     /**
116     * Output ignorable white space
117     */

118
119     public void ignorableWhitespace(char[] chars, int start, int len) throws TransformerException JavaDoc {
120         // ignore it
121
}
122
123     /**
124     * Output a comment
125     */

126
127     public void comment(char[] chars, int start, int len) throws TransformerException JavaDoc {
128         super.comment(chars, start, len);
129         afterTag = true;
130     }
131
132     /**
133     * End of document
134     */

135
136     public void endDocument() throws TransformerException JavaDoc {
137         super.endDocument();
138     }
139
140     /**
141     * Output white space to reflect the current indentation level
142     */

143
144     private void indent() throws TransformerException JavaDoc {
145         if (suppressedAtLevel >= 0) {
146             // indentation has been suppressed (by xmlspace="preserve")
147
return;
148         }
149         int spaces = level * indentSpaces;
150         while (spaces > indentChars.length()) {
151             indentChars += indentChars;
152         }
153         char[] array = new char[spaces + 1];
154         array[0] = '\n';
155         indentChars.getChars(0, spaces, array, 1);
156         super.characters(array, 0, spaces+1);
157         sameline = false;
158     }
159
160 };
161
162 //
163
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
164
// you may not use this file except in compliance with the License. You may obtain a copy of the
165
// License at http://www.mozilla.org/MPL/
166
//
167
// Software distributed under the License is distributed on an "AS IS" basis,
168
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
169
// See the License for the specific language governing rights and limitations under the License.
170
//
171
// The Original Code is: all this file.
172
//
173
// The Initial Developer of the Original Code is
174
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
175
//
176
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
177
//
178
// Contributor(s): none.
179
//
180

181
Popular Tags