001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019
020package org.apache.isis.core.progmodel.facets.value.url;
021
022import java.net.MalformedURLException;
023
024import org.apache.isis.applib.adapters.EncoderDecoder;
025import org.apache.isis.applib.adapters.Parser;
026import org.apache.isis.applib.profiles.Localization;
027import org.apache.isis.core.commons.config.IsisConfiguration;
028import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
029import org.apache.isis.core.metamodel.facetapi.Facet;
030import org.apache.isis.core.metamodel.facetapi.FacetHolder;
031import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderAndFacetAbstract;
032import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderContext;
033
034public class URLValueSemanticsProvider extends ValueSemanticsProviderAndFacetAbstract<java.net.URL> implements URLValueFacet {
035
036    public static Class<? extends Facet> type() {
037        return URLValueFacet.class;
038    }
039
040    private static final int TYPICAL_LENGTH = 100;
041    private static final java.net.URL DEFAULT_VALUE = null; // no default
042
043    /**
044     * Required because implementation of {@link Parser} and
045     * {@link EncoderDecoder}.
046     */
047    public URLValueSemanticsProvider() {
048        this(null, null, null);
049    }
050
051    public URLValueSemanticsProvider(
052            final FacetHolder holder, 
053            final IsisConfiguration configuration, 
054            final ValueSemanticsProviderContext context) {
055        super(type(), holder, java.net.URL.class, 
056                TYPICAL_LENGTH, Immutability.IMMUTABLE, EqualByContent.HONOURED, DEFAULT_VALUE, 
057                configuration, context);
058    }
059
060    // //////////////////////////////////////////////////////////////////
061    // Parser
062    // //////////////////////////////////////////////////////////////////
063
064    @Override
065    protected java.net.URL doParse(final Object context, final String entry) {
066        if (entry.trim().equals("")) {
067            return null;
068        } 
069        {
070            try {
071                return new java.net.URL(entry);
072            } catch (final MalformedURLException ex) {
073                throw new IllegalArgumentException("Not parseable as a URL ('" + entry + "')", ex);
074            }
075        }
076    }
077
078    @Override
079    public String titleString(final Object object, final Localization localization) {
080        return object != null? object.toString(): "";
081    }
082
083    @Override
084    public String titleStringWithMask(final Object object, final String usingMask) {
085        return titleString(object, null);
086    }
087
088    // //////////////////////////////////////////////////////////////////
089    // EncoderDecoder
090    // //////////////////////////////////////////////////////////////////
091
092    @Override
093    protected String doEncode(final Object object) {
094        final java.net.URL url = (java.net.URL) object;
095        return url != null? url.toString(): "NULL";
096    }
097
098    @Override
099    protected java.net.URL doRestore(final String data) {
100        if("NULL".equals(data)) {
101            return null;
102        }
103        try {
104            return new java.net.URL(data);
105        } catch (MalformedURLException e) {
106            return null;
107        }
108    }
109
110
111    // //////////////////////////////////////////////////////////////////
112    // StringValueFacet
113    // //////////////////////////////////////////////////////////////////
114
115    @Override
116    public java.net.URL urlValue(final ObjectAdapter object) {
117        return object == null ? null : (java.net.URL) object.getObject();
118    }
119
120    @Override
121    public ObjectAdapter createValue(final java.net.URL value) {
122        return getAdapterManager().adapterFor(value);
123    }
124
125    // /////// toString ///////
126
127    @Override
128    public String toString() {
129        return "URLValueSemanticsProvider";
130    }
131
132}