KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > schema > datatypes > DoubleType


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.schema.datatypes;
24
25 import java.text.DecimalFormat JavaDoc;
26 import java.util.Locale JavaDoc;
27
28 import org.xquark.schema.SchemaException;
29 import org.xquark.schema.validation.ValidationContextProvider;
30
31
32 class DoubleType extends ComparableType {
33     private static final String JavaDoc RCSRevision = "$Revision: 1.3 $";
34     private static final String JavaDoc RCSName = "$Name: $";
35
36     private static final String JavaDoc NAN = "NaN";
37     private static final String JavaDoc INF = "INF";
38     private static final String JavaDoc POSITIVE_INF = "+INF";
39     private static final String JavaDoc NEGATIVE_INF = "-INF";
40     
41     private DecimalFormat JavaDoc format = null;
42     
43     DoubleType() {
44         super("double", PrimitiveType.DOUBLE);
45         format = (DecimalFormat JavaDoc)DecimalFormat.getInstance(Locale.US);
46         format.applyPattern("0.0##############E0");
47     }
48
49     public Object JavaDoc toValidType(Object JavaDoc data) {
50         if (data instanceof Double JavaDoc) {
51             return data;
52         } else if (data instanceof Number JavaDoc) {
53             return new Double JavaDoc(((Number JavaDoc)data).doubleValue());
54         } else {
55             return (Double JavaDoc)data;
56         }
57     }
58     
59     protected Comparable JavaDoc toComparable(String JavaDoc value) throws SchemaException {
60         // special Double
61
if (INF.equals(value))
62             return new Double JavaDoc(Double.POSITIVE_INFINITY);
63         else if (POSITIVE_INF.equals(value))
64             return new Double JavaDoc(Double.POSITIVE_INFINITY);
65         else if (NEGATIVE_INF.equals(value))
66             return new Double JavaDoc(Double.NEGATIVE_INFINITY);
67         else if (NAN.equals(value))
68             return new Double JavaDoc(Double.NaN);
69         // normal Double
70
try {
71             return new Double JavaDoc(value);
72         } catch (NumberFormatException JavaDoc nfe) {
73             super.invalidValue(value);
74             return null;
75         }
76     }
77     
78     public String JavaDoc toXMLString(Object JavaDoc obj, ValidationContextProvider context) {
79         if (obj == null) return null;
80         Double JavaDoc data = (Double JavaDoc)obj;
81         if (data.isNaN()) return NAN;
82         else if (data.isInfinite()) {
83             if (data.doubleValue() > 0) return INF;
84             else return NEGATIVE_INF;
85         } else {
86             return format.format(data.doubleValue());
87         }
88     }
89 }
90
Popular Tags