1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/emul/compact/src/main/java/java/text/CharacterIteratorFieldDelegate.java Thu Oct 03 15:40:35 2013 +0200
1.3 @@ -0,0 +1,124 @@
1.4 +/*
1.5 + * Copyright (c) 2000, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +package java.text;
1.29 +
1.30 +import java.util.ArrayList;
1.31 +
1.32 +/**
1.33 + * CharacterIteratorFieldDelegate combines the notifications from a Format
1.34 + * into a resulting <code>AttributedCharacterIterator</code>. The resulting
1.35 + * <code>AttributedCharacterIterator</code> can be retrieved by way of
1.36 + * the <code>getIterator</code> method.
1.37 + *
1.38 + */
1.39 +class CharacterIteratorFieldDelegate implements Format.FieldDelegate {
1.40 + /**
1.41 + * Array of AttributeStrings. Whenever <code>formatted</code> is invoked
1.42 + * for a region > size, a new instance of AttributedString is added to
1.43 + * attributedStrings. Subsequent invocations of <code>formatted</code>
1.44 + * for existing regions result in invoking addAttribute on the existing
1.45 + * AttributedStrings.
1.46 + */
1.47 + private ArrayList attributedStrings;
1.48 + /**
1.49 + * Running count of the number of characters that have
1.50 + * been encountered.
1.51 + */
1.52 + private int size;
1.53 +
1.54 +
1.55 + CharacterIteratorFieldDelegate() {
1.56 + attributedStrings = new ArrayList();
1.57 + }
1.58 +
1.59 + public void formatted(Format.Field attr, Object value, int start, int end,
1.60 + StringBuffer buffer) {
1.61 + if (start != end) {
1.62 + if (start < size) {
1.63 + // Adjust attributes of existing runs
1.64 + int index = size;
1.65 + int asIndex = attributedStrings.size() - 1;
1.66 +
1.67 + while (start < index) {
1.68 + AttributedString as = (AttributedString)attributedStrings.
1.69 + get(asIndex--);
1.70 + int newIndex = index - as.length();
1.71 + int aStart = Math.max(0, start - newIndex);
1.72 +
1.73 + as.addAttribute(attr, value, aStart, Math.min(
1.74 + end - start, as.length() - aStart) +
1.75 + aStart);
1.76 + index = newIndex;
1.77 + }
1.78 + }
1.79 + if (size < start) {
1.80 + // Pad attributes
1.81 + attributedStrings.add(new AttributedString(
1.82 + buffer.substring(size, start)));
1.83 + size = start;
1.84 + }
1.85 + if (size < end) {
1.86 + // Add new string
1.87 + int aStart = Math.max(start, size);
1.88 + AttributedString string = new AttributedString(
1.89 + buffer.substring(aStart, end));
1.90 +
1.91 + string.addAttribute(attr, value);
1.92 + attributedStrings.add(string);
1.93 + size = end;
1.94 + }
1.95 + }
1.96 + }
1.97 +
1.98 + public void formatted(int fieldID, Format.Field attr, Object value,
1.99 + int start, int end, StringBuffer buffer) {
1.100 + formatted(attr, value, start, end, buffer);
1.101 + }
1.102 +
1.103 + /**
1.104 + * Returns an <code>AttributedCharacterIterator</code> that can be used
1.105 + * to iterate over the resulting formatted String.
1.106 + *
1.107 + * @pararm string Result of formatting.
1.108 + */
1.109 + public AttributedCharacterIterator getIterator(String string) {
1.110 + // Add the last AttributedCharacterIterator if necessary
1.111 + // assert(size <= string.length());
1.112 + if (string.length() > size) {
1.113 + attributedStrings.add(new AttributedString(
1.114 + string.substring(size)));
1.115 + size = string.length();
1.116 + }
1.117 + int iCount = attributedStrings.size();
1.118 + AttributedCharacterIterator iterators[] = new
1.119 + AttributedCharacterIterator[iCount];
1.120 +
1.121 + for (int counter = 0; counter < iCount; counter++) {
1.122 + iterators[counter] = ((AttributedString)attributedStrings.
1.123 + get(counter)).getIterator();
1.124 + }
1.125 + return new AttributedString(iterators).getIterator();
1.126 + }
1.127 +}