1 package com.icl.saxon.output; 2 import com.icl.saxon.*; 3 import com.icl.saxon.sort.HashMap; 4 import java.io.*; 5 import org.xml.sax.Attributes ; 6 import javax.xml.transform.TransformerException ; 7 8 17 18 19 public class HTMLIndenter 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 isInlineTag = false; 26 private boolean inFormattedTag = false; 27 private boolean afterInline = false; 28 private boolean afterFormatted = true; 30 31 34 private static String [] inlineTags = { 35 "tt", "i", "b", "u", "s", "strike", "big", "small", "em", "strong", "dfn", "code", "samp", 36 "kbd", "var", "cite", "abbr", "acronym", "a", "img", "applet", "object", "font", 37 "basefont", "br", "script", "map", "q", "sub", "sup", "span", "bdo", "iframe", "input", 38 "select", "textarea", "label", "button" }; 39 40 private static HashMap inlineTable = new HashMap(203); 41 42 static { 43 for (int j=0; j<inlineTags.length; j++) { 44 inlineTable.set(inlineTags[j]); 45 } 46 } 47 48 private static boolean isInline(String tag) { 49 return inlineTable.get(tag); 50 } 51 52 54 private static HashMap formattedTable = new HashMap(51); 55 56 static { 57 formattedTable.set("pre"); 58 formattedTable.set("script"); 59 formattedTable.set("style"); 60 formattedTable.set("textarea"); 61 formattedTable.set("xmp"); } 63 64 private static boolean isFormatted(String tag) { 65 return formattedTable.get(tag); 66 } 67 68 69 70 public HTMLIndenter() {} 71 72 73 76 77 public void startDocument() throws TransformerException { 78 super.startDocument(); 79 String s = outputProperties.getProperty(SaxonOutputKeys.INDENT_SPACES); 80 if (s==null) { 81 indentSpaces = 3; 82 } else { 83 try { 84 indentSpaces = Integer.parseInt(s); 85 } catch (Exception err) { 86 indentSpaces = 3; 87 } 88 } 89 } 90 91 94 95 public void startElement(int nameCode, Attributes atts, 96 int[] namespaces, int nscount) throws TransformerException { 97 String tag = namePool.getDisplayName(nameCode); 98 isInlineTag = isInline(tag); 99 inFormattedTag = inFormattedTag || isFormatted(tag); 100 if (!isInlineTag && !inFormattedTag && 101 !afterInline && !afterFormatted) { 102 indent(); 103 } 104 105 106 super.startElement(nameCode, atts, namespaces, nscount); 107 level++; 108 sameLine = true; 109 afterInline = false; 110 afterFormatted = false; 111 } 112 113 116 117 public void endElement(int nameCode) throws TransformerException { 118 level--; 119 String tag = namePool.getDisplayName(nameCode); 120 boolean thisInline = isInline(tag); 121 boolean thisFormatted = isFormatted(tag); 122 if (!thisInline && !thisFormatted && !afterInline && 123 !sameLine && !afterFormatted && !inFormattedTag) { 124 indent(); 125 afterInline = false; 126 afterFormatted = false; 127 } else { 128 afterInline = thisInline; 129 afterFormatted = thisFormatted; 130 } 131 super.endElement(nameCode); 132 inFormattedTag = inFormattedTag && !thisFormatted; 133 sameLine = false; 134 } 135 136 139 140 public void processingInstruction(String target, String data) throws TransformerException { 141 super.processingInstruction(target, data); 142 } 143 144 147 148 public void characters(char[] chars, int start, int len) throws TransformerException { 149 if (inFormattedTag) { 150 super.characters(chars, start, len); 151 } else { 152 int lastNL = start; 153 154 for (int i=start; i<start+len; i++) { 155 if (chars[i]=='\n' || (i-lastNL > 120 && chars[i]==' ')) { 156 sameLine = false; 157 super.characters(chars, lastNL, i-lastNL); 158 indent(); 159 lastNL = i+1; 160 while (lastNL<len && chars[lastNL]==' ') lastNL++; 161 } 162 } 163 if (lastNL<start+len) { 164 super.characters(chars, lastNL, start+len-lastNL); 165 } 166 } 167 afterInline = false; 168 } 169 170 173 174 public void ignorableWhitespace(char[] chars, int start, int len) throws TransformerException { 175 } 177 178 181 182 public void comment(char[] chars, int start, int len) throws TransformerException { 183 indent(); 184 super.comment(chars, start, len); 185 } 186 187 190 191 public void endDocument() throws TransformerException { 192 super.endDocument(); 193 } 194 195 198 199 private void indent() throws TransformerException { 200 int spaces = level * indentSpaces; 201 while (spaces > indentChars.length()) { 202 indentChars += indentChars; 203 } 204 char[] array = new char[spaces + 1]; 205 array[0] = '\n'; 206 indentChars.getChars(0, spaces, array, 1); 207 super.characters(array, 0, spaces+1); 208 sameLine = false; 209 } 210 211 }; 212 213 232 | Popular Tags |