| 1 |
package com.nwalsh.saxon; |
| 2 |
|
| 3 |
import java.util.Stack; |
| 4 |
import org.xml.sax.*; |
| 5 |
import org.w3c.dom.*; |
| 6 |
import javax.xml.transform.TransformerException; |
| 7 |
import com.icl.saxon.output.*; |
| 8 |
import com.icl.saxon.om.*; |
| 9 |
import com.icl.saxon.Controller; |
| 10 |
import com.icl.saxon.tree.AttributeCollection; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class UnwrapLinksEmitter extends CopyEmitter { |
| 45 |
|
| 46 |
protected Stack elementStack = null; |
| 47 |
protected Stack saveStack = null; |
| 48 |
|
| 49 |
|
| 50 |
protected static String foURI = "http://www.w3.org/1999/XSL/Format"; |
| 51 |
|
| 52 |
|
| 53 |
protected static String xhURI = "http://www.w3.org/1999/xhtml"; |
| 54 |
|
| 55 |
|
| 56 |
protected boolean foStylesheet = false; |
| 57 |
|
| 58 |
|
| 59 |
protected int linkDepth = 0; |
| 60 |
protected int skipDepth = 0; |
| 61 |
|
| 62 |
protected int htmlAFingerprint = 0; |
| 63 |
protected int xhtmlAFingerprint = 0; |
| 64 |
protected boolean inSkip = false; |
| 65 |
protected boolean tryAgain = false; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public UnwrapLinksEmitter(Controller controller, |
| 74 |
NamePool namePool, |
| 75 |
boolean foStylesheet) { |
| 76 |
super(controller,namePool); |
| 77 |
elementStack = new Stack(); |
| 78 |
this.foStylesheet = foStylesheet; |
| 79 |
|
| 80 |
htmlAFingerprint = namePool.getFingerprint("", "a"); |
| 81 |
xhtmlAFingerprint = namePool.getFingerprint(xhURI, "a"); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
public void startElement(int nameCode, |
| 86 |
org.xml.sax.Attributes attributes, |
| 87 |
int[] namespaces, |
| 88 |
int nscount) |
| 89 |
throws TransformerException { |
| 90 |
|
| 91 |
int thisFingerprint = namePool.getFingerprint(nameCode); |
| 92 |
boolean isLink = (thisFingerprint == htmlAFingerprint |
| 93 |
|| thisFingerprint == xhtmlAFingerprint); |
| 94 |
|
| 95 |
if (isLink) { |
| 96 |
linkDepth++; |
| 97 |
tryAgain = tryAgain || inSkip; |
| 98 |
} |
| 99 |
|
| 100 |
if (isLink && linkDepth > 1 && !inSkip) { |
| 101 |
inSkip = true; |
| 102 |
|
| 103 |
|
| 104 |
saveStack = new Stack(); |
| 105 |
Stack tempStack = new Stack(); |
| 106 |
while (!elementStack.empty()) { |
| 107 |
StartElementInfo elem = (StartElementInfo) elementStack.pop(); |
| 108 |
rtfEmitter.endElement(elem.getNameCode()); |
| 109 |
saveStack.push(elem); |
| 110 |
tempStack.push(elem); |
| 111 |
} |
| 112 |
|
| 113 |
while (!tempStack.empty()) { |
| 114 |
StartElementInfo elem = (StartElementInfo) tempStack.pop(); |
| 115 |
elementStack.push(elem); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
if (inSkip) { |
| 120 |
skipDepth++; |
| 121 |
} else { |
| 122 |
} |
| 123 |
|
| 124 |
rtfEmitter.startElement(nameCode,attributes,namespaces,nscount); |
| 125 |
|
| 126 |
StartElementInfo sei = new StartElementInfo(nameCode, attributes, |
| 127 |
namespaces, nscount); |
| 128 |
elementStack.push(sei); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
public void endElement(int nameCode) throws TransformerException { |
| 133 |
int thisFingerprint = namePool.getFingerprint(nameCode); |
| 134 |
boolean isLink = (thisFingerprint == htmlAFingerprint |
| 135 |
|| thisFingerprint == xhtmlAFingerprint); |
| 136 |
|
| 137 |
rtfEmitter.endElement(nameCode); |
| 138 |
elementStack.pop(); |
| 139 |
|
| 140 |
if (isLink) { |
| 141 |
linkDepth--; |
| 142 |
} |
| 143 |
|
| 144 |
if (inSkip) { |
| 145 |
skipDepth--; |
| 146 |
inSkip = (skipDepth > 0); |
| 147 |
if (!inSkip) { |
| 148 |
|
| 149 |
while (!saveStack.empty()) { |
| 150 |
StartElementInfo elem = (StartElementInfo) saveStack.pop(); |
| 151 |
|
| 152 |
AttributeCollection attr = (AttributeCollection)elem.getAttributes(); |
| 153 |
AttributeCollection newAttr = new AttributeCollection(namePool); |
| 154 |
|
| 155 |
for (int acount = 0; acount < attr.getLength(); acount++) { |
| 156 |
String localName = attr.getLocalName(acount); |
| 157 |
String type = attr.getType(acount); |
| 158 |
String value = attr.getValue(acount); |
| 159 |
String uri = attr.getURI(acount); |
| 160 |
String prefix = ""; |
| 161 |
|
| 162 |
if (localName.indexOf(':') > 0) { |
| 163 |
prefix = localName.substring(0, localName.indexOf(':')); |
| 164 |
localName = localName.substring(localName.indexOf(':')+1); |
| 165 |
} |
| 166 |
|
| 167 |
if (uri.equals("") |
| 168 |
&& ((foStylesheet |
| 169 |
&& localName.equals("id")) |
| 170 |
|| (!foStylesheet |
| 171 |
&& (localName.equals("id") |
| 172 |
|| localName.equals("name"))))) { |
| 173 |
|
| 174 |
} else { |
| 175 |
newAttr.addAttribute(prefix, uri, localName, type, value); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
rtfEmitter.startElement(elem.getNameCode(), |
| 180 |
newAttr, |
| 181 |
elem.getNamespaces(), |
| 182 |
elem.getNSCount()); |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
public boolean tryAgain() |
| 189 |
throws TransformerException { |
| 190 |
return tryAgain; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
private class StartElementInfo { |
| 202 |
private int _nameCode; |
| 203 |
org.xml.sax.Attributes _attributes; |
| 204 |
int[] _namespaces; |
| 205 |
int _nscount; |
| 206 |
|
| 207 |
public StartElementInfo(int nameCode, |
| 208 |
org.xml.sax.Attributes attributes, |
| 209 |
int[] namespaces, |
| 210 |
int nscount) { |
| 211 |
_nameCode = nameCode; |
| 212 |
_attributes = attributes; |
| 213 |
_namespaces = namespaces; |
| 214 |
_nscount = nscount; |
| 215 |
} |
| 216 |
|
| 217 |
public int getNameCode() { |
| 218 |
return _nameCode; |
| 219 |
} |
| 220 |
|
| 221 |
public org.xml.sax.Attributes getAttributes() { |
| 222 |
return _attributes; |
| 223 |
} |
| 224 |
|
| 225 |
public int[] getNamespaces() { |
| 226 |
return _namespaces; |
| 227 |
} |
| 228 |
|
| 229 |
public int getNSCount() { |
| 230 |
return _nscount; |
| 231 |
} |
| 232 |
} |
| 233 |
} |