Search in sources :

Example 1 with ProjectTMX

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

the class Searcher method searchProject.

private void searchProject() {
    // reset the number of search hits
    m_numFinds = 0;
    // search the Memory, if requested
    if (m_searchExpression.memory) {
        // search through all project entries
        IProject dataEngine = m_project;
        for (int i = 0; i < m_project.getAllEntries().size(); i++) {
            // stop searching if the max. nr of hits has been reached
            if (m_numFinds >= expression.numberOfResults) {
                return;
            }
            // get the source and translation of the next entry
            SourceTextEntry ste = dataEngine.getAllEntries().get(i);
            TMXEntry te = m_project.getTranslationInfo(ste);
            checkEntry(ste.getSrcText(), te.translation, te.note, ste.getComment(), te, i, null);
            checkStop.checkInterrupted();
        }
        // search in orphaned
        if (!m_searchExpression.excludeOrphans) {
            m_project.iterateByDefaultTranslations(new IProject.DefaultTranslationsIterator() {

                final String file = OStrings.getString("CT_ORPHAN_STRINGS");

                public void iterate(String source, TMXEntry en) {
                    // stop searching if the max. nr of hits has been reached
                    if (m_numFinds >= expression.numberOfResults) {
                        return;
                    }
                    checkStop.checkInterrupted();
                    if (m_project.isOrphaned(source)) {
                        checkEntry(en.source, en.translation, en.note, null, en, ENTRY_ORIGIN_ORPHAN, file);
                    }
                }
            });
            m_project.iterateByMultipleTranslations(new IProject.MultipleTranslationsIterator() {

                final String file = OStrings.getString("CT_ORPHAN_STRINGS");

                public void iterate(EntryKey source, TMXEntry en) {
                    // reached
                    if (m_numFinds >= expression.numberOfResults) {
                        return;
                    }
                    checkStop.checkInterrupted();
                    if (m_project.isOrphaned(source)) {
                        checkEntry(en.source, en.translation, en.note, null, en, ENTRY_ORIGIN_ORPHAN, file);
                    }
                }
            });
        }
    }
    // search the TM, if requested
    if (m_searchExpression.tm) {
        // that case.
        if (!expression.searchAuthor && !expression.searchDateAfter && !expression.searchDateBefore) {
            for (Map.Entry<String, ExternalTMX> tmEn : m_project.getTransMemories().entrySet()) {
                final String fileTM = tmEn.getKey();
                if (!searchEntries(tmEn.getValue().getEntries(), fileTM)) {
                    return;
                }
                checkStop.checkInterrupted();
            }
            for (Map.Entry<Language, ProjectTMX> tmEn : m_project.getOtherTargetLanguageTMs().entrySet()) {
                final Language langTM = tmEn.getKey();
                if (!searchEntriesAlternative(tmEn.getValue().getDefaults(), langTM.getLanguage())) {
                    return;
                }
                if (!searchEntriesAlternative(tmEn.getValue().getAlternatives(), langTM.getLanguage())) {
                    return;
                }
                checkStop.checkInterrupted();
            }
        }
    }
    // search the glossary, if requested
    if (m_searchExpression.glossary) {
        String intro = OStrings.getString("SW_GLOSSARY_RESULT");
        List<GlossaryEntry> entries = Core.getGlossaryManager().getLocalEntries();
        for (GlossaryEntry en : entries) {
            checkEntry(en.getSrcText(), en.getLocText(), null, null, null, ENTRY_ORIGIN_GLOSSARY, intro);
            // stop searching if the max. nr of hits has been reached
            if (m_numFinds >= expression.numberOfResults) {
                return;
            }
            checkStop.checkInterrupted();
        }
    }
}
Also used : ProjectTMX(org.omegat.core.data.ProjectTMX) EntryKey(org.omegat.core.data.EntryKey) IProject(org.omegat.core.data.IProject) Language(org.omegat.util.Language) GlossaryEntry(org.omegat.gui.glossary.GlossaryEntry) SourceTextEntry(org.omegat.core.data.SourceTextEntry) ExternalTMX(org.omegat.core.data.ExternalTMX) HashMap(java.util.HashMap) Map(java.util.Map) PrepareTMXEntry(org.omegat.core.data.PrepareTMXEntry) TMXEntry(org.omegat.core.data.TMXEntry)

