001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.runtime.model.impl;
023
024import java.io.Serializable;
025import java.util.ArrayList;
026import java.util.HashMap;
027import java.util.Map;
028import java.util.StringTokenizer;
029
030import org.apache.commons.logging.Log;
031import org.apache.commons.logging.LogFactory;
032import org.nuxeo.runtime.api.Framework;
033
034/**
035 * @author Bogdan Stefanescu
036 */
037public abstract class PropertyDecoder {
038
039    private static final Log log = LogFactory.getLog(PropertyDecoder.class);
040
041    private static final Map<String, PropertyDecoder> decoders = new HashMap<String, PropertyDecoder>();
042
043    public static Serializable decode(String type, String value) {
044        // expand value if needed
045        if (value != null) {
046            value = Framework.getRuntime().expandVars(value);
047        }
048        PropertyDecoder decoder = decoders.get(type);
049        try {
050            return decoder == null ? value : decoder.decode(value);
051        } catch (IllegalArgumentException t) {
052            log.error(t);
053            return null;
054        }
055    }
056
057    public static PropertyDecoder getDecoder(String type) {
058        return decoders.get(type);
059    }
060
061    public static void registerDecoder(String type, PropertyDecoder decoder) {
062        decoders.put(type, decoder);
063    }
064
065    public abstract Serializable decode(String value);
066
067    public static final PropertyDecoder STRING = new PropertyDecoder() {
068        @Override
069        public Serializable decode(String value) {
070            return value;
071        }
072    };
073
074    public static final PropertyDecoder LIST = new PropertyDecoder() {
075        @Override
076        public Serializable decode(String value) {
077            ArrayList<String> values = new ArrayList<String>();
078            StringTokenizer tokenizer = new StringTokenizer(value, ",");
079            while (tokenizer.hasMoreTokens()) {
080                String tok = tokenizer.nextToken();
081                tok = tok.trim();
082                values.add(tok);
083            }
084            return values;
085        }
086    };
087
088    public static final PropertyDecoder LONG = new PropertyDecoder() {
089        @Override
090        public Serializable decode(String value) {
091            return Long.valueOf(value);
092        }
093    };
094
095    public static final PropertyDecoder INTEGER = new PropertyDecoder() {
096        @Override
097        public Serializable decode(String value) {
098            return Integer.valueOf(value);
099        }
100    };
101
102    public static final PropertyDecoder DOUBLE = new PropertyDecoder() {
103        @Override
104        public Serializable decode(String value) {
105            return Double.valueOf(value);
106        }
107    };
108
109    public static final PropertyDecoder FLOAT = new PropertyDecoder() {
110        @Override
111        public Serializable decode(String value) {
112            return Float.valueOf(value);
113        }
114    };
115
116    public static final PropertyDecoder BOOLEAN = new PropertyDecoder() {
117        @Override
118        public Serializable decode(String value) {
119            return Boolean.valueOf(value);
120        }
121    };
122
123    public static final PropertyDecoder BYTE = new PropertyDecoder() {
124        @Override
125        public Serializable decode(String value) {
126            return Byte.valueOf(value);
127        }
128    };
129
130    public static final PropertyDecoder CHAR = new PropertyDecoder() {
131        @Override
132        public Serializable decode(String value) {
133            if (value.length() == 0) {
134                return 0;
135            }
136            return value.charAt(0);
137        }
138    };
139
140    public static final PropertyDecoder SHORT = new PropertyDecoder() {
141        @Override
142        public Serializable decode(String value) {
143            return Short.valueOf(value);
144        }
145    };
146
147    public static final PropertyDecoder OBJECT = new PropertyDecoder() {
148        @Override
149        public Serializable decode(String value) {
150            return null; // TODO not yet impl
151        }
152    };
153
154    public static final PropertyDecoder CLASS = new PropertyDecoder() {
155        @Override
156        public Serializable decode(String value) {
157            return null; // TODO not yet impl
158        }
159    };
160
161    public static final PropertyDecoder INSTANCE = new PropertyDecoder() {
162        @Override
163        public Serializable decode(String value) {
164            return null; // TODO not yet impl
165        }
166    };
167
168    public static final PropertyDecoder COMPONENT = new PropertyDecoder() {
169        @Override
170        public Serializable decode(String value) {
171            return null; // TODO not yet impl
172        }
173    };
174
175    static {
176        registerDecoder("String", STRING);
177        registerDecoder("List", LIST);
178        registerDecoder("Long", LONG);
179        registerDecoder("Integer", INTEGER);
180        registerDecoder("Double", DOUBLE);
181        registerDecoder("Float", FLOAT);
182        registerDecoder("Boolean", BOOLEAN);
183        registerDecoder("Class", CLASS);
184        registerDecoder("Instance", INSTANCE);
185        registerDecoder("Object", OBJECT);
186        registerDecoder("Component", COMPONENT);
187        registerDecoder("Byte", BYTE);
188        registerDecoder("Char", CHAR);
189        registerDecoder("Short", SHORT);
190    }
191
192}