1   /*
2    * Confluence BibSonomy plugin
3    * Copyright (C) 2007 Christian Schenk
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    * 
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18   */
19  package org.christianschenk.confluence.plugin.bibsonomy;
20  
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.christianschenk.confluence.plugin.bibsonomy.helper.BibSonomyHelper;
25  import org.christianschenk.confluence.plugin.bibsonomy.helper.ConfluenceHelper;
26  import org.christianschenk.confluence.plugin.bibsonomy.helper.HttpClientHelper;
27  import org.christianschenk.confluence.plugin.bibsonomy.model.BibTex;
28  import org.christianschenk.confluence.plugin.bibsonomy.model.Post;
29  import org.dom4j.Document;
30  
31  import com.atlassian.renderer.RenderContext;
32  import com.atlassian.renderer.v2.RenderMode;
33  import com.atlassian.renderer.v2.macro.BaseMacro;
34  import com.atlassian.renderer.v2.macro.MacroException;
35  
36  /**
37   * This macro displays information about your BibTeXs.
38   * 
39   * @author Christian Schenk
40   */
41  public class BibSonomyBibTexMacro extends BaseMacro {
42  
43  	private String username;
44  	private String apiKey;
45  
46  	public String execute(@SuppressWarnings("unchecked") Map params, String body, RenderContext renderContext) throws MacroException {
47  		// parse the parameters
48  		this.username = ConfluenceHelper.getParam(params, new String[] { "0", "username", "userName" }, true);
49  		this.apiKey = ConfluenceHelper.getParam(params, new String[] { "1", "apikey", "apiKey" }, true);
50  
51  		final Document doc = new HttpClientHelper(this.username, this.apiKey).getDocument("http://www.bibsonomy.org/api/users/" + this.username + "/posts?resourcetype=bibtex");
52  		final List<Post<BibTex>> posts = BibSonomyHelper.parseDocument4BibTex(doc);
53  
54  		final StringBuffer buf = new StringBuffer();
55  		buf.append("<ul>\n");
56  		for (final Post<BibTex> post : posts) {
57  			buf.append("<li>"+post.getResource().getTitle()+"</li>\n");
58  		}
59  		buf.append("</ul>\n");
60  		return buf.toString();
61  	}
62  
63  	public boolean isInline() {
64  		return false;
65  	}
66  
67  	public boolean hasBody() {
68  		return false;
69  	}
70  
71  	public RenderMode getBodyRenderMode() {
72  		return RenderMode.NO_RENDER;
73  	}
74  }