Search in sources :

Example 46 with Engine

use of org.apache.wiki.api.core.Engine in project jspwiki by apache.

the class Search method renderResults.

private String renderResults(final Collection<SearchResult> results, final Context context, final int maxItems) {
    final Engine engine = context.getEngine();
    final Element table = XhtmlUtil.element(XHTML.table);
    // table.setAttribute(XHTML.ATTR_border,"0");
    // table.setAttribute(XHTML.ATTR_cellpadding,"4");
    table.setAttribute(XHTML.ATTR_class, "wikitable search-result");
    Element row = XhtmlUtil.element(XHTML.tr);
    table.addContent(row);
    final Element th1 = XhtmlUtil.element(XHTML.th, "Page");
    th1.setAttribute(XHTML.ATTR_width, "30%");
    th1.setAttribute(XHTML.ATTR_align, "left");
    row.addContent(th1);
    final Element th2 = XhtmlUtil.element(XHTML.th, "Score");
    th2.setAttribute(XHTML.ATTR_align, "left");
    row.addContent(th2);
    int idx = 0;
    for (final Iterator<SearchResult> i = results.iterator(); i.hasNext() && idx++ <= maxItems; ) {
        final SearchResult sr = i.next();
        row = XhtmlUtil.element(XHTML.tr);
        final Element name = XhtmlUtil.element(XHTML.td);
        name.setAttribute(XHTML.ATTR_width, "30%");
        name.addContent(XhtmlUtil.link(context.getURL(ContextEnum.PAGE_VIEW.getRequestContext(), sr.getPage().getName()), engine.getManager(RenderingManager.class).beautifyTitle(sr.getPage().getName())));
        row.addContent(name);
        row.addContent(XhtmlUtil.element(XHTML.td, "" + sr.getScore()));
        table.addContent(row);
    }
    if (results.isEmpty()) {
        row = XhtmlUtil.element(XHTML.tr);
        final Element td = XhtmlUtil.element(XHTML.td);
        td.setAttribute(XHTML.ATTR_colspan, "2");
        final Element b = XhtmlUtil.element(XHTML.b, "No results");
        td.addContent(b);
        row.addContent(td);
        table.addContent(row);
    }
    return XhtmlUtil.serialize(table);
}
Also used : RenderingManager(org.apache.wiki.render.RenderingManager) Element(org.jdom2.Element) SearchResult(org.apache.wiki.api.search.SearchResult) Engine(org.apache.wiki.api.core.Engine)

Example 47 with Engine

use of org.apache.wiki.api.core.Engine in project jspwiki by apache.

the class TableOfContents method execute.

/**
 *  {@inheritDoc}
 */