Example 2 with ProjectTMX

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

the class EditorController method loadDocument.

/**
 * Displays the {@link Preferences#EDITOR_INITIAL_SEGMENT_LOAD_COUNT}
 * segments surrounding the entry with index {@link #displayedEntryIndex}.
 */
protected void loadDocument() {
    UIThreadsUtil.mustBeSwingThread();
    // Currently displayed file
    IProject.FileInfo file;
    try {
        file = Core.getProject().getProjectFiles().get(displayedFileIndex);
    } catch (IndexOutOfBoundsException ex) {
        // there is no displayedFileIndex file in project - load first file
        file = Core.getProject().getProjectFiles().get(0);
    }
    // remove old segments
    if (m_docSegList != null) {
        markerController.removeAll();
    }
    // check if RTL support required for document
    boolean hasRTL = sourceLangIsRTL || targetLangIsRTL || EditorUtils.localeIsRTL() || currentOrientation != Document3.ORIENTATION.ALL_LTR;
    Map<Language, ProjectTMX> otherLanguageTMs = Core.getProject().getOtherTargetLanguageTMs();
    for (Map.Entry<Language, ProjectTMX> entry : otherLanguageTMs.entrySet()) {
        hasRTL = hasRTL || EditorUtils.isRTL(entry.getKey().getLanguageCode().toLowerCase(Locale.ENGLISH));
    }
    Document3 doc = new Document3(this);
    // Create all SegmentBuilders now...
    ArrayList<SegmentBuilder> tmpSegList = new ArrayList<SegmentBuilder>(file.entries.size());
    for (SourceTextEntry ste : file.entries) {
        if (entriesFilter == null || entriesFilter.allowed(ste)) {
            SegmentBuilder sb = new SegmentBuilder(this, doc, settings, ste, ste.entryNum(), hasRTL);
            tmpSegList.add(sb);
        }
    }
    m_docSegList = tmpSegList.toArray(new SegmentBuilder[tmpSegList.size()]);
    // Clamp displayedSegment to actually available entries.
    displayedEntryIndex = Math.max(0, Math.min(m_docSegList.length - 1, displayedEntryIndex));
    // Calculate start, end indices of a span of initialSegCount segments
    // centered around displayedEntryIndex and clamped to [0, m_docSegList.length).
    final int initialSegCount = Preferences.getPreferenceDefault(Preferences.EDITOR_INITIAL_SEGMENT_LOAD_COUNT, Preferences.EDITOR_INITIAL_SEGMENT_LOAD_COUNT_DEFAULT);
    firstLoaded = Math.max(0, displayedEntryIndex - initialSegCount / 2);
    lastLoaded = Math.min(file.entries.size() - 1, firstLoaded + initialSegCount - 1);
    // ...but only display the ones in [firstLoaded, lastLoaded]
    for (int i = 0; i < m_docSegList.length; i++) {
        if (i >= firstLoaded && i <= lastLoaded) {
            SegmentBuilder sb = m_docSegList[i];
            sb.createSegmentElement(false, Core.getProject().getTranslationInfo(sb.ste));
            sb.addSegmentSeparator();
        }
    }
    doc.setDocumentFilter(new DocumentFilter3());
    // add locate for target language to editor
    Locale targetLocale = Core.getProject().getProjectProperties().getTargetLanguage().getLocale();
    editor.setLocale(targetLocale);
    editor.setDocument(doc);
    doc.addUndoableEditListener(editor.undoManager);
    editor.undoManager.reset();
    doc.addDocumentListener(new DocumentListener() {

        // we cannot edit the document here, only other stuff.
        public void changedUpdate(DocumentEvent e) {
            showLengthMessage();
            onTextChanged();
        }

        public void insertUpdate(DocumentEvent e) {
            showLengthMessage();
            onTextChanged();
        }

        public void removeUpdate(DocumentEvent e) {
            showLengthMessage();
            onTextChanged();
        }
    });
    markerController.process(m_docSegList);
    editor.repaint();
}
Also used : ProjectTMX(org.omegat.core.data.ProjectTMX) Locale(java.util.Locale) DocumentListener(javax.swing.event.DocumentListener) ArrayList(java.util.ArrayList) DocumentEvent(javax.swing.event.DocumentEvent) IProject(org.omegat.core.data.IProject) Point(java.awt.Point) Language(org.omegat.util.Language) SourceTextEntry(org.omegat.core.data.SourceTextEntry) FileInfo(org.omegat.core.data.IProject.FileInfo) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with ProjectTMX

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

