要解决Adobe CQ / AEM Link Checker Transformer不会剥离HTML扩展名的问题,您可以通过自定义Transformer来处理。
以下是一个示例代码,演示如何自定义Link Checker Transformer以剥离HTML扩展名:
import org.apache.sling.rewriter.Transformer;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.rewriter.TransformerFactory;
import org.apache.sling.rewriter.TransformerType;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import javax.xml.transform.TransformerConfigurationException;
@Component(immediate = true)
@Service
@Properties({
@Property(name = "pipeline.type", value = "custom-link-checker")
})
public class CustomLinkCheckerTransformerFactory implements TransformerFactory {
@Override
public Transformer createTransformer() throws TransformerConfigurationException {
return new CustomLinkCheckerTransformer();
}
@Override
public TransformerType[] getTypes() {
return new TransformerType[]{TransformerType.HTML};
}
}
class CustomLinkCheckerTransformer implements Transformer {
private ContentHandler contentHandler;
private Locator locator;
@Override
public void setXMLReader(XMLReader xmlReader) {
if (xmlReader != null) {
xmlReader.setContentHandler(contentHandler);
xmlReader.setEntityResolver(contentHandler);
xmlReader.setErrorHandler(contentHandler);
try {
xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", contentHandler);
} catch (SAXException e) {
// Handle exception
}
}
}
@Override
public void setContentHandler(ContentHandler contentHandler) {
this.contentHandler = contentHandler;
}
@Override
public void setLocator(Locator locator) {
this.locator = locator;
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
contentHandler.characters(ch, start, length);
}
@Override
public void startDocument() throws SAXException {
contentHandler.startDocument();
}
@Override
public void endDocument() throws SAXException {
contentHandler.endDocument();
}
@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
if (localName.equalsIgnoreCase("a")) {
String href = atts.getValue("href");
if (href != null && href.endsWith(".html")) {
// Remove .html extension
href = href.substring(0, href.length() - 5);
atts.setValue("href", href);
}
}
contentHandler.startElement(uri, localName, qName, atts);
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
contentHandler.endElement(uri, localName, qName);
}
}
要使用此自定义Transformer,请按照以下步骤操作:
这样,Link Checker将在检查链接时剥离HTML扩展名。