/** * Creates a TextFragment with the content made of a given coded text * and a list of codes. * @param newCodedText the new coded text. * @param newCodes the list of codes. */ public TextFragment (String newCodedText, List<Code> newCodes) { setCodedText(newCodedText, newCodes, false); }
/** * Sets the coded text of the fragment and its corresponding codes. * @param newCodedText the coded text to apply. * @param newCodes the list of the corresponding codes. * @throws InvalidContentException when the coded text is not valid or does * not correspond to the new codes. */ public void setCodedText (String newCodedText, List<Code> newCodes) { setCodedText(newCodedText, newCodes, false); }
/** * Sets the coded text of the fragment, using its the existing codes. The coded * text must be valid for the existing codes. * @param newCodedText the coded text to apply. * @throws InvalidContentException when the coded text is not valid, or does * not correspond to the existing codes. */ public void setCodedText (String newCodedText) { setCodedText(newCodedText, codes, false); }
/** * Sets the coded text of the fragment, using its the existing codes. The coded * text must be valid for the existing codes. * @param newCodedText The coded text to apply. * @param allowCodeDeletion True when missing in-line codes in the coded text * means the corresponding codes should be deleted from the fragment. * @throws InvalidContentException When the coded text is not valid, or does * not correspond to the existing codes. */ public void setCodedText (String newCodedText, boolean allowCodeDeletion) { setCodedText(newCodedText, codes, allowCodeDeletion); }
private void processFragment (TextFragment frag) { String text = frag.getCodedText(); String result = changeWidth(text, params); // Record if we modified something wasModified = !text.equals(result); // Set back the modified text frag.setCodedText(result); }
/** * Removes all inline tags from the given {@link TextFragment} * @param tf the given text fragment */ public static void removeCodes(TextFragment tf) { StringBuilder tmp = new StringBuilder(); StringBuilder text = new StringBuilder(tf.getText()); for (int i=0; i<text.length(); i++) { switch (text.charAt(i)) { case TextFragment.MARKER_OPENING: case TextFragment.MARKER_CLOSING: case TextFragment.MARKER_ISOLATED: i++; // skip index marker as well break; default: tmp.append(text.charAt(i)); break; } } tf.clear(); tf.setCodedText(tmp.toString()); }
/** * Creates a TextFragment with the content of a given TextFragment. * @param fragment the content to use. */ public TextFragment (TextFragment fragment) { this.setCodedText(fragment.getCodedText(), fragment.getCodes(), false); this.lastCodeID = fragment.lastCodeID; }
/** * 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); }
@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 boolean applyChanges () { try { if ( !modified ) return true; cacheContent(null); if ( textFrag != null ) { textFrag.setCodedText(codedText, codes, true); } else { createContainerFromFragment(); } modified = false; } catch ( Throwable e ) { Dialogs.showError(edit.getShell(), "Error when applying changes.\n"+e.getLocalizedMessage(), null); edit.setFocus(); return false; } return true; }
if (pos == -1) { // Whole string is whitespaces skelTF = new TextFragment(st); textFragment.setCodedText(""); } else { skelTF = textFragment.subSequence(0, pos); textFragment.setCodedText(st.substring(pos));
public void simplifyOpeningClosing (TextFragment tf) { prepare(tf.getCodedText(), tf.getCodes()); simplifyOpeningClosing(); tf.setCodedText(getCodedText(), getCodes()); }
/** * Simplifies the place-holders in a given text fragment. * @param tf the text fragment to modify. */ public void simplifyIsolated (TextFragment tf) { prepare(tf.getCodedText(), tf.getCodes()); simplifyIsolated(); tf.setCodedText(getCodedText(), getCodes()); }
public void simplifyEmptyOpeningClosing (TextFragment tf) { prepare(tf.getCodedText(), tf.getCodes()); simplifyEmptyOpeningClosing(); tf.setCodedText(getCodedText(), getCodes()); }
@Override protected Event handleTextUnit (Event event) { ITextUnit tu = event.getTextUnit(); // Skip non-translatable if ( !tu.isTranslatable() ) { return event; } // Else: do the requested modifications // Make sure we have a target where to set data TextContainer cont = tu.createTarget(trgLang, false, IResource.COPY_ALL); String res; try { for ( TextPart part : cont ) { if ( params.getConversionType() == Parameters.UNESCAPE ) { res = unescape(part.text.getCodedText()); } else { res = escape(part.text.getCodedText()); } part.text.setCodedText(res); // No change of the inline codes } } catch ( Exception e ) { logger.error("Error when updating content: '{}'", cont.toString(), e); } return event; }
/** * Creates a {@link TranslationUnit} for a given document. * * @param doc * the document from which to create the new translation unit. * @param srcCodedText * the source coded text to re-use. * @param srcCodes * the source codes to re-use. * @return a new translation unit for the given document. */ private TranslationUnit createTranslationUnit(Document doc, String srcCodedText, List<Code> srcCodes) { TextFragment frag = new TextFragment(); frag.setCodedText(srcCodedText, srcCodes, false); TranslationUnitVariant srcTuv = new TranslationUnitVariant(getLocaleValue(doc, TranslationUnitField.SOURCE_LANG), frag); frag = new TextFragment(); List<Code> codes = Code .stringToCodes(getFieldValue(doc, TranslationUnitField.TARGET_CODES)); String codedText = getFieldValue(doc, TranslationUnitField.TARGET); frag.setCodedText(codedText == null ? "" : codedText, codes, false); TranslationUnitVariant trgTuv = new TranslationUnitVariant(getLocaleValue(doc, TranslationUnitField.TARGET_LANG), frag); TranslationUnit tu = new TranslationUnit(srcTuv, trgTuv); for (MetadataType type : MetadataType.values()) { tu.setMetadataValue(type, getFieldValue(doc, type)); } return tu; }
private TranslationUnit createTranslationUnit(Document doc) { TextFragment frag = new TextFragment(); List<Code> codes = Code .stringToCodes(getFieldValue(doc, TranslationUnitField.SOURCE_CODES)); frag.setCodedText(getFieldValue(doc, TranslationUnitField.SOURCE_EXACT), codes, false); TranslationUnitVariant srcTuv = new TranslationUnitVariant(getLocaleValue(doc, TranslationUnitField.SOURCE_LANG), frag); frag = new TextFragment(); codes = Code.stringToCodes(getFieldValue(doc, TranslationUnitField.TARGET_CODES)); String codedText = getFieldValue(doc, TranslationUnitField.TARGET); frag.setCodedText(codedText == null ? "" : codedText, codes, false); TranslationUnitVariant trgTuv = new TranslationUnitVariant(getLocaleValue(doc, TranslationUnitField.TARGET_LANG), frag); TranslationUnit tu = new TranslationUnit(srcTuv, trgTuv); for (MetadataType type : MetadataType.values()) { tu.setMetadataValue(type, getFieldValue(doc, type)); } return tu; }
@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; }