Package org.jpox.store.mapped.mapping

Source Code of org.jpox.store.mapped.mapping.URLMapping

/**********************************************************************
Copyright (c) 2005 Erik Bengtson and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Contributors:
    ...
**********************************************************************/

package org.jpox.store.mapped.mapping;

import java.net.MalformedURLException;
import java.net.URL;

import org.jpox.ClassLoaderResolver;
import org.jpox.exceptions.JPOXDataStoreException;

/**
* SCO Mapping for java.net.URL type.
*
* @version $Revision: 1.16 $
**/
public class URLMapping extends ObjectAsStringMapping
{
    private static URL mappingSampleValue;

    static
    {
        try
        {
            mappingSampleValue = new URL("http://jpox.org");
        }
        catch (MalformedURLException e)
        {
            //do nothing
        }
    }

    public Object getSampleValue(ClassLoaderResolver clr)
    {
        return mappingSampleValue;
    }

    /**
     * Method to return the Java type. In our case a java.net.URL.
     * @see org.jpox.store.mapped.mapping.JavaTypeMapping#getJavaType()
     */
    public Class getJavaType()
    {
        return URL.class;
    }

    /**
     * Method to set the datastore string value based on the object value.
     * @param object The object
     * @return The string value to pass to the datastore
     */
    protected String objectToString(Object object)
    {
        String url;
        if (object instanceof URL)
        {
            url = ((URL)object).toString();
        }
        else
        {
            url = (String)object;
        }
        return url;
    }

    /**
     * Method to extract the objects value from the datastore string value.
     * @param datastoreValue Value obtained from the datastore
     * @return The value of this object (derived from the datastore string value)
     */
    protected Object stringToObject(String datastoreValue)
    {
        URL url = null;
        try
        {
            url = new java.net.URL(datastoreValue.trim());
        }
        catch (MalformedURLException mue)
        {
            throw new JPOXDataStoreException(LOCALISER.msg("041033", datastoreValue), mue);
        }
        return url;
    }
}
TOP

Related Classes of org.jpox.store.mapped.mapping.URLMapping

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.