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.string;
021
022import org.apache.isis.applib.adapters.EncoderDecoder;
023import org.apache.isis.applib.adapters.Parser;
024import org.apache.isis.applib.profiles.Localization;
025import org.apache.isis.core.commons.config.IsisConfiguration;
026import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
027import org.apache.isis.core.metamodel.facetapi.Facet;
028import org.apache.isis.core.metamodel.facetapi.FacetHolder;
029import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderAndFacetAbstract;
030import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderContext;
031
032public class StringValueSemanticsProvider extends ValueSemanticsProviderAndFacetAbstract<String> implements StringValueFacet {
033
034    public static Class<? extends Facet> type() {
035        return StringValueFacet.class;
036    }
037
038    public static final int TYPICAL_LENGTH = 25;
039    private static final String DEFAULT_VALUE = null; // no default
040
041    /**
042     * Required because implementation of {@link Parser} and
043     * {@link EncoderDecoder}.
044     */
045    public StringValueSemanticsProvider() {
046        this(null, null, null);
047    }
048
049    public StringValueSemanticsProvider(final FacetHolder holder, final IsisConfiguration configuration, final ValueSemanticsProviderContext context) {
050        super(type(), holder, String.class, TYPICAL_LENGTH, Immutability.IMMUTABLE, EqualByContent.HONOURED, DEFAULT_VALUE, configuration, context);
051    }
052
053    // //////////////////////////////////////////////////////////////////
054    // Parser
055    // //////////////////////////////////////////////////////////////////
056
057    @Override
058    protected String doParse(final Object context, final String entry) {
059        if (entry.trim().equals("")) {
060            return null;
061        } else {
062            return entry;
063        }
064    }
065
066    @Override
067    public String titleString(final Object object, final Localization localization) {
068        final String string = (String) (object == null ? "" : object);
069        return string;
070    }
071
072    @Override
073    public String titleStringWithMask(final Object object, final String usingMask) {
074        return titleString(object, null);
075    }
076
077    // //////////////////////////////////////////////////////////////////
078    // EncoderDecoder
079    // //////////////////////////////////////////////////////////////////
080
081    @Override
082    protected String doEncode(final Object object) {
083        final String text = (String) object;
084        if (text.equals("NULL") || isEscaped(text)) {
085            return escapeText(text);
086        } else {
087            return text;
088        }
089    }
090
091    @Override
092    protected String doRestore(final String data) {
093        if (isEscaped(data)) {
094            return data.substring(1);
095        } else {
096            return data;
097        }
098    }
099
100    private boolean isEscaped(final String text) {
101        return text.startsWith("/");
102    }
103
104    private String escapeText(final String text) {
105        return "/" + text;
106    }
107
108    // //////////////////////////////////////////////////////////////////
109    // StringValueFacet
110    // //////////////////////////////////////////////////////////////////
111
112    @Override
113    public String stringValue(final ObjectAdapter object) {
114        return object == null ? "" : (String) object.getObject();
115    }
116
117    @Override
118    public ObjectAdapter createValue(final String value) {
119        return getAdapterManager().adapterFor(value);
120    }
121
122    // /////// toString ///////
123
124    @Override
125    public String toString() {
126        return "StringValueSemanticsProvider";
127    }
128
129}