|
package com.surajkamdi.core.services.rewriter; |
|
|
|
import com.surajkamdi.core.utils.PathUtil; |
|
import com.adobe.acs.commons.util.ModeUtil; |
|
import java.io.IOException; |
|
import org.apache.commons.lang3.BooleanUtils; |
|
import org.apache.sling.rewriter.ProcessingComponentConfiguration; |
|
import org.apache.sling.rewriter.ProcessingContext; |
|
import org.apache.sling.rewriter.Transformer; |
|
import org.xml.sax.Attributes; |
|
import org.xml.sax.ContentHandler; |
|
import org.xml.sax.Locator; |
|
import org.xml.sax.SAXException; |
|
import org.xml.sax.helpers.AttributesImpl; |
|
|
|
/** LinkRewriter will rewrite sites urls to remove "/content/abc". */ |
|
public class LinkRewriter implements Transformer { |
|
|
|
|
|
private static final String HREF = "href"; |
|
private static final String DATA_NO_REWRITE = "data-no-rewrite"; |
|
private static final String A = "a"; |
|
|
|
private ContentHandler contentHandler; |
|
|
|
@Override |
|
public void setDocumentLocator(Locator locator) { |
|
contentHandler.setDocumentLocator(locator); |
|
} |
|
|
|
@Override |
|
public void startDocument() throws SAXException { |
|
contentHandler.startDocument(); |
|
} |
|
|
|
@Override |
|
public void endDocument() throws SAXException { |
|
contentHandler.endDocument(); |
|
} |
|
|
|
@Override |
|
public void startPrefixMapping(String prefix, String uri) throws SAXException { |
|
contentHandler.startPrefixMapping(prefix, uri); |
|
} |
|
|
|
@Override |
|
public void endPrefixMapping(String prefix) throws SAXException { |
|
contentHandler.endPrefixMapping(prefix); |
|
} |
|
|
|
@Override |
|
public void startElement(String uri, String localName, String qualifiedName, Attributes atts) |
|
throws SAXException { |
|
if (shouldRewrite(localName, atts)) { |
|
|
|
String href = PathUtil.removeContentPrefix(atts.getValue(HREF)); |
|
if (href != null) { |
|
AttributesImpl modifiedAttributes = new AttributesImpl(atts); |
|
modifiedAttributes.setValue(atts.getIndex(HREF), href); |
|
contentHandler.startElement(uri, localName, qualifiedName, modifiedAttributes); |
|
} else { |
|
contentHandler.startElement(uri, localName, qualifiedName, atts); |
|
} |
|
} else { |
|
contentHandler.startElement(uri, localName, qualifiedName, atts); |
|
} |
|
} |
|
|
|
@Override |
|
public void endElement(String uri, String localName, String qualifiedName) throws SAXException { |
|
contentHandler.endElement(uri, localName, qualifiedName); |
|
} |
|
|
|
@Override |
|
public void characters(char[] ch, int start, int length) throws SAXException { |
|
contentHandler.characters(ch, start, length); |
|
} |
|
|
|
@Override |
|
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { |
|
contentHandler.ignorableWhitespace(ch, start, length); |
|
} |
|
|
|
@Override |
|
public void processingInstruction(String target, String data) throws SAXException { |
|
contentHandler.processingInstruction(target, data); |
|
} |
|
|
|
@Override |
|
public void skippedEntity(String name) throws SAXException { |
|
contentHandler.skippedEntity(name); |
|
} |
|
|
|
@Override |
|
public void dispose() { |
|
// nothing to dispose. |
|
} |
|
|
|
@Override |
|
public void init(ProcessingContext context, ProcessingComponentConfiguration config) |
|
throws IOException { |
|
// nothing to init. |
|
} |
|
|
|
@Override |
|
public void setContentHandler(ContentHandler handler) { |
|
this.contentHandler = handler; |
|
} |
|
|
|
private boolean shouldRewrite(String localName, Attributes atts) { |
|
return A.equalsIgnoreCase(localName) |
|
&& ModeUtil.isPublish() |
|
&& !BooleanUtils.toBoolean(atts.getValue(DATA_NO_REWRITE)); |
|
} |
|
} |