// Path: src/main/java/info/gianlucacosta/easypmd/ide/Injector.java
// public class Injector {
// 
//     @ServiceProvider(service = TestService.class)
//     public static class TestService {
//     }
// 
//     private static final Lookup lookup = Lookup.getDefault();
// 
//     private static Lookup getLookup() {
//         return lookup;
//     }
// 
//     public static <T> T lookup(Class<T> serviceClass) {
//         T result = getLookup().lookup(serviceClass);
// 
//         validateLookupResult(serviceClass, result);
// 
//         return result;
//     }
// 
//     public static <T> Collection<? extends T> lookupAll(Class<T> serviceClass) {
//         Collection<? extends T> result = getLookup().lookupAll(serviceClass);
// 
//         validateLookupResult(serviceClass, result);
// 
//         return result;
//     }
// 
//     private static void validateLookupResult(Class<?> serviceClass, Object lookupResult) {
//         if (lookupResult == null) {
//             if (shouldThrowLookupExceptions(serviceClass)) {
//                 throw new IllegalArgumentException(String.format("Service '%s' not registered!", serviceClass.getSimpleName()));
//             }
//         }
//     }
// 
//     //This code prevents exceptions in the visual form editors within NetBeans
//     private static boolean shouldThrowLookupExceptions(Class<?> serviceClass) {
//         if (serviceClass != TestService.class) {
//             TestService testService = lookup.lookup(TestService.class);
//             if (testService == null) {
//                 return false;
//             }
//         }
// 
//         return true;
//     }
// 
//     private Injector() {
//     }
// }
// 
// Path: src/main/java/info/gianlucacosta/easypmd/ide/PathService.java
// public interface PathService {
// 
//     /**
//      * The root of the plugin directory within the user's home directory
//      */
//     Path getRootPath();
// 
//     /**
//      * The directory containing cache information
//      */
//     Path getCachePath();
// }
// 
// Path: src/main/java/info/gianlucacosta/easypmd/util/Throwables.java
// public interface Throwables {
// 
//     /**
//      * Converts the stack trace of a Throwable to a string
//      *
//      * @param throwable
//      * @return the string representation of the stack trace
//      */
//     static String getStackTraceString(Throwable throwable) {
//         final Writer result = new StringWriter();
//         final PrintWriter printWriter = new PrintWriter(result);
// 
//         throwable.printStackTrace(printWriter);
// 
//         return result.toString();
//     }
// 
//     /**
//      * Returns the throwable message, or the message of its cause, recursively,
//      * or a simple message if no message was found in the chain
//      *
//      * @param throwable The subject throwable
//      * @return a non-empty string
//      */
//     static String getNonEmptyMessage(Throwable throwable) {
//         String message = throwable.getMessage();
// 
//         if (message == null || message.isEmpty()) {
//             Throwable cause = throwable.getCause();
// 
//             if (cause != null) {
//                 return getNonEmptyMessage(cause);
//             } else {
//                 return "(no message)";
//             }
//         } else {
//             return message;
//         }
//     }
// 
//     /**
//      * Returns a string showing a Throwable (type and message) and the related
//      * stack trace
//      *
//      * @param throwable
//      * @return
//      */
//     static String toStringWithStackTrace(Throwable throwable) {
//         return String.format(
//                 "%s (%s)\n%s",
//                 throwable.getClass().getName(),
//                 throwable.getMessage(),
//                 getStackTraceString(throwable)
//         );
//     }
// }


import info.gianlucacosta.easypmd.ide.PathService;
import info.gianlucacosta.easypmd.util.Throwables;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.nio.file.Files;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import info.gianlucacosta.easypmd.ide.Injector;
import org.openide.util.lookup.ServiceProvider;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.util.logging.Logger;
import java.nio.file.Path;
/*
 * ==========================================================================%%#
 * EasyPmd
 * ===========================================================================%%
 * Copyright (C) 2009 - 2017 Gianluca Costa
 * ===========================================================================%%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * ==========================================================================%##
 */
package info.gianlucacosta.easypmd.ide.options.profiles;



/**
 * Default implementation of ProfileContextRepository.
 */
@ServiceProvider(service = ProfileContextRepository.class)
public class DefaultProfileContextRepository implements ProfileContextRepository {

    private static final Logger logger = Logger.getLogger(DefaultProfileContextFactory.class.getName());

    private static final String PROFILES_FILE_NAME = "Profiles.xml";

    private final EasyPmdXStream xmlStream = new EasyPmdXStream();

    private final Path profilesPath;

    private ProfileContext profileContext;

