001/*
002 * (C) Copyright 2006-2007 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 *     Jean-Marc Orliaguet, Chalmers
018 *
019 * $Id$
020 */
021
022package org.nuxeo.theme.formats;
023
024import org.nuxeo.theme.Manager;
025import org.nuxeo.theme.themes.ThemeException;
026import org.nuxeo.theme.types.TypeFamily;
027import org.nuxeo.theme.uids.UidManager;
028
029public final class FormatFactory {
030
031    // This class is not supposed to be instantiated.
032    private FormatFactory() {
033    }
034
035    public static Format create(final String typeName) throws ThemeException {
036        Format format = null;
037        final FormatType formatType = (FormatType) Manager.getTypeRegistry().lookup(TypeFamily.FORMAT, typeName);
038        if (formatType == null) {
039            throw new ThemeException("Unknown format type: " + typeName);
040        }
041
042        final UidManager uidManager = Manager.getUidManager();
043        try {
044            format = (Format) Class.forName(formatType.getFormatClass()).newInstance();
045        } catch (InstantiationException e) {
046            throw new ThemeException(e);
047        } catch (IllegalAccessException e) {
048            throw new ThemeException(e);
049        } catch (ClassNotFoundException e) {
050            throw new ThemeException("Format creation failed: " + typeName, e);
051        }
052        format.setFormatType(formatType);
053        uidManager.register(format);
054        return format;
055    }
056
057}