1 package net.sf.saxon.event; 2 3 import net.sf.saxon.om.AttributeCollectionImpl; 4 import net.sf.saxon.om.NamePool; 5 import net.sf.saxon.om.NamespaceConstant; 6 import net.sf.saxon.style.StandardNames; 7 import net.sf.saxon.trans.XPathException; 8 9 import javax.xml.transform.OutputKeys ; 10 import java.util.Properties ; 11 12 17 18 public class MetaTagAdjuster extends ProxyReceiver { 19 20 boolean seekingHead = true; 21 int droppingMetaTags = -1; 22 boolean inMetaTag = false; 23 boolean foundHead = false; 24 int metaCode; 25 short requiredURICode = 0; 26 AttributeCollectionImpl attributes; 27 String encoding; 28 String mediaType; 29 int level = 0; 30 boolean isXHTML = false; 31 32 35 36 public void setOutputProperties(Properties details) throws XPathException { 37 encoding = details.getProperty(OutputKeys.ENCODING); 38 if (encoding == null) { 39 encoding = "UTF-8"; 40 } 41 mediaType = details.getProperty(OutputKeys.MEDIA_TYPE); 42 if (mediaType == null) { 43 mediaType = "text/html"; 44 } 45 } 46 47 50 51 public void setIsXHTML(boolean xhtml) { 52 isXHTML = xhtml; 53 if (xhtml) { 54 requiredURICode = getNamePool().getCodeForURI(NamespaceConstant.XHTML); 55 } else { 56 requiredURICode = 0; 57 } 58 } 59 60 63 64 private boolean comparesEqual(String name1, String name2) { 65 if (isXHTML) { 66 return name1.equals(name2); 67 } else { 68 return name1.equalsIgnoreCase(name2); 69 } 70 71 } 72 73 80 81 public void startElement(int nameCode, int typeCode, int locationId, int properties) throws XPathException { 82 if (droppingMetaTags == level) { 83 metaCode = nameCode; 84 int uriCode = getNamePool().getURICode(nameCode); 85 String localName = getNamePool().getLocalName(nameCode); 86 if (uriCode == requiredURICode && comparesEqual(localName, "meta")) { 87 inMetaTag = true; 88 attributes.clear(); 89 return; 90 } 91 } 92 level++; 93 super.startElement(nameCode, typeCode, locationId, properties); 94 if (seekingHead) { 95 NamePool namePool = getNamePool(); 96 int uriCode = namePool.getURICode(nameCode); 97 String localName = namePool.getLocalName(nameCode); 98 if (uriCode == requiredURICode && comparesEqual(localName, "head")) { 99 foundHead = true; 100 } 101 } 102 103 } 104 105 117 118 public void attribute(int nameCode, int typeCode, CharSequence value, int locationId, int properties) throws XPathException { 119 if (inMetaTag) { 120 attributes.addAttribute(nameCode, typeCode, value.toString(), locationId, properties); 121 } else { 122 super.attribute(nameCode, typeCode, value, locationId, properties); 123 } 124 } 125 126 132 133 134 public void startContent() throws XPathException { 135 if (foundHead) { 136 foundHead = false; 137 NamePool namePool = getNamePool(); 138 super.startContent(); 139 int metaCode = namePool.allocate("", requiredURICode, "meta"); 140 super.startElement(metaCode, StandardNames.XDT_UNTYPED, 0, 0); 141 int httpEquivCode = namePool.allocate("", "", "http-equiv"); 142 super.attribute(httpEquivCode, StandardNames.XDT_UNTYPED_ATOMIC, "Content-Type", 0, 0); 143 int contentCode = namePool.allocate("", "", "content"); 144 super.attribute(contentCode, StandardNames.XDT_UNTYPED_ATOMIC, mediaType + "; charset=" + encoding, 0, 0); 145 super.startContent(); 146 droppingMetaTags = level; 147 seekingHead = false; 148 attributes = new AttributeCollectionImpl(namePool); 149 super.endElement(); 150 } 151 if (!inMetaTag) { 152 super.startContent(); 153 } 154 } 155 156 159 160 public void endElement() throws XPathException { 161 if (inMetaTag) { 162 inMetaTag = false; 163 boolean found = false; 165 for (int i=0; i<attributes.getLength(); i++) { 166 String name = attributes.getLocalName(i); 167 if (comparesEqual(name, "http-equiv")) { 168 String value = attributes.getValue(i).trim(); 169 if (value.equalsIgnoreCase("Content-Type")) { 170 found = true; 172 break; 173 } 174 } 175 } 176 if (!found) { 177 super.startElement(metaCode, StandardNames.XDT_UNTYPED, 0, 0); 179 for (int i=0; i<attributes.getLength(); i++) { 180 int nameCode = attributes.getNameCode(i); 181 int typeCode = attributes.getTypeAnnotation(i); 182 String value = attributes.getValue(i); 183 int locationId = attributes.getLocationId(i); 184 int properties = attributes.getProperties(i); 185 super.attribute(nameCode, typeCode, value, locationId, properties); 186 } 187 super.startContent(); 188 super.endElement(); 189 } 190 } else { 191 level--; 192 if (droppingMetaTags == level+1) { 193 droppingMetaTags = -1; 194 } 195 super.endElement(); 196 } 197 } 198 199 } 200 201 219 | Popular Tags |