001/* 002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * Nuxeo - initial API and implementation 011 * 012 * $Id$ 013 */ 014 015package org.nuxeo.runtime.model.impl; 016 017import java.io.Serializable; 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.Map; 021import java.util.StringTokenizer; 022 023import org.apache.commons.logging.Log; 024import org.apache.commons.logging.LogFactory; 025import org.nuxeo.runtime.api.Framework; 026 027/** 028 * @author Bogdan Stefanescu 029 */ 030public abstract class PropertyDecoder { 031 032 private static final Log log = LogFactory.getLog(PropertyDecoder.class); 033 034 private static final Map<String, PropertyDecoder> decoders = new HashMap<String, PropertyDecoder>(); 035 036 public static Serializable decode(String type, String value) { 037 // expand value if needed 038 if (value != null) { 039 value = Framework.getRuntime().expandVars(value); 040 } 041 PropertyDecoder decoder = decoders.get(type); 042 try { 043 return decoder == null ? value : decoder.decode(value); 044 } catch (IllegalArgumentException t) { 045 log.error(t); 046 return null; 047 } 048 } 049 050 public static PropertyDecoder getDecoder(String type) { 051 return decoders.get(type); 052 } 053 054 public static void registerDecoder(String type, PropertyDecoder decoder) { 055 decoders.put(type, decoder); 056 } 057 058 public abstract Serializable decode(String value); 059 060 public static final PropertyDecoder STRING = new PropertyDecoder() { 061 @Override 062 public Serializable decode(String value) { 063 return value; 064 } 065 }; 066 067 public static final PropertyDecoder LIST = new PropertyDecoder() { 068 @Override 069 public Serializable decode(String value) { 070 ArrayList<String> values = new ArrayList<String>(); 071 StringTokenizer tokenizer = new StringTokenizer(value, ","); 072 while (tokenizer.hasMoreTokens()) { 073 String tok = tokenizer.nextToken(); 074 tok = tok.trim(); 075 values.add(tok); 076 } 077 return values; 078 } 079 }; 080 081 public static final PropertyDecoder LONG = new PropertyDecoder() { 082 @Override 083 public Serializable decode(String value) { 084 return Long.valueOf(value); 085 } 086 }; 087 088 public static final PropertyDecoder INTEGER = new PropertyDecoder() { 089 @Override 090 public Serializable decode(String value) { 091 return Integer.valueOf(value); 092 } 093 }; 094 095 public static final PropertyDecoder DOUBLE = new PropertyDecoder() { 096 @Override 097 public Serializable decode(String value) { 098 return Double.valueOf(value); 099 } 100 }; 101 102 public static final PropertyDecoder FLOAT = new PropertyDecoder() { 103 @Override 104 public Serializable decode(String value) { 105 return Float.valueOf(value); 106 } 107 }; 108 109 public static final PropertyDecoder BOOLEAN = new PropertyDecoder() { 110 @Override 111 public Serializable decode(String value) { 112 return Boolean.valueOf(value); 113 } 114 }; 115 116 public static final PropertyDecoder BYTE = new PropertyDecoder() { 117 @Override 118 public Serializable decode(String value) { 119 return Byte.valueOf(value); 120 } 121 }; 122 123 public static final PropertyDecoder CHAR = new PropertyDecoder() { 124 @Override 125 public Serializable decode(String value) { 126 if (value.length() == 0) { 127 return 0; 128 } 129 return value.charAt(0); 130 } 131 }; 132 133 public static final PropertyDecoder SHORT = new PropertyDecoder() { 134 @Override 135 public Serializable decode(String value) { 136 return Short.valueOf(value); 137 } 138 }; 139 140 public static final PropertyDecoder OBJECT = new PropertyDecoder() { 141 @Override 142 public Serializable decode(String value) { 143 return null; // TODO not yet impl 144 } 145 }; 146 147 public static final PropertyDecoder CLASS = 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 INSTANCE = 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 COMPONENT = new PropertyDecoder() { 162 @Override 163 public Serializable decode(String value) { 164 return null; // TODO not yet impl 165 } 166 }; 167 168 static { 169 registerDecoder("String", STRING); 170 registerDecoder("List", LIST); 171 registerDecoder("Long", LONG); 172 registerDecoder("Integer", INTEGER); 173 registerDecoder("Double", DOUBLE); 174 registerDecoder("Float", FLOAT); 175 registerDecoder("Boolean", BOOLEAN); 176 registerDecoder("Class", CLASS); 177 registerDecoder("Instance", INSTANCE); 178 registerDecoder("Object", OBJECT); 179 registerDecoder("Component", COMPONENT); 180 registerDecoder("Byte", BYTE); 181 registerDecoder("Char", CHAR); 182 registerDecoder("Short", SHORT); 183 } 184 185}