| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.serialize; |
| 9 |
|
| 10 |
import net.sf.saxon.event.ProxyReceiver; |
| 11 |
import net.sf.saxon.event.Receiver; |
| 12 |
import net.sf.saxon.expr.parser.ExplicitLocation; |
| 13 |
import net.sf.saxon.expr.parser.Location; |
| 14 |
import net.sf.saxon.lib.NamespaceConstant; |
| 15 |
import net.sf.saxon.lib.SaxonOutputKeys; |
| 16 |
import net.sf.saxon.om.*; |
| 17 |
import net.sf.saxon.trans.XPathException; |
| 18 |
import net.sf.saxon.tree.util.AttributeCollectionImpl; |
| 19 |
import net.sf.saxon.type.BuiltInAtomicType; |
| 20 |
import net.sf.saxon.type.SchemaType; |
| 21 |
import net.sf.saxon.type.SimpleType; |
| 22 |
import net.sf.saxon.type.Untyped; |
| 23 |
import net.sf.saxon.value.Whitespace; |
| 24 |
|
| 25 |
import javax.xml.transform.OutputKeys; |
| 26 |
import java.util.ArrayList; |
| 27 |
import java.util.List; |
| 28 |
import java.util.Properties; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class MetaTagAdjuster extends ProxyReceiver { |
| 37 |
|
| 38 |
boolean seekingHead = true; |
| 39 |
int droppingMetaTags = -1; |
| 40 |
boolean inMetaTag = false; |
| 41 |
boolean foundHead = false; |
| 42 |
String headPrefix = null; |
| 43 |
String headURI = ""; |
| 44 |
NodeName metaCode; |
| 45 |
String requiredURI = ""; |
| 46 |
AttributeCollectionImpl attributes; |
| 47 |
List<NamespaceBinding> namespaces = new ArrayList<NamespaceBinding>(); |
| 48 |
String encoding; |
| 49 |
String mediaType; |
| 50 |
int level = 0; |
| 51 |
boolean isXHTML = false; |
| 52 |
int htmlVersion = 4; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public MetaTagAdjuster(Receiver next) { |
| 61 |
super(next); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public void setOutputProperties(Properties details) { |
| 71 |
encoding = details.getProperty(OutputKeys.ENCODING); |
| 72 |
if (encoding == null) { |
| 73 |
encoding = "UTF-8"; |
| 74 |
} |
| 75 |
mediaType = details.getProperty(OutputKeys.MEDIA_TYPE); |
| 76 |
if (mediaType == null) { |
| 77 |
mediaType = "text/html"; |
| 78 |
} |
| 79 |
String htmlVn = details.getProperty(SaxonOutputKeys.HTML_VERSION); |
| 80 |
if (htmlVn == null && !isXHTML) { |
| 81 |
htmlVn = details.getProperty(OutputKeys.VERSION); |
| 82 |
} |
| 83 |
if (htmlVn != null && htmlVn.startsWith("5")) { |
| 84 |
htmlVersion = 5; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public void setIsXHTML(boolean xhtml) { |
| 93 |
isXHTML = xhtml; |
| 94 |
if (xhtml) { |
| 95 |
requiredURI = NamespaceConstant.XHTML; |
| 96 |
} else { |
| 97 |
requiredURI = ""; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
private boolean comparesEqual(String name1, String name2) { |
| 106 |
if (isXHTML) { |
| 107 |
return name1.equals(name2); |
| 108 |
} else { |
| 109 |
return name1.equalsIgnoreCase(name2); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
private boolean matchesName(NodeName name, String local) { |
| 114 |
if (isXHTML) { |
| 115 |
if (!name.getLocalPart().equals(local)) { |
| 116 |
return false; |
| 117 |
} |
| 118 |
if (htmlVersion == 5) { |
| 119 |
return name.hasURI("") || name.hasURI(NamespaceConstant.XHTML); |
| 120 |
} else { |
| 121 |
return name.hasURI(NamespaceConstant.XHTML); |
| 122 |
} |
| 123 |
} else { |
| 124 |
return name.getLocalPart().equalsIgnoreCase(local); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public void startElement(NodeName nameCode, SchemaType typeCode, Location location, int properties) throws XPathException { |
| 137 |
if (droppingMetaTags == level) { |
| 138 |
metaCode = nameCode; |
| 139 |
if (matchesName(nameCode, "meta")) { |
| 140 |
inMetaTag = true; |
| 141 |
attributes.clear(); |
| 142 |
namespaces.clear(); |
| 143 |
return; |
| 144 |
} |
| 145 |
} |
| 146 |
level++; |
| 147 |
nextReceiver.startElement(nameCode, typeCode, location, properties); |
| 148 |
if (seekingHead) { |
| 149 |
if (matchesName(nameCode, "head")) { |
| 150 |
foundHead = true; |
| 151 |
headPrefix = nameCode.getPrefix(); |
| 152 |
headURI = nameCode.getURI(); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public void attribute(NodeName nameCode, SimpleType typeCode, CharSequence value, Location locationId, int properties) throws XPathException { |
| 175 |
if (inMetaTag) { |
| 176 |
attributes.addAttribute(nameCode, typeCode, value.toString(), locationId, properties); |
| 177 |
} else { |
| 178 |
nextReceiver.attribute(nameCode, typeCode, value, locationId, properties); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
@Override |
| 194 |
public void namespace(NamespaceBindingSet namespaceBindings, int properties) throws XPathException { |
| 195 |
if (inMetaTag) { |
| 196 |
for (NamespaceBinding ns : namespaceBindings) { |
| 197 |
namespaces.add(ns); |
| 198 |
} |
| 199 |
} else { |
| 200 |
nextReceiver.namespace(namespaceBindings, properties); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public void startContent() throws XPathException { |
| 213 |
if (foundHead) { |
| 214 |
foundHead = false; |
| 215 |
nextReceiver.startContent(); |
| 216 |
FingerprintedQName metaCode = new FingerprintedQName(headPrefix, headURI, "meta"); |
| 217 |
nextReceiver.startElement(metaCode, Untyped.getInstance(), null, 0); |
| 218 |
nextReceiver.attribute(new NoNamespaceName("http-equiv"), BuiltInAtomicType.UNTYPED_ATOMIC, "Content-Type", null, 0); |
| 219 |
nextReceiver.attribute(new NoNamespaceName("content"), BuiltInAtomicType.UNTYPED_ATOMIC, mediaType + "; charset=" + encoding, null, 0); |
| 220 |
nextReceiver.startContent(); |
| 221 |
droppingMetaTags = level; |
| 222 |
seekingHead = false; |
| 223 |
attributes = new AttributeCollectionImpl(getConfiguration()); |
| 224 |
nextReceiver.endElement(); |
| 225 |
} |
| 226 |
if (!inMetaTag) { |
| 227 |
nextReceiver.startContent(); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
public void endElement() throws XPathException { |
| 236 |
if (inMetaTag) { |
| 237 |
inMetaTag = false; |
| 238 |
|
| 239 |
boolean found = false; |
| 240 |
for (int i = 0; i < attributes.getLength(); i++) { |
| 241 |
String name = attributes.getLocalName(i); |
| 242 |
if (comparesEqual(name, "http-equiv")) { |
| 243 |
String value = Whitespace.trim(attributes.getValue(i)); |
| 244 |
if (value.equalsIgnoreCase("Content-Type")) { |
| 245 |
|
| 246 |
found = true; |
| 247 |
break; |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
if (!found) { |
| 252 |
|
| 253 |
nextReceiver.startElement(metaCode, Untyped.getInstance(), ExplicitLocation.UNKNOWN_LOCATION, 0); |
| 254 |
for (int i = 0; i < attributes.getLength(); i++) { |
| 255 |
NodeName name = attributes.getNodeName(i); |
| 256 |
SimpleType typeCode = attributes.getTypeAnnotation(i); |
| 257 |
String value = attributes.getValue(i); |
| 258 |
Location locationId = attributes.getLocation(i); |
| 259 |
int properties = attributes.getProperties(i); |
| 260 |
nextReceiver.attribute(name, typeCode, value, locationId, properties); |
| 261 |
} |
| 262 |
for (NamespaceBinding nb : namespaces) { |
| 263 |
nextReceiver.namespace(nb, 0); |
| 264 |
} |
| 265 |
nextReceiver.startContent(); |
| 266 |
nextReceiver.endElement(); |
| 267 |
} |
| 268 |
} else { |
| 269 |
level--; |
| 270 |
if (droppingMetaTags == level + 1) { |
| 271 |
droppingMetaTags = -1; |
| 272 |
} |
| 273 |
nextReceiver.endElement(); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
} |