/** * Gets the coded text of the first part of the source of a given text unit resource. * * @param textUnit * the text unit resource which source text should be returned. * @return the source part of the given text unit resource. */ public static String getSourceText (ITextUnit textUnit) { // if ( textUnit == null ) return ""; // return getCodedText(textUnit.getSourceContent()); return textUnit.getSource().getFirstContent().getCodedText(); }
/** * Sets the coded text of the un-segmented source of a given text unit resource. * * @param textUnit * the given text unit resource. * @param text * the text to be set. */ public static void setSourceText (ITextUnit textUnit, String text) { TextFragment source = textUnit.getSource().getFirstContent(); source.setCodedText(text); }
/** * Sets the coded text of the the target part of a given text unit resource in a given language. * * @param textUnit * the given text unit resource. * @param locId * the locale of the target part being set. * @param text * the text to be set. */ public static void setTargetText (ITextUnit textUnit, LocaleId locId, String text) { TextFragment target = textUnit.getTarget(locId).getFirstContent(); target.setCodedText(text); }
private void procEndElemAddToTuContent(EndElement endElement, ITextUnit tu) { StringBuilder sb = new StringBuilder(); sb.append("</" + endElement.getName().getLocalPart() + ">"); if (ts.currentMessageLocation == MessageLocation.SOURCE) { TextContainer tc = tu.getSource(); if (!tc.hasText()) { skel.addContentPlaceholder(tu); } tc.getFirstContent().append(sb.toString()); } else if (ts.currentMessageLocation == MessageLocation.TARGET) { TextContainer tc = tu.getTarget(trgLang); if (!tc.hasText()) { skel.addContentPlaceholder(tu, trgLang); } tc.getFirstContent().append(sb.toString()); } }
/** * Gets the coded text of the first part of a source part of a given text unit resource. * If removeCodes = false, and the text contains inline codes, * then the codes will be removed. * * @param textUnit * the text unit resource which source text should be returned. * @param removeCodes * true if possible inline codes should be removed. * @return the source part of the given text unit resource. */ public static String getSourceText (ITextUnit textUnit, boolean removeCodes) { if (textUnit == null) return ""; if (removeCodes) { return getText(textUnit.getSource().getFirstContent()); } else { return textUnit.getSource().getFirstContent().getCodedText(); } }
@Override public TextFragment setSourceContent (TextFragment content) { getSource().setContent(content); return getSource().getFirstContent(); }
private void writeQuotedContent (TextContainer tc) { try { String tmp; if ( tc.contentIsOneSegment() ) { tmp = GenericContent.fromFragmentToLetterCoded(tc.getFirstContent(), true); } else { // If the container is segmented tmp = GenericContent.fromFragmentToLetterCoded(tc.getUnSegmentedContentCopy(), true); } tmp = escapeIfNeeded(tmp); writer.write("\""); writer.write(tmp); // No wrapping needed writer.write("\""); } catch ( IOException e ) { throw new OkapiIOException("Error writing a quoted text.", e); } }
/** * Converts an un-segmented TextUnit into a TranslationUnit. The target may not exist, in that case * its content is stored as a null. * @param sourceLoc the source locale to transform. * @param targetLoc the target locale to transform. * @param textUnit the text unit to convert. * @return a TranslationUnit that represents the TextUnit */ public static TranslationUnit convertToTranslationUnit (LocaleId sourceLoc, LocaleId targetLoc, ITextUnit textUnit) { TranslationUnitVariant source = new TranslationUnitVariant(sourceLoc, textUnit.getSource().getFirstContent()); TextContainer tc = textUnit.getTarget(targetLoc); // Allow null target content TranslationUnitVariant target = new TranslationUnitVariant(targetLoc, (( tc==null ) ? null : tc.getFirstContent())); TranslationUnit tu = new TranslationUnit(source, target); populateMetaDataFromProperties(textUnit, tu); return tu; }
public void endCode(String tag) { Code c = null; try { c = codeStack.pop(); } catch (EmptyStackException e) { throw new OkapiIllegalFilterOperationException( "Trying to end a Code that does not exist. Did you call startCode?", e); } c.appendOuterData(tag); ITextUnit tu = peekMostRecentTextUnit(); if (tu == null) return; // We can use the first part as nothing is segment at this point tu.getSource().getFirstContent().append(c); }
/** * Gets text of the first part of the target of a given text unit resource in the given locale. * * @param textUnit * the text unit resource which source text should be returned. * @param locId * the locale the target part being sought. * @return the target part of the given text unit resource in the given loacle, or an empty string if the text unit * doesn't contain one. */ public static String getTargetText (ITextUnit textUnit, LocaleId locId) { if (textUnit == null) return ""; if (Util.isNullOrEmpty(locId)) return ""; return getCodedText(textUnit.getTarget(locId).getFirstContent()); }
@Override protected TextProcessingResult component_exec(TextContainer lineContainer) { if (lineContainer == null) return super.component_exec(lineContainer); if (splicedLines == null) return super.component_exec(lineContainer); //if (TextUnitUtil.getLastChar(lineContainer) == params.splicer) { // We can use getFirstPartContent() because nothing is segmented if (TextUnitUtil.endsWith(lineContainer.getFirstContent(), params.splicer)) { merging = true; splicedLines.add(lineContainer); return TextProcessingResult.DELAYED_DECISION; } else { if (merging) { merging = false; splicedLines.add(lineContainer); return (mergeLines()) ? TextProcessingResult.ACCEPTED : TextProcessingResult.REJECTED; } return super.component_exec(lineContainer); // Plain text filter's line processing } }
private void procCharacters(Characters chars, ITextUnit tu) { // --setting translator comment-- if (ts.procTransComment) { tu.setProperty(new Property(Property.TRANSNOTE, chars.getData())); } if (ts.currentMessageLocation == MessageLocation.RESOURCE) { procCharacters(chars); } else if (ts.currentMessageLocation == MessageLocation.SOURCE) { TextContainer tc = tu.getSource(); if (!tc.hasText()) { skel.addContentPlaceholder(tu); } // We can use getFirstPartContent() because nothing is segmented tc.getFirstContent().append(chars.getData()); } else if (ts.currentMessageLocation == MessageLocation.TARGET) { TextContainer tc = tu.getTarget(trgLang); if (!tc.hasText()) { skel.addContentPlaceholder(tu, trgLang); } // We can use getFirstPartContent() because nothing is segmented tc.getFirstContent().append(chars.getData()); } }
public void endCode() { Code c = null; try { c = codeStack.pop(); } catch (EmptyStackException e) { throw new OkapiIllegalFilterOperationException( "Trying to end a Code that does not exist. Did you call startCode?", e); } ITextUnit tu = peekMostRecentTextUnit(); if (tu == null) return; // We can use the first part as nothing is segment at this point LocaleId trgLoc = getTargetLocale(); if (trgLoc == null) { tu.getSource().getFirstContent().append(c); } else { if (tu.getTarget(trgLoc) == null) { tu.createTarget(trgLoc, true, ITextUnit.CREATE_EMPTY); } tu.getTarget(trgLoc).getFirstContent().append(c); } }
if (params.unescapeSource) _unescape(source.getFirstContent()); codeFinder.process(source.getFirstContent());
/** * Adds text to the current {@link TextUnit} * * @param text * the text * * @throws OkapiIllegalFilterOperationException if we try to perform an invalid operation. */ public void addToTextUnit(String text) { if (!isCurrentTextUnit()) { throw new OkapiIllegalFilterOperationException("Trying to add text to a TextUnit that does not exist."); } ITextUnit tu = peekMostRecentTextUnit(); if (tu == null) return; // We can use the first part as nothing is segment at this point LocaleId trgLoc = getTargetLocale(); if (trgLoc == null) { tu.getSource().getFirstContent().append(text); } else { if (tu.getTarget(trgLoc) == null) { tu.createTarget(trgLoc, true, ITextUnit.CREATE_EMPTY); } tu.getTarget(trgLoc).getFirstContent().append(text); } }
@Override protected ITextUnit postProcessTextUnit(ITextUnit textUnit) { TextFragment text = textUnit.getSource().getFirstContent(); text.setCodedText(text.getCodedText()); if ( codeFinder != null ) { encoderManager.updateEncoder(textUnit.getMimeType()); codeFinder.process(text); } text.setCodedText(text.getCodedText()); return textUnit; }
public Event next() { // Change the mime type by regex filter ("text/x-regex") to "text/plain" Event event = regex.next(); if (event == null) return event; // Returns null IResource res = event.getResource(); if (res == null) return event; // Do not change event if (event.getEventType() == EventType.TEXT_UNIT) { ITextUnit textUnit = event.getTextUnit(); // Change mime type textUnit.setMimeType(this.getMimeType()); // Lines are what the regex considers the lines, so line numbering is actually TU numbering ((ITextUnit)res).setSourceProperty(new Property(AbstractLineFilter.LINE_NUMBER, String.valueOf(++lineNumber), true)); // Automatically replace text fragments with in-line codes (based on regex rules of codeFinder) if (params.useCodeFinder && codeFinder != null) { // We can use getFirstPartContent() because nothing is segmented yet TextContainer source = textUnit.getSource(); if (source == null) return event; codeFinder.process(source.getFirstContent()); } } return event; }
@SuppressWarnings("resource") protected List<Event> callSubfilter(ITextUnit parent) { String parentId = parent.getId(); if (Util.isEmpty(parentId)) { parentId = idGenerator.getLastId(); } // force creation of the parent encoder SubFilter sf = new SubFilter(subFilter, getEncoderManager().getEncoder(), ++subfilterIndex, parentId, parent.getName()); // RawDocument closed inside the subfilter call return sf.getEvents(new RawDocument(parent.getSource().getFirstContent().getText(), srcLang)); }
@Override protected ITextUnit postProcessTextUnit (ITextUnit textUnit) { // We can use getFirstPartContent() because nothing is segmented TextFragment text = textUnit.getSource().getFirstContent(); // Treat the white spaces text.setCodedText(normalizeHtmlText(text.getCodedText(), false, textUnit.preserveWhitespaces())); // Apply the in-line codes rules if needed if ( useCodeFinder ) { encoderManager.setDefaultOptions(null, encoding, lineBreak); encoderManager.updateEncoder(textUnit.getMimeType()); // IEncoder encoder = encoderManager.getEncoder(); codeFinder.process(text); // Escape inline code content //TODO: This must be put back to fix issue 431 // but the encoder is null in several test cases // List<Code> codes = text.getCodes(); // for ( Code code : codes ) { // // Escape the data of the new inline code (and only them) // if ( code.getType().equals(InlineCodeFinder.TAGTYPE) ) { // //code.setData(encoder.encode(code.getData(), EncoderContext.SKELETON)); // code.setData(encoder.encode(code.getData(), EncoderContext.INLINE)); // } // } } return textUnit; }
@Override protected ITextUnit postProcessTextUnit(ITextUnit textUnit) { TextFragment text = textUnit.getSource().getFirstContent(); String unescaped = unescape(text); text.setCodedText(unescaped); if (codeFinder != null) { encoderManager.updateEncoder(textUnit.getMimeType()); codeFinder.process(text); // Pre-emptively re-encode anything we parsed out, since it won't happen otherwise for (Code code : text.getCodes()) { code.setData(encoderManager.encode(code.getData(), EncoderContext.TEXT)); String codeDisplayText = code.getDisplayText(); if (codeDisplayText != null) { code.setDisplayText(encoderManager.encode(codeDisplayText, EncoderContext.TEXT)); } } } return textUnit; }