2N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 2N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2N/A * This code is free software; you can redistribute it and/or modify it 2N/A * under the terms of the GNU General Public License version 2 only, as 2N/A * published by the Free Software Foundation. Oracle designates this 2N/A * particular file as subject to the "Classpath" exception as provided 2N/A * by Oracle in the LICENSE file that accompanied this code. 2N/A * This code is distributed in the hope that it will be useful, but WITHOUT 2N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2N/A * version 2 for more details (a copy is included in the LICENSE file that 2N/A * accompanied this code). 2N/A * You should have received a copy of the GNU General Public License version 2N/A * 2 along with this work; if not, write to the Free Software Foundation, 2N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2N/A * or visit www.oracle.com if you need additional information or have any 2N/Apackage xmlkit;
// -*- mode: java; indent-tabs-mode: nil -*- 2N/A * A List of Strings each representing a word or token. 2N/A * This object itself is a CharSequence whose characters consist 2N/A * of all the tokens, separated by blanks. /// All normal List mutators must check the frozen bit: /** Add a collection of tokens to the list, applying toString to each. */ // Note that if this sequence is empty, no tokens are added. // This is different from adding a null string, which is for (
int i =
beg; i <
end; i++) {
// Input was something like "tok1 tok2 ". /** Format the token string, using quotes and escapes. * Quotes must contain an odd number of 3 or more elements, * a sequence of begin/end quote pairs, plus a superquote. * For each token, the first begin/end pair is used for * which the end quote does not occur in the token. * If the token contains all end quotes, the last pair * is used, with all occurrences of the end quote replaced * by the superquote. If an end quote is the empty string, * the separator is used instead. // { modCount, hint, -1==beg[0], end[0]==beg[1], ..., length } // Note that each beg[i]..end[i] span includes a leading separator, // which is not part of the corresponding token. // Produce (and cache) an table of indexes for each token. for (
int i =
0; i <
size; i++) {
// Which token does the given index belong to? protected int which(
int i) {
return size();
// end of the line //System.out.println("i: "+beg+".."+end); charAt(-
1);
// raise exception //System.out.println("wh: "+begWh+".."+endWh); // Special case: Slice the separator. // Special case: Slice a single token. // Insert a leading null token to force an initial separator. /** Returns the concatenation of all tokens, * with intervening separator characters. for (
int i =
0; i <
size; i++) {
public static void main(String[] av) { if (av.length == 0) av = new String[]{"one", "2", "", "four"}; TokenList ts = new TokenList(); for (int i = -1; i < av.length; i++) { if (i >= 0) ts.addToken(av[i]); TokenList tsCopy = new TokenList(ts.toString(), SEP); if (!tsCopy.equals(ts)) { tsCopy.setSeparator(")("); System.out.println("!= ("+tsCopy+")"); TokenList tsBar = new TokenList(ts, "|"); System.out.println(tsBar); int[] ls = ts.getLengths(); System.out.println("ts: "+ts); System.out.print("ls: {"); for (int j = 0; j < ls.length; j++) System.out.print(" "+ls[j]); System.out.println(" }"); assert0(ts.size() == i+1); assert0(i < 0 || ts.get(i) == av[i]); String tss = ts.toString(); int tslen = tss.length(); assert0(ts.length() == tss.length()); for (int n = 0; n < tslen; n++) { assert0(ts.charAt(n) == tss.charAt(n)); for (int j = 0; j < tslen; j++) { for (int k = tslen; k >= j; k--) { CharSequence sub = ts.subSequence(j, k); //System.out.println("|"+sub+"|"); assert0(sub.toString().equals(tss.substring(j, k))); static void assert0(boolean z) { if (!z) throw new RuntimeException("assert failed"); // ---- TESTING CODE ----*/