1   /*
2    * BibSonomyExporter
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.bibsonomy.export.delicious;
20  
21  import java.io.InputStreamReader;
22  import java.io.StringWriter;
23  import java.io.Writer;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.velocity.VelocityContext;
28  import org.apache.velocity.app.Velocity;
29  import org.apache.velocity.exception.ParseErrorException;
30  import org.apache.velocity.exception.ResourceNotFoundException;
31  import org.bibsonomy.model.Bookmark;
32  import org.bibsonomy.model.Post;
33  import org.bibsonomy.model.Resource;
34  import org.christianschenk.bibsonomy.export.BibSonomyClient;
35  import org.christianschenk.bibsonomy.export.BibSonomyHelper;
36  import org.christianschenk.bibsonomy.export.model.SimplePost;
37  import org.christianschenk.bibsonomy.export.utils.VelocityNoOutputLogger;
38  
39  /**
40   * Exports posts from BibSonomy into the del.icio.us export format (Netscape
41   * Bookmark file). This way you can import your data into a lot of other social
42   * bookmarking systems.<br/>
43   * 
44   * Hint: we skip all non public posts.
45   * 
46   * @author Christian Schenk
47   */
48  public class DeliciousExportFormatWriter {
49  
50  	private final String bibSonomyUsername;
51  	private final String bibSonomyApiKey;
52  
53  	public DeliciousExportFormatWriter(final String bibSonomyUsername, final String bibSonomyApiKey) {
54  		this.bibSonomyUsername = bibSonomyUsername;
55  		this.bibSonomyApiKey = bibSonomyApiKey;
56  
57  		try {
58  			Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, new VelocityNoOutputLogger());
59  			Velocity.init();
60  		} catch (final Exception ignore) {
61  		}
62  	}
63  
64  	public void doExport() {
65  		// get all posts from BibSonomy
66  		final List<Post<? extends Resource>> posts = new BibSonomyClient(this.bibSonomyUsername, this.bibSonomyApiKey).getAllBookmarksFromBibSonomy();
67  		// copy all posts into a simpler bean for the Velocity template
68  		final List<SimplePost> simplePosts = new ArrayList<SimplePost>();
69  		for (final Post<? extends Resource> post : posts) {
70  			// skip all non public posts
71  			if (BibSonomyHelper.getShared(post.getGroups()) == false) continue;
72  
73  			final SimplePost simplePost = new SimplePost();
74  			final Bookmark bookmark = (Bookmark) post.getResource();
75  			simplePost.setTitle(bookmark.getTitle());
76  			simplePost.setUrl(bookmark.getUrl());
77  			simplePost.setTags(BibSonomyHelper.getTagsCommaSeparated(post.getTags()));
78  			String desc = post.getDescription();
79  			if (desc != null && "".equals(desc.trim())) desc = null;
80  			simplePost.setDescription(desc);
81  			simplePost.setDate(post.getDate().getTime());
82  			simplePosts.add(simplePost);
83  		}
84  
85  		// write Netscape Bookmark file to stdout
86  		try {
87  			final VelocityContext context = new VelocityContext();
88  			context.put("posts", simplePosts);
89  
90  			final Writer output = new StringWriter();
91  			Velocity.evaluate(context, output, "delicious-export", new InputStreamReader(ClassLoader.getSystemResourceAsStream("delicious-export.vm")));
92  			System.out.println(output);
93  		} catch (final ResourceNotFoundException ex) {
94  			ex.printStackTrace();
95  		} catch (final ParseErrorException ex) {
96  			ex.printStackTrace();
97  		} catch (final Exception ex) {
98  			ex.printStackTrace();
99  		}
100 	}
101 }