the class TeamTool method initTeamProject.

/**
 * Utility function to create a minimal project to serve as a base for a
 * team project. Will add/stage everything if invoked on a path already
 * containing a git working tree or svn checkout.
 *
 * @param dir
 *            Directory in which to create team project
 * @param srcLang
 *            Source language
 * @param trgLang
 *            Target language
 * @param showGui
 *            If true, show the Project Properties dialog
 * @throws Exception
 *             If specified dir is not a directory, is not writeable, etc.
 */
public static void initTeamProject(File dir, String srcLang, String trgLang) throws Exception {
    if (!dir.isDirectory()) {
        throw new IllegalArgumentException("Specified dir is not a directory: " + dir.getPath());
    }
    if (!dir.canWrite()) {
        throw new IOException("Specified dir is not writeable: " + dir.getPath());
    }
    // Create project properties
    ProjectProperties props = new ProjectProperties(dir);
    props.setSourceLanguage(srcLang);
    props.setTargetLanguage(trgLang);
    // Set default tokenizers
    props.setSourceTokenizer(PluginUtils.getTokenizerClassForLanguage(new Language(srcLang)));
    props.setTargetTokenizer(PluginUtils.getTokenizerClassForLanguage(new Language(trgLang)));
    // Create project internal directories
    props.autocreateDirectories();
    // Create version-controlled glossary file
    props.getWritableGlossaryFile().getAsFile().createNewFile();
    ProjectFileStorage.writeProjectFile(props);
    // Create empty project TM
    new ProjectTMX(props.getSourceLanguage(), props.getTargetLanguage(), props.isSentenceSegmentingEnabled(), null, null).save(props, new File(props.getProjectInternal(), OConsts.STATUS_EXTENSION).getPath(), false);
    // and set EOL handling correctly for cross-platform work
    if (new File(dir, ".svn").isDirectory()) {
        SVNClientManager mgr = SVNClientManager.newInstance();
        mgr.getWCClient().doSetProperty(dir, "svn:auto-props", SVNPropertyValue.create("*.txt = svn:eol-style=native\n*.tmx = svn:eol-style=native\n"), false, SVNDepth.EMPTY, null, null);
        mgr.getWCClient().doAdd(dir.listFiles(f -> !f.getName().startsWith(".")), false, false, true, SVNDepth.fromRecurse(true), false, false, false, true);
    } else if (new File(dir, ".git").isDirectory()) {
        try (BufferedWriter writer = Files.newBufferedWriter(new File(dir, ".gitattributes").toPath())) {
            writer.write("* text=auto\n");
            writer.write("*.tmx text\n");
            writer.write("*.txt text\n");
        }
        Git.open(dir).add().addFilepattern(".").call();
    }
    System.out.println(StringUtil.format(OStrings.getString("TEAM_TOOL_INIT_COMPLETE"), srcLang, trgLang));
}
Also used : ProjectTMX(org.omegat.core.data.ProjectTMX) OConsts(org.omegat.util.OConsts) Arrays(java.util.Arrays) ProjectProperties(org.omegat.core.data.ProjectProperties) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) CLIParameters(org.omegat.CLIParameters) Log(org.omegat.util.Log) SVNDepth(org.tmatesoft.svn.core.SVNDepth) IOException(java.io.IOException) Language(org.omegat.util.Language) PluginUtils(org.omegat.filters2.master.PluginUtils) SVNClientManager(org.tmatesoft.svn.core.wc.SVNClientManager) File(java.io.File) Level(java.util.logging.Level) StringUtil(org.omegat.util.StringUtil) ProjectTMX(org.omegat.core.data.ProjectTMX) OStrings(org.omegat.util.OStrings) SVNPropertyValue(org.tmatesoft.svn.core.SVNPropertyValue) ProjectFileStorage(org.omegat.util.ProjectFileStorage) Git(org.eclipse.jgit.api.Git) Collections(java.util.Collections) Preferences(org.omegat.util.Preferences) Language(org.omegat.util.Language) IOException(java.io.IOException) ProjectProperties(org.omegat.core.data.ProjectProperties) File(java.io.File) SVNClientManager(org.tmatesoft.svn.core.wc.SVNClientManager) BufferedWriter(java.io.BufferedWriter)

