Search in sources :

Example 16 with Attachment

use of org.apache.wiki.attachment.Attachment in project jspwiki by apache.

the class AtomFeed method getItems.

private Collection getItems() {
    ArrayList<Element> list = new ArrayList<Element>();
    WikiEngine engine = m_wikiContext.getEngine();
    ServletContext servletContext = null;
    if (m_wikiContext.getHttpRequest() != null)
        servletContext = m_wikiContext.getHttpRequest().getSession().getServletContext();
    for (Iterator i = m_entries.iterator(); i.hasNext(); ) {
        Entry e = (Entry) i.next();
        WikiPage p = e.getPage();
        Element entryEl = getElement("entry");
        // 
        // Mandatory elements
        // 
        entryEl.addContent(getElement("id").setText(getEntryID(e)));
        entryEl.addContent(getElement("title").setAttribute("type", "html").setText(e.getTitle()));
        entryEl.addContent(getElement("updated").setText(DateFormatUtils.formatUTC(p.getLastModified(), RFC3339FORMAT)));
        // 
        // Optional elements
        // 
        entryEl.addContent(getElement("author").addContent(getElement("name").setText(e.getAuthor())));
        entryEl.addContent(getElement("link").setAttribute("rel", "alternate").setAttribute("href", e.getURL()));
        entryEl.addContent(getElement("content").setAttribute("type", "html").setText(e.getContent()));
        if (engine.getAttachmentManager().hasAttachments(p) && servletContext != null) {
            try {
                Collection c = engine.getAttachmentManager().listAttachments(p);
                for (Iterator a = c.iterator(); a.hasNext(); ) {
                    Attachment att = (Attachment) a.next();
                    Element attEl = getElement("link");
                    attEl.setAttribute("rel", "enclosure");
                    attEl.setAttribute("href", engine.getURL(WikiContext.ATTACH, att.getName(), null, true));
                    attEl.setAttribute("length", Long.toString(att.getSize()));
                    attEl.setAttribute("type", getMimeType(servletContext, att.getFileName()));
                    entryEl.addContent(attEl);
                }
            } catch (ProviderException ex) {
            // FIXME: log.info("Can't get attachment data",ex);
            }
        }
        list.add(entryEl);
    }
    return list;
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) Element(org.jdom2.Element) WikiPage(org.apache.wiki.WikiPage) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ServletContext(javax.servlet.ServletContext) Collection(java.util.Collection) Attachment(org.apache.wiki.attachment.Attachment) WikiEngine(org.apache.wiki.WikiEngine)

Example 17 with Attachment

use of org.apache.wiki.attachment.Attachment in project jspwiki by apache.

the class RSSGenerator method generateBlogRSS.

/**
 *  Creates RSS from modifications as if this page was a blog (using the WeblogPlugin).
 *
 *  @param wikiContext The WikiContext, as usual.
 *  @param changed A list of the changed pages.
 *  @param feed A valid Feed object.  The feed will be used to create the RSS/Atom, depending
 *              on which kind of an object you want to put in it.
 *  @return A String of valid RSS or Atom.
 *  @throws ProviderException If reading of pages was not possible.
 */
@SuppressWarnings("unchecked")
protected String generateBlogRSS(WikiContext wikiContext, List changed, Feed feed) throws ProviderException {
    if (log.isDebugEnabled())
        log.debug("Generating RSS for blog, size=" + changed.size());
    String ctitle = m_engine.getVariable(wikiContext, PROP_CHANNEL_TITLE);
    if (ctitle != null)
        feed.setChannelTitle(ctitle);
    else
        feed.setChannelTitle(m_engine.getApplicationName() + ":" + wikiContext.getPage().getName());
    feed.setFeedURL(wikiContext.getViewURL(wikiContext.getPage().getName()));
    String language = m_engine.getVariable(wikiContext, PROP_CHANNEL_LANGUAGE);
    if (language != null)
        feed.setChannelLanguage(language);
    else
        feed.setChannelLanguage(m_channelLanguage);
    String channelDescription = m_engine.getVariable(wikiContext, PROP_CHANNEL_DESCRIPTION);
    if (channelDescription != null) {
        feed.setChannelDescription(channelDescription);
    }
    Collections.sort(changed, new PageTimeComparator());
    int items = 0;
    for (Iterator i = changed.iterator(); i.hasNext() && items < 15; items++) {
        WikiPage page = (WikiPage) i.next();
        Entry e = new Entry();
        e.setPage(page);
        String url;
        if (page instanceof Attachment) {
            url = m_engine.getURL(WikiContext.ATTACH, page.getName(), null, true);
        } else {
            url = m_engine.getURL(WikiContext.VIEW, page.getName(), null, true);
        }
        e.setURL(url);
        // 
        // Title
        // 
        String pageText = m_engine.getPureText(page.getName(), WikiProvider.LATEST_VERSION);
        String title = "";
        int firstLine = pageText.indexOf('\n');
        if (firstLine > 0) {
            title = pageText.substring(0, firstLine).trim();
        }
        if (title.length() == 0)
            title = page.getName();
        // Remove wiki formatting
        while (title.startsWith("!")) title = title.substring(1);
        e.setTitle(title);
        if (firstLine > 0) {
            int maxlen = pageText.length();
            if (maxlen > MAX_CHARACTERS)
                maxlen = MAX_CHARACTERS;
            if (maxlen > 0) {
                pageText = m_engine.textToHTML(wikiContext, pageText.substring(firstLine + 1, maxlen).trim());
                if (maxlen == MAX_CHARACTERS)
                    pageText += "...";
                e.setContent(pageText);
            } else {
                e.setContent(title);
            }
        } else {
            e.setContent(title);
        }
        e.setAuthor(getAuthor(page));
        feed.addEntry(e);
    }
    return feed.getString();
}
Also used : WikiPage(org.apache.wiki.WikiPage) Iterator(java.util.Iterator) Attachment(org.apache.wiki.attachment.Attachment) PageTimeComparator(org.apache.wiki.util.comparators.PageTimeComparator)

