001package votorola.s.gwt.mediawiki; // Copyright 2012, Michael Allan.  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE.
002
003import com.google.gwt.core.client.*;
004import com.google.gwt.dom.client.*;
005import java.util.*;
006import votorola.a.diff.*;
007import votorola.s.gwt.stage.*;
008
009
010/** A parse of a textual difference between two position drafts.
011  */
012final class DifferenceParse
013{
014
015
016    /** The next character to be read in the parse of diffText.
017      */
018    int d;
019
020
021
022    /** The current difference being marked up.
023      */
024    ShadowedDiff diff;
025
026
027        /** Sets the current difference being marked up.
028          */
029        void setDiff( ShadowedDiff _diff )
030        {
031            diff = _diff;
032            diffText = diff.text();
033            d = 0;
034            mismatchedSegLocs.clear();
035        }
036
037
038
039    /** The {@linkplain ShadowedDiff#text() diff text}, de-referenced as a convenience.
040      */
041    String diffText;
042
043
044
045    /** The text of the document in which to search for difference segments.
046      */
047    final StringBuilder docC = new StringBuilder( /*initial capacity*/2000 );
048
049
050
051    /** Text nodes in document order.  Each has an extended property "offset" that
052      * specifies its offset in docC.
053      */
054    final ArrayList<Text> docN = new ArrayList<Text>( /*initial capacity*/100 );
055
056
057
058    /** A comparator for text nodes in docN, based on the extended property "offset".
059      */
060    static final Comparator<JavaScriptObject> DOC_N_COMPARATOR = new Comparator<JavaScriptObject>()
061    {
062        public int compare( final JavaScriptObject o1, final JavaScriptObject o2 )
063        {
064         // return Integer.compare( o1._getInt("offset"), o2._getInt("offset") );
065         /// undefined in GWT 2.5 emulation, so:
066            return Double.compare( o1._getInt("offset"), o2._getInt("offset") );
067        }
068    };
069
070
071
072    /** A list of segments not found in the wikitext, each in the form of a URL to the
073      * segment view in the difference bridge.
074      */
075    final LinkedList<String> mismatchedSegLocs = new LinkedList<String>();
076
077
078
079    /** Returns a message specifying the difference and current parser position
080      * {@linkplain #d d}.
081      */
082    String positionMessage()
083    {
084        return "diff " + diff.key() + ", position " + d;
085    }
086
087
088
089    /** Returns the substring of diffText that starts at index t (inclusive) and extends
090      * to the next newline (exclusive) or, failing that, to the last character of the
091      * diffText (inclusive).
092      */
093    String subline( final int t )
094    {
095        final int dN = diffText.length();
096        int tEnd = t;
097        while( tEnd < dN && diffText.charAt(tEnd) != '\n' ) ++tEnd;
098        return diffText.substring( t, tEnd );
099    }
100
101
102}
103