public void headingAdded(WikiContext context, Heading hd) {
    log.debug("HD: " + hd.m_level + ", " + hd.m_titleText + ", " + hd.m_titleAnchor);

    switch (hd.m_level) {
      case Heading.HEADING_SMALL:
        m_buf.append("<li class=\"toclevel-3\">");
        m_level3Index++;
        break;
      case Heading.HEADING_MEDIUM:
        m_buf.append("<li class=\"toclevel-2\">");
        m_level2Index++;
        break;
      case Heading.HEADING_LARGE:
        m_buf.append("<li class=\"toclevel-1\">");
        m_level1Index++;
        break;
      default:
        throw new InternalWikiException("Unknown depth in toc! (Please submit a bug report.)");
    }

    if (m_level1Index < m_starting) {
      // in case we never had a large heading ...
      m_level1Index++;
    }
    if ((m_lastLevel == Heading.HEADING_SMALL) && (hd.m_level != Heading.HEADING_SMALL)) {
      m_level3Index = 0;
    }
    if (((m_lastLevel == Heading.HEADING_SMALL) || (m_lastLevel == Heading.HEADING_MEDIUM))
        && (hd.m_level == Heading.HEADING_LARGE)) {
      m_level3Index = 0;
      m_level2Index = 0;
    }

    String titleSection = hd.m_titleSection.replace('%', '_');
    String pageName = context.getEngine().encodeName(context.getPage().getName()).replace('%', '_');

    String url = context.getURL(WikiContext.VIEW, context.getPage().getName());
    String sectref = "#section-" + pageName + "-" + titleSection;

    m_buf.append("<a class=\"wikipage\" href=\"" + url + sectref + "\">");
    if (m_usingNumberedList) {
      switch (hd.m_level) {
        case Heading.HEADING_SMALL:
          m_buf.append(m_prefix + m_level1Index + "." + m_level2Index + "." + m_level3Index + " ");
          break;
        case Heading.HEADING_MEDIUM:
          m_buf.append(m_prefix + m_level1Index + "." + m_level2Index + " ");
          break;
        case Heading.HEADING_LARGE:
          m_buf.append(m_prefix + m_level1Index + " ");
          break;
        default:
          throw new InternalWikiException("Unknown depth in toc! (Please submit a bug report.)");
      }
    }
    m_buf.append(TextUtil.replaceEntities(hd.m_titleText) + "</a></li>\n");

    m_lastLevel = hd.m_level;
  }
 /** Helper method to initialize the comparator for this page. */
 private void initSorter(WikiContext context, Map params) {
   String order = (String) params.get(PARAM_SORTORDER);
   if (order == null || order.length() == 0) {
     // Use the configured comparator
     m_sorter = context.getEngine().getPageSorter();
   } else if (order.equalsIgnoreCase(PARAM_SORTORDER_JAVA)) {
     // use Java "natural" ordering
     m_sorter = new PageSorter(JavaNaturalComparator.DEFAULT_JAVA_COMPARATOR);
   } else if (order.equalsIgnoreCase(PARAM_SORTORDER_LOCALE)) {
     // use this locale's ordering
     m_sorter = new PageSorter(LocaleComparator.DEFAULT_LOCALE_COMPARATOR);
   } else if (order.equalsIgnoreCase(PARAM_SORTORDER_HUMAN)) {
     // use human ordering
     m_sorter = new PageSorter(HumanComparator.DEFAULT_HUMAN_COMPARATOR);
   } else
     try {
       Collator collator = new RuleBasedCollator(order);
       collator.setStrength(Collator.PRIMARY);
       m_sorter = new PageSorter(new CollatorComparator(collator));
     } catch (ParseException pe) {
       log.info("Failed to parse requested collator - using default ordering", pe);
       m_sorter = context.getEngine().getPageSorter();
     }
 }
