Matcher wikiProtocolMatcher = Pattern.compile(REGEX_WIKI_PROTOCOL).matcher(linkText.trim());
Matcher knownProtocolMatcher = Pattern.compile(REGEX_KNOWN_PROTOCOL).matcher(linkText.trim());
Matcher customProtocolMatcher = Pattern.compile(REGEX_CUSTOM_PROTOCOL).matcher(linkText.trim());
WikiLink wikiLink;
// Check if its a common protocol
if (knownProtocolMatcher.find()) {
wikiLink = new WikiLink(false, true);
wikiLink.setUrl(linkText);
wikiLink.setDescription(linkText);
log.debug("link resolved to known protocol: " + linkText);
// Check if it is a wiki protocol
} else if (wikiProtocolMatcher.find()) {
// Find the node by PK
WikiFile file = wikiNodeDAO.findWikiFile(Long.valueOf(wikiProtocolMatcher.group(1)));
String fragment = wikiProtocolMatcher.group(2);
if (file != null) {
wikiLink = new WikiLink(false, false);
wikiLink.setFile(file);
wikiLink.setFragment(fragment);
wikiLink.setDescription(file.getName());
log.debug("link text resolved to existing node: " + file + " and fragment: " + fragment);
} else {
// Can't do anything, [=>wiki://123] no longer exists
wikiLink = new WikiLink(true, false);
wikiLink.setUrl(BROKENLINK_URL);
wikiLink.setDescription(BROKENLINK_DESCRIPTION);
log.debug("link tet could not be resolved: " + linkText);
}
// Check if it is a custom protocol
} else if (customProtocolMatcher.find()) {
if (linkProtocolMap.containsKey(customProtocolMatcher.group(1))) {
LinkProtocol protocol = linkProtocolMap.get(customProtocolMatcher.group(1));
wikiLink = new WikiLink(false, true);
wikiLink.setUrl(protocol.getRealLink(customProtocolMatcher.group(2)));
wikiLink.setDescription(protocol.getPrefix() + "://" + customProtocolMatcher.group(2));
log.debug("link text resolved to custom protocol: " + linkText);
} else {
wikiLink = new WikiLink(true, false);
wikiLink.setUrl(BROKENLINK_URL);
wikiLink.setDescription(BROKENLINK_DESCRIPTION);
log.debug("link text resolved to non-existant custom protocol: " + linkText);
}
// It must be a stored clear text link, such as [=>Target Name] or [=>Area Name|Target Name]
// (This can happen if the string [foo=>bar] or [foo=>bar|baz] was stored in the database because the
// targets didn't exist at the time of saving)
} else {
// Try a WikiWord search in the current or named area
WikiFile file = null;
String fragment = null;
Matcher linkTextMatcher = getCrossAreaMatcher(linkText);
if (linkTextMatcher != null) {
file = resolve(currentAreaNumber, linkTextMatcher.group(1), linkTextMatcher.group(2));
fragment = linkTextMatcher.group(3);
} else {
linkTextMatcher = getAreaMatcher(linkText);
if (linkTextMatcher != null) {
file = resolve(currentAreaNumber, null, linkTextMatcher.group(1));
fragment = linkTextMatcher.group(2);
}
}
if (file!=null) {
wikiLink = new WikiLink(false, false);
wikiLink.setFile(file);
wikiLink.setFragment(fragment);
wikiLink.setDescription(file.getName());
// Indicate that caller should update the wiki text that contains this link
wikiLink.setRequiresUpdating(true);
log.debug("link text resolved (needs updating to wiki protocol): " + file + " and fragment: " + fragment);
} else {
/* TODO: Not sure we should actually implement this..., one of these things that the wiki "designers" got wrong
// OK, so it's not any recognized URL and we can't find a node with that wikiname
// Let's assume its a page name and render /Area/WikiLink (but encoded, so it gets transported fully)
// into the edit page when the user clicks on the link to create the document
try {
String encodedPagename = currentDirectory.getWikiname() + "/" + URLEncoder.encode(linkText, "UTF-8");
wikiLink = new WikiLink(null, true, encodedPagename, linkText);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e); // Java is so great...
}
*/
wikiLink = new WikiLink(true, false);
wikiLink.setUrl(BROKENLINK_URL);
wikiLink.setDescription(BROKENLINK_DESCRIPTION);
log.debug("could not resolve link: " + linkText);
}
}
links.put(linkText, wikiLink);
}