    public DefaultProfileContextRepository() {
// Path: src/main/java/info/gianlucacosta/easypmd/ide/Injector.java
// public class Injector {
// 
//     @ServiceProvider(service = TestService.class)
//     public static class TestService {
//     }
// 
//     private static final Lookup lookup = Lookup.getDefault();
// 
//     private static Lookup getLookup() {
//         return lookup;
//     }
// 
//     public static <T> T lookup(Class<T> serviceClass) {
//         T result = getLookup().lookup(serviceClass);
// 
//         validateLookupResult(serviceClass, result);
// 
//         return result;
//     }
// 
//     public static <T> Collection<? extends T> lookupAll(Class<T> serviceClass) {
//         Collection<? extends T> result = getLookup().lookupAll(serviceClass);
// 
//         validateLookupResult(serviceClass, result);
// 
//         return result;
//     }
// 
//     private static void validateLookupResult(Class<?> serviceClass, Object lookupResult) {
//         if (lookupResult == null) {
//             if (shouldThrowLookupExceptions(serviceClass)) {
//                 throw new IllegalArgumentException(String.format("Service '%s' not registered!", serviceClass.getSimpleName()));
//             }
//         }
//     }
// 
//     //This code prevents exceptions in the visual form editors within NetBeans
//     private static boolean shouldThrowLookupExceptions(Class<?> serviceClass) {
//         if (serviceClass != TestService.class) {
//             TestService testService = lookup.lookup(TestService.class);
//             if (testService == null) {
//                 return false;
//             }
//         }
// 
//         return true;
//     }
// 
//     private Injector() {
//     }
// }
// 
// Path: src/main/java/info/gianlucacosta/easypmd/ide/PathService.java
// public interface PathService {
// 
//     /**
//      * The root of the plugin directory within the user's home directory
//      */
//     Path getRootPath();
// 
//     /**
//      * The directory containing cache information
//      */
//     Path getCachePath();
// }
// 
// Path: src/main/java/info/gianlucacosta/easypmd/util/Throwables.java
// public interface Throwables {
// 
//     /**
//      * Converts the stack trace of a Throwable to a string
//      *
//      * @param throwable
//      * @return the string representation of the stack trace
//      */
//     static String getStackTraceString(Throwable throwable) {
//         final Writer result = new StringWriter();
//         final PrintWriter printWriter = new PrintWriter(result);
// 
//         throwable.printStackTrace(printWriter);
// 
//         return result.toString();
//     }
// 
//     /**
//      * Returns the throwable message, or the message of its cause, recursively,
//      * or a simple message if no message was found in the chain
//      *
//      * @param throwable The subject throwable
//      * @return a non-empty string
//      */
//     static String getNonEmptyMessage(Throwable throwable) {
//         String message = throwable.getMessage();
// 
//         if (message == null || message.isEmpty()) {
//             Throwable cause = throwable.getCause();
// 
//             if (cause != null) {
//                 return getNonEmptyMessage(cause);
//             } else {
//                 return "(no message)";
//             }
//         } else {
//             return message;
//         }
//     }
// 
//     /**
//      * Returns a string showing a Throwable (type and message) and the related
//      * stack trace
//      *
//      * @param throwable
//      * @return
//      */
//     static String toStringWithStackTrace(Throwable throwable) {
//         return String.format(
//                 "%s (%s)\n%s",
//                 throwable.getClass().getName(),
//                 throwable.getMessage(),
//                 getStackTraceString(throwable)
//         );
//     }
// }


// Path: src/main/java/info/gianlucacosta/easypmd/ide/options/profiles/DefaultProfileContextRepository.java
import info.gianlucacosta.easypmd.ide.PathService;
import info.gianlucacosta.easypmd.util.Throwables;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.nio.file.Files;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import info.gianlucacosta.easypmd.ide.Injector;
import org.openide.util.lookup.ServiceProvider;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.util.logging.Logger;
import java.nio.file.Path;

/*
 * ==========================================================================%%#
 * EasyPmd
 * ===========================================================================%%
 * Copyright (C) 2009 - 2017 Gianluca Costa
 * ===========================================================================%%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * ==========================================================================%##
 */
package info.gianlucacosta.easypmd.ide.options.profiles;



/**
 * Default implementation of ProfileContextRepository.
 */
@ServiceProvider(service = ProfileContextRepository.class)
public class DefaultProfileContextRepository implements ProfileContextRepository {

    private static final Logger logger = Logger.getLogger(DefaultProfileContextFactory.class.getName());

    private static final String PROFILES_FILE_NAME = "Profiles.xml";

    private final EasyPmdXStream xmlStream = new EasyPmdXStream();

    private final Path profilesPath;

    private ProfileContext profileContext;

    public DefaultProfileContextRepository() {
        PathService pathService = Injector.lookup(PathService.class);