1 package com.nwalsh.saxon; 2 3 import java.util.Stack ; 4 import java.util.StringTokenizer ; 5 import org.xml.sax.*; 6 import org.w3c.dom.*; 7 import javax.xml.transform.TransformerException ; 8 import com.icl.saxon.output.*; 9 import com.icl.saxon.om.*; 10 import com.icl.saxon.Controller; 11 import com.icl.saxon.tree.AttributeCollection; 12 import com.icl.saxon.expr.FragmentValue; 13 14 50 public class NumberLinesEmitter extends CopyEmitter { 51 52 protected Stack elementStack = null; 53 54 55 protected int lineNumber = 0; 56 57 58 protected boolean firstElement = false; 59 60 61 protected static String foURI = "http://www.w3.org/1999/XSL/Format"; 62 63 64 protected static String xhURI = "http://www.w3.org/1999/xhtml"; 65 66 67 protected int startinglinenumber = 1; 68 69 70 protected int modulus = 5; 71 72 73 protected int width = 3; 74 75 76 protected String separator = " "; 77 78 79 protected boolean foStylesheet = false; 80 81 89 public NumberLinesEmitter(Controller controller, 90 NamePool namePool, 91 int startingLineNumber, 92 int modulus, 93 int width, 94 String separator, 95 boolean foStylesheet) { 96 super(controller,namePool); 97 elementStack = new Stack (); 98 firstElement = true; 99 100 this.modulus = modulus; 101 this.startinglinenumber = startingLineNumber; 102 this.width = width; 103 this.separator = separator; 104 this.foStylesheet = foStylesheet; 105 } 106 107 108 public void characters(char[] chars, int start, int len) 109 throws TransformerException { 110 111 firstElement = false; 113 114 if (lineNumber == 0) { 115 lineNumber = startinglinenumber; 117 formatLineNumber(lineNumber); 118 } 119 120 char[] newChars = new char[len]; 122 int pos = 0; 123 for (int count = start; count < start+len; count++) { 124 if (chars[count] == '\n') { 125 128 if (pos > 0) { 129 rtfEmitter.characters(newChars, 0, pos); 131 pos = 0; 132 } 133 134 Stack tempStack = new Stack (); 136 while (!elementStack.empty()) { 137 StartElementInfo elem = (StartElementInfo) elementStack.pop(); 138 rtfEmitter.endElement(elem.getNameCode()); 139 tempStack.push(elem); 140 } 141 142 newChars[pos++] = chars[count]; 144 rtfEmitter.characters(newChars, 0, pos); 145 pos = 0; 146 147 formatLineNumber(++lineNumber); 149 150 while (!tempStack.empty()) { 152 StartElementInfo elem = (StartElementInfo) tempStack.pop(); 153 AttributeCollection attr = (AttributeCollection)elem.getAttributes(); 154 AttributeCollection newAttr = new AttributeCollection(namePool); 155 156 for (int acount = 0; acount < attr.getLength(); acount++) { 157 String localName = attr.getLocalName(acount); 158 int nameCode = attr.getNameCode(acount); 159 String type = attr.getType(acount); 160 String value = attr.getValue(acount); 161 String uri = attr.getURI(acount); 162 String prefix = ""; 163 164 if (localName.indexOf(':') > 0) { 165 prefix = localName.substring(0, localName.indexOf(':')); 166 localName = localName.substring(localName.indexOf(':')+1); 167 } 168 169 if (uri.equals("") 170 && ((foStylesheet 171 && localName.equals("id")) 172 || (!foStylesheet 173 && (localName.equals("id") 174 || localName.equals("name"))))) { 175 } else { 177 newAttr.addAttribute(prefix, uri, localName, type, value); 178 } 179 } 180 181 rtfEmitter.startElement(elem.getNameCode(), 182 newAttr, 183 elem.getNamespaces(), 184 elem.getNSCount()); 185 186 elementStack.push(elem); 187 } 188 } else { 189 newChars[pos++] = chars[count]; 190 } 191 } 192 193 if (pos > 0) { 194 rtfEmitter.characters(newChars, 0, pos); 195 pos = 0; 196 } 197 } 198 199 204 protected void formatLineNumber(int lineNumber) 205 throws TransformerException { 206 207 char ch = 160; 209 String lno = ""; 210 if (lineNumber == 1 211 || (modulus >= 1 && (lineNumber % modulus == 0))) { 212 lno = "" + lineNumber; 213 } 214 215 while (lno.length() < width) { 216 lno = ch + lno; 217 } 218 219 lno += separator; 220 221 char chars[] = new char[lno.length()]; 222 for (int count = 0; count < lno.length(); count++) { 223 chars[count] = lno.charAt(count); 224 } 225 226 characters(chars, 0, lno.length()); 227 } 228 229 230 public void endElement(int nameCode) 231 throws TransformerException { 232 if (!elementStack.empty()) { 233 elementStack.pop(); 237 } 238 rtfEmitter.endElement(nameCode); 239 } 240 241 242 public void startElement(int nameCode, 243 org.xml.sax.Attributes attributes, 244 int[] namespaces, 245 int nscount) 246 throws TransformerException { 247 248 if (!skipThisElement(nameCode)) { 249 StartElementInfo sei = new StartElementInfo(nameCode, attributes, 250 namespaces, nscount); 251 elementStack.push(sei); 252 } 253 254 firstElement = false; 255 256 rtfEmitter.startElement(nameCode, attributes, namespaces, nscount); 257 } 258 259 275 protected boolean skipThisElement(int nameCode) { 276 if (firstElement) { 278 int thisFingerprint = namePool.getFingerprint(nameCode); 279 int foBlockFingerprint = namePool.getFingerprint(foURI, "block"); 280 int htmlPreFingerprint = namePool.getFingerprint("", "pre"); 281 int htmlDivFingerprint = namePool.getFingerprint("", "div"); 282 int xhtmlPreFingerprint = namePool.getFingerprint(xhURI, "pre"); 283 int xhtmlDivFingerprint = namePool.getFingerprint(xhURI, "div"); 284 285 if ((foStylesheet && thisFingerprint == foBlockFingerprint) 286 || (!foStylesheet && (thisFingerprint == htmlPreFingerprint 287 || thisFingerprint == htmlDivFingerprint 288 || thisFingerprint == xhtmlPreFingerprint 289 || thisFingerprint == xhtmlDivFingerprint))) { 290 return true; 292 } 293 } 294 295 return false; 296 } 297 298 306 private class StartElementInfo { 307 private int _nameCode; 308 org.xml.sax.Attributes _attributes; 309 int[] _namespaces; 310 int _nscount; 311 312 public StartElementInfo(int nameCode, 313 org.xml.sax.Attributes attributes, 314 int[] namespaces, 315 int nscount) { 316 _nameCode = nameCode; 317 _attributes = attributes; 318 _namespaces = namespaces; 319 _nscount = nscount; 320 } 321 322 public int getNameCode() { 323 return _nameCode; 324 } 325 326 public org.xml.sax.Attributes getAttributes() { 327 return _attributes; 328 } 329 330 public int[] getNamespaces() { 331 return _namespaces; 332 } 333 334 public int getNSCount() { 335 return _nscount; 336 } 337 } 338 } 339 | Popular Tags |