protected String convertLink()

in src/main/java/com/atlassian/uwc/converters/dokuwiki/HierarchyLinkConverter.java [43:153]


	protected String convertLink(String input, String currentPath, String spacekey, String pagepath) {
		Matcher linkFinder = link.matcher(input);
		String currentSpacekey = spacekey;
		Vector<String> allspaces = getSpaces();
		HashMap<String,String> namespaces = getDokuDirectories();
		StringBuffer sb = new StringBuffer();
		boolean found = false;
		while (linkFinder.find()) {
			found = true;
			String target = linkFinder.group(1);
			log.debug("link target orig: " + target);
			String alias = null;
			if (target.startsWith("\\\\")) continue; //UNC link
			if (target.contains("|")) {
				String[] parts = target.split("\\|");
				if (parts.length > 1) {
					target = parts[0];
					alias = parts[1];
				}
			}
			//FIXME anchors? we're not ready to transform these, so just get rid of the anchor part
			if (target.contains("#")) target = target.replaceAll("#[^|]*", "");
			log.debug("target: '" + target + "' and alias: '" + alias + "'");
			//remove any opening colons (:namespace:page)
			target = target.trim();
			if (!isExternal(target)) {
				if (target.startsWith(".")) {
					target = target.replaceAll("^[.]*", "");
					if (currentPath != null && !currentPath.equals(currentSpacekey)) { //need to add hierarchy in
						String pre = currentPath.replaceAll("\\/", ":");
//						log.debug("pre = " +pre);
						if (pre.endsWith(".txt")) {
							pre = pre.replaceFirst("[.]txt$", "");
							String sibling = target.replaceFirst(":[^:]+$", "");
//							log.debug("sibling = " +sibling);
							if (pre.endsWith(sibling)) {
								target = target.replaceFirst("^:[^:]+:", "");
							}
						}
						target = pre + ":" + target;
					}
				}
				if (target.startsWith(":")) target = target.replaceFirst(":", "");
				//figure out if we've already got the space represented
				String targetPart1 = target.replaceFirst(":[^:]+$", "");
				boolean containsSpace = false;
//				log.debug("targetPart1 = " + targetPart1);
				String nsFromTarget = findNSFromTarget(namespaces, target);
				if (nsFromTarget != null) {
					targetPart1 = nsFromTarget;
				}
				if (allspaces.contains(targetPart1)) 
					containsSpace = true;
//				log.debug("--LinkConverter");
//				log.debug("targetPart1 =" + targetPart1);
				//get rid of unnecessary links to start 
				//(start page content will be moved to parent in DokuwikiHierarchy
				//unless the start page is a top level page in the space)
				boolean isOne = (onecolon.matcher(target)).matches();
				if (!(containsSpace && isOne)) 
					target = target.replaceFirst(":start$", "");
				String hierarchy = target; //save for later
				//is there a meta title to be used?
//				log.debug("pagepath = " + pagepath);
				String origMetaFilename = getMetaFilename(pagepath, ".meta");
				String metaFilename = origMetaFilename;
//				log.debug("isOne = " + isOne + ", target = " + target + ", metaFilename = " + metaFilename);
				metaFilename = getTargetMetaFilename(target, metaFilename, isOne);
//				log.debug("metaFilename = " + metaFilename);
				String metatitle = HierarchyTitleConverter.getMetaTitle(metaFilename);
				log.debug("metatitle = " + metatitle);
				//get confluence page name and fix the case to match HierarchyTitleConverter
				if (metatitle == null || "".equals(metatitle)) 
					target = target.replaceFirst("^.*:", ""); //remove everything to the last colon
				else //title was set with metadata
					target = metatitle;
				target = HierarchyTitleConverter.fixTitle(target); //foo_bar becomes Foo Bar
				//fix collisions
				String linkSpacekey = currentSpacekey;
				targetPart1 = targetPart1.replaceAll(":+", File.separator);
//				log.debug("containsSpace: " + containsSpace + ", " +
//						"ns: "+ namespaces.containsKey(targetPart1)
//						+", tp1: '" + targetPart1+"'");
				if (!containsSpace && namespaces.containsKey(targetPart1)) {
					linkSpacekey = namespaces.get(targetPart1); 
					log.debug("linkSpacekey = " + linkSpacekey);
				}
				if (containsSpace) linkSpacekey = targetPart1;
				target = fixCollisions(target, hierarchy, linkSpacekey, metaFilename);
				//underscores to spaces
				target = target.replaceAll("_", " ");
				//add spacekey to target if necessary
				if (!target.contains(":") || containsSpace) 
					target = linkSpacekey + ":" + target;
				blogLinkReport(namespaces, targetPart1, origMetaFilename, spacekey, target);
				log.debug("link target = " + target);
			}
			//build complete link
			String replacement = (alias == null)?
					target:
					alias.trim() + "|" + target;
			//replace
			replacement = RegexUtil.handleEscapesInReplacement(replacement);
			linkFinder.appendReplacement(sb, replacement);
		}
		if (found) {
			linkFinder.appendTail(sb);
			return sb.toString();
		}
		return input;
	}