Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.devtools.webtrans.util
Class EnclosingStringChangeSet  view EnclosingStringChangeSet download EnclosingStringChangeSet.java

java.lang.Object
  extended byorg.devtools.webtrans.util.EnclosingStringChangeSet
All Implemented Interfaces:
org.devtools.webtrans.Changes, java.lang.Cloneable, org.devtools.webtrans.StringChanges

public class EnclosingStringChangeSet
extends java.lang.Object
implements org.devtools.webtrans.StringChanges

A list of edits to an integer-indexed String, along with some facilities to merge changes together, or apply them to the document.

This is a newer version of StringChangeSet, which attempts to have a simpler implementation during insertion and deletion ops. I'm attempting to push all of the computational and representational complexity into the apply() method.

It was a goal of the old version of this interface to furnish a way to resolve conflicts between groups of changes. In practice, I think it will make more sense (and be easier) to reduce the probability of conflicts in the first place by furnishing a number of ways to express the types of changes you'd like to make. For example, I'm going to add enclose() as a way of changing the document, which will allow parenthetical ordering to be observed.

Merging another ChangeSet into this one will have the same semantics as if the changes in the Set to be moved in were just added to this ChangeSet one by one, in order.

Enclosures: An enclosure is any parenthetical edit, like converting the text "'''{hello}''' bob" into "hello bob". Edits like these are usually made to SGML-style documents, which are parsed with a stack. As such, it would be terrible if the translators decided to convert "'''{hello}''' bob" into "hello bob" instead. To avoid this, we accept enclosure edits, which give the apply() method the freedom to reorder the beginnings and endings of several coincidental edits so that they may still be stack-parsable. (I'm referring to this as the stack property).


Nested Class Summary
(package private)  class EnclosingStringChangeSet.Enclosure
          An enclosure, which is a pair of text insertions which need to be ordered in a certain way.
 
Field Summary
private  java.lang.StringBuffer appends
          The String which wants to be appended to the document.
private static java.lang.String complexTestDoc
           
private static java.lang.String complexTestSolution
           
private  org.devtools.util.debug.Debugger D
           
private  java.util.Hashtable deletions
          A Hashtable the maps document locations (Integers) to the object MARK.
private  java.util.Hashtable enclosures
          A Hashtable the maps document locations (Integers) to Vectors of Enclosures.
private  org.devtools.util.adt.IntegerFlyweight fly
          A place from whence integers may hail.
private  java.util.Hashtable insertions
          A Hashtable the maps document locations (Integers) to Strings.
private static java.lang.String longTestDoc
           
private static java.lang.Object MARK
           
private  int postTrims
          The number of characters that should be trimmed from the end of the document
private  java.lang.StringBuffer prepends
          The String which wants to be prepended to the document.
private  int preTrims
          The number of characters that should be trimmed from the beginning of the document
private static java.lang.String testSolutionDoc
           
 
Constructor Summary
EnclosingStringChangeSet()
          Constructs a new, empty StringChangeSet.
 
Method Summary
 void append(java.lang.String text)
          Appends the given string to the end of the document.
private  void appendP(java.lang.String text)
           
 java.lang.Object[] apply(java.lang.Object[] document)
          Applies the changes to the given document, returning a copy of the document with the changes applied to it.
 java.lang.String apply(java.lang.String doc)
          Applies the changes to the given document, returning a copy of the document with the changes applied to it.
private  java.util.Hashtable convertEnclosures(int docLen)
          Returns a table of the same form as the insertions table, except that it contains all of the insertions that would be the result of correctly applying all of the enclosures.
 void delete(int start, int len)
          Removes the give number of characters from the given position in the document.
private  void deleteP(java.lang.Integer pos)
           
 void enclose(int start, int len, java.lang.String s, java.lang.String e)
          Encloses the segment of the document starting at start and extending for len characters with the strings s and e.
private  void encloseP(java.lang.Integer pos, EnclosingStringChangeSet.Enclosure enc)
           
 void insert(int location, java.lang.Object[] items)
          Inserts the given list of items into the document at the given position.
 void insert(int start, java.lang.String insertText)
          Inserts a new chunk of text into the document at the given position.
private  void insertP(java.util.Hashtable insertions, java.lang.Integer start, java.lang.String text)
           
static void main(java.lang.String[] args)
           
 void mergeFrom(org.devtools.webtrans.Changes c)
          Merges the changes mentioned in the given ChangeSet, noting any conflicts that occur.
 void mergeFrom(org.devtools.webtrans.StringChanges sc)
          Merges in the given StringChanges.
private  void normalizeEnclosures(int docLen)
          Reorders all of the enclosures which start at the same position (share a Vector at a certain position).
 void posttrim(int eLen)
          Deletes the given number of characters from the end of the document.
private  void posttrimP(int eLen)
           
 void prepend(java.lang.String text)
          Prepends the given string to the beginning of the document.
private  void prependP(java.lang.String text)
           
 void pretrim(int sLen)
          Deletes the given number of characters from the beginning of the document.
private  void pretrimP(int sLen)
           
 void replace(int start, int len, java.lang.Object[] replaceWith)
          Replaces the given region of the document with the given array of objects.
 void replace(int start, int len, java.lang.String replaceWith)
          Replaces the given substring of the document with the given String.
private  void resolveEnclosures(int docLen)
          Removes any Enclosures which could not possibly preserve the stack property.
private static boolean testComplexEnclose()
          Convert the test document to the solution using enclose commands.
private static boolean testMergingEnclose()
          Convert the test document to the solution using only replace commands with no merging.
private static boolean testSimpleReplace()
          Convert the test document to the solution using only replace commands with no merging.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MARK

private static final java.lang.Object MARK

fly

private org.devtools.util.adt.IntegerFlyweight fly
A place from whence integers may hail.


insertions

private java.util.Hashtable insertions
A Hashtable the maps document locations (Integers) to Strings. If there are several strings at a point in the document, they are merged together before being mapped in the Hashtable.


deletions

private java.util.Hashtable deletions
A Hashtable the maps document locations (Integers) to the object MARK.


enclosures

private java.util.Hashtable enclosures
A Hashtable the maps document locations (Integers) to Vectors of Enclosures. Each Enclosure in the Vector represents that an enclosure would like to start at the document position denoted by the key Integer.


appends

private java.lang.StringBuffer appends
The String which wants to be appended to the document.


prepends

private java.lang.StringBuffer prepends
The String which wants to be prepended to the document.


preTrims

private int preTrims
The number of characters that should be trimmed from the beginning of the document


postTrims

private int postTrims
The number of characters that should be trimmed from the end of the document


complexTestDoc

private static java.lang.String complexTestDoc

complexTestSolution

private static java.lang.String complexTestSolution

longTestDoc

private static java.lang.String longTestDoc

testSolutionDoc

private static java.lang.String testSolutionDoc

D

private org.devtools.util.debug.Debugger D
Constructor Detail

EnclosingStringChangeSet

public EnclosingStringChangeSet()
Constructs a new, empty StringChangeSet.

Method Detail

insert

public void insert(int start,
                   java.lang.String insertText)
Inserts a new chunk of text into the document at the given position. null strings are ignored. If an insertion already is at this point, the new insertion goes after it.

Specified by:
insert in interface org.devtools.webtrans.StringChanges

append

public void append(java.lang.String text)
Appends the given string to the end of the document. This append will appear before any append()s that have already been added, but after any subsequent append()s. (Or something.) The idea is to ensure that appends and prepends have the opposite order semantics, so that parenthetical-ness will be preserved. null strings are ignored.

Specified by:
append in interface org.devtools.webtrans.StringChanges

prepend

public void prepend(java.lang.String text)
Prepends the given string to the beginning of the document. This append will appear after any prepend()s that have already been added, but before any subsequent prepend()s. (Or something.) The idea is to ensure that appends and prepends have the opposite order semantics, so that parenthetical-ness will be preserved. null strings are ignored.

Specified by:
prepend in interface org.devtools.webtrans.StringChanges

pretrim

public void pretrim(int sLen)
Deletes the given number of characters from the beginning of the document.


posttrim

public void posttrim(int eLen)
Deletes the given number of characters from the end of the document.


replace

public void replace(int start,
                    int len,
                    java.lang.String replaceWith)
Replaces the given substring of the document with the given String.

Specified by:
replace in interface org.devtools.webtrans.StringChanges

delete

public void delete(int start,
                   int len)
Removes the give number of characters from the given position in the document.

Specified by:
delete in interface org.devtools.webtrans.StringChanges

enclose

public void enclose(int start,
                    int len,
                    java.lang.String s,
                    java.lang.String e)
Encloses the segment of the document starting at start and extending for len characters with the strings s and e. That is, this change inserts s and e at the beginning and end of a segment of the text. You would use this method to express that s and e are parenthetical. If a segment has multiple enclosures, the starting and ending insertions of the enclosure will be ordered such that one of the enclosures is inner and one is outer.


mergeFrom

public void mergeFrom(org.devtools.webtrans.StringChanges sc)
Merges in the given StringChanges. This must be a StringChangeSet in order to work.

Specified by:
mergeFrom in interface org.devtools.webtrans.StringChanges

apply

public java.lang.String apply(java.lang.String doc)
Applies the changes to the given document, returning a copy of the document with the changes applied to it. If conflicts exist, they will be auto-resolved as follows:

In replacement conflicts, the replacement text of the earlier (in time) merge is placed before that of the later merge.

Specified by:
apply in interface org.devtools.webtrans.StringChanges

insertP

private void insertP(java.util.Hashtable insertions,
                     java.lang.Integer start,
                     java.lang.String text)

appendP

private void appendP(java.lang.String text)

prependP

private void prependP(java.lang.String text)

pretrimP

private void pretrimP(int sLen)

posttrimP

private void posttrimP(int eLen)

deleteP

private void deleteP(java.lang.Integer pos)

encloseP

private void encloseP(java.lang.Integer pos,
                      EnclosingStringChangeSet.Enclosure enc)

convertEnclosures

private java.util.Hashtable convertEnclosures(int docLen)
Returns a table of the same form as the insertions table, except that it contains all of the insertions that would be the result of correctly applying all of the enclosures. The returned insertions will correctly preserve the stack property of enclosures according to the strategy requested by the user.


resolveEnclosures

private void resolveEnclosures(int docLen)
Removes any Enclosures which could not possibly preserve the stack property. For example, if one enclosure spands position 3 to 7 and another spans position 5 to 9, these cannot ever be resolved. This implementation actually does nothing at all. But a future implementation might decide to split an enclosure into two. For example, if 1 and 2 are enclosures, the algorithm might convert this: ...1...2.......1...2... into this: ...1..121......1...2... or this: ...1...2......212..2...


normalizeEnclosures

private void normalizeEnclosures(int docLen)
Reorders all of the enclosures which start at the same position (share a Vector at a certain position). For every Enclosure A(x...y) and B(x..z), A is placed before B if yz.


main

public static void main(java.lang.String[] args)

testMergingEnclose

private static boolean testMergingEnclose()
Convert the test document to the solution using only replace commands with no merging.


testSimpleReplace

private static boolean testSimpleReplace()
Convert the test document to the solution using only replace commands with no merging.


testComplexEnclose

private static boolean testComplexEnclose()
Convert the test document to the solution using enclose commands.


insert

public void insert(int location,
                   java.lang.Object[] items)
Inserts the given list of items into the document at the given position.

Specified by:
insert in interface org.devtools.webtrans.Changes

mergeFrom

public void mergeFrom(org.devtools.webtrans.Changes c)
Merges the changes mentioned in the given ChangeSet, noting any conflicts that occur. The conflicts in this ChangeSet can be obtained at any time from the getConflicts method. Merging is done by flattening the two lists of changes into one. However, this may result in conflicts, which can then either be resolved, or the default resolution behavior can be used instead during apply(). The following are conflict conditions:
  • Non-disjoint replacements are conflicts.
  • Enclosures which share the same insertion point for either of their ends are reordered so that, if possible, no enclosure contains only one of the two ends of another enclosure. This is always possible unless two enclosures are both intersecting and neither is a sub-list of the other, in which case this condition is an Enclosure conflict.
  • Replacements that are not disjoint with enclosure ends are considered replace conflicts.
  • Specified by:
    mergeFrom in interface org.devtools.webtrans.Changes

    replace

    public void replace(int start,
                        int len,
                        java.lang.Object[] replaceWith)
    Replaces the given region of the document with the given array of objects. If this replacement overlaps with a previous replacement that was added to this ChangeSet, the two replacements will be consolidated into one (so if conflicts later arise, only one conflict will be shown for the given location.) Whichever replacement begins to the leftmost will be first, unless they have the same beginning and ending, in which case the earlier-added replacement will be first.

    Specified by:
    replace in interface org.devtools.webtrans.Changes

    apply

    public java.lang.Object[] apply(java.lang.Object[] document)
    Applies the changes to the given document, returning a copy of the document with the changes applied to it. If conflicts exist, they will be resolved as follows: Conflict Resolution Rules:
  • In replacement conflicts, the replacement text of the earlier (in time) merge is placed before that of the later merge.
  • Enclosure conflicts are ignored and the enclosures are allowed to overlap.
  • Note that these conflict resolutions should be temporary, so that if conflicts existed in the ChangeSet before the apply(), they will still exist.

    Specified by:
    apply in interface org.devtools.webtrans.Changes