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.helper;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.List;
24  
25  import org.christianschenk.confluence.plugin.bibsonomy.model.BibTex;
26  import org.christianschenk.confluence.plugin.bibsonomy.model.Group;
27  import org.christianschenk.confluence.plugin.bibsonomy.model.Post;
28  import org.christianschenk.confluence.plugin.bibsonomy.model.Resource;
29  import org.christianschenk.confluence.plugin.bibsonomy.model.Tag;
30  import org.christianschenk.confluence.plugin.bibsonomy.model.User;
31  import org.dom4j.Document;
32  import org.dom4j.Node;
33  
34  /**
35   * This is a fast hack that assembles an object model (like the one used in BibSonomy) from the XML
36   * output which we get from the webservice. This is a temporary solution as long as the client
37   * provided by BibSonomy doesn't work in Confluence.
38   * 
39   * @author Christian Schenk
40   */
41  public class BibSonomyHelper {
42  
43  	@SuppressWarnings("unchecked")
44  	public static List<Post<BibTex>> parseDocument4BibTex(final Document doc) {
45  		if ("ok".equals(doc.selectSingleNode("/bibsonomy").valueOf("@stat").trim()) == false) {
46  			return Collections.emptyList();
47  		}
48  
49  		final List<Post<BibTex>> posts = new ArrayList<Post<BibTex>>();
50  		final List<Node> xmlPosts = doc.getRootElement().selectNodes("//posts/post");
51  		for (final Node xmlPost : xmlPosts) {
52  			// set post
53  			final Post<BibTex> post = new Post<BibTex>();
54  			post.setDate(DateUtils.parse(xmlPost.valueOf("@postingdate")));
55  			post.setDescription(xmlPost.valueOf("@description"));
56  			post.setUser(new User(xmlPost.valueOf("user/@name")));
57  			post.setGroup(new Group(xmlPost.valueOf("group/@name")));
58  			post.setTags(getTags(xmlPost));
59  			post.setResource(getBibtex(xmlPost.selectSingleNode("bibtex")));
60  			posts.add(post);
61  		}
62  		return posts;
63  	}
64  
65  	@SuppressWarnings("unchecked")
66  	private static List<Tag> getTags(final Node xmlPost) {
67  		final List<Tag> tags = new ArrayList<Tag>();
68  		final List<Node> xmlTags = xmlPost.selectNodes("tag");
69  		for (final Node node : xmlTags) {
70  			tags.add(new Tag(node.valueOf("@name")));
71  		}
72  		return tags;
73  	}
74  
75  	private static <T extends Resource> void setResourceProperties(final T resource, final Node xmlResource) {
76  		resource.setTitle(xmlResource.valueOf("@title"));
77  		resource.setInterHash(xmlResource.valueOf("@interhash"));
78  		resource.setIntraHash(xmlResource.valueOf("@intrahash"));
79  	}
80  
81  	private static BibTex getBibtex(final Node xmlBibTex) {
82  		final BibTex bibtex = new BibTex();
83  		setResourceProperties(bibtex, xmlBibTex);
84  		bibtex.setAuthor(xmlBibTex.valueOf("@author"));
85  		bibtex.setBibtexKey(xmlBibTex.valueOf("@bibtexKey"));
86  		bibtex.setBooktitle(xmlBibTex.valueOf("@booktitle"));
87  		bibtex.setCrossref(xmlBibTex.valueOf("@crossref"));
88  		bibtex.setEditor(xmlBibTex.valueOf("@editor"));
89  		bibtex.setEntrytype(xmlBibTex.valueOf("@entrytype"));
90  		bibtex.setMisc(xmlBibTex.valueOf("@misc"));
91  		bibtex.setPages(xmlBibTex.valueOf("@pages"));
92  		bibtex.setPrivnote(xmlBibTex.valueOf("@privnote"));
93  		bibtex.setYear(xmlBibTex.valueOf("@year"));
94  		return bibtex;
95  	}
96  }