001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.bind.v2.runtime.unmarshaller;
038:
039: import java.io.IOException;
040:
041: import com.sun.xml.bind.v2.runtime.output.Pcdata;
042: import com.sun.xml.bind.v2.runtime.output.UTF8XmlOutput;
043:
044: /**
045: * Typed {@link CharSequence} for int[].
046: *
047: * <p>
048: * Fed to unmarshaller when the 'text' data is actually
049: * a virtual image of int array.
050: *
051: * <p>
052: * This class holds int[] as a triplet of (data,start,len)
053: * where 'start' and 'len' represents the start position of the
054: * data and the length.
055: *
056: * @author Kohsuke Kawaguchi
057: */
058: public final class IntArrayData extends Pcdata {
059:
060: private int[] data;
061: private int start;
062: private int len;
063:
064: /**
065: * String representation of the data. Lazily computed.
066: */
067: private StringBuilder literal;
068:
069: public IntArrayData(int[] data, int start, int len) {
070: set(data, start, len);
071: }
072:
073: public IntArrayData() {
074: }
075:
076: /**
077: * Sets the int[] data to this object.
078: *
079: * <p>
080: * This method doesn't make a copy for a performance reason.
081: * The caller is still free to modify the array it passed to this method,
082: * but he should do so with a care. The unmarshalling code isn't expecting
083: * the value to be changed while it's being routed.
084: */
085: public void set(int[] data, int start, int len) {
086: this .data = data;
087: this .start = start;
088: this .len = len;
089: this .literal = null;
090: }
091:
092: public int length() {
093: return getLiteral().length();
094: }
095:
096: public char charAt(int index) {
097: return getLiteral().charAt(index);
098: }
099:
100: public CharSequence subSequence(int start, int end) {
101: return getLiteral().subSequence(start, end);
102: }
103:
104: /**
105: * Computes the literal form from the data.
106: */
107: private StringBuilder getLiteral() {
108: if (literal != null)
109: return literal;
110:
111: literal = new StringBuilder();
112: int p = start;
113: for (int i = len; i > 0; i--) {
114: if (literal.length() > 0)
115: literal.append(' ');
116: literal.append(data[p++]);
117: }
118:
119: return literal;
120: }
121:
122: public String toString() {
123: return literal.toString();
124: }
125:
126: public void writeTo(UTF8XmlOutput output) throws IOException {
127: int p = start;
128: for (int i = len; i > 0; i--) {
129: if (i != len)
130: output.write(' ');
131: output.text(data[p++]);
132: }
133: }
134: }
|