use of org.apache.wiki.WikiEngine in project jspwiki by apache.
the class PluginTag method executePlugin.
private String executePlugin(String plugin, String args, String body) throws PluginException, IOException {
WikiEngine engine = m_wikiContext.getEngine();
PluginManager pm = engine.getPluginManager();
m_evaluated = true;
Map<String, String> argmap = pm.parseArgs(args);
if (body != null) {
argmap.put("_body", body);
}
String result = pm.execute(m_wikiContext, plugin, argmap);
return result;
}
use of org.apache.wiki.WikiEngine in project jspwiki by apache.
the class WeblogPlugin method execute.
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
public String execute(WikiContext context, Map<String, String> params) throws PluginException {
Calendar startTime;
Calendar stopTime;
int numDays = DEFAULT_DAYS;
WikiEngine engine = context.getEngine();
AuthorizationManager mgr = engine.getAuthorizationManager();
//
// Parse parameters.
//
String days;
DateFormat entryFormat;
String startDay = null;
boolean hasComments = false;
int maxEntries;
String weblogName;
if ((weblogName = params.get(PARAM_PAGE)) == null) {
weblogName = context.getPage().getName();
}
if ((days = context.getHttpParameter("weblog." + PARAM_DAYS)) == null) {
days = params.get(PARAM_DAYS);
}
if ((params.get(PARAM_ENTRYFORMAT)) == null) {
entryFormat = Preferences.getDateFormat(context, TimeFormat.DATETIME);
} else {
entryFormat = new SimpleDateFormat(params.get(PARAM_ENTRYFORMAT));
}
if (days != null) {
if (days.equalsIgnoreCase("all")) {
numDays = Integer.MAX_VALUE;
} else {
numDays = TextUtil.parseIntParameter(days, DEFAULT_DAYS);
}
}
if ((startDay = params.get(PARAM_STARTDATE)) == null) {
startDay = context.getHttpParameter("weblog." + PARAM_STARTDATE);
}
if (TextUtil.isPositive(params.get(PARAM_ALLOWCOMMENTS))) {
hasComments = true;
}
maxEntries = TextUtil.parseIntParameter(params.get(PARAM_MAXENTRIES), Integer.MAX_VALUE);
//
// Determine the date range which to include.
//
startTime = Calendar.getInstance();
stopTime = Calendar.getInstance();
if (startDay != null) {
SimpleDateFormat fmt = new SimpleDateFormat(DEFAULT_DATEFORMAT);
try {
Date d = fmt.parse(startDay);
startTime.setTime(d);
stopTime.setTime(d);
} catch (ParseException e) {
return "Illegal time format: " + startDay;
}
}
//
// Mark this to be a weblog
//
context.getPage().setAttribute(ATTR_ISWEBLOG, "true");
//
// We make a wild guess here that nobody can do millisecond
// accuracy here.
//
startTime.add(Calendar.DAY_OF_MONTH, -numDays);
startTime.set(Calendar.HOUR, 0);
startTime.set(Calendar.MINUTE, 0);
startTime.set(Calendar.SECOND, 0);
stopTime.set(Calendar.HOUR, 23);
stopTime.set(Calendar.MINUTE, 59);
stopTime.set(Calendar.SECOND, 59);
StringBuilder sb = new StringBuilder();
try {
List<WikiPage> blogEntries = findBlogEntries(engine, weblogName, startTime.getTime(), stopTime.getTime());
Collections.sort(blogEntries, new PageDateComparator());
sb.append("<div class=\"weblog\">\n");
for (Iterator<WikiPage> i = blogEntries.iterator(); i.hasNext() && maxEntries-- > 0; ) {
WikiPage p = i.next();
if (mgr.checkPermission(context.getWikiSession(), new PagePermission(p, PagePermission.VIEW_ACTION))) {
addEntryHTML(context, entryFormat, hasComments, sb, p);
}
}
sb.append("</div>\n");
} catch (ProviderException e) {
log.error("Could not locate blog entries", e);
throw new PluginException("Could not locate blog entries: " + e.getMessage());
}
return sb.toString();
}
use of org.apache.wiki.WikiEngine in project jspwiki by apache.
the class WeblogPlugin method addEntryHTML.
/**
* Generates HTML for an entry.
*
* @param context
* @param entryFormat
* @param hasComments True, if comments are enabled.
* @param buffer The buffer to which we add.
* @param entry
* @throws ProviderException
*/
private void addEntryHTML(WikiContext context, DateFormat entryFormat, boolean hasComments, StringBuilder buffer, WikiPage entry) throws ProviderException {
WikiEngine engine = context.getEngine();
ResourceBundle rb = Preferences.getBundle(context, WikiPlugin.CORE_PLUGINS_RESOURCEBUNDLE);
buffer.append("<div class=\"weblogentry\">\n");
//
// Heading
//
buffer.append("<div class=\"weblogentryheading\">\n");
Date entryDate = entry.getLastModified();
buffer.append(entryFormat.format(entryDate));
buffer.append("</div>\n");
//
// Append the text of the latest version. Reset the
// context to that page.
//
WikiContext entryCtx = (WikiContext) context.clone();
entryCtx.setPage(entry);
String html = engine.getHTML(entryCtx, engine.getPage(entry.getName()));
// Extract the first h1/h2/h3 as title, and replace with null
buffer.append("<div class=\"weblogentrytitle\">\n");
Matcher matcher = HEADINGPATTERN.matcher(html);
if (matcher.find()) {
String title = matcher.group(2);
html = matcher.replaceFirst("");
buffer.append(title);
} else {
buffer.append(entry.getName());
}
buffer.append("</div>\n");
buffer.append("<div class=\"weblogentrybody\">\n");
buffer.append(html);
buffer.append("</div>\n");
//
// Append footer
//
buffer.append("<div class=\"weblogentryfooter\">\n");
String author = entry.getAuthor();
if (author != null) {
if (engine.pageExists(author)) {
author = "<a href=\"" + entryCtx.getURL(WikiContext.VIEW, author) + "\">" + engine.beautifyTitle(author) + "</a>";
}
} else {
author = "AnonymousCoward";
}
buffer.append(MessageFormat.format(rb.getString("weblogentryplugin.postedby"), author));
buffer.append("<a href=\"" + entryCtx.getURL(WikiContext.VIEW, entry.getName()) + "\">" + rb.getString("weblogentryplugin.permalink") + "</a>");
String commentPageName = TextUtil.replaceString(entry.getName(), "blogentry", "comments");
if (hasComments) {
int numComments = guessNumberOfComments(engine, commentPageName);
//
// We add the number of comments to the URL so that
// the user's browsers would realize that the page
// has changed.
//
buffer.append(" ");
String addcomment = rb.getString("weblogentryplugin.addcomment");
buffer.append("<a href=\"" + entryCtx.getURL(WikiContext.COMMENT, commentPageName, "nc=" + numComments) + "\">" + MessageFormat.format(addcomment, numComments) + "</a>");
}
buffer.append("</div>\n");
//
// Done, close
//
buffer.append("</div>\n");
}
use of org.apache.wiki.WikiEngine in project jspwiki by apache.
the class AtomFeed method getString.
/**
* {@inheritDoc}
*/
@Override
public String getString() {
Element root = getElement("feed");
WikiEngine engine = m_wikiContext.getEngine();
Date lastModified = new Date(0L);
for (Iterator i = m_entries.iterator(); i.hasNext(); ) {
Entry e = (Entry) i.next();
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));
String rssFeedURL = engine.getURL(WikiContext.NONE, "rss.jsp", "page=" + engine.encodeName(m_wikiContext.getPage().getName()) + "&mode=" + m_mode + "&type=atom", true);
Element self = getElement("link").setAttribute("rel", "self");
self.setAttribute("href", rssFeedURL);
root.addContent(self);
//
// Items
//
root.addContent(getItems());
//
// aaand output
//
XMLOutputter output = new XMLOutputter();
output.setFormat(Format.getPrettyFormat());
try {
StringWriter res = new StringWriter();
output.output(root, res);
return res.toString();
} catch (IOException e) {
return null;
}
}
use of org.apache.wiki.WikiEngine in project jspwiki by apache.
the class Feed method getSiteName.
/**
* Figure out a site name for a feed.
*
* @param context the wiki context
* @return the site name
*/
public static String getSiteName(WikiContext context) {
WikiEngine engine = context.getEngine();
String blogname = null;
try {
blogname = engine.getVariableManager().getValue(context, VAR_BLOGNAME);
} catch (NoSuchVariableException e) {
}
if (blogname == null) {
blogname = engine.getApplicationName() + ": " + context.getPage().getName();
}
return blogname;
}
Aggregations