Search in sources :

Example 1 with ProjectProperties

use of org.omegat.core.data.ProjectProperties in project omegat by omegat-org.

the class Main method selectProjectConsoleMode.

/**
 * creates the project class and adds it to the Core. Loads the project if
 * specified. An exit occurs on error loading the project. This method is
 * for the different console modes, to prevent code duplication.
 *
 * @param loadProject
 *            load the project or not
 * @return the project.
 */
private static RealProject selectProjectConsoleMode(boolean loadProject) {
    System.out.println(OStrings.getString("CONSOLE_LOADING_PROJECT"));
    // check if project okay
    ProjectProperties projectProperties = null;
    try {
        projectProperties = ProjectFileStorage.loadProjectProperties(projectLocation);
        projectProperties.verifyProject();
    } catch (Exception ex) {
        Log.logErrorRB(ex, "PP_ERROR_UNABLE_TO_READ_PROJECT_FILE");
        System.out.println(OStrings.getString("PP_ERROR_UNABLE_TO_READ_PROJECT_FILE"));
        System.exit(1);
    }
    RealProject p = new RealProject(projectProperties);
    Core.setProject(p);
    if (loadProject) {
        p.loadProject(true);
        if (!p.isProjectLoaded()) {
            Core.setProject(new NotLoadedProject());
        } else {
            executeConsoleScript(IProjectEventListener.PROJECT_CHANGE_TYPE.LOAD);
        }
    }
    return p;
}
Also used : NotLoadedProject(org.omegat.core.data.NotLoadedProject) ProjectProperties(org.omegat.core.data.ProjectProperties) RealProject(org.omegat.core.data.RealProject) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 2 with ProjectProperties

use of org.omegat.core.data.ProjectProperties in project omegat by omegat-org.

the class Main method runCreatePseudoTranslateTMX.

/**
 * Execute in console mode for translate.
 */
protected static int runCreatePseudoTranslateTMX() throws Exception {
    Log.log("Console pseudo-translate mode");
    Log.log("");
    System.out.println(OStrings.getString("CONSOLE_INITIALIZING"));
    Core.initializeConsole(PARAMS);
    RealProject p = selectProjectConsoleMode(true);
    validateTagsConsoleMode();
    System.out.println(OStrings.getString("CONSOLE_CREATE_PSEUDOTMX"));
    ProjectProperties config = p.getProjectProperties();
    List<SourceTextEntry> entries = p.getAllEntries();
    String pseudoTranslateTMXFilename = PARAMS.get(CLIParameters.PSEUDOTRANSLATETMX);
    PSEUDO_TRANSLATE_TYPE pseudoTranslateType = PSEUDO_TRANSLATE_TYPE.parse(PARAMS.get(CLIParameters.PSEUDOTRANSLATETYPE));
    String fname;
    if (!StringUtil.isEmpty(pseudoTranslateTMXFilename)) {
        if (!pseudoTranslateTMXFilename.endsWith(OConsts.TMX_EXTENSION)) {
            fname = pseudoTranslateTMXFilename + "." + OConsts.TMX_EXTENSION;
        } else {
            fname = pseudoTranslateTMXFilename;
        }
    } else {
        fname = "";
    }
    // prepare tmx
    Map<String, PrepareTMXEntry> data = new HashMap<>();
    for (SourceTextEntry ste : entries) {
        PrepareTMXEntry entry = new PrepareTMXEntry();
        entry.source = ste.getSrcText();
        switch(pseudoTranslateType) {
            case EQUAL:
                entry.translation = ste.getSrcText();
                break;
            case EMPTY:
                entry.translation = "";
                break;
        }
        data.put(ste.getSrcText(), entry);
    }
    try {
        // Write OmegaT-project-compatible TMX:
        TMXWriter.buildTMXFile(fname, false, false, config, data);
    } catch (IOException e) {
        Log.logErrorRB("CT_ERROR_CREATING_TMX");
        Log.log(e);
        throw new IOException(OStrings.getString("CT_ERROR_CREATING_TMX") + "\n" + e.getMessage());
    }
    p.closeProject();
    System.out.println(OStrings.getString("CONSOLE_FINISHED"));
    return 0;
}
Also used : SourceTextEntry(org.omegat.core.data.SourceTextEntry) HashMap(java.util.HashMap) RealProject(org.omegat.core.data.RealProject) ProjectProperties(org.omegat.core.data.ProjectProperties) IOException(java.io.IOException) PSEUDO_TRANSLATE_TYPE(org.omegat.CLIParameters.PSEUDO_TRANSLATE_TYPE) PrepareTMXEntry(org.omegat.core.data.PrepareTMXEntry)

