1 package com.icl.saxon.om; 2 import com.icl.saxon.Mode; 3 import com.icl.saxon.Context; 4 import com.icl.saxon.Controller; 5 import com.icl.saxon.ContentEmitter; 6 import com.icl.saxon.expr.*; 7 import com.icl.saxon.output.ProxyEmitter; 8 import com.icl.saxon.pattern.*; 9 import com.icl.saxon.tree.ElementImpl; 10 11 import org.xml.sax.Attributes ; 12 13 import javax.xml.transform.TransformerException ; 14 15 20 21 public class Stripper extends ProxyEmitter 22 { 23 private boolean preserveAll; private boolean stripAll; 26 31 34 private byte[] stripStack = new byte[100]; 35 private int top = 0; 36 37 40 private Mode stripperMode; 41 42 private DummyElement element = new DummyElement(); 44 45 private Context context; 48 49 private NamePool namePool; 51 52 55 56 protected Stripper() {} 57 58 63 64 public Stripper(Mode stripperRules) { 65 stripperMode = stripperRules; 66 preserveAll = (stripperRules==null); 67 stripAll = false; 68 } 69 70 73 74 public void setPreserveAll() { 75 preserveAll = true; 76 stripAll = false; 77 } 78 79 83 84 public boolean getPreserveAll() { 85 return preserveAll; 86 } 87 88 91 92 public void setStripAll() { 93 preserveAll = false; 94 stripAll = true; 95 } 96 97 101 102 public boolean getStripAll() { 103 return stripAll; 104 } 105 106 107 110 111 public void setController(Controller controller) { 112 context = controller.makeContext(element); 113 namePool = controller.getNamePool(); 114 } 115 116 122 123 public boolean isSpacePreserving(int nameCode) { 124 try { 125 if (preserveAll) return true; 126 if (stripAll) return false; 127 element.setNameCode(nameCode); 128 Object rule = stripperMode.getRule(element, context); 129 if (rule==null) return true; 130 return ((Boolean )rule).booleanValue(); 131 } catch (TransformerException err) { 132 return true; 133 } 134 } 135 136 139 140 public void startDocument () throws TransformerException 141 { 142 top = 0; 144 stripStack[top]=0x01; super.startDocument(); 146 } 147 148 151 152 public void startElement (int nameCode, Attributes atts, int[] namespaces, int nscount) 153 throws TransformerException 154 { 155 super.startElement(nameCode, atts, namespaces, nscount); 157 158 byte preserveParent = stripStack[top]; 159 160 String xmlspace = atts.getValue(Namespace.XML, "space"); 161 byte preserve = (byte)(preserveParent & 0x02); 162 if (xmlspace!=null) { 163 if (xmlspace.equals("preserve")) preserve = 0x02; 164 if (xmlspace.equals("default")) preserve = 0x00; 165 } 166 if (isSpacePreserving(nameCode)) { 167 preserve |= 0x01; 168 } 169 170 172 top++; 173 if (top >= stripStack.length) { 174 byte[] newStack = new byte[top*2]; 175 System.arraycopy(stripStack, 0, newStack, 0, top); 176 stripStack = newStack; 177 } 178 stripStack[top] = preserve; 179 } 180 181 184 185 public void endElement (int nameCode) throws TransformerException 186 { 187 super.endElement(nameCode); 188 top--; 189 } 190 191 194 195 public void characters (char ch[], int start, int length) throws TransformerException 196 { 197 199 if (length > 0) { 200 if (stripStack[top]!=0 || !isWhite(ch, start, length)) { 201 super.characters(ch, start, length); 202 } 203 } 204 } 205 206 209 210 private boolean isWhite(char[] ch, int start, int length) { 211 for (int i=start; i<start+length; i++) { 212 if ( (int)ch[i] > 0x20 ) { 213 return false; 214 } 215 } 216 return true; 217 } 218 219 220 221 private class DummyElement extends ElementImpl { 222 public short getURICode() { 223 return namePool.getURICode(getNameCode()); 224 } 225 226 } 227 228 } 230 | Popular Tags |