Search in sources :

Example 51 with Attachment

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

the class RSS20Feed method getItems.

private List getItems() {
    ArrayList<Element> list = new ArrayList<Element>();
    SimpleDateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'");
    WikiEngine engine = m_wikiContext.getEngine();
    ServletContext servletContext = null;
    if (m_wikiContext.getHttpRequest() != null)
        servletContext = m_wikiContext.getHttpRequest().getSession().getServletContext();
    for (Iterator<Entry> i = m_entries.iterator(); i.hasNext(); ) {
        Entry e = i.next();
        WikiPage p = e.getPage();
        String url = e.getURL();
        Element item = new Element("item");
        item.addContent(new Element("link").setText(url));
        item.addContent(new Element("title").setText(e.getTitle()));
        item.addContent(new Element("description").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 = new Element("enclosure");
                    attEl.setAttribute("url", engine.getURL(WikiContext.ATTACH, att.getName(), null, true));
                    attEl.setAttribute("length", Long.toString(att.getSize()));
                    attEl.setAttribute("type", getMimeType(servletContext, att.getFileName()));
                    item.addContent(attEl);
                }
            } catch (ProviderException ex) {
            // FIXME: log.info("Can't get attachment data",ex);
            }
        }
        // 
        // Modification date.
        // 
        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("pubDate").setText(fmt.format(cal.getTime())));
        list.add(item);
    }
    return list;
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) Element(org.jdom2.Element) WikiPage(org.apache.wiki.WikiPage) Attachment(org.apache.wiki.attachment.Attachment) ServletContext(javax.servlet.ServletContext) SimpleDateFormat(java.text.SimpleDateFormat) WikiEngine(org.apache.wiki.WikiEngine)

Example 52 with Attachment

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

the class RSSGenerator method generateWikiPageRSS.

/**
 *  Create RSS/Atom as if this page was a wikipage (in contrast to Blog mode).
 *
 * @param wikiContext The WikiContext
 * @param changed A List of changed WikiPages.
 * @param feed A Feed object to fill.
 * @return the RSS representation of the wiki context
 */
@SuppressWarnings("unchecked")
protected String generateWikiPageRSS(WikiContext wikiContext, List changed, Feed feed) {
    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(), "version=" + page.getVersion(), true);
        } else {
            url = m_engine.getURL(WikiContext.VIEW, page.getName(), "version=" + page.getVersion(), true);
        }
        // Unfortunately, this is needed because the code will again go through
        // replacement conversion
        url = TextUtil.replaceString(url, "&amp;", "&");
        e.setURL(url);
        e.setTitle(getEntryTitle(page));
        e.setContent(getEntryDescription(page));
        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 53 with Attachment

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

the class RSSGenerator method generateFullWikiRSS.

/**
 *  Generates an RSS feed for the entire wiki.  Each item should be an instance of the RSSItem class.
 *
 *  @param wikiContext A WikiContext
 *  @param feed A Feed to generate the feed to.
 *  @return feed.getString().
 */
protected String generateFullWikiRSS(WikiContext wikiContext, Feed feed) {
    feed.setChannelTitle(m_engine.getApplicationName());
    feed.setFeedURL(m_engine.getBaseURL());
    feed.setChannelLanguage(m_channelLanguage);
    feed.setChannelDescription(m_channelDescription);
    Collection changed = m_engine.getRecentChanges();
    WikiSession session = WikiSession.guestSession(m_engine);
    int items = 0;
    for (Iterator i = changed.iterator(); i.hasNext() && items < 15; items++) {
        WikiPage page = (WikiPage) i.next();
        if (!m_engine.getAuthorizationManager().checkPermission(session, new PagePermission(page, PagePermission.VIEW_ACTION))) {
            // No permission, skip to the next one.
            continue;
        }
        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);
        e.setTitle(page.getName());
        e.setContent(getEntryDescription(page));
        e.setAuthor(getAuthor(page));
        feed.addEntry(e);
    }
    return feed.getString();
}
Also used : WikiSession(org.apache.wiki.WikiSession) WikiPage(org.apache.wiki.WikiPage) Iterator(java.util.Iterator) Collection(java.util.Collection) Attachment(org.apache.wiki.attachment.Attachment) PagePermission(org.apache.wiki.auth.permissions.PagePermission)

Example 54 with Attachment

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