Example 4 with ProjectTMX

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

the class SegmentBuilder method createActiveSegmentElement.

/**
 * Create active segment for given entry
 */
private void createActiveSegmentElement(TMXEntry trans) throws BadLocationException {
    try {
        if (EditorSettings.DISPLAY_MODIFICATION_INFO_ALL.equals(settings.getDisplayModificationInfo()) || EditorSettings.DISPLAY_MODIFICATION_INFO_SELECTED.equals(settings.getDisplayModificationInfo())) {
            addModificationInfoPart(trans);
        }
        int prevOffset = offset;
        sourceText = addInactiveSegPart(true, ste.getSrcText());
        Map<Language, ProjectTMX> otherLanguageTMs = Core.getProject().getOtherTargetLanguageTMs();
        for (Map.Entry<Language, ProjectTMX> entry : otherLanguageTMs.entrySet()) {
            TMXEntry altTrans = entry.getValue().getDefaultTranslation(ste.getSrcText());
            if (altTrans != null && altTrans.isTranslated()) {
                Language language = entry.getKey();
                addOtherLanguagePart(altTrans.translation, language);
            }
        }
        posSourceBeg = doc.createPosition(prevOffset + (hasRTL ? 1 : 0));
        posSourceLength = sourceText.length();
        if (trans.isTranslated()) {
            // translation exist
            translationText = trans.translation;
        } else {
            boolean insertSource = !Preferences.isPreference(Preferences.DONT_INSERT_SOURCE_TEXT);
            if (controller.entriesFilter != null && controller.entriesFilter.isSourceAsEmptyTranslation()) {
                insertSource = true;
            }
            if (insertSource) {
                // need to insert source text on empty translation
                String srcText = ste.getSrcText();
                if (Preferences.isPreference(Preferences.GLOSSARY_REPLACE_ON_INSERT)) {
                    srcText = EditorUtils.replaceGlossaryEntries(srcText);
                }
                translationText = srcText;
            } else {
                // empty text on non-exist translation
                translationText = "";
            }
        }
        translationText = addActiveSegPart(translationText);
        posTranslationBeg = null;
        doc.activeTranslationBeginM1 = doc.createPosition(activeTranslationBeginOffset - 1);
        doc.activeTranslationEndP1 = doc.createPosition(activeTranslationEndOffset + 1);
    } catch (OutOfMemoryError oome) {
        // Oh shit, we're all out of storage space!
        // Of course we should've cleaned up after ourselves earlier,
        // but since we didn't, do a bit of cleaning up now, otherwise
        // we can't even inform the user about our slacking off.
        doc.remove(0, doc.getLength());
        // Well, that cleared up some, GC to the rescue!
        System.gc();
        // There, that should do it, now inform the user
        long memory = Runtime.getRuntime().maxMemory() / 1024 / 1024;
        Log.logErrorRB("OUT_OF_MEMORY", memory);
        Log.log(oome);
        Core.getMainWindow().showErrorDialogRB("TF_ERROR", "OUT_OF_MEMORY", memory);
        // Just quit, we can't help it anyway
        System.exit(0);
    }
}
Also used : ProjectTMX(org.omegat.core.data.ProjectTMX) Language(org.omegat.util.Language) Map(java.util.Map) TMXEntry(org.omegat.core.data.TMXEntry)

Aggregations

ProjectTMX (org.omegat.core.data.ProjectTMX)4 Language (org.omegat.util.Language)4 Map (java.util.Map)3 IProject (org.omegat.core.data.IProject)2 SourceTextEntry (org.omegat.core.data.SourceTextEntry)2 TMXEntry (org.omegat.core.data.TMXEntry)2 Point (java.awt.Point)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Locale (java.util.Locale)1 Level (java.util.logging.Level)1 DocumentEvent (javax.swing.event.DocumentEvent)1 DocumentListener (javax.swing.event.DocumentListener)1