1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License").  You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
23 * ident	"%Z%%M%	%I%	%E% SMI"
24 *
25 * Copyright (c) 2000 by Sun Microsystems, Inc.
26 * All rights reserved.
27 */
28
29/*
30 *        Copyright (C) 1996  Active Software, Inc.
31 *                  All rights reserved.
32 *
33 * @(#) DoubleArrayConverter.java 1.15 - last change made 06/17/97
34 */
35
36package sunsoft.jws.visual.rt.type;
37
38import sunsoft.jws.visual.rt.base.Attribute;
39import sunsoft.jws.visual.rt.base.Global;
40
41import java.util.Vector;
42import java.util.StringTokenizer;
43
44/**
45* Converts array of double to a string and back again.
46* The string representation is a single line of
47* comma-separated numbers.
48*
49* @version 	1.15, 06/17/97
50*/
51public class DoubleArrayConverter extends Converter {
52    /**
53     * Converts and array of double to a string representation.
54     */
55    public String convertToString(Object obj) {
56        if (obj != null) {
57            double[] a = (double[]) obj;
58
59            StringBuffer retval = new StringBuffer();
60            for (int i = 0; i < a.length; i++) {
61                retval.append(a[i]);
62                if (i != (a.length-1))
63                    retval.append(/* NOI18N */",");
64            }
65
66            return (retval.toString());
67        } else {
68            return (/* NOI18N */"");
69        }
70    }
71
72    /**
73     * Converts a string to an array of double.
74     *
75     * @exception ParseException when one of the numbers
76     * is badly formatted
77    */
78    public Object convertFromString(String s) {
79        if (s != null) {
80            Vector doublebuf = new Vector();
81
82            StringTokenizer st = new StringTokenizer(s, /* NOI18N */",");
83            for (; st.hasMoreTokens(); ) {
84                try {
85                    s = st.nextToken().trim();
86                    doublebuf.addElement(Double.valueOf(s));
87                } catch (NumberFormatException e) {
88                    /* JSTYLED */
89		    throw new ParseException(Global.getMsg("sunsoft.jws.visual.rt.type.DoubleArrayConverter.Badly__formatted__doub.28") + s);
90                }
91            }
92
93            if (doublebuf.size() > 0) {
94                double retval[] = new double[doublebuf.size()];
95                for (int i = 0; i < doublebuf.size(); i++)
96                    retval[i] = ((Double) doublebuf.elementAt(i)).
97			doubleValue();
98                return (retval);
99            }
100        }
101        return (null);
102    }
103
104    /**
105     * Converts an array of double (stored in an attribute) into
106     * a block
107     * of code that will create the array without using this
108     * converter at
109     * runtime.
110     */
111    public void convertToCodeBlock(String amName, Attribute a,
112				   int indent, StringBuffer buf) {
113        indent(buf, indent);
114        buf.append(/* NOI18N */"{");
115        Global.newline(buf);
116
117        indent += 2;
118        indent(buf, indent);
119        buf.append(/* NOI18N */"double _tmp[] = {");
120        convertToString(a.getValue(), buf);
121        buf.append(/* NOI18N */"};");
122        Global.newline(buf);
123
124        super.convertToCodeBlock(amName, a, indent, buf);
125
126        indent -= 2;
127        indent(buf, indent);
128        buf.append(/* NOI18N */"}");
129        Global.newline(buf);
130    }
131
132    /**
133     * Use convertToCodeBlock instead.
134     */
135    public String convertToCode(Object obj) {
136        return (/* NOI18N */"_tmp");
137    }
138}
139