Add method to delegate Sone parsing.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 21d7a80..e0f2649 100644 (file)
@@ -17,6 +17,7 @@
 
 package net.pterodactylus.sone.core;
 
+import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -438,6 +439,26 @@ public class Core extends AbstractService {
        }
 
        /**
+        * Loads a Sone from an input stream.
+        *
+        * @param soneInputStream
+        *            The input stream to load the Sone from
+        * @return The parsed Sone, or {@code null} if the Sone could not be parsed
+        */
+       public Sone loadSone(InputStream soneInputStream) {
+               Sone parsedSone = soneDownloader.parseSone(soneInputStream);
+               if (parsedSone == null) {
+                       return null;
+               }
+               if (parsedSone.getInsertUri() != null) {
+                       addLocalSone(parsedSone);
+               } else {
+                       addSone(parsedSone);
+               }
+               return parsedSone;
+       }
+
+       /**
         * Loads and updates the given Sone.
         *
         * @param sone
@@ -615,6 +636,7 @@ public class Core extends AbstractService {
        /**
         * Loads the configuration.
         */
+       @SuppressWarnings("unchecked")
        private void loadConfiguration() {
                logger.entering(Core.class.getName(), "loadConfiguration()");
 
@@ -625,7 +647,20 @@ public class Core extends AbstractService {
                                SoneInserter.setInsertionDelay(newValue);
                        }
 
-               })).set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
+               }));
+
+               options.addBooleanOption("ClearOnNextRestart", new DefaultOption<Boolean>(false)).set(configuration.getBooleanValue("Option/ClearOnNextRestart").getValue(null));
+               options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption<Boolean>(false)).set(configuration.getBooleanValue("Option/ReallyClearOnNextRestart").getValue(null));
+
+               boolean clearConfiguration = options.getBooleanOption("ClearOnNextRestart").get() && options.getBooleanOption("ReallyClearOnNextRestart").get();
+               options.getBooleanOption("ClearOnNextRestart").set(null);
+               options.getBooleanOption("ReallyClearOnNextRestart").set(null);
+               if (clearConfiguration) {
+                       /* stop loading the configuration. */
+                       return;
+               }
+
+               options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
 
                /* parse local Sones. */
                logger.log(Level.INFO, "Loading Sones…");
@@ -772,6 +807,8 @@ public class Core extends AbstractService {
                try {
                        /* store the options first. */
                        configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
+                       configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
+                       configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
 
                        /* store all Sones. */
                        int soneId = 0;