Android Open Source - tpblogr File Blog Manager






From Project

Back to project page tpblogr.

License

The source code is released under:

MIT License

If you think the Android project tpblogr listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package blogr.vpm.fr.blogr.persistence;
/* w  w w  .j a  v a 2  s. c om*/
import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import blogr.vpm.fr.blogr.bean.Blog;
import blogr.vpm.fr.blogr.bean.EmailBlog;
import blogr.vpm.fr.blogr.bean.GithubBlog;
import blogr.vpm.fr.blogr.bean.TPBlog;

/**
 * Manages the files for the blogs
 *
 * Created by vince on 01/12/14.
 */
public class FileBlogManager implements BlogRetriever, BlogSaver {

  public static final String BLOGS_DIR = "blogs";
  public static final String APP_DIR = "BlogR";
  public static final String EXT_PROPS = ".properties";

  @Override
  public List<Blog> retrieveAll() throws IOException {
    File blogsDir = getBlogsDir();
    List<Blog> blogs = new ArrayList<Blog>();
    if (blogsDir.exists() && blogsDir.isDirectory()) {
      for (File blogFile : blogsDir.listFiles()) {
        if (blogFile.getPath().contains(EXT_PROPS)) {
          blogs.add(getBlogFromMetadata(blogFile));
        }
      }
    }
    return blogs;
  }

  @Override
  public boolean persist(Blog blog) throws IOException {
    Properties blogProps = getBlogProperties(blog);
    FileOutputStream blogStream = new FileOutputStream(getBlogFile(blog));
    blogProps.store(blogStream, "updating blog metadata");
    blogStream.close();
    return true;
  }

  @Override
  public boolean update(Blog oldBlog, Blog newBlog) throws IOException {
    File oldBlogFile = getBlogFile(oldBlog);
    if (oldBlogFile.exists()){
      oldBlogFile.delete();
    }
    return persist(newBlog);
  }

  private Properties getBlogProperties(Blog blog) {
    Blog.Storage blogPropCreator = null;
    if (blog instanceof TPBlog){
      blogPropCreator = new TPBlog.Storer();
    } else if (blog instanceof EmailBlog){
      blogPropCreator = new EmailBlog.Storer();
    } else if (blog instanceof GithubBlog){
      blogPropCreator = new GithubBlog.Storer();
    }
    assert blogPropCreator != null;
    return blogPropCreator.marshall(blog);
  }

  /**
   * Extracts information from a Blog metadata file (a text file "<BlogTitle>.properties" in the blogs directory)
   * @param blogFile the metadata file for the blog
   * @return a blog
   * @throws IOException if there is any issue related to I/O
   */
  private Blog getBlogFromMetadata(File blogFile) throws IOException {
    // extract properties from file
    FileInputStream blogStream = new FileInputStream(blogFile);
    Properties props = new Properties();
    props.load(blogStream);
    blogStream.close();

    // create blog from properties
    String type = props.getProperty(Blog.Storage.TYPE_KEY);
    Blog blog = null;
    if (EmailBlog.class.getName().equals(type)) {
      blog = new EmailBlog.Storer().unmarshall(props);
    } else if (GithubBlog.class.getName().equals(type)) {
      blog = new GithubBlog.Storer().unmarshall(props);
    } else if (TPBlog.class.getName().equals(type)) {
      blog = new TPBlog.Storer().unmarshall(props);
    }
    return blog;
  }

  /**
   * Determines (and create is non-existent) the file for a blog (a text file "<BlogTitle>.properties" in the blogs directory)
   * @param blog the blog to get the file for
   * @return a property file to store metadata
   * @throws IOException if there is any issue related to I/O
   */
  private File getBlogFile(Blog blog) throws IOException {
    File blogsDir = getBlogsDir();
    File blogMetadataFile = new File(blogsDir, blog.getTitle() + EXT_PROPS);
    if (!blogMetadataFile.exists()){
      blogMetadataFile.createNewFile();
    }
    return blogMetadataFile;
  }

  /**
   * Determines (and create if non-existent) the directory file where blog metadata files are
   * @return an existing file and directory
   */
  private File getBlogsDir() {
    File blogsDir = new File(Environment.getExternalStoragePublicDirectory(APP_DIR), BLOGS_DIR);
    blogsDir.mkdirs();
    return blogsDir;
  }
}




