001/* 002 * Copyright (c) 2004-2012, Willem Cazander 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, are permitted provided 006 * that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, this list of conditions and the 009 * following disclaimer. 010 * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and 011 * the following disclaimer in the documentation and/or other materials provided with the distribution. 012 * 013 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 014 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 015 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 016 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 017 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 018 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 019 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 020 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 021 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 022 */ 023 024package org.x4o.xml.impl; 025 026import java.util.logging.Logger; 027 028import org.x4o.xml.core.config.X4OLanguageClassLoader; 029import org.x4o.xml.element.Element; 030import org.x4o.xml.element.ElementClass; 031import org.x4o.xml.element.ElementLanguage; 032import org.x4o.xml.element.ElementNamespaceContext; 033import org.x4o.xml.element.ElementNamespaceInstanceProvider; 034import org.x4o.xml.element.ElementNamespaceInstanceProviderException; 035 036/** 037 * DefaultElementNamespaceInstanceProvider creates and configures an Element instance. 038 * 039 * @author Willem Cazander 040 * @version 1.0 Aug 17, 2005 041 */ 042public class DefaultElementNamespaceInstanceProvider implements ElementNamespaceInstanceProvider { 043 044 private Logger logger = null; 045 private ElementLanguage elementLanguage = null; 046 private ElementNamespaceContext elementNamespaceContext = null; 047 048 /** 049 * Creates new DefaultElementNamespaceInstanceProvider. 050 */ 051 public DefaultElementNamespaceInstanceProvider() { 052 logger = Logger.getLogger(DefaultElementNamespaceInstanceProvider.class.getName()); 053 } 054 055 /** 056 * @param elementLanguage The elementLanguage of this provider. 057 * @param elementNamespaceContext The elementNamespaceContext for this provider. 058 * @see org.x4o.xml.element.ElementNamespaceInstanceProvider#start(org.x4o.xml.element.ElementLanguage, org.x4o.xml.element.ElementNamespaceContext) 059 */ 060 public void start(ElementLanguage elementLanguage,ElementNamespaceContext elementNamespaceContext) { 061 this.elementLanguage=elementLanguage; 062 this.elementNamespaceContext=elementNamespaceContext; 063 logger.finer("Starting DefaultElementNamespaceInstanceProvider for: "+elementNamespaceContext.getUri()); 064 } 065 066 /** 067 * @param tag The xml tag to create an Element instance for. 068 * @return The Element to handle the given tag. 069 * @throws ElementNamespaceInstanceProviderException 070 * @see org.x4o.xml.element.ElementNamespaceInstanceProvider#createElementInstance(java.lang.String) 071 */ 072 public Element createElementInstance(String tag) throws ElementNamespaceInstanceProviderException { 073 ElementClass elementClass = elementNamespaceContext.getElementClass(tag); 074 Element element = null; 075 076 if (elementClass==null) { 077 throw new ElementNamespaceInstanceProviderException(this,"Tag: " + tag + " unknown in: " + elementNamespaceContext.getUri()); 078 } 079 080 try { 081 if (elementClass.getElementClass()!=null) { 082 Object obj = X4OLanguageClassLoader.newInstance(elementClass.getElementClass()); 083 if ((obj instanceof Element) == false) { 084 throw new ElementNamespaceInstanceProviderException(this,"Provided elementClassName is not an Element: "+obj.getClass()); 085 } 086 element = (Element) obj; 087 } else { 088 element = (Element)X4OLanguageClassLoader.newInstance((elementLanguage.getLanguageConfiguration().getDefaultElement())); 089 } 090 091 if (elementClass.getObjectClass()!=null) { 092 element.setElementObject(X4OLanguageClassLoader.newInstance(elementClass.getObjectClass())); 093 } 094 } catch (InstantiationException e) { 095 throw new ElementNamespaceInstanceProviderException(this,"Error while providing Element: "+e.getMessage(),e); 096 } catch (IllegalAccessException e) { 097 throw new ElementNamespaceInstanceProviderException(this,"Error while providing Element: "+e.getMessage(),e); 098 } /*catch (ClassNotFoundException e) { 099 throw new ElementNamespaceInstanceProviderException(this,"Error while providing Element: "+e.getMessage(),e); 100 } */ 101 element.setElementClass(elementClass); 102 element.setElementLanguage(elementLanguage); 103 return element; 104 } 105}