com.github.cereda.sucuri.Sucuri.java Source code

Java tutorial

Introduction

Here is the source code for com.github.cereda.sucuri.Sucuri.java

Source

/**
 * \cond LICENSE
 * Sucuri -- the localization tool for arara
 * Copyright (c) 2012, Paulo Roberto Massa Cereda
 * All rights reserved.
 *
 * Redistribution and  use in source  and binary forms, with  or without
 * modification, are  permitted provided  that the  following conditions
 * are met:
 *
 * 1. Redistributions  of source  code must  retain the  above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form  must reproduce the above copyright
 * notice, this list  of conditions and the following  disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * 3. Neither  the name  of the  project's author nor  the names  of its
 * contributors may be used to  endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS  PROVIDED BY THE COPYRIGHT  HOLDERS AND CONTRIBUTORS
 * "AS IS"  AND ANY  EXPRESS OR IMPLIED  WARRANTIES, INCLUDING,  BUT NOT
 * LIMITED  TO, THE  IMPLIED WARRANTIES  OF MERCHANTABILITY  AND FITNESS
 * FOR  A PARTICULAR  PURPOSE  ARE  DISCLAIMED. IN  NO  EVENT SHALL  THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE  LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY,  OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT  NOT LIMITED  TO, PROCUREMENT  OF SUBSTITUTE  GOODS OR  SERVICES;
 * LOSS  OF USE,  DATA, OR  PROFITS; OR  BUSINESS INTERRUPTION)  HOWEVER
 * CAUSED AND  ON ANY THEORY  OF LIABILITY, WHETHER IN  CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
 * WAY  OUT  OF  THE USE  OF  THIS  SOFTWARE,  EVEN  IF ADVISED  OF  THE
 * POSSIBILITY OF SUCH DAMAGE.
 * \endcond
 */
// package definition
package com.github.cereda.sucuri;

// needed imports
import com.github.cereda.sucuri.model.LanguageFile;
import com.github.cereda.sucuri.model.Report;
import com.github.cereda.sucuri.utils.CommandLineAnalyzer;
import com.github.cereda.sucuri.utils.SucuriConstants;
import com.github.cereda.sucuri.utils.SucuriException;
import com.github.cereda.sucuri.utils.SucuriUtils;
import java.util.List;
import java.util.Set;
import org.apache.commons.collections.CollectionUtils;

/**
 * The main class.
 * 
 * @author Paulo Roberto Massa Cereda
 * @version 1.0
 * @since 1.0
 */
public class Sucuri {

    /**
     * Main method.
     * 
     * @param args The command line arguments.
     */
    public static void main(String[] args) {
        try {

            // draw header
            SucuriUtils.drawHeader();

            // provide the command line arguments
            CommandLineAnalyzer cmd = new CommandLineAnalyzer(args);

            // try to parse them
            if (cmd.parse()) {

                // get the template
                LanguageFile template = new LanguageFile();
                template.load(cmd.getTemplate());

                // get the input
                LanguageFile input = new LanguageFile();
                input.load(cmd.getInput(), cmd.getEncoding());

                // set the output
                LanguageFile output = new LanguageFile();

                // get the differences
                List<String> newKeysOnTemplate = (List<String>) CollectionUtils.subtract(template.getKeys(),
                        input.getKeys());
                List<String> oldKeysOnInput = (List<String>) CollectionUtils.subtract(input.getKeys(),
                        template.getKeys());

                // check for report
                if (cmd.hasReport()) {

                    // add new info
                    Report report = new Report(cmd.getOutput().getName().concat(".txt"));
                    report.generate(cmd.getTemplate().getName(), cmd.getInput().getName(),
                            cmd.getOutput().getName(), newKeysOnTemplate, oldKeysOnInput);
                }

                // set header
                String header = "Converted from '" + cmd.getTemplate().getName() + "' and '" + cmd.getInput()
                        + "'\n";
                header = header.concat("by sucuri ").concat(SucuriConstants.VERSION).concat(" on ")
                        .concat(SucuriUtils.getDate());
                output.setHeader(header);

                // sets
                Set<String> keysTemplate = template.getKeys();
                Set<String> keysInput = input.getKeys();

                // iterate
                for (String key : keysTemplate) {

                    // if it's a new key
                    if (!keysInput.contains(key)) {

                        // set from template
                        output.setProperty(key, template.getProperty(key));
                    } else {

                        // set from input
                        output.setProperty(key, input.getProperty(key));
                    }
                }

                // save new output
                output.save(cmd.getOutput());

                // print message
                System.out.println("File converted successfully.");

            }
        } catch (SucuriException sucuriException) {

            // an error ocurred, print it
            System.out.println("\n".concat(sucuriException.getMessage()));
        }
    }
}