01: /*
02: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
03: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04: *
05: * This code is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU General Public License version 2 only, as
07: * published by the Free Software Foundation. Sun designates this
08: * particular file as subject to the "Classpath" exception as provided
09: * by Sun in the LICENSE file that accompanied this code.
10: *
11: * This code is distributed in the hope that it will be useful, but WITHOUT
12: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14: * version 2 for more details (a copy is included in the LICENSE file that
15: * accompanied this code).
16: *
17: * You should have received a copy of the GNU General Public License version
18: * 2 along with this work; if not, write to the Free Software Foundation,
19: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20: *
21: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22: * CA 95054 USA or visit www.sun.com if you need additional information or
23: * have any questions.
24: */
25:
26: package com.sun.xml.internal.bind.v2.runtime.unmarshaller;
27:
28: import java.io.IOException;
29:
30: import com.sun.xml.internal.bind.v2.runtime.output.Pcdata;
31: import com.sun.xml.internal.bind.v2.runtime.output.UTF8XmlOutput;
32:
33: /**
34: * {@link Pcdata} that represents a single integer.
35: *
36: * @author Kohsuke Kawaguchi
37: */
38: public class IntData extends Pcdata {
39: /**
40: * The int value that this {@link Pcdata} represents.
41: *
42: * Modifiable.
43: */
44: private int data;
45:
46: /**
47: * Length of the {@link #data} in ASCII string.
48: * For example if data=-10, then length=3
49: */
50: private int length;
51:
52: public void reset(int i) {
53: this .data = i;
54: if (i == Integer.MIN_VALUE)
55: length = 11;
56: else
57: length = (i < 0) ? stringSizeOfInt(-i) + 1
58: : stringSizeOfInt(i);
59: }
60:
61: private final static int[] sizeTable = { 9, 99, 999, 9999, 99999,
62: 999999, 9999999, 99999999, 999999999, Integer.MAX_VALUE };
63:
64: // Requires positive x
65: private static int stringSizeOfInt(int x) {
66: for (int i = 0;; i++)
67: if (x <= sizeTable[i])
68: return i + 1;
69: }
70:
71: public String toString() {
72: return String.valueOf(data);
73: }
74:
75: public int length() {
76: return length;
77: }
78:
79: public char charAt(int index) {
80: return toString().charAt(index);
81: }
82:
83: public CharSequence subSequence(int start, int end) {
84: return toString().substring(start, end);
85: }
86:
87: public void writeTo(UTF8XmlOutput output) throws IOException {
88: output.text(data);
89: }
90: }
|