*/
public void rewriteLinks(Document originalTargetDocument, Document newTargetDocument) {
Publication publication = originalTargetDocument.getPublication();
String area = originalTargetDocument.getArea();
DocumentIdentityMap identityMap = originalTargetDocument.getIdentityMap();
Document[] documents;
ServiceSelector selector = null;
SiteManager siteManager = null;
try {
selector = (ServiceSelector) this.manager.lookup(SiteManager.ROLE + "Selector");
siteManager = (SiteManager) selector.select(publication.getSiteManagerHint());
documents = siteManager.getDocuments(identityMap, publication, area);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (selector != null) {
if (siteManager != null) {
selector.release(siteManager);
}
this.manager.release(selector);
}
}
Request request = ObjectModelHelper.getRequest(this.objectModel);
String contextPath = request.getContextPath();
try {
for (int documentIndex = 0; documentIndex < documents.length; documentIndex++) {
Document examinedDocument = documents[documentIndex];
if (examinedDocument.exists()) {
if (getLogger().isDebugEnabled()) {
getLogger().debug("Rewriting links in document [" + examinedDocument + "]");
}
boolean linksRewritten = false;
DocumentType doctype = examinedDocument.getResourceType();
String[] xPaths = doctype.getLinkAttributeXPaths();
try {
org.w3c.dom.Document xmlDocument = SourceUtil.readDOM(examinedDocument.getSourceURI(), this.manager);
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 (identityMap.isDocument(webappUrl)) {
Document targetDocument = identityMap.getFromURL(webappUrl);
if (matches(targetDocument, originalTargetDocument)) {
String newTargetUrl = getNewTargetURL(targetDocument,
originalTargetDocument,
newTargetDocument);