01: /**********************************************************************
02: Copyright (c) 2005 Erik Bengtson and others. All rights reserved.
03: Licensed under the Apache License, Version 2.0 (the "License");
04: you may not use this file except in compliance with the License.
05: You may obtain a copy of the License at
06:
07: http://www.apache.org/licenses/LICENSE-2.0
08:
09: Unless required by applicable law or agreed to in writing, software
10: distributed under the License is distributed on an "AS IS" BASIS,
11: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: See the License for the specific language governing permissions and
13: limitations under the License.
14:
15: Contributors:
16: ...
17: **********************************************************************/package org.jpox.store.mapping;
18:
19: import java.net.MalformedURLException;
20: import java.net.URL;
21:
22: import org.jpox.ClassLoaderResolver;
23: import org.jpox.exceptions.JPOXDataStoreException;
24:
25: /**
26: * SCO Mapping for java.net.URL type.
27: *
28: * @version $Revision: 1.16 $
29: **/
30: public class URLMapping extends ObjectAsStringMapping {
31: private static URL mappingSampleValue;
32:
33: static {
34: try {
35: mappingSampleValue = new URL("http://jpox.org");
36: } catch (MalformedURLException e) {
37: //do nothing
38: }
39: }
40:
41: public Object getSampleValue(ClassLoaderResolver clr) {
42: return mappingSampleValue;
43: }
44:
45: /**
46: * Method to return the Java type. In our case a java.net.URL.
47: * @see org.jpox.store.mapping.JavaTypeMapping#getJavaType()
48: */
49: public Class getJavaType() {
50: return URL.class;
51: }
52:
53: /**
54: * Method to set the datastore string value based on the object value.
55: * @param object The object
56: * @return The string value to pass to the datastore
57: */
58: protected String objectToString(Object object) {
59: String url;
60: if (object instanceof URL) {
61: url = ((URL) object).toString();
62: } else {
63: url = (String) object;
64: }
65: return url;
66: }
67:
68: /**
69: * Method to extract the objects value from the datastore string value.
70: * @param datastoreValue Value obtained from the datastore
71: * @return The value of this object (derived from the datastore string value)
72: */
73: protected Object stringToObject(String datastoreValue) {
74: URL url = null;
75: try {
76: url = new java.net.URL(datastoreValue.trim());
77: } catch (MalformedURLException mue) {
78: throw new JPOXDataStoreException(LOCALISER.msg("041033",
79: datastoreValue), mue);
80: }
81: return url;
82: }
83: }
|