All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ibm.commons.runtime.beans.AbstractXmlConfigBeanFactory Maven / Gradle / Ivy

The newest version!
/*
 * © Copyright IBM Corp. 2012
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at:
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing 
 * permissions and limitations under the License.
 */

package com.ibm.commons.runtime.beans;

import java.io.InputStream;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.ibm.commons.runtime.Context;
import com.ibm.commons.util.StringUtil;
import com.ibm.commons.xml.DOMUtil;


/**
 * SBT ManagedBean Factory.
 *
 * This class creates the managed beans when necessary.
 *  
 * @author Philippe Riand
 */
public class AbstractXmlConfigBeanFactory extends AbstractBeanFactory {

	public AbstractXmlConfigBeanFactory() {
	}
	
	
	/**
	 * 
	 * Based on the JSF 1.1 documentation
  		
  		
    		aaaaa
    		bbbbb
    		ccccc
			
				xxxx
				yyyy
			
			
        		yyyy
        		
            		
                		kkkk
                		vvvv
            		
            		...
        		
    		  
    		
        		zzzz
        		
            		vvvv
            		...
        		
    		
    	
    	
  	 *	
	 * @param fileName
	 * @return
	 */
	public static Factory[] readFactories(InputStream is) {
		if(is==null) {
			return null;
		}
		try {
			Document doc = DOMUtil.createDocument(is);
			return readFactories(doc);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public static Factory[] readFactories(Document doc) {
		List factories = new ArrayList();
		readFactories(factories, doc.getDocumentElement());
		return factories.toArray(new Factory[factories.size()]);
	}
	
	private static void readFactories(List factories, Element root) {
		NodeList l = DOMUtil.getChildElementsByTagName(root, "managed-bean");
		if(l!=null) {
			for(int i=0; i properties = null;
		NodeList l = root.getChildNodes();
		if(l!=null) {
			for(int i=0; i();
						}
						BeanProperty p = readProperty((Element)n);
						properties.add(p);
					} else {
						throw new IllegalArgumentException(StringUtil.format("Invalid element {0} in bean definition",n.getNodeName()));
					}
				}
			}
		}

		String name = getText(eName);
		if(StringUtil.isEmpty(name)) {
			throw new IllegalArgumentException("Missing managed-bean-name in bean property definition");
		}
		String className = getText(eClassName);
		if(StringUtil.isEmpty(className)) {
			throw new IllegalArgumentException("Missing managed-bean-class in bean property definition");
		}
		String scope = getText(eScope);
		if(StringUtil.isEmpty(scope)) {
			throw new IllegalArgumentException("Missing managed-bean-scope in bean property definition");
		}
		int iscope = Context.SCOPE_NONE;
		if(scope.endsWith("global")) {
			iscope = Context.SCOPE_GLOBAL;
		} else if(scope.endsWith("application")) {
			iscope = Context.SCOPE_APPLICATION;
		} else if(scope.endsWith("session")) {
			iscope = Context.SCOPE_SESSION;
		} else if(scope.endsWith("request")) {
			iscope = Context.SCOPE_REQUEST;
		} else {
			throw new IllegalArgumentException("Invalid managed-bean-scope value in bean property definition");
		}
		
		BeanProperty[] p = properties!=null ? properties.toArray(new BeanProperty[properties.size()]) : null;
		return new Factory(iscope, name, className, p);
	}

	private static BeanProperty readProperty(Element root) {
		Element eClassName=null;
		String name=null; Object value=null;
		NodeList l = root.getChildNodes();
		if(l!=null) {
			for(int i=0; i readMap(Element elt) {
		Map map = new HashMap();
		NodeList l = elt.getChildNodes();
		for(int i=0; i map) {
		String key=null, value=null;
		NodeList l = elt.getChildNodes();
		for(int i=0; i readList(Element elt) {
		List list = new ArrayList();
		NodeList l = elt.getChildNodes();
		for(int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy