package 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. import com.google.gwt.core.client.*; import com.google.gwt.dom.client.*; import java.util.*; import votorola.a.diff.*; import votorola.s.gwt.stage.*; /** A parse of a textual difference between two position drafts. */ final class DifferenceParse { /** The next character to be read in the parse of diffText. */ int d; /** The current difference being marked up. */ ShadowedDiff diff; /** Sets the current difference being marked up. */ void setDiff( ShadowedDiff _diff ) { diff = _diff; diffText = diff.text(); d = 0; mismatchedSegLocs.clear(); } /** The {@linkplain ShadowedDiff#text() diff text}, de-referenced as a convenience. */ String diffText; /** The text of the document in which to search for difference segments. */ final StringBuilder docC = new StringBuilder( /*initial capacity*/2000 ); /** Text nodes in document order. Each has an extended property "offset" that * specifies its offset in docC. */ final ArrayList docN = new ArrayList( /*initial capacity*/100 ); /** A comparator for text nodes in docN, based on the extended property "offset". */ static final Comparator DOC_N_COMPARATOR = new Comparator() { public int compare( final JavaScriptObject o1, final JavaScriptObject o2 ) { // return Integer.compare( o1._getInt("offset"), o2._getInt("offset") ); /// undefined in GWT 2.5 emulation, so: return Double.compare( o1._getInt("offset"), o2._getInt("offset") ); } }; /** A list of segments not found in the wikitext, each in the form of a URL to the * segment view in the difference bridge. */ final LinkedList mismatchedSegLocs = new LinkedList(); /** Returns a message specifying the difference and current parser position * {@linkplain #d d}. */ String positionMessage() { return "diff " + diff.key() + ", position " + d; } /** Returns the substring of diffText that starts at index t (inclusive) and extends * to the next newline (exclusive) or, failing that, to the last character of the * diffText (inclusive). */ String subline( final int t ) { final int dN = diffText.length(); int tEnd = t; while( tEnd < dN && diffText.charAt(tEnd) != '\n' ) ++tEnd; return diffText.substring( t, tEnd ); } }