@Override
public String execute(final Context context, final Map<String, String> params) throws PluginException {
    final Engine engine = context.getEngine();
    final Page page = context.getPage();
    final ResourceBundle rb = Preferences.getBundle(context, Plugin.CORE_PLUGINS_RESOURCEBUNDLE);
    if (context.getVariable(VAR_ALREADY_PROCESSING) != null) {
        // return rb.getString("tableofcontents.title");
        return "<a href=\"#section-TOC\" class=\"toc\">" + rb.getString("tableofcontents.title") + "</a>";
    }
    final StringBuilder sb = new StringBuilder();
    sb.append("<div class=\"toc\">\n");
    sb.append("<div class=\"collapsebox\">\n");
    final String title = params.get(PARAM_TITLE);
    sb.append("<h4 id=\"section-TOC\">");
    if (title != null) {
        sb.append(TextUtil.replaceEntities(title));
    } else {
        sb.append(rb.getString("tableofcontents.title"));
    }
    sb.append("</h4>\n");
    // should we use an ordered list?
    m_usingNumberedList = false;
    if (params.containsKey(PARAM_NUMBERED)) {
        final String numbered = 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;
        final String startStr = 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 = TextUtil.replaceEntities(params.get(PARAM_PREFIX));
        if (m_prefix == null)
            m_prefix = "";
        m_lastLevel = Heading.HEADING_LARGE;
    }
    try {
        String wikiText = engine.getManager(PageManager.class).getPureText(page);
        final boolean runFilters = "true".equals(engine.getManager(VariableManager.class).getValue(context, VariableManager.VAR_RUNFILTERS, "true"));
        if (runFilters) {
            try {
                final FilterManager fm = engine.getManager(FilterManager.class);
                wikiText = fm.doPreTranslateFiltering(context, wikiText);
            } catch (final Exception e) {
                log.error("Could not construct table of contents: Filter Error", e);
                throw new PluginException("Unable to construct table of contents (see logs)");
            }
        }
        context.setVariable(VAR_ALREADY_PROCESSING, "x");
        final MarkupParser parser = engine.getManager(RenderingManager.class).getParser(context, wikiText);
        parser.addHeadingListener(this);
        parser.parse();
        sb.append("<ul>\n").append(m_buf).append("</ul>\n");
    } catch (final 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();
}
Also used : PluginException(org.apache.wiki.api.exceptions.PluginException) Page(org.apache.wiki.api.core.Page) IOException(java.io.IOException) InternalWikiException(org.apache.wiki.InternalWikiException) IOException(java.io.IOException) PluginException(org.apache.wiki.api.exceptions.PluginException) FilterManager(org.apache.wiki.filters.FilterManager) PageManager(org.apache.wiki.pages.PageManager) RenderingManager(org.apache.wiki.render.RenderingManager) ResourceBundle(java.util.ResourceBundle) Engine(org.apache.wiki.api.core.Engine) MarkupParser(org.apache.wiki.parser.MarkupParser)

Example 48 with Engine

use of org.apache.wiki.api.core.Engine in project jspwiki by apache.

the class RSS10Feed method addItemList.

private void addItemList(final Element root) {
    final SimpleDateFormat iso8601fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    final Engine engine = m_wikiContext.getEngine();
    for (final Entry entry : m_entries) {
        final String url = entry.getURL();
        final Element item = new Element("item", NS_XMNLS);
        item.setAttribute("about", url, NS_RDF);
        item.addContent(new Element("title", NS_XMNLS).addContent(entry.getTitle()));
        item.addContent(new Element("link", NS_XMNLS).addContent(url));
        final Element content = new Element("description", NS_XMNLS);
        // TODO: Add a size limiter here
        content.addContent(entry.getContent());
        item.addContent(content);
        final Page p = entry.getPage();
        if (p.getVersion() != -1) {
            item.addContent(new Element("version", NS_WIKI).addContent(Integer.toString(p.getVersion())));
        }
        if (p.getVersion() > 1) {
            item.addContent(new Element("diff", NS_WIKI).addContent(engine.getURL(ContextEnum.PAGE_DIFF.getRequestContext(), p.getName(), "r1=-1")));
        }
        // 
        // Modification date.
        final Calendar cal = Calendar.getInstance();
        cal.setTime(p.getLastModified());
        cal.add(Calendar.MILLISECOND, -(cal.get(Calendar.ZONE_OFFSET) + (cal.getTimeZone().inDaylightTime(p.getLastModified()) ? cal.get(Calendar.DST_OFFSET) : 0)));
        item.addContent(new Element("date", NS_DC).addContent(iso8601fmt.format(cal.getTime())));
        // 
        // Author
        String author = entry.getAuthor();
        if (author == null) {
            author = "unknown";
        }
        final Element contributor = new Element("creator", NS_DC);
        item.addContent(contributor);
        /*
            Element description = new Element("Description", NS_RDF);
            if( m_wikiContext.getEngine().pageExists(author) ) {
                description.setAttribute( "link", engine.getURL( WikiContext.VIEW, author, null, true ), NS_XMNLS );
            }

            description.addContent( new Element("value", NS_XMNLS).addContent( author) );
            contributor.addContent( description );
           */
        // Not too many aggregators seem to like this.  Therefore we're just adding the name here.
        contributor.addContent(author);
        // 
        // PageHistory
        item.addContent(new Element("history", NS_WIKI).addContent(engine.getURL(ContextEnum.PAGE_INFO.getRequestContext(), p.getName(), null)));
        // 
        // Add to root
        root.addContent(item);
    }
}
Also used : Element(org.jdom2.Element) Calendar(java.util.Calendar) Page(org.apache.wiki.api.core.Page) SimpleDateFormat(java.text.SimpleDateFormat) Engine(org.apache.wiki.api.core.Engine)

Example 49 with Engine

use of org.apache.wiki.api.core.Engine in project jspwiki by apache.

the class RSS20Feed method getString.

/**
 *  {@inheritDoc}
 */
@Override
public String getString() {
    final Engine engine = m_wikiContext.getEngine();
    final Element root = new Element("rss");
    root.setAttribute("version", "2.0");
    final Element channel = new Element("channel");
    root.addContent(channel);
    // 
    // Mandatory parts
    // 
    channel.addContent(new Element("title").setText(getChannelTitle()));
    channel.addContent(new Element("link").setText(engine.getBaseURL()));
    channel.addContent(new Element("description").setText(getChannelDescription()));
    // 
    // Optional
    // 
    channel.addContent(new Element("language").setText(getChannelLanguage()));
    channel.addContent(new Element("generator").setText("JSPWiki " + Release.VERSTR));
    String mail = engine.getManager(VariableManager.class).getVariable(m_wikiContext, RSSGenerator.PROP_RSS_AUTHOREMAIL);
    if (mail != null) {
        final String editor = engine.getManager(VariableManager.class).getVariable(m_wikiContext, RSSGenerator.PROP_RSS_AUTHOR);
        if (editor != null)
            mail = mail + " (" + editor + ")";
        channel.addContent(new Element("managingEditor").setText(mail));
    }
    // 
    // Items
    // 
    channel.addContent(getItems());
    // 
    // aaand output
    // 
    final XMLOutputter output = new XMLOutputter();
    output.setFormat(Format.getPrettyFormat());
    try {
        final StringWriter res = new StringWriter();
        output.output(root, res);
        return res.toString();
    } catch (final IOException e) {
        return null;
    }
}
Also used : VariableManager(org.apache.wiki.variables.VariableManager) XMLOutputter(org.jdom2.output.XMLOutputter) StringWriter(java.io.StringWriter) Element(org.jdom2.Element) IOException(java.io.IOException) Engine(org.apache.wiki.api.core.Engine)

Example 50 with Engine

use of org.apache.wiki.api.core.Engine in project jspwiki by apache.

the class AtomFeed method getString.

/**
 *  {@inheritDoc}
 */
@Override
public String getString() {
    final Element root = getElement("feed");
    final Engine engine = m_wikiContext.getEngine();
    Date lastModified = new Date(0L);
    for (final Entry e : m_entries) {
        if (e.getPage().getLastModified().after(lastModified)) {
            lastModified = e.getPage().getLastModified();
        }
    }
    // Mandatory parts
    root.addContent(getElement("title").setText(getChannelTitle()));
    root.addContent(getElement("id").setText(getFeedID()));
    root.addContent(getElement("updated").setText(DateFormatUtils.formatUTC(lastModified, RFC3339FORMAT)));
    // Optional
    // root.addContent( getElement("author").addContent(getElement("name").setText(format())))
    root.addContent(getElement("link").setAttribute("href", engine.getBaseURL()));
    root.addContent(getElement("generator").setText("JSPWiki " + Release.VERSTR));
    final String rssFeedURL = engine.getURL(ContextEnum.PAGE_NONE.getRequestContext(), "rss.jsp", "page=" + engine.encodeName(m_wikiContext.getPage().getName()) + "&mode=" + m_mode + "&type=atom");
    final Element self = getElement("link").setAttribute("rel", "self");
    self.setAttribute("href", rssFeedURL);
    root.addContent(self);
    // Items
    root.addContent(getItems());
    // aaand output
    final XMLOutputter output = new XMLOutputter();
    output.setFormat(Format.getPrettyFormat());
    try {
        final StringWriter res = new StringWriter();
        output.output(root, res);
        return res.toString();
    } catch (final IOException e) {
        return null;
    }
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) StringWriter(java.io.StringWriter) Element(org.jdom2.Element) IOException(java.io.IOException) Engine(org.apache.wiki.api.core.Engine) Date(java.util.Date)

Aggregations

Engine (org.apache.wiki.api.core.Engine)60 Page (org.apache.wiki.api.core.Page)30 PageManager (org.apache.wiki.pages.PageManager)20 ProviderException (org.apache.wiki.api.exceptions.ProviderException)11 Attachment (org.apache.wiki.api.core.Attachment)10 JspWriter (javax.servlet.jsp.JspWriter)9 Context (org.apache.wiki.api.core.Context)9 AttachmentManager (org.apache.wiki.attachment.AttachmentManager)8 Element (org.jdom2.Element)7 IOException (java.io.IOException)6 Calendar (java.util.Calendar)6 ResourceBundle (java.util.ResourceBundle)6 SimpleDateFormat (java.text.SimpleDateFormat)5 Date (java.util.Date)5 PageContext (javax.servlet.jsp.PageContext)5 Principal (java.security.Principal)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 PluginException (org.apache.wiki.api.exceptions.PluginException)4 RenderingManager (org.apache.wiki.render.RenderingManager)4 ArrayList (java.util.ArrayList)3