KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > impl > dv > xs > DoubleDV


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package com.sun.org.apache.xerces.internal.impl.dv.xs;
59
60 import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
61 import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
62
63 /**
64  * Represent the schema type "double"
65  *
66  * @author Neeraj Bajaj, Sun Microsystems, inc.
67  * @author Sandy Gao, IBM
68  *
69  * @version $Id: DoubleDV.java,v 1.6 2003/02/17 13:45:57 sandygao Exp $
70  */

71 public class DoubleDV extends TypeValidator {
72
73     public short getAllowedFacets(){
74         return ( XSSimpleTypeDecl.FACET_PATTERN | XSSimpleTypeDecl.FACET_WHITESPACE | XSSimpleTypeDecl.FACET_ENUMERATION |XSSimpleTypeDecl.FACET_MAXINCLUSIVE |XSSimpleTypeDecl.FACET_MININCLUSIVE | XSSimpleTypeDecl.FACET_MAXEXCLUSIVE | XSSimpleTypeDecl.FACET_MINEXCLUSIVE );
75     }//getAllowedFacets()
76

77     //convert a String to Double form, we have to take care of cases specified in spec like INF, -INF and NaN
78
public Object JavaDoc getActualValue(String JavaDoc content, ValidationContext context) throws InvalidDatatypeValueException {
79         try{
80             return new XDouble(content);
81         } catch (NumberFormatException JavaDoc ex){
82             throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object JavaDoc[]{content, "double"});
83         }
84     }//getActualValue()
85

86     // Can't call Double#compareTo method, because it's introduced in jdk 1.2
87
public int compare(Object JavaDoc value1, Object JavaDoc value2) {
88         return ((XDouble)value1).compareTo((XDouble)value2);
89     }//compare()
90

91     private static final class XDouble {
92         private double value;
93         public XDouble(String JavaDoc s) throws NumberFormatException JavaDoc {
94             try {
95                 value = Double.parseDouble(s);
96             }
97             catch ( NumberFormatException JavaDoc nfe ) {
98                 if ( s.equals("INF") ) {
99                     value = Double.POSITIVE_INFINITY;
100                 }
101                 else if ( s.equals("-INF") ) {
102                     value = Double.NEGATIVE_INFINITY;
103                 }
104                 else if ( s.equals("NaN" ) ) {
105                     value = Double.NaN;
106                 }
107                 else {
108                     throw nfe;
109                 }
110             }
111         }
112
113         public boolean equals(Object JavaDoc val) {
114             if (val == this)
115                 return true;
116     
117             if (!(val instanceof XDouble))
118                 return false;
119             XDouble oval = (XDouble)val;
120
121             // NOTE: we don't distinguish 0.0 from -0.0
122
if (value == oval.value)
123                 return true;
124             
125             if (value != value && oval.value != oval.value)
126                 return true;
127
128             return false;
129         }
130
131         private int compareTo(XDouble val) {
132             double oval = val.value;
133
134             // this < other
135
if (value < oval)
136                 return -1;
137             // this > other
138
if (value > oval)
139                 return 1;
140             // this == other
141
// NOTE: we don't distinguish 0.0 from -0.0
142
if (value == oval)
143                 return 0;
144
145             // one of the 2 values or both is/are NaN(s)
146

147             if (value != value) {
148                 // this = NaN = other
149
if (oval != oval)
150                     return 0;
151                 // this is NaN <> other
152
return INDETERMINATE;
153             }
154
155             // other is NaN <> this
156
return INDETERMINATE;
157         }
158
159         private String JavaDoc canonical;
160         public synchronized String JavaDoc toString() {
161             if (canonical == null) {
162                 if (value == Double.POSITIVE_INFINITY)
163                     canonical = "INF";
164                 else if (value == Double.NEGATIVE_INFINITY)
165                     canonical = "-INF";
166                 else if (value != value)
167                     canonical = "NaN";
168                 // NOTE: we don't distinguish 0.0 from -0.0
169
else if (value == 0)
170                     canonical = "0.0E1";
171                 else {
172                     // REVISIT: use the java algorithm for now, because we
173
// don't know what to output for 1.1d (which is no
174
// actually 1.1)
175
canonical = Double.toString(value);
176                     // if it contains 'E', then it should be a valid schema
177
// canonical representation
178
if (canonical.indexOf('E') == -1) {
179                         int len = canonical.length();
180                         // at most 3 longer: E, -, 9
181
char[] chars = new char[len+3];
182                         canonical.getChars(0, len, chars, 0);
183                         // expected decimal point position
184
int edp = chars[0] == '-' ? 2 : 1;
185                         // for non-zero integer part
186
if (value >= 1 || value <= -1) {
187                             // decimal point position
188
int dp = canonical.indexOf('.');
189                             // move the digits: ddd.d --> d.ddd
190
for (int i = dp; i > edp; i--) {
191                                 chars[i] = chars[i-1];
192                             }
193                             chars[edp] = '.';
194                             // trim trailing zeros: d00.0 --> d.000 --> d.
195
while (chars[len-1] == '0')
196                                 len--;
197                             // add the last zero if necessary: d. --> d.0
198
if (chars[len-1] == '.')
199                                 len++;
200                             // append E: d.dd --> d.ddE
201
chars[len++] = 'E';
202                             // how far we shifted the decimal point
203
int shift = dp - edp;
204                             // append the exponent --> d.ddEd
205
// the exponent is at most 7
206
chars[len++] = (char)(shift + '0');
207                         }
208                         else {
209                             // non-zero digit point
210
int nzp = edp + 1;
211                             // skip zeros: 0.003
212
while (chars[nzp] == '0')
213                                 nzp++;
214                             // put the first non-zero digit to the left of '.'
215
chars[edp-1] = chars[nzp];
216                             chars[edp] = '.';
217                             // move other digits (non-zero) to the right of '.'
218
for (int i = nzp+1, j = edp+1; i < len; i++, j++)
219                                 chars[j] = chars[i];
220                             // adjust the length
221
len -= nzp - edp;
222                             // append 0 if nessary: 0.03 --> 3. --> 3.0
223
if (len == edp + 1)
224                                 chars[len++] = '0';
225                             // append E-: d.dd --> d.ddE-
226
chars[len++] = 'E';
227                             chars[len++] = '-';
228                             // how far we shifted the decimal point
229
int shift = nzp - edp;
230                             // append the exponent --> d.ddEd
231
// the exponent is at most 3
232
chars[len++] = (char)(shift + '0');
233                         }
234                         canonical = new String JavaDoc(chars, 0, len);
235                     }
236                 }
237             }
238             return canonical;
239         }
240     }
241 } // class DoubleDV
242
Popular Tags