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