ShowMeMoreUtilities.java [plain text]
package org.blojsom.plugin.showmore;
import org.blojsom.blog.BlojsomConfiguration;
import org.blojsom.util.BlojsomProperties;
import javax.servlet.ServletConfig;
import java.io.IOException;
import java.io.InputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;
public class ShowMeMoreUtilities {
private ShowMeMoreUtilities() {
}
public static ShowMeMoreConfiguration loadConfiguration(String blogID, String showMeMoreConfigurationFile, BlojsomConfiguration blojsomConfiguration, ServletConfig servletConfig) throws IOException {
Properties showMeMoreProperties = new BlojsomProperties();
String configurationFile = blojsomConfiguration.getBaseConfigurationDirectory() + blogID + '/' + showMeMoreConfigurationFile;
InputStream is = servletConfig.getServletContext().getResourceAsStream(configurationFile);
if (is == null) {
throw new IOException("No show me more configuration file found: " + configurationFile);
} else {
showMeMoreProperties.load(is);
is.close();
String moreText = showMeMoreProperties.getProperty(ShowMeMorePlugin.SHOW_ME_MORE_TEXT);
String textCutoff = showMeMoreProperties.getProperty(ShowMeMorePlugin.ENTRY_TEXT_CUTOFF);
String textCutoffStart = showMeMoreProperties.getProperty(ShowMeMorePlugin.ENTRY_TEXT_CUTOFF_START);
String textCutoffEnd = showMeMoreProperties.getProperty(ShowMeMorePlugin.ENTRY_TEXT_CUTOFF_END);
int cutoff;
try {
cutoff = Integer.parseInt(showMeMoreProperties.getProperty(ShowMeMorePlugin.ENTRY_LENGTH_CUTOFF));
} catch (NumberFormatException e) {
cutoff = ShowMeMorePlugin.ENTRY_TEXT_CUTOFF_DEFAULT;
}
ShowMeMoreConfiguration showMeMore = new ShowMeMoreConfiguration(cutoff, textCutoff, moreText, textCutoffStart, textCutoffEnd);
return showMeMore;
}
}
public static void saveConfiguration(String blogID, String showMeMoreConfigurationFile, BlojsomConfiguration blojsomConfiguration, ShowMeMoreConfiguration showMeMoreConfiguration) throws IOException {
Properties showMeMoreProperties = new BlojsomProperties();
showMeMoreProperties.put(ShowMeMorePlugin.ENTRY_TEXT_CUTOFF, showMeMoreConfiguration.getTextCutoff());
showMeMoreProperties.put(ShowMeMorePlugin.ENTRY_TEXT_CUTOFF_START, showMeMoreConfiguration.getTextCutoffStart());
showMeMoreProperties.put(ShowMeMorePlugin.ENTRY_TEXT_CUTOFF_END, showMeMoreConfiguration.getTextCutoffEnd());
showMeMoreProperties.put(ShowMeMorePlugin.SHOW_ME_MORE_TEXT, showMeMoreConfiguration.getMoreText());
showMeMoreProperties.put(ShowMeMorePlugin.ENTRY_LENGTH_CUTOFF, Integer.toString(showMeMoreConfiguration.getCutoff()));
File showMeMoreConfig = new File(blojsomConfiguration.getInstallationDirectory() +
blojsomConfiguration.getBaseConfigurationDirectory() + blogID + "/" +
showMeMoreConfigurationFile);
FileOutputStream fos = new FileOutputStream(showMeMoreConfig);
showMeMoreProperties.store(fos, null);
fos.close();
}
}