Example 3 with ProjectProperties

use of org.omegat.core.data.ProjectProperties in project omegat by omegat-org.

the class ProjectMedProcessing method extractFromMed.

/**
 * It creates project internals from MED zip file.
 */
public static void extractFromMed(File medZip, ProjectProperties props) throws Exception {
    String medName = medZip.getName().replaceAll("\\.zip$", "");
    // extract source and target languages
    Properties p = new Properties();
    try (ZipFile zip = new ZipFile(medZip)) {
        ZipEntry e = zip.getEntry(medName + "/dossier/workflow/translation");
        if (e == null) {
            throw new Exception("Wrong MED zip structure");
        }
        try (InputStream in = zip.getInputStream(e)) {
            p.load(in);
        }
    }
    // get source language
    String slang = p.getProperty("slang");
    if (slang == null) {
        throw new Exception("Bad MED format: slang not defined");
    }
    // get target language
    String tlang = p.getProperty("tlang");
    if (tlang == null) {
        throw new Exception("Bad MED format: tlang not defined");
    }
    props.setSourceLanguage(slang);
    props.setTargetLanguage(tlang);
    // copy source files
    try (ZipFile zip = new ZipFile(medZip)) {
        extractFiles(zip, medName + "/doc/main/" + props.getSourceLanguage().getLanguage() + "/", null, props.getSourceDir().getAsFile());
        extractFiles(zip, medName + "/doc/support/" + props.getSourceLanguage().getLanguage() + "/", ".tmx", new File(props.getTMAutoRoot()));
        extractFiles(zip, medName + "/doc/support/" + props.getSourceLanguage().getLanguage() + "/term/", null, props.getGlossaryDir().getAsFile());
    }
    // copy original zip
    File outZip = new File(props.getProjectInternalDir(), "med/" + medZip.getName());
    outZip.getParentFile().mkdirs();
    FileUtils.copyFile(medZip, outZip);
}
Also used : ZipFile(java.util.zip.ZipFile) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) Properties(java.util.Properties) ProjectProperties(org.omegat.core.data.ProjectProperties) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 4 with ProjectProperties

use of org.omegat.core.data.ProjectProperties in project omegat by omegat-org.

the class ProjectUICommands method projectOpen.

/**
 * Open project. Does nothing if a project is already open and closeCurrent is false.
 *
 * @param projectDirectory
 *            project directory or null if user must choose it
 * @param closeCurrent
 *            whether or not to close the current project first, if any
 */