Example 18 with Attachment

use of org.apache.wiki.attachment.Attachment in project jspwiki by apache.

the class AttachmentsIteratorTag method doAfterBody.

/**
 *  {@inheritDoc}
 */
@Override
public final int doAfterBody() {
    if (bodyContent != null) {
        try {
            JspWriter out = getPreviousOut();
            out.print(bodyContent.getString());
            bodyContent.clearBody();
        } catch (IOException e) {
            log.error("Unable to get inner tag text", e);
        // FIXME: throw something?
        }
    }
    if (m_iterator != null && m_iterator.hasNext()) {
        Attachment att = (Attachment) m_iterator.next();
        WikiContext context = (WikiContext) m_wikiContext.clone();
        context.setPage(att);
        pageContext.setAttribute(WikiTagBase.ATTR_CONTEXT, context, PageContext.REQUEST_SCOPE);
        pageContext.setAttribute(getId(), att);
        return EVAL_BODY_BUFFERED;
    }
    return SKIP_BODY;
}
Also used : WikiContext(org.apache.wiki.WikiContext) Attachment(org.apache.wiki.attachment.Attachment) IOException(java.io.IOException) JspWriter(javax.servlet.jsp.JspWriter)

Example 19 with Attachment

use of org.apache.wiki.attachment.Attachment in project jspwiki by apache.

the class LinkToTag method doWikiStartTag.

public int doWikiStartTag() throws IOException {
    String pageName = m_pageName;
    boolean isattachment = false;
    if (m_pageName == null) {
        WikiPage p = m_wikiContext.getPage();
        if (p != null) {
            pageName = p.getName();
            isattachment = p instanceof Attachment;
        } else {
            return SKIP_BODY;
        }
    }
    JspWriter out = pageContext.getOut();
    String url;
    String linkclass;
    if (isattachment) {
        url = m_wikiContext.getURL(WikiContext.ATTACH, pageName, (getVersion() != null) ? "version=" + getVersion() : null);
        linkclass = "attachment";
    } else {
        StringBuilder params = new StringBuilder();
        if (getVersion() != null)
            params.append("version=" + getVersion());
        if (getTemplate() != null)
            params.append((params.length() > 0 ? "&amp;" : "") + "skin=" + getTemplate());
        url = m_wikiContext.getURL(WikiContext.VIEW, pageName, params.toString());
        linkclass = "wikipage";
    }
    switch(m_format) {
        case ANCHOR:
            out.print("<a class=\"" + linkclass + "\" href=\"" + url + "\" accesskey=\"" + m_accesskey + "\" title=\"" + m_title + "\">");
            break;
        case URL:
            out.print(url);
            break;
    }
    return EVAL_BODY_INCLUDE;
}
Also used : WikiPage(org.apache.wiki.WikiPage) Attachment(org.apache.wiki.attachment.Attachment) JspWriter(javax.servlet.jsp.JspWriter)

Example 20 with Attachment

use of org.apache.wiki.attachment.Attachment in project jspwiki by apache.

the class PageNameTag method doWikiStartTag.

public final int doWikiStartTag() throws IOException {
    WikiEngine engine = m_wikiContext.getEngine();
    WikiPage page = m_wikiContext.getPage();
    if (page != null) {
        if (page instanceof Attachment) {
            pageContext.getOut().print(((Attachment) page).getFileName());
        } else {
            pageContext.getOut().print(engine.beautifyTitle(m_wikiContext.getName()));
        }
    }
    return SKIP_BODY;
}
Also used : WikiPage(org.apache.wiki.WikiPage) Attachment(org.apache.wiki.attachment.Attachment) WikiEngine(org.apache.wiki.WikiEngine)

Aggregations

Attachment (org.apache.wiki.attachment.Attachment)62 WikiPage (org.apache.wiki.WikiPage)26 Test (org.junit.Test)23 File (java.io.File)20 ProviderException (org.apache.wiki.api.exceptions.ProviderException)17 Collection (java.util.Collection)13 Date (java.util.Date)11 Iterator (java.util.Iterator)10 WikiEngine (org.apache.wiki.WikiEngine)10 AttachmentManager (org.apache.wiki.attachment.AttachmentManager)7 FileInputStream (java.io.FileInputStream)6 IOException (java.io.IOException)6 List (java.util.List)6 Vector (java.util.Vector)6 WikiContext (org.apache.wiki.WikiContext)5 Hashtable (java.util.Hashtable)4 Element (net.sf.ehcache.Element)4 PagePermission (org.apache.wiki.auth.permissions.PagePermission)4 ArrayList (java.util.ArrayList)3 Calendar (java.util.Calendar)3