001/*
002 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     bstefanescu
016 */
017package org.nuxeo.shell.impl;
018
019import org.nuxeo.shell.CommandRegistry;
020import org.nuxeo.shell.CommandType;
021import org.nuxeo.shell.Shell;
022import org.nuxeo.shell.ShellException;
023import org.nuxeo.shell.ValueAdapter;
024
025/**
026 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
027 */
028public class DefaultValueAdapter implements ValueAdapter {
029
030    @SuppressWarnings("unchecked")
031    public <T> T getValue(Shell shell, Class<T> type, String value) {
032        if (type == CharSequence.class || type == String.class) {
033            return (T) value;
034        }
035        if (type.isPrimitive()) {
036            if (type == Boolean.TYPE) {
037                return (T) Boolean.valueOf(value);
038            } else if (type == Integer.TYPE) {
039                return (T) Integer.valueOf(value);
040            } else if (type == Float.TYPE) {
041                return (T) Float.valueOf(value);
042            } else if (type == Long.TYPE) {
043                return (T) Long.valueOf(value);
044            } else if (type == Double.TYPE) {
045                return (T) Double.valueOf(value);
046            } else if (type == Character.TYPE) {
047                return (T) (Character.valueOf(value == null || value.length() == 0 ? '\0' : value.charAt(0)));
048            }
049        } else if (type == Boolean.class) {
050            return (T) Boolean.valueOf(value);
051        } else if (Number.class.isAssignableFrom(type)) {
052            if (type == Integer.class) {
053                return (T) Integer.valueOf(value);
054            } else if (type == Float.class) {
055                return (T) Float.valueOf(value);
056            } else if (type == Long.class) {
057                return (T) Long.valueOf(value);
058            } else if (type == Double.class) {
059                return (T) Double.valueOf(value);
060            }
061        } else if (type == Character.class) {
062            return (T) (Character.valueOf(value == null || value.length() == 0 ? '\0' : value.charAt(0)));
063        } else if (CommandType.class.isAssignableFrom(type)) {
064            CommandType cmd = shell.getActiveRegistry().getCommandType(value);
065            if (cmd == null) {
066                throw new ShellException("Unknown command: " + value);
067            }
068            return (T) cmd;
069        } else if (CommandRegistry.class.isAssignableFrom(type)) {
070            CommandRegistry reg = shell.getRegistry(value);
071            if (reg == null) {
072                throw new ShellException("Unknown namespace: " + value);
073            }
074            return (T) reg;
075        }
076        return null;
077
078    }
079
080}