From: David ‘Bombe’ Roden Date: Thu, 13 Nov 2008 23:14:30 +0000 (+0100) Subject: Use MapWriter instead of Properties.store() and Properties.load(). X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=753d511d1a39e40c5bd8acbff47587ba634b274c;p=jSite2.git Use MapWriter instead of Properties.store() and Properties.load(). --- diff --git a/src/net/pterodactylus/jsite/core/ProjectManager.java b/src/net/pterodactylus/jsite/core/ProjectManager.java index 2e28ab9..c44581a 100644 --- a/src/net/pterodactylus/jsite/core/ProjectManager.java +++ b/src/net/pterodactylus/jsite/core/ProjectManager.java @@ -22,19 +22,21 @@ package net.pterodactylus.jsite.core; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.FileWriter; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; +import java.io.Reader; +import java.io.Writer; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.List; -import java.util.Properties; +import java.util.Map; import java.util.Map.Entry; import java.util.logging.Level; import java.util.logging.Logger; +import net.pterodactylus.util.collection.MapWriter; import net.pterodactylus.util.io.Closer; import net.pterodactylus.util.logging.Logging; @@ -117,25 +119,26 @@ public class ProjectManager implements PropertyChangeListener { if (!projectFile.exists() || !projectFile.isFile() || !projectFile.canRead()) { return; } - Properties projectProperties = new Properties(); - InputStream projectInputStream = null; + logger.log(Level.INFO, "Loading projects from “" + projectFile + "”..."); + Map projectProperties; + Reader reader = null; try { - projectInputStream = new FileInputStream(projectFile); - projectProperties.load(projectInputStream); + reader = new FileReader(projectFile); + projectProperties = MapWriter.read(reader); } finally { - Closer.close(projectInputStream); + Closer.close(reader); } int projectIndex = 0; while (projectProperties.containsKey("projects." + projectIndex + ".name")) { String projectPrefix = "projects." + projectIndex; - String projectId = projectProperties.getProperty(projectPrefix + ".id"); - String projectName = projectProperties.getProperty(projectPrefix + ".name"); - String projectDescription = projectProperties.getProperty(projectPrefix + ".description"); - String projectPrivateKey = projectProperties.getProperty(projectPrefix + ".privateKey"); - String projectPublicKey = projectProperties.getProperty(projectPrefix + ".publicKey"); - String projectBasePath = projectProperties.getProperty(projectPrefix + ".basePath"); - String projectDefaultFile = projectProperties.getProperty(projectPrefix + ".defaultFile"); - String projectNode = projectProperties.getProperty(projectPrefix + ".node"); + String projectId = projectProperties.get(projectPrefix + ".id"); + String projectName = projectProperties.get(projectPrefix + ".name"); + String projectDescription = projectProperties.get(projectPrefix + ".description"); + String projectPrivateKey = projectProperties.get(projectPrefix + ".privateKey"); + String projectPublicKey = projectProperties.get(projectPrefix + ".publicKey"); + String projectBasePath = projectProperties.get(projectPrefix + ".basePath"); + String projectDefaultFile = projectProperties.get(projectPrefix + ".defaultFile"); + String projectNode = projectProperties.get(projectPrefix + ".node"); Project project = new Project(); project.setId(projectId); project.setName(projectName); @@ -150,8 +153,8 @@ public class ProjectManager implements PropertyChangeListener { } int overrideIndex = 0; while (projectProperties.containsKey(projectPrefix + ".overrides." + overrideIndex + ".override")) { - String filePath = projectProperties.getProperty(projectPrefix + ".overrides." + overrideIndex + ".filePath"); - FileOverride override = FileOverride.valueOf(projectProperties.getProperty(projectPrefix + ".overrides." + overrideIndex + ".override")); + String filePath = projectProperties.get(projectPrefix + ".overrides." + overrideIndex + ".filePath"); + FileOverride override = FileOverride.valueOf(projectProperties.get(projectPrefix + ".overrides." + overrideIndex + ".override")); project.addFileOverride(filePath, override); logger.log(Level.FINEST, "read override: " + filePath + ", " + override); overrideIndex++; @@ -175,35 +178,35 @@ public class ProjectManager implements PropertyChangeListener { throw new IOException("could not create directory: " + directory); } } - Properties projectProperties = new Properties(); + Map projectProperties = new HashMap(); int projectIndex = 0; for (Project project : projects) { String projectPrefix = "projects." + projectIndex; - projectProperties.setProperty(projectPrefix + ".id", project.getId()); - projectProperties.setProperty(projectPrefix + ".name", project.getName()); - projectProperties.setProperty(projectPrefix + ".description", project.getDescription()); - projectProperties.setProperty(projectPrefix + ".privateKey", project.getPrivateKey()); - projectProperties.setProperty(projectPrefix + ".publicKey", project.getPublicKey()); - projectProperties.setProperty(projectPrefix + ".basePath", project.getBasePath()); - projectProperties.setProperty(projectPrefix + ".defaultFile", project.getDefaultFile()); + projectProperties.put(projectPrefix + ".id", project.getId()); + projectProperties.put(projectPrefix + ".name", project.getName()); + projectProperties.put(projectPrefix + ".description", project.getDescription()); + projectProperties.put(projectPrefix + ".privateKey", project.getPrivateKey()); + projectProperties.put(projectPrefix + ".publicKey", project.getPublicKey()); + projectProperties.put(projectPrefix + ".basePath", project.getBasePath()); + projectProperties.put(projectPrefix + ".defaultFile", project.getDefaultFile()); if (project.getNode() != null) { - projectProperties.setProperty(projectPrefix + ".node", project.getNode().getId()); + projectProperties.put(projectPrefix + ".node", project.getNode().getId()); } int overrideIndex = 0; for (Entry overrideEntry : project.getFileOverrides().entrySet()) { - projectProperties.setProperty(projectPrefix + ".overrides." + overrideIndex + ".filePath", overrideEntry.getKey()); - projectProperties.setProperty(projectPrefix + ".overrides." + overrideIndex + ".override", overrideEntry.getValue().toString()); + projectProperties.put(projectPrefix + ".overrides." + overrideIndex + ".filePath", overrideEntry.getKey()); + projectProperties.put(projectPrefix + ".overrides." + overrideIndex + ".override", overrideEntry.getValue().toString()); overrideIndex++; } projectIndex++; } File projectFile = new File(directoryFile, "projects.properties"); - OutputStream projectOutputStream = null; + Writer writer = null; try { - projectOutputStream = new FileOutputStream(projectFile); - projectProperties.store(projectOutputStream, "jSite projects"); + writer = new FileWriter(projectFile); + MapWriter.write(writer, projectProperties); } finally { - Closer.close(projectOutputStream); + Closer.close(writer); } }