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 ; 7 import javax.xml.transform.OutputKeys ; 8 import javax.xml.transform.TransformerException ; 9 10 17 18 19 public class XMLIndenter extends ProxyEmitter { 20 21 private int level = 0; 22 private int indentSpaces = 3; 23 private String indentChars = " "; 24 private boolean sameline = false; 25 private boolean afterTag = true; 26 private boolean allWhite = true; 27 private int suppressedAtLevel = -1; 28 30 33 34 public void startDocument() throws TransformerException { 35 super.startDocument(); 36 37 String s = outputProperties.getProperty(SaxonOutputKeys.INDENT_SPACES); 38 if (s!=null) { 39 try { 40 indentSpaces = Integer.parseInt(s); 41 } catch (Exception err) { 42 indentSpaces = 3; 43 } 44 } 45 46 String omit = outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION); 47 afterTag = omit==null || !omit.equals("yes") || 48 outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM)!=null ; 49 } 50 51 54 55 public void startElement(int tag, Attributes atts, 56 int[] namespaces, int nscount) throws TransformerException { 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 73 74 public void endElement(int tag) throws TransformerException { 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 } 85 } 86 87 90 91 public void processingInstruction(String target, String data) throws TransformerException { 92 super.processingInstruction(target, data); 93 afterTag = true; 94 } 95 96 99 100 public void characters(char[] chars, int start, int len) throws TransformerException { 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 118 119 public void ignorableWhitespace(char[] chars, int start, int len) throws TransformerException { 120 } 122 123 126 127 public void comment(char[] chars, int start, int len) throws TransformerException { 128 super.comment(chars, start, len); 129 afterTag = true; 130 } 131 132 135 136 public void endDocument() throws TransformerException { 137 super.endDocument(); 138 } 139 140 143 144 private void indent() throws TransformerException { 145 if (suppressedAtLevel >= 0) { 146 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 181 | Popular Tags |