public String rewrite(String linkUrl) {
String rewrittenUrl = null;
URLInformation info = new URLInformation(this.currentUrl);
if (linkUrl.startsWith(SCHEME)) {
String anchor = null;
String url = null;
int anchorIndex = linkUrl.indexOf("#");
if (anchorIndex > -1) {
url = linkUrl.substring(0, anchorIndex);
anchor = linkUrl.substring(anchorIndex + 1);
} else {
url = linkUrl;
}
StringTokenizer tokenizer = new StringTokenizer(url, "?");
String linkUri = tokenizer.nextToken();
String queryString = null;
String requiredExtension = null;
if (tokenizer.hasMoreTokens()) {
queryString = tokenizer.nextToken();
Query query = new Query(queryString);
requiredExtension = query.getValue(EXTENSION_PARAM);
query.removeValue(EXTENSION_PARAM);
queryString = query.toString();
}
LinkTarget target;
try {
if (this.currentDoc != null) {
target = this.linkResolver.resolve(this.currentDoc, linkUri);
} else {
Link link = getAbsoluteLink(info, linkUri);
target = this.linkResolver.resolve(this.factory, link.getUri());
}
if (target.exists() && target.getDocument().hasLink()) {
Document targetDocument = target.getDocument();
String extension = getExtension(targetDocument, requiredExtension);
rewrittenUrl = getWebappUrl(targetDocument, anchor, queryString, extension);
} else {
rewrittenUrl = null;
}
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
/*
* This is legacy code. It rewrites links to non-document images (in
* resources/shared). These images shouldn't be referenced in
* documents since this violates the separation between content and
* layout.
*/
String prefix = "/" + info.getPublicationId() + "/";
if (linkUrl.startsWith(prefix)) {
String pubUrl = linkUrl.substring(prefix.length());
StringTokenizer tokenizer = new StringTokenizer(pubUrl, "/");
String area = tokenizer.nextToken();