public static void projectOpen(final File projectDirectory, boolean closeCurrent) {
    UIThreadsUtil.mustBeSwingThread();
    if (Core.getProject().isProjectLoaded()) {
        if (closeCurrent) {
            // Register to try again after closing the current project.
            CoreEvents.registerProjectChangeListener(new IProjectEventListener() {

                public void onProjectChanged(PROJECT_CHANGE_TYPE eventType) {
                    if (eventType == PROJECT_CHANGE_TYPE.CLOSE) {
                        projectOpen(projectDirectory, false);
                        CoreEvents.unregisterProjectChangeListener(this);
                    }
                }
            });
            projectClose();
        }
        return;
    }
    final File projectRootFolder;
    if (projectDirectory == null) {
        // select existing project file - open it
        OmegaTFileChooser pfc = new OpenProjectFileChooser();
        if (OmegaTFileChooser.APPROVE_OPTION != pfc.showOpenDialog(Core.getMainWindow().getApplicationFrame())) {
            return;
        }
        projectRootFolder = pfc.getSelectedFile();
    } else {
        projectRootFolder = projectDirectory;
    }
    new SwingWorker<Void, Void>() {

        protected Void doInBackground() throws Exception {
            IMainWindow mainWindow = Core.getMainWindow();
            Cursor hourglassCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
            Cursor oldCursor = mainWindow.getCursor();
            mainWindow.setCursor(hourglassCursor);
            try {
                // convert old projects if need
                ConvertProject.convert(projectRootFolder);
            } catch (Exception ex) {
                Log.logErrorRB(ex, "PP_ERROR_UNABLE_TO_CONVERT_PROJECT");
                Core.getMainWindow().displayErrorRB(ex, "PP_ERROR_UNABLE_TO_CONVERT_PROJECT");
                mainWindow.setCursor(oldCursor);
                return null;
            }
            // check if project okay
            ProjectProperties props;
            try {
                props = ProjectFileStorage.loadProjectProperties(projectRootFolder.getAbsoluteFile());
            } catch (Exception ex) {
                Log.logErrorRB(ex, "PP_ERROR_UNABLE_TO_READ_PROJECT_FILE");
                Core.getMainWindow().displayErrorRB(ex, "PP_ERROR_UNABLE_TO_READ_PROJECT_FILE");
                mainWindow.setCursor(oldCursor);
                return null;
            }
            try {
                boolean needToSaveProperties = false;
                if (props.hasRepositories()) {
                    // This is a remote project
                    if (!Core.getParams().containsKey(CLIParameters.NO_TEAM)) {
                        // Save repository mapping
                        List<RepositoryDefinition> repos = props.getRepositories();
                        // Update project.properties
                        mainWindow.showStatusMessageRB("TEAM_OPEN");
                        try {
                            RemoteRepositoryProvider remoteRepositoryProvider = new RemoteRepositoryProvider(props.getProjectRootDir(), props.getRepositories());
                            remoteRepositoryProvider.switchToVersion(OConsts.FILE_PROJECT, null);
                            // Overwrite omegat.project
                            remoteRepositoryProvider.copyFilesFromRepoToProject(OConsts.FILE_PROJECT);
                            // Reload project properties
                            props = ProjectFileStorage.loadProjectProperties(projectRootFolder.getAbsoluteFile());
                            if (props.getRepositories() == null) {
                                // We have a 3.6 style project,
                                // so we restore the mapping we just lost
                                props.setRepositories(repos);
                                needToSaveProperties = true;
                            }
                        } catch (Exception e) {
                            Log.logErrorRB(e, "TF_PROJECT_PROPERTIES_ERROR");
                            Log.log(e);
                            throw new IOException(OStrings.getString("TF_PROJECT_PROPERTIES_ERROR") + "\n" + e.getMessage(), e);
                        }
                    }
                    // team project - non-exist directories could be created from repo
                    props.autocreateDirectories();
                } else {
                    // not a team project - ask for non-exist directories
                    while (!props.isProjectValid()) {
                        needToSaveProperties = true;
                        // something wrong with the project - display open dialog
                        // to fix it
                        ProjectPropertiesDialog prj = new ProjectPropertiesDialog(Core.getMainWindow().getApplicationFrame(), props, new File(projectRootFolder, OConsts.FILE_PROJECT).getAbsolutePath(), ProjectPropertiesDialog.Mode.RESOLVE_DIRS);
                        prj.setVisible(true);
                        props = prj.getResult();
                        prj.dispose();
                        if (props == null) {
                            // user clicks on 'Cancel'
                            mainWindow.setCursor(oldCursor);
                            return null;
                        }
                    }
                }
                final ProjectProperties propsP = props;
                Core.executeExclusively(true, () -> ProjectFactory.loadProject(propsP, true));
                if (needToSaveProperties) {
                    Core.getProject().saveProjectProperties();
                }
            } catch (Exception ex) {
                Log.logErrorRB(ex, "PP_ERROR_UNABLE_TO_READ_PROJECT_FILE");
                Core.getMainWindow().displayErrorRB(ex, "PP_ERROR_UNABLE_TO_READ_PROJECT_FILE");
                mainWindow.setCursor(oldCursor);
                return null;
            }
            RecentProjects.add(projectRootFolder.getAbsolutePath());
            mainWindow.setCursor(oldCursor);
            return null;
        }

        protected void done() {
            try {
                get();
                SwingUtilities.invokeLater(Core.getEditor()::requestFocus);
            } catch (Exception ex) {
                Log.logErrorRB(ex, "PP_ERROR_UNABLE_TO_READ_PROJECT_FILE");
                Core.getMainWindow().displayErrorRB(ex, "PP_ERROR_UNABLE_TO_READ_PROJECT_FILE");
            }
        }
    }.execute();
}
Also used : RemoteRepositoryProvider(org.omegat.core.team2.RemoteRepositoryProvider) OmegaTFileChooser(org.omegat.util.gui.OmegaTFileChooser) ProjectProperties(org.omegat.core.data.ProjectProperties) IOException(java.io.IOException) Cursor(java.awt.Cursor) KnownException(org.omegat.core.KnownException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) OpenProjectFileChooser(org.omegat.util.gui.OpenProjectFileChooser) ArrayList(java.util.ArrayList) List(java.util.List) ProjectPropertiesDialog(org.omegat.gui.dialogs.ProjectPropertiesDialog) IProjectEventListener(org.omegat.core.events.IProjectEventListener) File(java.io.File)

