// WikiCode.java // ------------------------------------- // part of YACY // // (C) 2005, 2006 by Alexander Schier, Marc Nause, Franz Brausze // // $LastChangedDate$ // $LastChangedRevision$ // $LastChangedBy$ // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA package de.anomic.data.wiki; import java.io.BufferedReader; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import net.yacy.document.parser.html.CharacterCoding; import de.anomic.server.serverCore; /** Provides methods to handle texts that have been posted in the yacyWiki or other * parts of YaCy which use wiki code, like the blog or the profile. * * @author Alexander Schier [AS], Franz Brausze [FB], Marc Nause [MN] */ public class WikiCode extends AbstractWikiParser implements WikiParser { private static final String EMPTY = ""; private static final String PIPE_ESCAPED = "|"; private static final String REGEX_NOT_CHAR_NUM_OR_UNDERSCORE = "[^a-zA-Z0-9_]"; private static enum Tags { HEADLINE_1("=", "

", "

"), HEADLINE_2("==", "

", "

"), HEADLINE_3("===", "

", "

"), HEADLINE_4("====", "

", "

"), HEADLINE_5("=====", "
", "
"), HEADLINE_6("======", "
", "
"), EMPHASIZE_1("\'\'", "", ""), EMPHASIZE_2("\'\'\'", "", ""), EMPHASIZE_3("\'\'\'\'\'", "", ""), STRIKE("<s>", "</s>", "", ""); final String openHTML; final String closeHTML; final String openWiki; final String closeWiki; final int openWikiLength; final int closeWikiLength; Tags(final String openWiki, final String closeWiki, final String openHTML, final String closeHTML) { if (openHTML == null || closeHTML == null || openWiki == null || closeWiki == null) { throw new IllegalArgumentException("Parameter may not be null."); } this.openHTML = openHTML; this.closeHTML = closeHTML; this.openWiki = openWiki; this.closeWiki = closeWiki; openWikiLength = openWiki.length(); closeWikiLength = closeWiki.length(); } Tags(final String wiki, final String openHTML, final String closeHTML) { this(wiki, wiki, openHTML, closeHTML); } } private static final String HTML_OPEN_DEFINITION_DESCRIPTION = "
"; private static final String HTML_CLOSE_DEFINITION_DESCRIPTION = "
"; private static final String HTML_OPEN_DEFINITION_ITEM = "
"; private static final String HTML_CLOSE_DEFINITION_ITEM = "
"; private static final String HTML_OPEN_DEFINITION_LIST = "
"; private static final String HTML_CLOSE_DEFINITION_LIST = "
"; private static final String HTML_OPEN_UNORDERED_LIST = ""; private static final String HTML_CLOSE_BLOCKQUOTE = ""; private static final String HTML_CLOSE_LIST_ELEMENT = ""; private static final String HTML_CLOSE_ORDERED_LIST = ""; private static final String HTML_OPEN_BLOCKQUOTE = "
"; private static final String HTML_OPEN_LIST_ELEMENT = "
  • "; private static final String HTML_OPEN_ORDERED_LIST = "
      "; private static final String WIKI_CLOSE_LINK = "]]"; private static final String WIKI_OPEN_LINK = "[["; private static final String WIKI_CLOSE_EXTERNAL_LINK = "]"; private static final String WIKI_OPEN_EXTERNAL_LINK = "["; private static final String WIKI_CLOSE_PRE_ESCAPED = "</pre>"; private static final String WIKI_HR_LINE = "----"; private static final String WIKI_IMAGE = "Image:"; private static final String WIKI_OPEN_PRE_ESCAPED = "<pre>"; private static final char ASTERISK = '*'; private static final char ONE = '1'; private static final char TWO = '2'; private static final char THREE = '3'; private static final char FOUR = '4'; private static final char FIVE = '5'; private static final char SIX = '6'; private static final char WIKI_FORMATTED = ' '; private static final char WIKI_INDENTION = ':'; private static final int LEN_WIKI_CLOSE_PRE_ESCAPED = WIKI_CLOSE_PRE_ESCAPED.length(); private static final int LEN_WIKI_OPEN_PRE_ESCAPED = WIKI_OPEN_PRE_ESCAPED.length(); private static final int LEN_WIKI_OPEN_LINK = WIKI_OPEN_LINK.length(); private static final int LEN_WIKI_IMAGE = WIKI_IMAGE.length(); private static final int LEN_WIKI_OPEN_EXTERNAL_LINK = WIKI_OPEN_EXTERNAL_LINK.length(); private static final int LEN_WIKI_CLOSE_EXTERNAL_LINK = WIKI_CLOSE_EXTERNAL_LINK.length(); private static final int LEN_WIKI_HR_LINE = WIKI_HR_LINE.length(); private static final int LEN_PIPE_ESCAPED = PIPE_ESCAPED.length(); private final TableOfContent tableOfContent = new TableOfContent(); /** List of properties which can be used in tables. */ private final static String[] TABLE_PROPERTIES = {"rowspan", "colspan", "vspace", "hspace", "cellspacing", "cellpadding", "border"}; /** Map which contains possible values for several parameters. */ private final static Map PROPERTY_VALUES = new HashMap(); /** Tags for different types of headlines in wikiCode. */ private final static String[] HEADLINE_TAGS = new String[]{Tags.HEADLINE_6.openWiki, Tags.HEADLINE_5.openWiki, Tags.HEADLINE_4.openWiki, Tags.HEADLINE_3.openWiki, Tags.HEADLINE_2.openWiki, Tags.HEADLINE_1.openWiki}; private final static char[] HEADLINE_LEVEL = new char[]{ONE, TWO, THREE, FOUR, FIVE, SIX}; private String orderedListLevel = EMPTY; private String unorderedListLevel = EMPTY; private String defListLevel = EMPTY; private boolean processingCell = false; //needed for prevention of double-execution of replaceHTML private boolean processingDefList = false; //needed for definition lists private boolean escape = false; //needed for escape private boolean escaped = false; //needed for
       not getting in the way
          private boolean newRowStart = false;                //needed for the first row not to be empty
          private boolean noList = false;                     //needed for handling of [= and 
       in lists
          private boolean processingPreformattedText = false; //needed for preformatted text
          private boolean preformattedSpanning = false;       //needed for 
       and 
      spanning over several lines private boolean replacedHtmlAlready = false; //indicates if method replaceHTML has been used with line already private boolean processingTable = false; //needed for tables, because they reach over several lines private int preindented = 0; //needed for indented
      s
      
          static {
              /* Arrays must be sorted since Arrays.searchBinary() is used later. For more info go to
               * http://java.sun.com/javase/6/docs/api/java/util/Arrays.html#binarySearch(T[], T, java.util.Comparator)
               */
              Arrays.sort(HEADLINE_LEVEL);
              Arrays.sort(HEADLINE_TAGS);
              Arrays.sort(TABLE_PROPERTIES);
              String[] array;
              Arrays.sort(array = new String[]{"void", "above", "below", "hsides", "lhs", "rhs", "vsides", "box", "border"});
              PROPERTY_VALUES.put("frame", array);
              Arrays.sort(array = new String[]{"none", "groups", "rows", "cols", "all"});
              PROPERTY_VALUES.put("rules", array);
              Arrays.sort(array = new String[]{"top", "middle", "bottom", "baseline"});
              PROPERTY_VALUES.put("valign", array);
              Arrays.sort(array = new String[]{"left", "right", "center"});
              PROPERTY_VALUES.put("align", array);
          }
      
          private enum ListType {
              ORDERED, UNORDERED;
          }
      
          /**
           * Constructor
           * @param address
           */
          public WikiCode() {
              super();
          }
      
          /**
           * Transforms a text which contains wiki code to HTML fragment.
           * @param reader contains the text to be transformed.
           * @param length expected length of text, used to create buffer with right size.
           * @return HTML fragment.
           * @throws IOException in case input from reader can not be read.
           */
          protected String transform(String hostport, final BufferedReader reader, final int length)
                  throws IOException {
              final StringBuilder out = new StringBuilder(length);
              String line;
              while ((line = reader.readLine()) != null) {
                  out.append(processLineOfWikiCode(hostport, line)).append(serverCore.CRLF_STRING);
              }
              return out.insert(0, createTableOfContents()).toString();
          }
      
          // contributed by [FB], changes by [MN]
          /**
           * Processes tags which are connected to tables.
           * @param line line of text to be transformed from wiki code to HTML
           * @return HTML fragment
           */
          private String processTable(final String line) {
              //some variables that make it easier to change codes for the table
              final StringBuilder out = new StringBuilder();
              final String tableStart = "{" + PIPE_ESCAPED;        // {|
              final String newLine = PIPE_ESCAPED + "-";                // |-
              final String cellDivider = PIPE_ESCAPED + PIPE_ESCAPED;   // ||
              final String tableEnd = PIPE_ESCAPED + "}";          // |}
              final String attribDivider = PIPE_ESCAPED;                // |
              final int lenTableStart = tableStart.length();
              final int lenCellDivider = cellDivider.length();
              final int lenTableEnd = tableEnd.length();
              final int lenAttribDivider = attribDivider.length();
      
              if (line.startsWith(tableStart) && !processingTable) {
                  processingTable = true;
                  newRowStart = true;
                  out.append(" lenTableStart) {
                      out.append(filterTableProperties(line.substring(lenTableStart).trim()));
                  }
                  out.append(">");
              } else if (line.startsWith(newLine) && processingTable) {          // new row
                  if (!newRowStart) {
                      out.append("\t\n");
                  } else {
                      newRowStart = false;
                  }
                  out.append("\t");
              } else if (line.startsWith(cellDivider) && processingTable) {
                  out.append("\t\t 0) ? (line.indexOf(cellDivider, lenCellDivider)) : (line.length());
                  int propEnd = line.indexOf(attribDivider, lenCellDivider);
                  final int occImage = line.indexOf("[[Image:", lenCellDivider);
                  final int occEscape = line.indexOf("[=", lenCellDivider);
                  //If resultOf("[[Image:") is less than propEnd, that means that there is no
                  //property for this cell, only an image. Without this, YaCy could get confused
                  //by a | in [[Image:picture.png|alt-text]] or [[Image:picture.png|alt-text]]
                  //Same for [= (part of [= =])
                  if ((propEnd > lenCellDivider) && ((occImage > propEnd) || (occImage < 0)) && ((occEscape > propEnd) || (occEscape < 0))) {
                      propEnd = line.indexOf(attribDivider, lenCellDivider) + lenAttribDivider;
                  } else {
                      propEnd = cellEnd;
                  }
                  // both point at same place => new line
                  if (propEnd == cellEnd) {
                      propEnd = lenCellDivider;
                  } else {
                      out.append(filterTableProperties(line.substring(lenCellDivider, propEnd - lenAttribDivider).trim()));
                  }
                  // quick&dirty fix [MN]
                  if (propEnd > cellEnd) {
                      propEnd = lenCellDivider;
                  }
                  processingTable = false;
                  processingCell = true;
                  out.append(">");
                  out.append(processTable(line.substring(propEnd, cellEnd).trim()));
                  out.append("");
                  processingTable = true;
                  processingCell = false;
                  if (cellEnd < line.length()) {
                      out.append("\n");
                      out.append(processTable(line.substring(cellEnd)));
                  }
              } else if (line.startsWith(tableEnd) && (processingTable)) {          // Table end
                  processingTable = false;
                  out.append("\t\n");
                  out.append(line.substring(lenTableEnd));
              } else {
                  out.append(line);
              }
              return out.toString();
          }
      
          // contributed by [MN], changes by [FB]
          /** Takes possible table properties and tests if they are valid.
           * Valid in this case means if they are a property for the table, tr or td
           * tag as stated in the HTML Pocket Reference by Jennifer Niederst (1st edition)
           * The method is important to avoid XSS attacks on the wiki via table properties.
           * @param properties String which may contain several table properties and/or junk.
           * @return String containing only table properties.
           */
          private StringBuilder filterTableProperties(final String properties) {
              final String[] values = properties.replaceAll(""", EMPTY).split("[= ]");     //splitting the string at = and blanks
              final StringBuilder stringBuilder = new StringBuilder(properties.length());
              String key, value;
              String[] posVals;
              final int numberOfValues = values.length;
              for (int i = 0; i < numberOfValues; i++) {
                  key = values[i].trim();
                  if ("nowrap".equals(key)) {
                      appendKeyValuePair("nowrap", "nowrap", stringBuilder);
                  } else if (i + 1 < numberOfValues) {
                      value = values[++i].trim();
                      if (("summary".equals(key))
                              || ("bgcolor".equals(key) && value.matches("#{0,1}[0-9a-fA-F]{1,6}|[a-zA-Z]{3,}"))
                              || (("width".equals(key) || "height".equals(key)) && value.matches("\\d+%{0,1}"))
                              || ((posVals = PROPERTY_VALUES.get(key)) != null && Arrays.binarySearch(posVals, value) >= 0)
                              || (Arrays.binarySearch(TABLE_PROPERTIES, key) >= 0 && value.matches("\\d+"))) {
                          appendKeyValuePair(key, value, stringBuilder);
                      }
                  }
              }
              return stringBuilder;
          }
      
          /**
           * Appends a key/value pair in HTML syntax to a given StringBuilder.
           * @param key key to be appended.
           * @param value value of key.
           * @param stringBuilder this is what key/value are appended to.
           * @return
           */
          private StringBuilder appendKeyValuePair(final String key, final String value, final StringBuilder stringBuilder) {
              return stringBuilder.append(" ").append(key).append("=\"").append(value).append("\"");
          }
      
          /**
           * Processes tags which are connected to ordered lists.
           * @param line line of text to be transformed from wiki code to HTML
           * @return HTML fragment
           */
          private String processOrderedList(final String line) {
              return processList(line, ListType.ORDERED);
          }
      
          /**
           * Processes tags which are connected to unordered lists.
           * @param line line of text to be transformed from wiki code to HTML
           * @return HTML fragment
           */
          private String processUnorderedList(String line) {
              return processList(line, ListType.UNORDERED);
          }
      
          /**
           * Processes tags which are connected to ordered or unordered lists.
           * @author contains code by [AS]
           * @param line line of text to be transformed from wiki code to HTML
           * @param listType type of tags to be processed
           * @return HTML fragment
           */
          private String processList(final String line, final ListType listType) {
      
              final String ret;
      
              if (!noList) {    //lists only get processed if not forbidden (see code for [= and 
      ).
      
                  String listLevel;
                  final String htmlOpenList;
                  final String htmlCloseList;
                  final char symbol;
      
                  if (ListType.ORDERED.equals(listType)) {
                      listLevel = orderedListLevel;
                      symbol = '#';
                      htmlOpenList = HTML_OPEN_ORDERED_LIST;
                      htmlCloseList = HTML_CLOSE_ORDERED_LIST;
                  } else if (ListType.UNORDERED.equals(listType)) {
                      listLevel = unorderedListLevel;
                      symbol = ASTERISK;
                      htmlOpenList = HTML_OPEN_UNORDERED_LIST;
                      htmlCloseList = HTML_CLOSE_UNORDERED_LIST;
                  } else {
                      throw new IllegalArgumentException("Unknown list type " + listType);
                  }
      
                  if (line.startsWith(listLevel + symbol)) {      //more #
                      final StringBuilder stringBuilder = new StringBuilder();
                      stringBuilder.append(htmlOpenList);
                      stringBuilder.append(serverCore.CRLF_STRING);
                      stringBuilder.append(HTML_OPEN_LIST_ELEMENT);
                      stringBuilder.append(line.substring(listLevel.length() + 1).trim());
                      stringBuilder.append(HTML_CLOSE_LIST_ELEMENT);
                      ret = stringBuilder.toString();
                      listLevel += symbol;
                  } else if (!listLevel.isEmpty() && line.startsWith(listLevel)) {           //equal number of #
                      final StringBuilder stringBuilder = new StringBuilder();
                      stringBuilder.append(HTML_OPEN_LIST_ELEMENT);
                      stringBuilder.append(line.substring(listLevel.length()).trim());
                      stringBuilder.append(HTML_CLOSE_LIST_ELEMENT);
                      ret = stringBuilder.toString();
                  } else if (!listLevel.isEmpty()) {            //less #
                      final StringBuilder stringBuilder = new StringBuilder();
                      final StringBuilder tmp = new StringBuilder();
      
                      int i = listLevel.length();
                      while (!line.startsWith(listLevel.substring(0, i))) {
                          tmp.append(htmlCloseList);
                          i--;
                      }
                      listLevel = listLevel.substring(0, i);
                      
                      final int startOfContent = listLevel.length();
      
                      if (startOfContent > 0) {
                          stringBuilder.append(tmp);
                          stringBuilder.append(HTML_OPEN_LIST_ELEMENT);
                          stringBuilder.append(line.substring(startOfContent).trim());
                          stringBuilder.append(HTML_CLOSE_LIST_ELEMENT);
                      } else {
                          stringBuilder.append(tmp);
                          stringBuilder.append(line.substring(startOfContent).trim());
                      }
                      ret = stringBuilder.toString();
                  }  else {
                      ret = line;
                  }
      
                  if (ListType.ORDERED.equals(listType)) {
                      orderedListLevel = listLevel;
                  } else if (ListType.UNORDERED.equals(listType)) {
                      unorderedListLevel = listLevel;
                  }
              } else {
                  ret = line;
              }
              return ret;
          }
      
          /**
           * Processes tags which are connected to definition lists.
           * @param line line of text to be transformed from wiki code to HTML
           * @return HTML fragment
           */
          private String processDefinitionList(final String line) {
              final String ret;
      
              if (!noList) {    //lists only get processed if not forbidden (see code for [= and 
      ). [MN]
      
                  if (line.startsWith(defListLevel + ";")) { //more semicolons
                      final String copyOfLine = line.substring(defListLevel.length() + 1);
                      final int positionOfOpeningTag;
                      if ((positionOfOpeningTag = copyOfLine.indexOf(":")) > 0) {
                          final String definitionItem = copyOfLine.substring(0, positionOfOpeningTag);
                          final String definitionDescription = copyOfLine.substring(positionOfOpeningTag + 1);
                          final StringBuilder stringBuilder = new StringBuilder();
                          stringBuilder.append(HTML_OPEN_DEFINITION_LIST);
                          stringBuilder.append(HTML_OPEN_DEFINITION_ITEM);
                          stringBuilder.append(definitionItem);
                          stringBuilder.append(HTML_CLOSE_DEFINITION_ITEM);
                          stringBuilder.append(HTML_OPEN_DEFINITION_DESCRIPTION);
                          stringBuilder.append(definitionDescription);
                          processingDefList = true;
                          ret = stringBuilder.toString();
                      } else {
                          ret = line;
                      }
                      defListLevel += ";";
                  } else if (!defListLevel.isEmpty() && line.startsWith(defListLevel)) { //equal number of semicolons
                      final String copyOfLine = line.substring(defListLevel.length());
                      final int positionOfOpeningTag;
                      if ((positionOfOpeningTag = copyOfLine.indexOf(":")) > 0) {
                          final String definitionItem = copyOfLine.substring(0, positionOfOpeningTag);
                          final String definitionDescription = copyOfLine.substring(positionOfOpeningTag + 1);
                          final StringBuilder stringBuilder = new StringBuilder();
                          stringBuilder.append(HTML_OPEN_DEFINITION_ITEM);
                          stringBuilder.append(definitionItem);
                          stringBuilder.append(HTML_CLOSE_DEFINITION_ITEM);
                          stringBuilder.append(HTML_OPEN_DEFINITION_DESCRIPTION);
                          stringBuilder.append(definitionDescription);
                          processingDefList = true;
                          ret = stringBuilder.toString();
                      } else {
                          ret = line;
                      }
                  } else if (!defListLevel.isEmpty()) { //less semicolons
                      int i = defListLevel.length();
                      String tmp = EMPTY;
                      while (!line.startsWith(defListLevel.substring(0, i))) {
                          tmp = HTML_CLOSE_DEFINITION_DESCRIPTION + HTML_CLOSE_DEFINITION_LIST;
                          i--;
                      }
                      defListLevel = defListLevel.substring(0, i);
                      int positionOfOpeningTag = defListLevel.length();
                      if (!defListLevel.isEmpty()) {
                          final String copyOfLine = line.substring(positionOfOpeningTag);
                          if ((positionOfOpeningTag = copyOfLine.indexOf(":")) > 0) {
                              final String definitionItem = copyOfLine.substring(0, positionOfOpeningTag);
                              final String definitionDescription = copyOfLine.substring(positionOfOpeningTag + 1);
                              final StringBuilder stringBuilder = new StringBuilder();
                              stringBuilder.append(tmp);
                              stringBuilder.append(HTML_OPEN_DEFINITION_ITEM);
                              stringBuilder.append(definitionItem);
                              stringBuilder.append(HTML_CLOSE_DEFINITION_ITEM);
                              stringBuilder.append(HTML_OPEN_DEFINITION_DESCRIPTION);
                              stringBuilder.append(definitionDescription);
                              processingDefList = true;
                              ret = stringBuilder.toString();
                          } else {
                              ret = line;
                          }
                      } else {
                          final StringBuilder stringBuilder = new StringBuilder();
                          stringBuilder.append(tmp);
                          stringBuilder.append(line.substring(positionOfOpeningTag));
                          ret = stringBuilder.toString();
                      }
                  } else {
                      ret = line;
                  }
              } else {
                  ret = line;
              }
              return ret;
          }
      
          /**
           * Processes tags which are connected to links and images.
           * @author [AS], [MN]
           * @param line line of text to be transformed from wiki code to HTML
           * @return HTML fragment
           */
          private String processLinksAndImages(String hostport, String line) {
      
              // create links
              String kl, kv, alt, align;
              int p;
              int positionOfOpeningTag;
              int positionOfClosingTag;
              // internal links and images
              while ((positionOfOpeningTag = line.indexOf(WIKI_OPEN_LINK)) >= 0) {
                  positionOfClosingTag = line.indexOf(WIKI_CLOSE_LINK, positionOfOpeningTag + LEN_WIKI_OPEN_LINK);
                  if (positionOfClosingTag <= positionOfOpeningTag) {
                      break;
                  }
                  kl = line.substring(positionOfOpeningTag + LEN_WIKI_OPEN_LINK, positionOfClosingTag);
      
                  // this is the part of the code that's responsible for images
                  if (kl.startsWith(WIKI_IMAGE)) {
                      alt = EMPTY;
                      align = EMPTY;
                      kv = EMPTY;
                      kl = kl.substring(LEN_WIKI_IMAGE);
      
                      // are there any arguments for the image?
                      if ((p = kl.indexOf(PIPE_ESCAPED)) > 0) {
                          kv = kl.substring(p + LEN_WIKI_IMAGE);
                          kl = kl.substring(0, p);
                          // if there are 2 arguments, write them into ALIGN and ALT
                          if ((p = kv.indexOf(PIPE_ESCAPED)) > 0) {
                              align = kv.substring(0, p);
                              //checking validity of value for align. Only non browser specific
                              //values get supported. Not supported: absmiddle, baseline, texttop
                              if (("bottom".equals(align))
                                      || ("center".equals(align))
                                      || ("left".equals(align))
                                      || ("middle".equals(align))
                                      || ("right".equals(align))
                                      || ("top".equals(align))) {
                                  align = " align=\"" + align + "\"";
                              } else {
                                  align = EMPTY;
                              }
                              alt = " alt=\"" + kv.substring(p + LEN_WIKI_IMAGE) + "\"";
                          } // if there is just one, put it into ALT
                          else {
                              alt = " alt=\"" + kv + "\"";
                          }
                      }
      
                      // replace incomplete URLs and make them point to http://peerip:port/...
                      // with this feature you can access an image in DATA/HTDOCS/share/yacy.gif
                      // using the wikicode [[Image:share/yacy.gif]]
                      // or an image DATA/HTDOCS/grafics/kaskelix.jpg with [[Image:grafics/kaskelix.jpg]]
                      // you are free to use other sub-paths of DATA/HTDOCS
                      if (kl.indexOf("://") < 1) {
                          kl = "http://" + hostport + "/" + kl;
                      }
      
                      line = line.substring(0, positionOfOpeningTag) + "" + line.substring(positionOfClosingTag + 2);
                  }
                  // if it's no image, it might be an internal link
                  else {
                      if ((p = kl.indexOf(PIPE_ESCAPED)) > 0) {
                          kv = kl.substring(p + LEN_PIPE_ESCAPED);
                          kl = kl.substring(0, p);
                      } else {
                          kv = kl;
                      }
                      line = line.substring(0, positionOfOpeningTag) + "" + kv + "" + line.substring(positionOfClosingTag + 2); // oob exception in append() !
                  }
              }
      
              // external links
              while ((positionOfOpeningTag = line.indexOf(WIKI_OPEN_EXTERNAL_LINK)) >= 0) {
                  positionOfClosingTag = line.indexOf(WIKI_CLOSE_EXTERNAL_LINK, positionOfOpeningTag + LEN_WIKI_OPEN_EXTERNAL_LINK);
                  if (positionOfClosingTag <= positionOfOpeningTag) {
                      break;
                  }
                  kl = line.substring(positionOfOpeningTag + LEN_WIKI_OPEN_EXTERNAL_LINK, positionOfClosingTag);
                  if ((p = kl.indexOf(" ")) > 0) {
                      kv = kl.substring(p + 1);
                      kl = kl.substring(0, p);
                  } // No text for the link? -> http://www.url.com/
                  else {
                      kv = kl;
                  }
                  // replace incomplete URLs and make them point to http://peerip:port/...
                  // with this feature you can access a file at DATA/HTDOCS/share/page.html
                  // using the wikicode [share/page.html]
                  // or a file DATA/HTDOCS/www/page.html with [www/page.html]
                  // you are free to use other sub-paths of DATA/HTDOCS
                  if (kl.indexOf("://") < 1) {
                      kl = "http://" + hostport + "/" + kl;
                  }
                  line = line.substring(0, positionOfOpeningTag) + "" + kv + "" + line.substring(positionOfClosingTag + LEN_WIKI_CLOSE_EXTERNAL_LINK);
              }
              return line;
          }
      
          /**
           * Processes tags which are connected preformatted text (<pre> </pre>).
           * @param line line of text to be transformed from wiki code to HTML
           * @return HTML fragment
           */
          private String processPreformattedText(String hostport, String line) {
              if (!escaped) {
                  final int positionOfOpeningTag = line.indexOf(WIKI_OPEN_PRE_ESCAPED);
                  final int positionOfClosingTag = line.indexOf(WIKI_CLOSE_PRE_ESCAPED);
                  //both 
       and 
      in the same line if (positionOfOpeningTag >= 0 && positionOfClosingTag > 0) { if (positionOfOpeningTag < positionOfClosingTag) { final StringBuilder preformattedText = new StringBuilder(); preformattedText.append("
      ");
                          preformattedText.append(line.substring(positionOfOpeningTag + LEN_WIKI_OPEN_PRE_ESCAPED, positionOfClosingTag));
                          preformattedText.append("
      "); line = processLineOfWikiCode(hostport, line.substring(0, positionOfOpeningTag).replaceAll("!pre!", "!pre!!") + "!pre!txt!" + line.substring(positionOfClosingTag + LEN_WIKI_CLOSE_PRE_ESCAPED).replaceAll("!pre!", "!pre!!")); line = line.replace("!pre!txt!", preformattedText.toString().replaceAll("!pre!", "!pre!!")); line = line.replaceAll("!pre!!", "!pre!"); } //handles cases like
       
       
      that would cause an exception otherwise else { processingPreformattedText = true; final String temp1 = processLineOfWikiCode(hostport, line.substring(0, positionOfOpeningTag - 1).replaceAll("!tmp!", "!tmp!!") + "!tmp!txt!"); noList = true; final String temp2 = processLineOfWikiCode(hostport, line.substring(positionOfOpeningTag)); noList = false; line = temp1.replaceAll("!tmp!txt!", temp2); line = line.replaceAll("!tmp!!", "!tmp!"); processingPreformattedText = false; } } //start
                  else if (positionOfOpeningTag >= 0 && !preformattedSpanning) {
                      processingPreformattedText = true;    //prevent surplus line breaks
                      final StringBuilder openBlockQuoteTags = new StringBuilder();  //gets filled with 
      s as needed String preformattedText = "
      " + line.substring(positionOfOpeningTag + LEN_WIKI_OPEN_PRE_ESCAPED);
                      preformattedText = preformattedText.replaceAll("!pre!", "!pre!!");
                      //taking care of indented lines
                      while (preindented < positionOfOpeningTag && positionOfOpeningTag < line.length() &&
                              line.substring(preindented, positionOfOpeningTag).charAt(0) == WIKI_INDENTION) {
                          preindented++;
                          openBlockQuoteTags.append(HTML_OPEN_BLOCKQUOTE);
                      }
                      line = processLineOfWikiCode(hostport, line.substring(preindented, positionOfOpeningTag).replaceAll("!pre!", "!pre!!") + "!pre!txt!");
                      line = openBlockQuoteTags + line.replace("!pre!txt!", preformattedText);
                      line = line.replaceAll("!pre!!", "!pre!");
                      preformattedSpanning = true;
                  } //end 
      else if (positionOfClosingTag >= 0 && preformattedSpanning) { preformattedSpanning = false; final StringBuilder endBlockQuoteTags = new StringBuilder(); //gets filled with
      s as needed String preformattedText = line.substring(0, positionOfClosingTag) + "
      "; preformattedText = preformattedText.replaceAll("!pre!", "!pre!!"); //taking care of indented lines while (preindented > 0) { endBlockQuoteTags.append(HTML_CLOSE_BLOCKQUOTE); preindented--; } line = processLineOfWikiCode(hostport, "!pre!txt!" + line.substring(positionOfClosingTag + LEN_WIKI_CLOSE_PRE_ESCAPED).replaceAll("!pre!", "!pre!!")); line = line.replace("!pre!txt!", preformattedText) + endBlockQuoteTags; line = line.replaceAll("!pre!!", "!pre!"); processingPreformattedText = false; } //Getting rid of surplus
      else if (positionOfOpeningTag >= 0 && !preformattedSpanning) { int posTag; while ((posTag = line.indexOf(WIKI_CLOSE_PRE_ESCAPED)) >= 0) { line = line.substring(0, posTag) + line.substring(posTag + LEN_WIKI_CLOSE_PRE_ESCAPED); } line = processLineOfWikiCode(hostport, line); } } return line; } /** Creates table of contents for a wiki page. * @return HTML fragment */ private StringBuilder createTableOfContents() { final StringBuilder directory = new StringBuilder(); String element; int s = 0; int level = 1; int level1 = 0; int level2 = 0; int level3 = 0; int level4 = 0; int level5 = 0; int level6 = 0; int doubles = 0; String anchorext = EMPTY; if ((s = tableOfContent.size()) > 2) { directory.append("
      \n"); for (int i = 0; i < s; i++) { if (i >= tableOfContent.size()) { break; } element = tableOfContent.get(i); if (element == null) { continue; } //counting double headlines doubles = 0; for (int j = 0; j < i; j++) { if (j >= tableOfContent.size()) { break; } final String d = tableOfContent.get(j); if (d == null || d.isEmpty()) { continue; } final String a = d.substring(1).replaceAll(" ", "_").replaceAll(REGEX_NOT_CHAR_NUM_OR_UNDERSCORE, EMPTY); final String b = element.substring(1).replaceAll(" ", "_").replaceAll(REGEX_NOT_CHAR_NUM_OR_UNDERSCORE, EMPTY); if (a.equals(b)) { doubles++; } } //if there are doubles, create anchor extension if (doubles > 0) { anchorext = "_" + (doubles + 1); } final char l = element.charAt(0); String temp = ""; if (Arrays.binarySearch(HEADLINE_LEVEL, l) >= 0 && !element.isEmpty()) { switch (l) { case SIX: { if (level < 6) { level = 6; level6 = 0; } level6++; temp = element.substring(1); element = level1 + "." + level2 + "." + level3 + "." + level4 + "." + level5 + "." + level6 + " " + temp; directory.append("             1) { level = 1; level2 = 0; level3 = 0; level4 = 0; level5 = 0; level6 = 0; } level1++; temp = element.substring(1); element = level1 + ". " + temp; directory.append(""); directory.append(element); directory.append("
      \n"); } anchorext = EMPTY; } directory.append("
      \n"); } return directory; } /** * Replaces the wiki representation of tags with the HTML representation. * @param input String which potentially contains tags to be replaced. * @param tags tags to be replaced. * @return input String with replaced tags. */ private String tagReplace(String input, final Tags tags) { String direlem = null; //string to keep headlines until they get added to List dirElements int firstPosition; final int secondPosition; //replace pattern if a pair of the pattern can be found in the line if (((firstPosition = input.indexOf(tags.openWiki)) >= 0) && ((secondPosition = input.indexOf(tags.closeWiki, firstPosition + tags.openWikiLength)) >= 0)) { //extra treatment for headlines if (Arrays.binarySearch(HEADLINE_TAGS, tags.openWiki) >= 0) { //add anchor and create headline if ((direlem = input.substring(firstPosition + tags.openWikiLength, secondPosition)) != null) { //counting double headlines int doubles = 0; final Iterator iterator = tableOfContent.iterator(); String element; while (iterator.hasNext()) { element = iterator.next(); // no element with null value should ever be in directory assert (element != null); if (element.substring(1).equals(direlem)) { doubles++; } } String anchor = direlem.replaceAll(" ", "_").replaceAll(REGEX_NOT_CHAR_NUM_OR_UNDERSCORE, EMPTY); //replace blanks with underscores and delete everything thats not a regular character, a number or _ //if there are doubles, add underscore and number of doubles plus one if (doubles > 0) { anchor = anchor + "_" + (doubles + 1); } input = input.substring(0, firstPosition) + "" + tags.openHTML + direlem + tags.closeHTML + input.substring(secondPosition + tags.closeWikiLength); //add headlines to list of headlines (so TOC can be created) if (Arrays.binarySearch(HEADLINE_TAGS, tags.openWiki) >= 0) { tableOfContent.add((tags.openWikiLength - 1) + direlem); } } } else { input = input.substring(0, firstPosition) + tags.openHTML + (input.substring(firstPosition + tags.openWikiLength, secondPosition)) + tags.closeHTML + input.substring(secondPosition + tags.closeWikiLength); } } //recursion if another pair of the pattern can still be found in the line if (((firstPosition = input.indexOf(tags.openWiki)) >= 0) && (input.indexOf(tags.closeWiki, firstPosition + tags.openWikiLength) >= 0)) { input = tagReplace(input, tags); } return input; } /** Replaces wiki tags with HTML tags in one line of text. * @param line line of text to be transformed from wiki code to HTML * @return HTML fragment */ public String processLineOfWikiCode(String hostport, String line) { //If HTML has not been replaced yet (can happen if method gets called in recursion), replace now! if ((!replacedHtmlAlready || preformattedSpanning) && line.indexOf(WIKI_CLOSE_PRE_ESCAPED) < 0) { line = CharacterCoding.unicode2html(line, true); replacedHtmlAlready = true; } //check if line contains preformatted symbols or if we are in a preformatted sequence already. if ((line.indexOf(WIKI_OPEN_PRE_ESCAPED) >= 0) || (line.indexOf(WIKI_CLOSE_PRE_ESCAPED) >= 0) || preformattedSpanning) { line = processPreformattedText(hostport, line); } else { //tables first -> wiki-tags in cells can be treated after that line = processTable(line); // format lines if (!line.isEmpty() && line.charAt(0) == WIKI_FORMATTED) { line = "" + line.substring(1) + ""; } if (line.startsWith(WIKI_HR_LINE)) { line = "
      " + line.substring(LEN_WIKI_HR_LINE); } if (!line.isEmpty() && line.charAt(0) == WIKI_INDENTION) { final StringBuilder head = new StringBuilder(); final StringBuilder tail = new StringBuilder(); while (!line.isEmpty() && line.charAt(0) == WIKI_INDENTION) { head.append(HTML_OPEN_BLOCKQUOTE); tail.append(HTML_CLOSE_BLOCKQUOTE); line = line.substring(1); } line = head + line + tail; } // format headers line = tagReplace(line, Tags.HEADLINE_6); line = tagReplace(line, Tags.HEADLINE_5); line = tagReplace(line, Tags.HEADLINE_4); line = tagReplace(line, Tags.HEADLINE_3); line = tagReplace(line, Tags.HEADLINE_2); line = tagReplace(line, Tags.HEADLINE_1); line = tagReplace(line, Tags.EMPHASIZE_3); line = tagReplace(line, Tags.EMPHASIZE_2); line = tagReplace(line, Tags.EMPHASIZE_1); line = tagReplace(line, Tags.STRIKE); line = processUnorderedList(line); line = processOrderedList(line); line = processDefinitionList(line); line = processLinksAndImages(hostport, line); } if (!processingPreformattedText) { replacedHtmlAlready = false; } if (!(line.endsWith(HTML_CLOSE_LIST_ELEMENT) || processingDefList || escape || processingPreformattedText || processingTable || processingCell)) { line += "
      "; } return line; } private class TableOfContent { private final List toc = new ArrayList(); // needs to be list which ensures order int size() { return toc.size(); } String get(final int index) { return toc.get(index); } synchronized boolean add(final String element) { return toc.add(element); } Iterator iterator() { return toc.iterator(); } } }