Exemple #3
0
  /** {@inheritDoc} */
  public String execute(WikiContext context, Map params) throws PluginException {
    //
    //  First, determine which kind of name we use to store in
    //  the WikiContext.
    //
    String countername = (String) params.get(PARAM_NAME);

    if (countername == null) {
      countername = DEFAULT_NAME;
    } else {
      countername = DEFAULT_NAME + "-" + countername;
    }

    //
    //  Fetch the old value
    //
    Integer val = (Integer) context.getVariable(countername);

    if (val == null) {
      val = 0;
    }

    //
    //  Check if we need to reset this
    //

    String start = (String) params.get(PARAM_START);

    if (start != null) {
      val = Integer.parseInt(start);
    } else {
      //
      //  Determine how much to increment
      //
      Object incrementObj = params.get(PARAM_INCREMENT);

      int increment = DEFAULT_INCREMENT;

      if (incrementObj != null) {
        increment = (new Integer((String) incrementObj)).intValue();
      }

      val = val + increment;
    }

    context.setVariable(countername, val);

    //
    // check if we want to hide the result (just count, don't show result on the page
    //
    Object showObj = params.get(PARAM_SHOW_RESULT);

    boolean show = DEFAULT_SHOW_RESULT;

    if (showObj != null) {
      show = TextUtil.isPositive((String) showObj);
    }

    if (show) {
      return val.toString();
    }

    return "";
  }
  public String execute(WikiContext context, Map params) throws PluginException {
    WikiEngine engine = context.getEngine();
    WikiPage page = context.getPage();

    if (context.getVariable(VAR_ALREADY_PROCESSING) != null) return "Table of Contents";

    StringBuffer sb = new StringBuffer();

    sb.append("<div class=\"toc\">\n");
    sb.append("<div class=\"collapsebox\">\n");

    String title = (String) params.get(PARAM_TITLE);
    if (title != null) {
      sb.append("<h4>" + TextUtil.replaceEntities(title) + "</h4>\n");
    } else {
      sb.append("<h4>Table of Contents</h4>\n");
    }

    // should we use an ordered list?
    m_usingNumberedList = false;
    if (params.containsKey(PARAM_NUMBERED)) {
      String numbered = (String) params.get(PARAM_NUMBERED);
      if (numbered.equalsIgnoreCase("true")) {
        m_usingNumberedList = true;
      } else if (numbered.equalsIgnoreCase("yes")) {
        m_usingNumberedList = true;
      }
    }

    // if we are using a numbered list, get the rest of the parameters (if any) ...
    if (m_usingNumberedList) {
      int start = 0;
      String startStr = (String) params.get(PARAM_START);
      if ((startStr != null) && (startStr.matches("^\\d+$"))) {
        start = Integer.parseInt(startStr);
      }
      if (start < 0) start = 0;

      m_starting = start;
      m_level1Index = start - 1;
      if (m_level1Index < 0) m_level1Index = 0;
      m_level2Index = 0;
      m_level3Index = 0;
      m_prefix = (String) params.get(PARAM_PREFIX);
      if (m_prefix == null) m_prefix = "";
      m_lastLevel = Heading.HEADING_LARGE;
    }

    try {
      String wikiText = engine.getPureText(page);

      context.setVariable(VAR_ALREADY_PROCESSING, "x");
      JSPWikiMarkupParser parser = new JSPWikiMarkupParser(context, new StringReader(wikiText));
      parser.addHeadingListener(this);

      parser.parse();

      sb.append("<ul>\n" + m_buf.toString() + "</ul>\n");
    } catch (IOException e) {
      log.error("Could not construct table of contents", e);
      throw new PluginException("Unable to construct table of contents (see logs)");
    }

    sb.append("</div>\n</div>\n");

    return sb.toString();
  }
  /**
   * Figures out the full attachment name from the context and attachment name.
   *
   * @param context The current WikiContext
   * @param attachmentname The file name of the attachment.
   * @param version A particular version.
   * @return Attachment, or null, if no such attachment or version exists.
   * @throws ProviderException If something goes wrong.
   */
  public Attachment getAttachmentInfo(WikiContext context, String attachmentname, int version)
      throws ProviderException {
    if (m_provider == null) {
      return null;
    }

    WikiPage currentPage = null;

    if (context != null) {
      currentPage = context.getPage();
    }

    //
    //  Figure out the parent page of this attachment.  If we can't find it,
    //  we'll assume this refers directly to the attachment.
    //
    int cutpt = attachmentname.lastIndexOf('/');

    if (cutpt != -1) {
      String parentPage = attachmentname.substring(0, cutpt);
      parentPage = MarkupParser.cleanLink(parentPage);
      attachmentname = attachmentname.substring(cutpt + 1);

      // If we for some reason have an empty parent page name;
      // this can't be an attachment
      if (parentPage.length() == 0) return null;

      currentPage = m_engine.getPage(parentPage);

      //
      // Go check for legacy name
      //
      // FIXME: This should be resolved using CommandResolver,
      //        not this adhoc way.  This also assumes that the
      //        legacy charset is a subset of the full allowed set.
      if (currentPage == null) {
        currentPage = m_engine.getPage(MarkupParser.wikifyLink(parentPage));
      }
    }

    //
    //  If the page cannot be determined, we cannot possibly find the
    //  attachments.
    //
    if (currentPage == null || currentPage.getName().length() == 0) {
      return null;
    }

    // System.out.println("Seeking info on "+currentPage+"::"+attachmentname);

    //
    //  Finally, figure out whether this is a real attachment or a generated
    //  attachment.
    //
    Attachment att;

    att = getDynamicAttachment(currentPage.getName() + "/" + attachmentname);

    if (att == null) {
      att = m_provider.getAttachmentInfo(currentPage, attachmentname, version);
    }

    return att;
  }
  // FIXME: The compiled pattern strings should really be cached somehow.
  public void initialize(WikiContext context, Map params) throws PluginException {
    m_dateFormat = Preferences.getDateFormat(context, TimeFormat.DATETIME);
    m_engine = context.getEngine();
    m_maxwidth = TextUtil.parseIntParameter((String) params.get(PARAM_MAXWIDTH), Integer.MAX_VALUE);
    if (m_maxwidth < 0) m_maxwidth = 0;

    String s = (String) params.get(PARAM_SEPARATOR);

    if (s != null) {
      m_separator = s;
      // pre-2.1.145 there was a separator at the end of the list
      // if they set the parameters, we use the new format of
      // before Item1 after separator before Item2 after separator before Item3 after
      m_after = "";
    }

    s = (String) params.get(PARAM_BEFORE);

    if (s != null) {
      m_before = s;
    }

    s = (String) params.get(PARAM_AFTER);

    if (s != null) {
      m_after = s;
    }

    s = (String) params.get(PARAM_EXCLUDE);

    if (s != null) {
      try {
        PatternCompiler pc = new GlobCompiler();

        String[] ptrns = StringUtils.split(s, ",");

        m_exclude = new Pattern[ptrns.length];

        for (int i = 0; i < ptrns.length; i++) {
          m_exclude[i] = pc.compile(ptrns[i]);
        }
      } catch (MalformedPatternException e) {
        throw new PluginException("Exclude-parameter has a malformed pattern: " + e.getMessage());
      }
    }

    // TODO: Cut-n-paste, refactor
    s = (String) params.get(PARAM_INCLUDE);

    if (s != null) {
      try {
        PatternCompiler pc = new GlobCompiler();

        String[] ptrns = StringUtils.split(s, ",");

        m_include = new Pattern[ptrns.length];

        for (int i = 0; i < ptrns.length; i++) {
          m_include[i] = pc.compile(ptrns[i]);
        }
      } catch (MalformedPatternException e) {
        throw new PluginException("Include-parameter has a malformed pattern: " + e.getMessage());
      }
    }

    // log.debug( "Requested maximum width is "+m_maxwidth );
    s = (String) params.get(PARAM_SHOW);

    if (s != null) {
      if (s.equalsIgnoreCase("count")) {
        m_show = "count";
      }
    }

    s = (String) params.get(PARAM_LASTMODIFIED);

    if (s != null) {
      if (s.equalsIgnoreCase("true")) {
        if (m_show.equals("count")) {
          m_lastModified = true;
        } else {
          throw new PluginException(
              "showLastModified=true is only valid if show=count is also specified");
        }
      }
    }

    initSorter(context, params);
  }