Search in sources :

Example 1 with AbstractExporter

use of com.igormaznitsa.mindmap.plugins.api.AbstractExporter in project netbeans-mmd-plugin by raydac.

the class Main method convertData.

private static boolean convertData(@Nonnull @MustNotContainNull final String[] args) {
    MindMapPluginRegistry.getInstance().registerPlugin(new LocalMMDExporter());
    MindMapPluginRegistry.getInstance().registerPlugin(new LocalMMDImporter());
    final String[] params = new String[5];
    final Properties options = new Properties();
    final int IN_FILE = 0;
    final int OUT_FILE = 1;
    final int IN_TYPE = 2;
    final int OUT_TYPE = 3;
    final int SETTINGS = 4;
    final int OPTION = 5;
    // NOI18N
    params[IN_TYPE] = "mmd";
    // NOI18N
    params[OUT_TYPE] = "mmd";
    // NOI18N
    params[SETTINGS] = "";
    int detected = -1;
    boolean allOk = true;
    for (int i = 1; i < args.length; i++) {
        if (detected >= 0) {
            if (detected == OPTION) {
                final String text = args[i];
                // NOI18N
                final String[] splitted = text.split("\\=");
                if (splitted.length < 2) {
                    // NOI18N
                    options.put(splitted[0], "true");
                } else {
                    options.put(splitted[0], splitted[1]);
                }
            } else {
                params[detected] = args[i];
            }
            detected = -1;
        } else {
            if ("--in".equalsIgnoreCase(args[i])) {
                // NOI18N
                detected = IN_FILE;
            } else if ("--out".equalsIgnoreCase(args[i])) {
                // NOI18N
                detected = OUT_FILE;
            } else if ("--from".equalsIgnoreCase(args[i])) {
                // NOI18N
                detected = IN_TYPE;
            } else if ("--to".equalsIgnoreCase(args[i])) {
                // NOI18N
                detected = OUT_TYPE;
            } else if ("--settings".equalsIgnoreCase(args[i])) {
                // NOI18N
                detected = SETTINGS;
            } else if ("--option".equalsIgnoreCase(args[i])) {
                // NOI18N
                detected = OPTION;
            } else {
                // NOI18N
                LOGGER.error("Unexpected argument : " + args[i]);
                allOk = false;
                break;
            }
        }
    }
    if (allOk) {
        for (final String s : params) {
            if (s == null) {
                // NOI18N
                LOGGER.error("Not provided required parameter");
                allOk = true;
                break;
            }
        }
        if (allOk) {
            final File inFile = new File(params[IN_FILE]);
            final File outFile = new File(params[OUT_FILE]);
            final AbstractImporter importer = MindMapPluginRegistry.getInstance().findImporterForMnemonic(params[IN_TYPE]);
            final AbstractExporter exporter = MindMapPluginRegistry.getInstance().findExporterForMnemonic(params[OUT_TYPE]);
            if (importer == null) {
                // NOI18N
                LOGGER.error("Unknown importer : " + params[IN_TYPE]);
                allOk = false;
            } else if (exporter == null) {
                // NOI18N
                LOGGER.error("Unknown exporter : " + params[OUT_TYPE]);
                allOk = false;
            }
            if (allOk) {
                final File settingsFile = params[SETTINGS].isEmpty() ? null : new File(params[SETTINGS]);
                final MindMapPanelConfig config = new MindMapPanelConfig();
                if (settingsFile != null) {
                    try {
                        config.loadFrom(new PropertiesPreferences(FileUtils.readFileToString(settingsFile, "UTF-8")));
                    } catch (IOException ex) {
                        // NOI18N
                        LOGGER.error("Can't load settings file : " + settingsFile, ex);
                        allOk = false;
                    }
                }
                if (allOk && importer != null && exporter != null) {
                    try {
                        makeConversion(inFile, importer, outFile, exporter, config, options);
                    } catch (final Exception ex) {
                        if (ex instanceof IllegalArgumentException) {
                            LOGGER.error(ex.getMessage());
                        } else {
                            // NOI18N
                            LOGGER.error("Unexpected error during conversion", ex);
                        }
                        allOk = false;
                    }
                }
            }
        }
    }
    return allOk;
}
Also used : AbstractExporter(com.igormaznitsa.mindmap.plugins.api.AbstractExporter) IOException(java.io.IOException) Properties(java.util.Properties) Point(java.awt.Point) MindMapPanelConfig(com.igormaznitsa.mindmap.swing.panel.MindMapPanelConfig) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) AbstractImporter(com.igormaznitsa.mindmap.plugins.api.AbstractImporter) PropertiesPreferences(com.igormaznitsa.mindmap.swing.panel.utils.PropertiesPreferences) File(java.io.File)

Aggregations

AbstractExporter (com.igormaznitsa.mindmap.plugins.api.AbstractExporter)1 AbstractImporter (com.igormaznitsa.mindmap.plugins.api.AbstractImporter)1 MindMapPanelConfig (com.igormaznitsa.mindmap.swing.panel.MindMapPanelConfig)1 PropertiesPreferences (com.igormaznitsa.mindmap.swing.panel.utils.PropertiesPreferences)1 Point (java.awt.Point)1 File (java.io.File)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 Properties (java.util.Properties)1