コード例 #1
0
ファイル: TreeTag.java プロジェクト: jianbinyang/winlet
  public int doStartTag() throws JspException {
    try {
      SiteContext sc =
          (SiteContext) pageContext.getRequest().getAttribute(SiteContext.SITE_CONTEXT_KEY);
      AccessRuleEngine re =
          ContextUtils.getAccessRuleEngine((HttpServletRequest) pageContext.getRequest());

      Page page = null;
      Page currentInTree = sc.getPage();

      if (m_strParent != null) // 直接指定了要展现的根栏目
      page = (Page) pageContext.getAttribute(m_strParent);
      else if (m_iLevel > -1) { // 指定了要展现的级别
        if (m_iLevel <= currentInTree.getLevel()) {
          page = currentInTree;
          while (m_iLevel < page.getLevel()) page = page.getParent();
        }
      } else { // 没有指定根栏目
        TreeTag tt = (TreeTag) TagSupport.findAncestorWithClass(this, TreeTag.class);
        if (tt != null) page = tt.getPage();
        else page = sc.getBranch().getHome(re);
      }

      if (page == null) return SKIP_BODY;

      m_enum = Collections.enumeration(page.getPages(re, false, true));
      Object next = nextElement();
      if (next == null) return SKIP_BODY;

      m_bFirst = true;

      if (m_strName != null) pageContext.setAttribute(m_strName, next);
      return EVAL_BODY_BUFFERED;
    } catch (Exception e) {
      e.printStackTrace();
      throw new JspException(e.getMessage());
    }
  }
コード例 #2
0
ファイル: TocController.java プロジェクト: yangjm/winlet_doc
  /**
   *
   *
   * <pre>
   * 根据area中的内容里包含的 &lt; h1 &gt; 和 &lt; h2 &gt; 元素自动生成目录
   * </pre>
   *
   * @param request
   * @param area
   * @param model
   * @return
   */
  @Window("toc")
  public String tocWindow(
      HttpServletRequest request, @RequestParam("area") String area, Model model) {
    SiteContext sc = (SiteContext) request.getAttribute(SiteContext.SITE_CONTEXT_KEY);
    if (sc == null) return "";

    Page page = sc.getPage();
    String index = tocMap.get(page.getPath());
    if (index == null) {
      List<Area> areas = page.getAreas(area);
      if (areas == null || areas.size() < 1) return "";

      StringBuffer sb = new StringBuffer();
      sb.append(
          "<nav id=\"docindex\" class=\"bs-docs-sidebar\"><ul class=\"nav bs-docs-sidenav\">");
      int level = 0;

      Matcher m = LINKS.matcher(areas.get(0).getContent());
      while (m.find()) {
        switch (m.group(1)) {
          case "1":
            switch (level) {
              case 0:
                sb.append("<li>");
                break;
              case 1:
                sb.append("</li><li>");
                break;
              case 2:
                sb.append("</li></ul></li><li>");
                break;
            }

            level = 1;
            break;
          case "2":
            switch (level) {
              case 0:
                sb.append("<li><ul class=\"nav\"><li>");
                break;
              case 1:
                sb.append("<ul class=\"nav\"><li>");
                break;
              case 2:
                sb.append("</li><li>");
                break;
            }

            level = 2;
            break;
        }

        sb.append("<a class=\"scroll\" href=\"#")
            .append(m.group(3))
            .append("\">")
            .append(m.group(4))
            .append("</a>");
      }

      switch (level) {
        case 1:
          sb.append("</li></ul></nav>");
          break;
        case 2:
          sb.append("</li></ul></li></ul></nav>");
          break;
      }

      index = sb.toString();
      tocMap.put(page.getPath(), index);
    }

    model.addAttribute("content", index);
    return "toc";
  }