public static TreeTable configuration()

in endorsed/src/org.apache.sis.util/main/org/apache/sis/setup/About.java [189:536]


    public static TreeTable configuration(final Set<About> sections, Locale locale, final TimeZone timezone) {
        ArgumentChecks.ensureNonNull("sections", sections);
        final Locale formatLocale;
        if (locale != null) {
            formatLocale = locale;
        } else {
            locale       = Locale.getDefault(Locale.Category.DISPLAY);
            formatLocale = Locale.getDefault(Locale.Category.FORMAT);
        }
        String userHome = null;
        String javaHome = null;
        final Date now = new Date();
        final Vocabulary resources = Vocabulary.forLocale(locale);
        final DefaultTreeTable table = new DefaultTreeTable(NAME, VALUE_AS_TEXT);
        final TreeTable.Node root = table.getRoot();
        root.setValue(NAME, resources.getString(Vocabulary.Keys.LocalConfiguration));
        table.setRoot(root);
        /*
         * Begin with the "Versions" section. The `newSection` variable will be updated in the
         * switch statement when new section will begin, and reset to `null` after the `section`
         * variable has been updated accordingly.
         */
        TreeTable.Node section = null;
        About newSection = VERSIONS;
fill:   for (int i=0; ; i++) {
            short    nameKey  = 0;          // The Vocabulary.Key for `name`, used only if name is null.
            String   name     = null;       // The value to put in the "Name" column of the table.
            Object   value    = null;       // The value to put in the "Value" column of the table.
            String[] children = null;       // Optional children to write below the node.
            switch (i) {
                case 0: {
                    if (sections.contains(VERSIONS)) {
                        name  = "Apache SIS";
                        value = Version.SIS;
                    }
                    break;
                }
                case 1: {
                    if (sections.contains(VERSIONS)) {
                        name  = "Java";
                        value = concatenate(getProperty("java.version"), getProperty("java.vendor"), true);
                    }
                    break;
                }
                case 2: {
                    if (sections.contains(VERSIONS)) {
                        name  = "JavaFX";
                        value = getProperty("javafx.version");
                    }
                    break;
                }
                case 3: {
                    if (sections.contains(VERSIONS)) {
                        nameKey = Vocabulary.Keys.OperatingSystem;
                        value = concatenate(concatenate(getProperty("os.name"),
                                getProperty("os.version"), false), getProperty("os.arch"), true);
                    }
                    break;
                }
                case 4: {
                    if (sections.contains(VERSIONS)) {
                        nameKey = Vocabulary.Keys.Container;
                        value = Shutdown.getContainer();        // Sometimes contains version information.
                    }
                    break;
                }
                case 5: {
                    if (sections.contains(VERSIONS)) {
                        nameKey = Vocabulary.Keys.GeodeticDataset;
                        value = MetadataServices.getInstance().getInformation(Constants.EPSG, locale);
                    }
                    break;
                }
                case 6: {
                    newSection = LOCALIZATION;
                    if (sections.contains(LOCALIZATION)) {
                        final Locale current = Locale.getDefault();
                        if (current != null) {
                            nameKey = Vocabulary.Keys.Locale;
                            value = current.getDisplayName(locale);
                            final CharSequence code = concatenate(getCode(locale, false), getCode(locale, true), true);
                            if (code != null) {
                                children = new String[] {resources.getString(Vocabulary.Keys.Code_1, "ISO"), code.toString()};
                            }
                        }
                    }
                    break;
                }
                case 7: {
                    if (sections.contains(LOCALIZATION)) {
                        final TimeZone current = TimeZone.getDefault();
                        if (current != null) {
                            nameKey = Vocabulary.Keys.Timezone;
                            final boolean inDaylightTime = current.inDaylightTime(now);
                            value = concatenate(current.getDisplayName(inDaylightTime, TimeZone.LONG, locale), current.getID(), true);
                            final DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT, formatLocale);
                            df.setTimeZone(TimeZone.getTimeZone(UTC));
                            int offset = current.getOffset(now.getTime());
                            StringBuffer buffer = format(df, offset, new StringBuffer("UTC "));
                            offset -= current.getRawOffset();
                            if (offset != 0) {
                                buffer = format(df, offset, buffer.append(" (")
                                        .append(resources.getString(Vocabulary.Keys.DaylightTime)).append(' ')).append(')');
                            }
                            children = new String[] {resources.getString(Vocabulary.Keys.Offset), buffer.toString()};
                        }
                    }
                    break;
                }
                case 8: {
                    if (sections.contains(LOCALIZATION)) {
                        nameKey = Vocabulary.Keys.CurrentDateTime;
                        final DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, formatLocale);
                        if (timezone != null) {
                            df.setTimeZone(timezone);
                        }
                        value = df.format(now);
                    }
                    break;
                }
                case 9: {
                    if (sections.contains(LOCALIZATION)) {
                        final Charset current = Charset.defaultCharset();
                        if (current != null) {
                            nameKey = Vocabulary.Keys.CharacterEncoding;
                            value = current.displayName(locale);
                            final Set<String> aliases = current.aliases();
                            if (aliases != null && !aliases.isEmpty()) {
                                final StringBuilder buffer = new StringBuilder((String) value);
                                String separator = " (";
                                for (final String alias : aliases) {
                                    buffer.append(separator).append(alias);
                                    separator = ", ";
                                }
                                value = buffer.append(')');
                            }
                        }
                    }
                    break;
                }
                case 10: {
                    newSection = PLUGINS;
                    if (sections.contains(PLUGINS)) try {
                        children = (String[]) Class.forName("org.apache.sis.storage.base.Capability")
                                .getMethod("providers", Locale.class, Vocabulary.class).invoke(null, locale, resources);
                        value = resources.getString(Vocabulary.Keys.EntryCount_1, children.length / 2);
                    } catch (ClassNotFoundException e) {
                        // org.apache.sis.storage module not on the module path.
                        Logging.recoverableException(getLogger(Modules.STORAGE), About.class, "configuration", e);
                    } catch (ReflectiveOperationException e) {
                        value = Exceptions.unwrap(e).toString();
                    }
                    nameKey = Vocabulary.Keys.DataFormats;
                    break;
                }
                case 11: {
                    newSection = LOGGING;
                    if (sections.contains(LOGGING)) {
                        nameKey = Vocabulary.Keys.Implementation;
                        value = "java.util.logging";
                        for (final Handler handler : getLogger("").getHandlers()) {
                            final String c = handler.getClass().getPackageName();
                            if (!value.equals(c)) {
                                value = c;
                                break;
                            }
                        }
                    }
                    break;
                }
                case 12: {
                    if (sections.contains(LOGGING)) {
                        nameKey = Vocabulary.Keys.Level;
                        final Level level = getLogger("").getLevel();   // Root logger level.
                        if (level == null) {
                            // May happen when some code outside Apache SIS define their own loggers.
                            value = resources.getString(Vocabulary.Keys.NotKnown);
                        } else {
                            value = level.getLocalizedName();
                            final Map<String,Level> levels = Loggers.getEffectiveLevels();
                            if (levels.size() != 1 || !level.equals(levels.get(Loggers.ROOT))) {
                                int j = 0;
                                children = new String[levels.size() * 2];
                                for (final Map.Entry<String,Level> entry : levels.entrySet()) {
                                    children[j++] = entry.getKey();
                                    children[j++] = entry.getValue().getLocalizedName();
                                }
                            }
                        }
                    }
                    break;
                }
                case 13: {
                    newSection = PATHS;
                    if (sections.contains(PATHS)) {
                        nameKey = Vocabulary.Keys.UserHome;
                        value = userHome = getProperty("user.home");
                    }
                    break;
                }
                case 14: {
                    if (sections.contains(PATHS)) {
                        nameKey = Vocabulary.Keys.CurrentDirectory;
                        value = getProperty("user.dir");
                    }
                    break;
                }
                case 15: {
                    if (sections.contains(PATHS)) {
                        nameKey = Vocabulary.Keys.DataDirectory;
                        try {
                            value = DataDirectory.getenv();
                        } catch (SecurityException e) {
                            value = e.toString();
                        }
                        if (value == null) {
                            value = Messages.forLocale(locale).getString(Messages.Keys.DataDirectoryNotSpecified_1, DataDirectory.ENV);
                        } else {
                            final Path path = DataDirectory.getRootDirectory();
                            if (path != null) {
                                value = path.toString();
                            } else {
                                value = value + " (" + resources.getString(Vocabulary.Keys.Invalid) + ')';
                            }
                        }
                    }
                    break;
                }
                case 16: {
                    if (sections.contains(PATHS)) {
                        nameKey = Vocabulary.Keys.DataBase;
                        value = MetadataServices.getInstance().getInformation("DataSource", locale);
                    }
                    break;
                }
                case 17: {
                    if (sections.contains(PATHS)) {
                        nameKey = Vocabulary.Keys.JavaHome;
                        value = javaHome = getProperty("java.home");
                    }
                    break;
                }
                case 18: {
                    if (sections.contains(PATHS)) {
                        nameKey = Vocabulary.Keys.TemporaryFiles;
                        value = getProperty("java.io.tmpdir");
                    }
                    break;
                }
                case 19: {
                    newSection = LIBRARIES;
                    if (sections.contains(LIBRARIES)) {
                        nameKey = Vocabulary.Keys.ModulePath;
                        value = modulePath(getProperty("jdk.module.path"), false);
                    }
                    break;
                }
                case 20: {
                    if (sections.contains(LIBRARIES)) {
                        nameKey = Vocabulary.Keys.Classpath;
                        value = modulePath(getProperty("java.class.path"), true);
                    }
                    break;
                }
                default: break fill;
            }
            /*
             * At this point, we have the information about one node to create.
             * If the `newSection` variable is non-null, then this new node shall
             * appear in a new section.
             */
            if (value == null) {
                continue;
            }
            if (newSection != null) {
                section = root.newChild();
                section.setValue(NAME, resources.getString(newSection.resourceKey));
                newSection = null;
            }
            if (name == null) {
                name = resources.getString(nameKey);
            }
            @SuppressWarnings("null")   // `section` is non-null because of initially non-null `newSection`.
            final TreeTable.Node node = section.newChild();
            node.setValue(NAME, name);
            if (children != null) {
                for (int j=0; j<children.length; j+=2) {
                    final String c = children[j+1];
                    if (c != null) {
                        final TreeTable.Node child = node.newChild();
                        child.setValue(NAME, children[j]);
                        child.setValue(VALUE_AS_TEXT, c);
                    }
                }
            }
            if (!(value instanceof Map<?,?>)) {
                node.setValue(VALUE_AS_TEXT, value.toString());
                continue;
            }
            /*
             * Special case for values of kind Map<File,String>.
             * They are extension paths or application class paths.
             */
            final Map<?,?> paths = (Map<?,?>) value;
