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