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.elements;
016
017import org.apache.commons.logging.Log;
018import org.apache.commons.logging.LogFactory;
019import org.nuxeo.theme.Manager;
020import org.nuxeo.theme.types.TypeFamily;
021import org.nuxeo.theme.uids.UidManager;
022
023public final class ElementFactory {
024
025    private static final Log log = LogFactory.getLog(ElementFactory.class);
026
027    private ElementFactory() {
028        // This class is not supposed to be instantiated.
029    }
030
031    public static Element create(final String typeName) {
032        final ElementType elementType = (ElementType) Manager.getTypeRegistry().lookup(TypeFamily.ELEMENT, typeName);
033        final String className = elementType.getClassName();
034        final UidManager uidManager = Manager.getUidManager();
035
036        if (className == null) {
037            // throw an exception
038            return null;
039        }
040
041        Element element = null;
042        try {
043            element = (Element) Class.forName(className).newInstance();
044            element.setElementType(elementType);
045            uidManager.register(element);
046        } catch (ReflectiveOperationException e) {
047            log.error("Could not create element", e);
048        }
049        return element;
050    }
051
052}