the class Image method execute.

/**
 *  {@inheritDoc}
 */
public String execute(WikiContext context, Map<String, String> params) throws PluginException {
    WikiEngine engine = context.getEngine();
    String src = getCleanParameter(params, PARAM_SRC);
    String align = getCleanParameter(params, PARAM_ALIGN);
    String ht = getCleanParameter(params, PARAM_HEIGHT);
    String wt = getCleanParameter(params, PARAM_WIDTH);
    String alt = getCleanParameter(params, PARAM_ALT);
    String caption = getCleanParameter(params, PARAM_CAPTION);
    String link = getCleanParameter(params, PARAM_LINK);
    String target = getCleanParameter(params, PARAM_TARGET);
    String style = getCleanParameter(params, PARAM_STYLE);
    String cssclass = getCleanParameter(params, PARAM_CLASS);
    // String map     = getCleanParameter( params, PARAM_MAP );
    String border = getCleanParameter(params, PARAM_BORDER);
    String title = getCleanParameter(params, PARAM_TITLE);
    if (src == null) {
        throw new PluginException("Parameter 'src' is required for Image plugin");
    }
    if (target != null && !validTargetValue(target)) {
        // not a valid value so ignore
        target = null;
    }
    try {
        AttachmentManager mgr = engine.getAttachmentManager();
        Attachment att = mgr.getAttachmentInfo(context, src);
        if (att != null) {
            src = context.getURL(WikiContext.ATTACH, att.getName());
        }
    } catch (ProviderException e) {
        throw new PluginException("Attachment info failed: " + e.getMessage());
    }
    StringBuilder result = new StringBuilder();
    result.append("<table border=\"0\" class=\"imageplugin\"");
    if (title != null) {
        result.append(" title=\"" + title + "\"");
    }
    if (align != null) {
        if (align.equals("center")) {
            result.append(" style=\"margin-left: auto; margin-right: auto; text-align:center; vertical-align:middle;\"");
        } else {
            result.append(" style=\"float:" + align + ";\"");
        }
    }
    result.append(">\n");
    if (caption != null) {
        result.append("<caption>" + caption + "</caption>\n");
    }
    // move css class and style to the container of the image,
    // so it doesn't affect the caption
    result.append("<tr><td");
    if (cssclass != null) {
        result.append(" class=\"" + cssclass + "\"");
    }
    if (style != null) {
        result.append(" style=\"" + style);
        // Make sure that we add a ";" to the end of the style string
        if (result.charAt(result.length() - 1) != ';')
            result.append(";");
        result.append("\"");
    }
    result.append(">");
    if (link != null) {
        result.append("<a href=\"" + link + "\"");
        if (target != null) {
            result.append(" target=\"" + target + "\"");
        }
        result.append(">");
    }
    result.append("<img src=\"" + src + "\"");
    if (ht != null)
        result.append(" height=\"" + ht + "\"");
    if (wt != null)
        result.append(" width=\"" + wt + "\"");
    if (alt != null)
        result.append(" alt=\"" + alt + "\"");
    if (border != null)
        result.append(" border=\"" + border + "\"");
    // if( map != null )    result.append(" map=\""+map+"\"");
    result.append(" />");
    if (link != null)
        result.append("</a>");
    result.append("</td></tr>\n");
    result.append("</table>\n");
    return result.toString();
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) PluginException(org.apache.wiki.api.exceptions.PluginException) Attachment(org.apache.wiki.attachment.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) WikiEngine(org.apache.wiki.WikiEngine)

Example 55 with Attachment

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

the class RecentChangesPlugin method execute.

