/src/com/atlassian/uwc/converters/dokuwiki/DokuWikiImageConverter.java

https://bitbucket.org/atlassianlabs/universal-wiki-connector · Java · 176 lines · 131 code · 20 blank · 25 comment · 22 complexity · 4a70c96d774ee9b6958562e86064dd5a MD5 · raw file

  1. package com.atlassian.uwc.converters.dokuwiki;
  2. import com.atlassian.uwc.converters.BaseConverter;
  3. import com.atlassian.uwc.ui.ConfluenceSettingsForm;
  4. import com.atlassian.uwc.ui.ConverterEngine;
  5. import com.atlassian.uwc.ui.Page;
  6. import com.atlassian.uwc.ui.UWCForm2;
  7. import org.apache.log4j.Logger;
  8. import java.io.File;
  9. /**
  10. * Pre-processes file names in images and attachments to that the ImageAttachmentConverter can
  11. * find them.
  12. * @author Rex
  13. * @version $Id$
  14. */
  15. public class DokuWikiImageConverter extends BaseConverter {
  16. private Logger log = Logger.getLogger(DokuWikiLinkConverter.class);
  17. private static final String IMAGE_START = "{{";
  18. private static final String IMAGE_END = "}}";
  19. private static final String SEPARATOR = "|";
  20. private static final String QUALIFIER = "?";
  21. /**
  22. * Converts any file names in image tags from the DokuWiki format to Confluence's format.
  23. * The converter also handles other types of attachments.
  24. *
  25. * @param page A page with text to be converted.
  26. */
  27. public void convert(Page page) {
  28. assert page != null;
  29. assert page.getOriginalText() != null;
  30. if (log.isDebugEnabled()) {
  31. log.debug(">convert(" + page.getName() + ")");
  32. }
  33. String text = page.getOriginalText();
  34. int linkStart = text.indexOf(IMAGE_START);
  35. while (linkStart >= 0) {
  36. int linkEnd = text.indexOf(IMAGE_END, linkStart);
  37. if (linkEnd < 0) {
  38. break; // No end tag found
  39. }
  40. String link = text.substring(linkStart + IMAGE_START.length(), linkEnd);
  41. String filePath = link.trim();
  42. String linkText = "";
  43. int separator = link.indexOf(SEPARATOR);
  44. if (separator >= 0) {
  45. filePath = link.substring(0, separator).trim();
  46. linkText = link.substring(separator + 1).trim();
  47. // Remove any line breaks from the link text
  48. linkText = linkText.replaceAll("\r\n", " ");
  49. linkText = linkText.replaceAll("\r", " ");
  50. linkText = linkText.replaceAll("\n", " ");
  51. }
  52. String qualifiers = "";
  53. int qualifierIndex = link.indexOf(QUALIFIER);
  54. if (qualifierIndex >= 0) {
  55. filePath = link.substring(0, qualifierIndex);
  56. qualifiers = link.substring(qualifierIndex + 1);
  57. }
  58. // log.info("Link: \"" + link + "\", " +
  59. // "Path: \"" + filePath + "\", " +
  60. // "Linktext: \"" + linkText + "\", " +
  61. // "Qualifiers: \"" + qualifiers + "\"");
  62. StringBuffer newLink = new StringBuffer();
  63. if (DokuWikiLinkConverter.isPageReference(filePath)) {
  64. // Replace colons (namespace separators) with the directory separator, since DokuWiki stores files in
  65. // subdirectories named after the namespace.
  66. String fileSeparator = File.separator;
  67. if ("\\".equals(fileSeparator)) {
  68. // Escape the backslash
  69. fileSeparator = "\\\\";
  70. }
  71. filePath = filePath.replaceAll(":", fileSeparator);
  72. addAttachment(filePath, page);
  73. // Now decide if this is an image or some other attachment that should be linked in stead
  74. File file = new File(filePath);
  75. if (isImage(file)) {
  76. makeImageTag(newLink, file.getName(), qualifiers);
  77. } else {
  78. // Not an image! Make this a link in stead!
  79. filePath = makeAttachmentTag(filePath, newLink, linkText);
  80. }
  81. } else {
  82. // this is an external link. Is is an image?
  83. filePath = DokuWikiLinkConverter.normalizeLink(filePath);
  84. File file = new File(filePath.substring(Math.max(0, filePath.lastIndexOf("/"))));
  85. if (isImage(file)) {
  86. makeImageTag(newLink, filePath, qualifiers);
  87. } else {
  88. makeLinkTag(filePath, newLink, linkText);
  89. }
  90. }
  91. StringBuffer newText = new StringBuffer(filePath);
  92. newText.append(filePath).append(qualifiers);
  93. // log.info("New Link: " + newLink);
  94. text = text.substring(0, linkStart) +
  95. newLink.toString() +
  96. text.substring(linkEnd + IMAGE_END.length());
  97. linkStart = text.indexOf(IMAGE_START);
  98. }
  99. page.setConvertedText(text);
  100. log.debug("<convert()");
  101. }
  102. private boolean isImage(File file) {
  103. String mimetype = ConverterEngine.determineContentType(file);
  104. return mimetype.startsWith("image");
  105. }
  106. private void makeLinkTag(String filePath, StringBuffer newLink, String linkText) {
  107. newLink.append("[");
  108. if (!"".equals(linkText)) {
  109. newLink.append(linkText).append("|");
  110. }
  111. newLink.append(filePath);
  112. newLink.append("]");
  113. }
  114. private void makeImageTag(StringBuffer newLink, String location, String qualifiers) {
  115. newLink.append("!").
  116. append(location);
  117. appendQualifiers(qualifiers, newLink);
  118. newLink.append("!");
  119. }
  120. private String makeAttachmentTag(String filePath, StringBuffer newLink, String linkText) {
  121. File file;
  122. filePath = DokuWikiLinkConverter.normalizeLink(filePath.trim());
  123. file = new File(filePath);
  124. newLink.append("[");
  125. if ("".equals(linkText)) {
  126. newLink.append("^")
  127. .append(file.getName());
  128. } else {
  129. newLink.append(linkText)
  130. .append("|^")
  131. .append(file.getName());
  132. }
  133. newLink.append("]");
  134. return filePath;
  135. }
  136. private void appendQualifiers(String qualifiers, StringBuffer newLink) {
  137. if (qualifiers == null || "".equals(qualifiers)) {
  138. return;
  139. }
  140. newLink.append("|width=");
  141. int x = qualifiers.indexOf("x");
  142. if (x < 0) {
  143. newLink.append(qualifiers);
  144. } else {
  145. newLink.append(qualifiers.substring(0, x))
  146. .append(", height=")
  147. .append(qualifiers.substring(x + 1));
  148. }
  149. }
  150. private void addAttachment(String filePath, Page page) {
  151. // Add the attachment to the page object
  152. String baseDir = this.getAttachmentDirectory();
  153. File attachment = new File(baseDir + File.separator + filePath);
  154. page.addAttachment(attachment);
  155. }
  156. }