View Javadoc
1   /*
2    * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * 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 : stringSizeOfInt(i);
58      }
59  
60      private final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
61                                       99999999, 999999999, Integer.MAX_VALUE };
62  
63      // Requires positive x
64      private static int stringSizeOfInt(int x) {
65          for (int i=0; ; i++)
66              if (x <= sizeTable[i])
67                  return i+1;
68      }
69  
70      public String toString() {
71          return String.valueOf(data);
72      }
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  }