Java Source Code List

blogr.vpm.fr.blogr.activity.AllPreferencesActivity.java
blogr.vpm.fr.blogr.activity.BlogActivity.java
blogr.vpm.fr.blogr.activity.BlogListDialogFragment.java
blogr.vpm.fr.blogr.activity.DialogDismissedListener.java
blogr.vpm.fr.blogr.activity.EmailBlogEditionFragment.java
blogr.vpm.fr.blogr.activity.FlickrDialogFragment.java
blogr.vpm.fr.blogr.activity.GithubBlogEditionFragment.java
blogr.vpm.fr.blogr.activity.InvalidatedModelListener.java
blogr.vpm.fr.blogr.activity.PostEditionActivity.java
blogr.vpm.fr.blogr.activity.PostEditionFragment.java
blogr.vpm.fr.blogr.activity.PostListActivity.java
blogr.vpm.fr.blogr.activity.PostListChoiceModeListener.java
blogr.vpm.fr.blogr.activity.PostListFragment.java
blogr.vpm.fr.blogr.activity.PostSelectionListener.java
blogr.vpm.fr.blogr.activity.PreferenceCategoryFragment.java
blogr.vpm.fr.blogr.apis.flickr.FlickrJAndroidProvider.java
blogr.vpm.fr.blogr.apis.flickr.FlickrJAsyncTaskProvider.java
blogr.vpm.fr.blogr.apis.flickr.FlickrProvider.java
blogr.vpm.fr.blogr.apis.flickr.ParcelableFlickrPhoto.java
blogr.vpm.fr.blogr.bean.Blog.java
blogr.vpm.fr.blogr.bean.EmailBlog.java
blogr.vpm.fr.blogr.bean.GithubBlog.java
blogr.vpm.fr.blogr.bean.Post.java
blogr.vpm.fr.blogr.bean.TPBlog.java
blogr.vpm.fr.blogr.format.AlignCenterTagsProvider.java
blogr.vpm.fr.blogr.format.AlignLeftTagsProvider.java
blogr.vpm.fr.blogr.format.AlignRightTagsProvider.java
blogr.vpm.fr.blogr.insertion.DefaultInserter.java
blogr.vpm.fr.blogr.insertion.Inserter.java
blogr.vpm.fr.blogr.insertion.SingleTagProvider.java
blogr.vpm.fr.blogr.insertion.SurroundingTagsProvider.java
blogr.vpm.fr.blogr.location.AndroidLocationProvider.java
blogr.vpm.fr.blogr.location.LatLongTagProvider.java
blogr.vpm.fr.blogr.location.LocationProvider.java
blogr.vpm.fr.blogr.persistence.BlogRetriever.java
blogr.vpm.fr.blogr.persistence.BlogSaver.java
blogr.vpm.fr.blogr.persistence.FileBlogManager.java
blogr.vpm.fr.blogr.persistence.FilePostRetriever.java
blogr.vpm.fr.blogr.persistence.FilePostSaver.java
blogr.vpm.fr.blogr.persistence.PostRetriever.java
blogr.vpm.fr.blogr.persistence.PostSaver.java
blogr.vpm.fr.blogr.picture.AsyncPictureLoader.java
blogr.vpm.fr.blogr.picture.ImageViewLoader.java
blogr.vpm.fr.blogr.picture.PictureLoadedListener.java
blogr.vpm.fr.blogr.picture.PictureMdTagsProvider.java
blogr.vpm.fr.blogr.picture.PicturePickedListener.java
blogr.vpm.fr.blogr.picture.PictureTagProvider.java
blogr.vpm.fr.blogr.picture.PictureTpTagsProvider.java
blogr.vpm.fr.blogr.publish.Formatter.java
blogr.vpm.fr.blogr.publish.HtmlFormatter.java
blogr.vpm.fr.blogr.publish.IdentityFormatter.java
blogr.vpm.fr.blogr.publish.PostPublisher.java
blogr.vpm.fr.blogr.publish.StdEmailPostPublisher.java
blogr.vpm.fr.blogr.publish.TPJavaMailPostPublisher.java
blogr.vpm.fr.blogr.publish.TPPostPublisher.java
blogr.vpm.fr.blogr.service.PostPublishingPreferencesProvider.java
blogr.vpm.fr.blogr.service.PostPublishingServiceProvider.java