pathTree:   for (int j=0; ; j++) {
                TreeTable.Node directory = null;
                final String home;
                final short homeKey;
                switch (j) {
                    case 0: home = javaHome; homeKey = Vocabulary.Keys.JavaHome; break;
                    case 1: home = userHome; homeKey = Vocabulary.Keys.UserHome; break;
                    case 2: home = "";       homeKey = 0; directory = node;      break;
                    default: break pathTree;
                }
                if (home == null) {
                    // Should never happen since "user.home" and "java.home" are
                    // standard properties of the Java platform, but let be safe.
                    continue;
                }
                final File homeDirectory = home.isEmpty() ? null : new File(home);
                for (final Iterator<? extends Map.Entry<?,?>> it=paths.entrySet().iterator(); it.hasNext();) {
                    final Map.Entry<?,?> entry = it.next();
                    File file = (File) entry.getKey();
                    if (homeDirectory != null) {
                        file = relativize(homeDirectory, file);
                        if (file == null) continue;
                    }
                    if (directory == null) {
                        directory = node.newChild();
                        directory.setValue(NAME, parenthesis(resources.getString(homeKey)));
                    }
                    CharSequence title = (CharSequence) entry.getValue();
                    if (title == null || title.length() == 0) {
                        title = parenthesis(resources.getString(file.isDirectory() ?
                                Vocabulary.Keys.Directory : Vocabulary.Keys.Untitled).toLowerCase(locale));
                    }
                    TreeTables.nodeForPath(directory, NAME, file).setValue(VALUE_AS_TEXT, title);
                    it.remove();
                }
                if (directory != null) {
                    concatenateSingletons(directory, true);
                    omitMavenRedundancy(directory);
                }
            }
        }
        TreeTables.replaceCharSequences(table, locale);
        return table;
    }