/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
public String execute(WikiContext context, Map<String, String> params) throws PluginException {
    int since = TextUtil.parseIntParameter(params.get("since"), DEFAULT_DAYS);
    String spacing = "4";
    boolean showAuthor = true;
    boolean showChangenote = true;
    String tablewidth = "4";
    WikiEngine engine = context.getEngine();
    // 
    if ("compact".equals(params.get(PARAM_FORMAT))) {
        spacing = "0";
        showAuthor = false;
        showChangenote = false;
        tablewidth = "2";
    }
    Calendar sincedate = new GregorianCalendar();
    sincedate.add(Calendar.DAY_OF_MONTH, -since);
    log.debug("Calculating recent changes from " + sincedate.getTime());
    // FIXME: Should really have a since date on the getRecentChanges method.
    Collection<WikiPage> changes = engine.getRecentChanges();
    super.initialize(context, params);
    changes = super.filterCollection(changes);
    if (changes != null) {
        Date olddate = new Date(0);
        DateFormat fmt = getDateFormat(context, params);
        DateFormat tfmt = getTimeFormat(context, params);
        Element rt = XhtmlUtil.element(XHTML.table);
        rt.setAttribute(XHTML.ATTR_class, "recentchanges");
        rt.setAttribute(XHTML.ATTR_cellpadding, spacing);
        for (Iterator<WikiPage> i = changes.iterator(); i.hasNext(); ) {
            WikiPage pageref = i.next();
            Date lastmod = pageref.getLastModified();
            if (lastmod.before(sincedate.getTime())) {
                break;
            }
            if (!isSameDay(lastmod, olddate)) {
                Element row = XhtmlUtil.element(XHTML.tr);
                Element col = XhtmlUtil.element(XHTML.td);
                col.setAttribute(XHTML.ATTR_colspan, tablewidth);
                col.setAttribute(XHTML.ATTR_class, "date");
                col.addContent(XhtmlUtil.element(XHTML.b, fmt.format(lastmod)));
                rt.addContent(row);
                row.addContent(col);
                olddate = lastmod;
            }
            String href = context.getURL(pageref instanceof Attachment ? WikiContext.ATTACH : WikiContext.VIEW, pageref.getName());
            Element link = XhtmlUtil.link(href, engine.beautifyTitle(pageref.getName()));
            Element row = XhtmlUtil.element(XHTML.tr);
            Element col = XhtmlUtil.element(XHTML.td);
            col.setAttribute(XHTML.ATTR_width, "30%");
            col.addContent(link);
            // 
            if (pageref instanceof Attachment) {
                link = XhtmlUtil.link(context.getURL(WikiContext.INFO, pageref.getName()), null);
                link.setAttribute(XHTML.ATTR_class, "infolink");
                Element img = XhtmlUtil.img(context.getURL(WikiContext.NONE, "images/attachment_small.png"), null);
                link.addContent(img);
                col.addContent(link);
            }
            row.addContent(col);
            rt.addContent(row);
            if (pageref instanceof Attachment) {
                Element td = XhtmlUtil.element(XHTML.td, tfmt.format(lastmod));
                td.setAttribute(XHTML.ATTR_class, "lastchange");
                row.addContent(td);
            } else {
                Element infocol = XhtmlUtil.element(XHTML.td);
                infocol.setAttribute(XHTML.ATTR_class, "lastchange");
                infocol.addContent(XhtmlUtil.link(context.getURL(WikiContext.DIFF, pageref.getName(), "r1=-1"), tfmt.format(lastmod)));
                row.addContent(infocol);
            }
            if (showAuthor) {
                String author = pageref.getAuthor();
                Element authorinfo = XhtmlUtil.element(XHTML.td);
                authorinfo.setAttribute(XHTML.ATTR_class, "author");
                if (author != null) {
                    if (engine.pageExists(author)) {
                        authorinfo.addContent(XhtmlUtil.link(context.getURL(WikiContext.VIEW, author), author));
                    } else {
                        authorinfo.addContent(author);
                    }
                } else {
                    authorinfo.addContent(Preferences.getBundle(context, InternationalizationManager.CORE_BUNDLE).getString("common.unknownauthor"));
                }
                row.addContent(authorinfo);
            }
            // Change note
            if (showChangenote) {
                String changenote = (String) pageref.getAttribute(WikiPage.CHANGENOTE);
                Element td_changenote = XhtmlUtil.element(XHTML.td, changenote);
                td_changenote.setAttribute(XHTML.ATTR_class, "changenote");
                row.addContent(td_changenote);
            }
        // Revert note
        /*                
                if( context.hasAdminPermissions() )
                {
                    row.addElement( new td("Revert") );
                }
 */
        }
        return XhtmlUtil.serialize(rt, XhtmlUtil.EXPAND_EMPTY_NODES);
    }
    return "";
}
Also used : GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) WikiPage(org.apache.wiki.WikiPage) Element(org.jdom2.Element) GregorianCalendar(java.util.GregorianCalendar) Attachment(org.apache.wiki.attachment.Attachment) Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) 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