use of org.omegat.core.data.ProtectedPart in project omegat by omegat-org.
the class Handler method translateButDontFlash.
/**
* One of the main methods of the XML filter: it collects all the data,
* adjusts it, and sends for translation.
*
* @see #translateAndFlush()
*/
private void translateButDontFlash() throws TranslationException {
if (currEntry().isEmpty()) {
return;
}
boolean isTranslated = true;
List<ProtectedPart> shortcutDetails = new ArrayList<ProtectedPart>();
boolean tagsAggregation = isTagsAggregationEnabled();
String src = currEntry().sourceToShortcut(tagsAggregation, dialect, shortcutDetails);
Element lead = currEntry().get(0);
String translation = src;
if ((lead instanceof Tag) && (isPreformattingTag(((Tag) lead).getTag(), ((Tag) lead).getAttributes()) || isSpacePreservingTag()) && isTranslatableTag() && !StringUtil.isEmpty(src)) {
resetSpacePreservingTag();
translation = translator.translate(src, shortcutDetails);
} else {
String compressed = src;
if (Core.getFilterMaster().getConfig().isRemoveSpacesNonseg()) {
compressed = StringUtil.compressSpaces(src);
}
if (isTranslatableTag()) {
translation = translator.translate(compressed, shortcutDetails);
}
// untranslated is written out uncompressed
if (compressed.equals(translation)) {
translation = src;
isTranslated = false;
}
}
if (lead instanceof XMLTag) {
dialect.handleXMLTag((XMLTag) lead, isTranslated);
}
currEntry().setTranslation(translation, dialect, new ArrayList<ProtectedPart>());
}
use of org.omegat.core.data.ProtectedPart in project omegat by omegat-org.
the class ProtectedPartsMarker method getMarksForEntry.
@Override
public List<Mark> getMarksForEntry(SourceTextEntry ste, String sourceText, String translationText, boolean isActive) throws Exception {
HighlightPainter painter;
AttributeSet attrs;
if (((EditorController) Core.getEditor()).getOrientation() == Document3.ORIENTATION.ALL_LTR) {
attrs = ATTRIBUTES_LTR;
painter = null;
} else {
attrs = null;
painter = PAINTER_RTL;
}
if (ste.getProtectedParts().length == 0) {
return null;
}
if (sourceText == null && translationText == null) {
return null;
}
List<Mark> r = new ArrayList<Mark>();
// find protected parts
for (ProtectedPart pp : ste.getProtectedParts()) {
if (sourceText != null) {
int pos = -1;
while ((pos = sourceText.indexOf(pp.getTextInSourceSegment(), pos + 1)) >= 0) {
Mark m = new Mark(Mark.ENTRY_PART.SOURCE, pos, pos + pp.getTextInSourceSegment().length());
m.painter = painter;
m.attributes = attrs;
m.toolTipText = escapeHtml(pp.getDetailsFromSourceFile());
r.add(m);
}
}
if (translationText != null) {
int pos = -1;
while ((pos = translationText.indexOf(pp.getTextInSourceSegment(), pos + 1)) >= 0) {
Mark m = new Mark(Mark.ENTRY_PART.TRANSLATION, pos, pos + pp.getTextInSourceSegment().length());
m.painter = painter;
m.attributes = attrs;
m.toolTipText = escapeHtml(pp.getDetailsFromSourceFile());
r.add(m);
}
}
}
return r;
}
use of org.omegat.core.data.ProtectedPart in project omegat by omegat-org.
the class CalcStandardStatistics method buildProjectStats.
/**
* Builds a file with statistic info about the project. The total word &
* character count of the project, the total number of unique segments, plus
* the details for each file.
*/
public static StatsResult buildProjectStats(final IProject project) {
StatCount total = new StatCount();
StatCount remaining = new StatCount();
StatCount unique = new StatCount();
StatCount remainingUnique = new StatCount();
// find unique segments
Map<String, SourceTextEntry> uniqueSegment = new HashMap<String, SourceTextEntry>();
Set<String> translated = new HashSet<String>();
for (SourceTextEntry ste : project.getAllEntries()) {
String src = ste.getSrcText();
for (ProtectedPart pp : ste.getProtectedParts()) {
src = src.replace(pp.getTextInSourceSegment(), pp.getReplacementUniquenessCalculation());
}
if (!uniqueSegment.containsKey(src)) {
uniqueSegment.put(src, ste);
}
TMXEntry tr = project.getTranslationInfo(ste);
if (tr.isTranslated()) {
translated.add(src);
}
}
Set<String> filesUnique = new HashSet<String>();
Set<String> filesRemainingUnique = new HashSet<String>();
for (Map.Entry<String, SourceTextEntry> en : uniqueSegment.entrySet()) {
/* Number of words and chars calculated without all tags and protected parts. */
StatCount count = new StatCount(en.getValue());
// add to unique
unique.add(count);
filesUnique.add(en.getValue().getKey().file);
// add to unique remaining
if (!translated.contains(en.getKey())) {
remainingUnique.add(count);
filesRemainingUnique.add(en.getValue().getKey().file);
}
}
unique.addFiles(filesUnique.size());
remainingUnique.addFiles(filesRemainingUnique.size());
List<FileData> counts = new ArrayList<FileData>();
Map<String, Boolean> firstSeenUniqueSegment = new HashMap<String, Boolean>();
for (FileInfo file : project.getProjectFiles()) {
FileData numbers = new FileData();
numbers.filename = file.filePath;
counts.add(numbers);
int fileTotal = 0;
int fileRemaining = 0;
for (SourceTextEntry ste : file.entries) {
String src = ste.getSrcText();
for (ProtectedPart pp : ste.getProtectedParts()) {
src = src.replace(pp.getTextInSourceSegment(), pp.getReplacementUniquenessCalculation());
}
/* Number of words and chars calculated without all tags and protected parts. */
StatCount count = new StatCount(ste);
// add to total
total.add(count);
fileTotal = 1;
// add to remaining
TMXEntry tr = project.getTranslationInfo(ste);
if (!tr.isTranslated()) {
remaining.add(count);
fileRemaining = 1;
}
// add to file's info
numbers.total.add(count);
Boolean firstSeen = firstSeenUniqueSegment.get(src);
if (firstSeen == null) {
firstSeenUniqueSegment.put(src, false);
numbers.unique.add(count);
if (!tr.isTranslated()) {
numbers.remainingUnique.add(count);
}
}
if (!tr.isTranslated()) {
numbers.remaining.add(count);
}
}
total.addFiles(fileTotal);
remaining.addFiles(fileRemaining);
}
return new StatsResult(total, remaining, unique, remainingUnique, translated, counts);
}
use of org.omegat.core.data.ProtectedPart in project omegat by omegat-org.
the class EditorTextArea3 method wholeTagDelete.
/**
* Whole tag delete before or after cursor
*
* @param checkTagStart
* true if check tag start, false if check tag end
* @return true if tag deleted
*/
boolean wholeTagDelete(boolean checkTagStart) throws BadLocationException {
Document3 doc = getOmDocument();
SourceTextEntry ste = doc.controller.getCurrentEntry();
String text = doc.extractTranslation();
int off = getCaretPosition() - doc.getTranslationStart();
// iterate by 'protected parts'
if (ste != null) {
for (ProtectedPart pp : ste.getProtectedParts()) {
if (checkTagStart) {
if (StringUtil.isSubstringAfter(text, off, pp.getTextInSourceSegment())) {
int pos = off + doc.getTranslationStart();
doc.remove(pos, pp.getTextInSourceSegment().length());
return true;
}
} else {
if (StringUtil.isSubstringBefore(text, off, pp.getTextInSourceSegment())) {
int pos = off + doc.getTranslationStart() - pp.getTextInSourceSegment().length();
doc.remove(pos, pp.getTextInSourceSegment().length());
return true;
}
}
}
}
return false;
}
use of org.omegat.core.data.ProtectedPart in project omegat by omegat-org.
the class TagUtil method buildTagList.
/**
* Builds a list of format tags within the supplied string. Format tags are
* 'protected parts' and OmegaT style tags: <xx02> or </yy01>.
*/
public static List<Tag> buildTagList(String str, ProtectedPart[] protectedParts) {
if (protectedParts == null || protectedParts.length == 0) {
return Collections.emptyList();
}
List<Tag> tags = new ArrayList<Tag>();
// Put string in temporary buffer and replace tags with spaces as we find them.
// This ensures that we don't find identical tags multiple times unless they are
// actually present multiple times.
StringBuilder sb = new StringBuilder(str);
while (true) {
boolean loopAgain = false;
for (ProtectedPart pp : protectedParts) {
int pos = -1;
if ((pos = sb.indexOf(pp.getTextInSourceSegment())) != -1) {
tags.add(new Tag(pos, pp.getTextInSourceSegment()));
replaceWith(sb, pos, pos + pp.getTextInSourceSegment().length(), TEXT_REPLACEMENT);
loopAgain = true;
}
}
if (!loopAgain) {
break;
}
}
Collections.sort(tags, TAG_COMPARATOR);
return tags;
}
Aggregations