1 /*
2 * $Id: Converter.java,v 1.16 2007/04/27 22:00:07 ofung Exp $
3 */
4
5 /*
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
7 *
8 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
9 *
10 * The contents of this file are subject to the terms of either the GNU
11 * General Public License Version 2 only ("GPL") or the Common Development
12 * and Distribution License("CDDL") (collectively, the "License"). You
13 * may not use this file except in compliance with the License. You can obtain
14 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
15 * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
16 * language governing permissions and limitations under the License.
17 *
18 * When distributing the software, include this License Header Notice in each
19 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
20 * Sun designates this particular file as subject to the "Classpath" exception
21 * as provided by Sun in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the License
23 * Header, with the fields enclosed by brackets [] replaced by your own
24 * identifying information: "Portions Copyrighted [year]
25 * [name of copyright owner]"
26 *
27 * Contributor(s):
28 *
29 * If you wish your version of this file to be governed by only the CDDL or
30 * only the GPL Version 2, indicate your decision by adding "[Contributor]
31 * elects to include this software in this distribution under the [CDDL or GPL
32 * Version 2] license." If you don't indicate a single choice of license, a
33 * recipient has the option to distribute your version of this file under
34 * either the CDDL, the GPL Version 2 or to extend the choice of license to
35 * its licensees as provided above. However, if you add GPL Version 2 code
36 * and therefore, elected the GPL Version 2 license, then the option applies
37 * only if the new code is made subject to such option by the copyright
38 * holder.
39 */
40
41 package javax.faces.convert;
42
43
44 import javax.faces.component.StateHolder;
45 import javax.faces.component.UIComponent;
46 import javax.faces.context.FacesContext;
47
48
49 /**
50 * <p><strong>Converter</strong> is an interface describing a Java class
51 * that can perform Object-to-String and String-to-Object conversions
52 * between model data objects and a String representation of those
53 * objects that is suitable for rendering.</p>
54 * <p/>
55 * <p>{@link Converter} implementations must have a zero-arguments public
56 * constructor. In addition, if the {@link Converter} class wishes to have
57 * configuration property values saved and restored with the component tree,
58 * the implementation must also implement {@link StateHolder}.</p>
59 * <p/>
60 * <p>Starting with version 1.2 of the specification, an exception to the above
61 * zero-arguments constructor requirement has been introduced. If a converter has
62 * a single argument constructor that takes a <code>Class</code> instance and
63 * the <code>Class</code> of the data to be converted is
64 * known at converter instantiation time, this constructor
65 * must be used to instantiate the converter instead of the zero-argument
66 * version. This enables the per-class conversion
67 * of Java enumerated types.</p>
68 * <p/>
69 * <p>If any <code>Converter</code> implementation requires a
70 * <code>java.util.Locale</code> to perform its job, it must obtain that
71 * <code>Locale</code> from the {@link javax.faces.component.UIViewRoot}
72 * of the current {@link FacesContext}, unless the
73 * <code>Converter</code> maintains its own <code>Locale</code> as part
74 * of its state.</p>
75 */
76
77 public interface Converter {
78
79
80 /**
81 * <p>Convert the specified string value, which is associated with
82 * the specified {@link UIComponent}, into a model data object that
83 * is appropriate for being stored during the <em>Apply Request
84 * Values</em> phase of the request processing lifecycle.</p>
85 *
86 * @param context {@link FacesContext} for the request being processed
87 * @param component {@link UIComponent} with which this model object
88 * value is associated
89 * @param value String value to be converted (may be <code>null</code>)
90 * @return <code>null</code> if the value to convert is <code>null</code>,
91 * otherwise the result of the conversion
92 * @throws ConverterException if conversion cannot be successfully
93 * performed
94 * @throws NullPointerException if <code>context</code> or
95 * <code>component</code> is <code>null</code>
96 */
97 public Object getAsObject(FacesContext context, UIComponent component,
98 String value);
99
100
101 /**
102 * <p>Convert the specified model object value, which is associated with
103 * the specified {@link UIComponent}, into a String that is suitable
104 * for being included in the response generated during the
105 * <em>Render Response</em> phase of the request processing
106 * lifeycle.</p>
107 *
108 * @param context {@link FacesContext} for the request being processed
109 * @param component {@link UIComponent} with which this model object
110 * value is associated
111 * @param value Model object value to be converted
112 * (may be <code>null</code>)
113 * @return a zero-length String if value is <code>null</code>,
114 * otherwise the result of the conversion
115 * @throws ConverterException if conversion cannot be successfully
116 * performed
117 * @throws NullPointerException if <code>context</code> or
118 * <code>component</code> is <code>null</code>
119 */
120 public String getAsString(FacesContext context, UIComponent component,
121 Object value);
122
123
124 }