Example 5 with ProjectProperties

use of org.omegat.core.data.ProjectProperties in project omegat by omegat-org.

the class MatchesVarExpansion method expandVariables.

@Override
public String expandVariables(NearString match) {
    // do not modify template directly, so that we can reuse for another change
    String localTemplate = this.template;
    localTemplate = localTemplate.replace(VAR_INITIAL_CREATION_ID, match.creator == null ? "" : match.creator);
    // VAR_CREATION_ID is an alias for VAR_CHANGED_ID, for backwards compatibility.
    for (String s : new String[] { VAR_CHANGED_ID, VAR_CREATION_ID }) {
        localTemplate = localTemplate.replace(s, match.changer == null ? "" : match.changer);
    }
    if (match.creationDate > 0) {
        localTemplate = localTemplate.replace(VAR_INITIAL_CREATION_DATE, DateFormat.getInstance().format(new Date(match.creationDate)));
    } else {
        localTemplate = localTemplate.replace(VAR_INITIAL_CREATION_DATE, "");
    }
    // VAR_CREATION_DATE is an alias for VAR_CHANGED_DATE, for backwards compatibility.
    for (String s : new String[] { VAR_CHANGED_DATE, VAR_CREATION_DATE }) {
        if (match.changedDate > 0) {
            localTemplate = localTemplate.replace(s, DateFormat.getInstance().format(new Date(match.changedDate)));
        } else {
            localTemplate = localTemplate.replace(s, "");
        }
    }
    localTemplate = localTemplate.replace(VAR_SCORE_BASE, Integer.toString(match.scores[0].score));
    localTemplate = localTemplate.replace(VAR_SCORE_NOSTEM, Integer.toString(match.scores[0].scoreNoStem));
    localTemplate = localTemplate.replace(VAR_SCORE_ADJUSTED, Integer.toString(match.scores[0].adjustedScore));
    localTemplate = localTemplate.replace(VAR_TARGET_TEXT, match.translation);
    localTemplate = localTemplate.replace(VAR_FUZZY_FLAG, match.fuzzyMark ? (OStrings.getString("MATCHES_FUZZY_MARK") + " ") : "");
    ProjectProperties props = Core.getProject().getProjectProperties();
    if (props != null) {
        localTemplate = expandFileNames(localTemplate, match.projs, props.getTMRoot());
    }
    return localTemplate;
}
Also used : NearString(org.omegat.core.matching.NearString) ProjectProperties(org.omegat.core.data.ProjectProperties) Date(java.util.Date)

Aggregations

ProjectProperties (org.omegat.core.data.ProjectProperties)29 File (java.io.File)20 Test (org.junit.Test)11 IOException (java.io.IOException)10 ExecutionException (java.util.concurrent.ExecutionException)6 KnownException (org.omegat.core.KnownException)6 Omegat (gen.core.project.Omegat)4 Cursor (java.awt.Cursor)4 List (java.util.List)4 Language (org.omegat.util.Language)4 ArrayList (java.util.ArrayList)3 NotLoadedProject (org.omegat.core.data.NotLoadedProject)3 ProjectPropertiesDialog (org.omegat.gui.dialogs.ProjectPropertiesDialog)3 RepositoryDefinition (gen.core.project.RepositoryDefinition)2 RepositoryMapping (gen.core.project.RepositoryMapping)2 InputStream (java.io.InputStream)2 Properties (java.util.Properties)2 ZipEntry (java.util.zip.ZipEntry)2 ZipFile (java.util.zip.ZipFile)2 Before (org.junit.Before)2