@Override public void append(TextFragment fragment, boolean collapseIfPreviousEmpty) { append(new Segment(null, fragment), collapseIfPreviousEmpty); }
/** * Sets content of this TextContainer from a string created by {@link #contentToString(TextContainer)}. * @param data the string to process. * @return this TextConatiner. */ public TextContainer setContentFromString (String data) { this.setHasBeenSegmentedFlag((data.charAt(0)=='1')); String[] chunks = data.substring(1).split(PARTSEP2, 0); this.parts.clear(); for ( String chunk : chunks ) { int n1 = chunk.indexOf(PARTSEP1CHAR); int n2 = chunk.indexOf(PARTSEP1CHAR, n1+1); TextFragment tf = new TextFragment(chunk.substring(1, n1), Code.stringToCodes(chunk.substring(n1+1, n2))); this.parts.add((chunk.charAt(0)=='1') ? new Segment(chunk.substring(n2+1), tf) : new TextPart(tf)); } return this; }
@Override public Segment clone () { TextPart part = super.clone(); Segment newSeg = new Segment(id, part.getContent()); newSeg.properties = part.properties; newSeg.annotations = part.annotations; return newSeg; }
/** * Creates a new TextContainer object with optional text. * * @param text the text, or null for not text. */ private void createSingleSegment (String text) { resetParts(); // Note: don't use appendSegment() as it uses createSingleSegment(). Segment seg = new Segment("0", new TextFragment(text)); parts.add(seg); segApplied = false; }
/** * Changes the type of a given part. * If the part was a segment this makes it a non-segment (except if this is the only part * in the content. In that case the part remains unchanged). If this part was not a segment * this makes it a segment (with its identifier automatically set). * * @param partIndex the index of the part to change. Note that even if the part is a segment * this index must be the part index not the segment index. */ public void changePart(int partIndex) { if ( parts.get(partIndex).isSegment() ) { // If it's a segment, make it a non-segment if ( hasOnlyOneSegment() ) { // Except if it's the only segment, to ensure at-least-1-segment return; } parts.set(partIndex, new TextPart(parts.get(partIndex).text)); } else { // If it's a non-segment, make it a segment (with auto-id) Segment seg = new Segment(null, parts.get(partIndex).text); segments.validateSegmentId(seg); parts.set(partIndex, seg); segApplied = true; } }
if ( !parts.get(0).isSegment() ) { parts.set(0, new Segment(null, parts.get(0).text));
TextFragment tf = new TextFragment(chunk.substring(1, n), Code.stringToCodes(codesParts[i])); if ( (chunk.charAt(0)=='1') ) { // It is a segment tc.parts.add(new Segment(chunk.substring(n+1), tf));
seg = new Segment(); seg.id = code.getOuterData().substring(1, code.getOuterData().length()-1);
@Override public Segment getSourceSegment(String segId, boolean createIfNeeded) { Segment seg = getSource().getSegments().get(segId); if ((seg == null) && createIfNeeded) { seg = new Segment(segId); getSource().getSegments().append(seg); } return seg; }
@Override public Segment getTargetSegment(LocaleId trgLoc, String segId, boolean createIfNeeded) { Segment seg = createTarget(trgLoc, false, IResource.COPY_SEGMENTATION).getSegments().get(segId); if (( seg == null ) && createIfNeeded ) { // If the segment does not exists: create a new one if requested seg = new Segment(segId); getTarget(trgLoc).getSegments().append(seg); //TODO consider appending a segment to variant source if present } return seg; }
frag.append(part.getContent()); Segment seg = new Segment(segId, frag); container.getSegments().append(seg);
if ( trgSeg == null ) { trgSeg = new Segment(srcSeg.getId(), new TextFragment()); trgSegs.append(trgSeg);
if ( part.isSegment() ) { String id = part.getId() == null ? Integer.toString(segId) : part.getId(); Segment seg = new Segment(id); tp = seg;
private void insertOrAppend(boolean append, int index, Segment srcSeg, Segment trgSeg, LocaleId trgLoc) { Segment sourceSeg = (srcSeg != null ? srcSeg : new Segment(null, new TextFragment(""))); Segment targetSeg = (trgSeg != null ? trgSeg : new Segment(null, new TextFragment(""))); String originalId = null; String insertedId = null; ContainerIterator ci = new ContainerIterator(trgLoc); if (append) { targetSeg.id = sourceSeg.id; // make sure ids match if (ci.hasSource()) ci.getSource().getSegments().append(sourceSeg); if (ci.hasTarget()) ci.getTarget().getSegments().append(targetSeg); } else { // insert // get id at insertion location originalId = getSource(trgLoc).getSegments().get(index).id; if (ci.hasSource()) insertedId = doInsert(ci.getSource(), index, null, null, sourceSeg); if (ci.hasTarget()) insertedId = doInsert(ci.getTarget(), index, originalId, insertedId, targetSeg); } }
ts = new Segment(srcSeg.id, new TextFragment("")); trgSegs.append(ts);
@Override public Segment splitSource(LocaleId trgLoc, Segment srcSeg, int splitPos) { TextContainer theSource = getSource(trgLoc); ISegments srcSegs = theSource.getSegments(); int segIndex = srcSegs.getIndex(srcSeg.id); if (segIndex == -1) return null; // segment id not found in the container int partIndex = srcSegs.getPartIndex(segIndex); // split the source theSource.split(partIndex, splitPos, splitPos, false); Segment newSeg = srcSegs.get(segIndex + 1); ISegments currentSegs; Segment currentSeg; ContainerIterator ci = new ContainerIterator(trgLoc); // inserting new segments in all the places they go if (ci.hasTarget()) { currentSegs = ci.getTarget().getSegments(); currentSeg = currentSegs.get(srcSeg.id); if (currentSeg != null) currentSegs.insert(currentSegs.getIndex(srcSeg.id) + 1, new Segment(srcSeg.id, new TextFragment(""))); } return newSeg; }
@Override public Segment splitTarget(LocaleId trgLoc, Segment trgSeg, int splitPos) { TextContainer theTarget = parent.createTarget(trgLoc, false, IResource.COPY_SEGMENTATION); ISegments trgSegs = theTarget.getSegments(); int segIndex = trgSegs.getIndex(trgSeg.id); if (segIndex == -1) return null; // segment id not found in the container int partIndex = trgSegs.getPartIndex(segIndex); // split the source theTarget.split(partIndex, splitPos, splitPos, false); Segment newSeg = trgSegs.get(segIndex + 1); ISegments currentSegs; Segment currentSeg; ContainerIterator ci = new ContainerIterator(trgLoc); // inserting new segments in all the places they go if (ci.hasSource()) { currentSegs = ci.getSource().getSegments(); currentSeg = currentSegs.get(trgSeg.id); if (currentSeg != null) currentSegs.insert(currentSegs.getIndex(trgSeg.id) + 1, new Segment(trgSeg.id, new TextFragment(""))); } return newSeg; }
ts = new Segment(srcSeg.id, new TextFragment("")); trgSegs.append(ts); LOGGER.warn("Cannot find matching target segment for source id: {}."