* @param contextPath The servlet context path.
*/
public void rewriteLinks(Document originalTargetDocument, Document newTargetDocument,
String contextPath) {
Publication publication = originalTargetDocument.getPublication();
String area = originalTargetDocument.getArea();
File[] files = getDocumentFiles(publication, area);
DocumentBuilder builder = publication.getDocumentBuilder();
try {
for (int fileIndex = 0; fileIndex < files.length; fileIndex++) {
org.w3c.dom.Document xmlDocument = DocumentHelper.readDocument(files[fileIndex]);
boolean linksRewritten = false;
String[] xPaths = publication.getRewriteAttributeXPaths();
for (int xPathIndex = 0; xPathIndex < xPaths.length; xPathIndex++) {
NodeList nodes = XPathAPI.selectNodeList(xmlDocument, xPaths[xPathIndex]);
for (int nodeIndex = 0; nodeIndex < nodes.getLength(); nodeIndex++) {
Node node = nodes.item(nodeIndex);
if (node.getNodeType() != Node.ATTRIBUTE_NODE) {
throw new RuntimeException("The XPath [" + xPaths[xPathIndex]
+ "] may only match attribute nodes!");
}
Attr attribute = (Attr) node;
final String url = attribute.getValue();
if (url.startsWith(contextPath + "/" + publication.getId())) {
final String webappUrl = url.substring(contextPath.length());
if (builder.isDocument(publication, webappUrl)) {
Document targetDocument = builder.buildDocument(publication, webappUrl);