/**
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You 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 org.apache.bval;


import org.apache.bval.model.MetaBean;
import org.apache.commons.lang.ClassUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * Description: internal implementation class to construct 
 * metabeans with factories<br/>
 */
public class MetaBeanBuilder {
  private static final Log log = LogFactory.getLog(MetaBeanBuilder.class);

  /**
   * here you can install different kinds of factories to create MetaBeans from
   */
  private MetaBeanFactory[] factories;

  /**
   * Create a new MetaBeanBuilder instance.
   */
  public MetaBeanBuilder() {
    this(new MetaBeanFactory[]{new IntrospectorMetaBeanFactory()});
  }

  /**
   * Create a new MetaBeanBuilder instance.
   * @param factories
   */
  public MetaBeanBuilder(MetaBeanFactory[] factories) {
    setFactories(factories);
  }

  /**
   * Get the configured set of {@link MetaBeanFactory} objects.
   * @return {@link MetaBeanFactory} array
   */
  public MetaBeanFactory[] getFactories() {
    return factories;
  }

  /**
   * Set the array of {@link MetaBeanFactory} instances with which to enrich {@link MetaBean}s.
   * @param factories
   */
  public void setFactories(MetaBeanFactory[] factories) {
    this.factories = factories;
  }

  /**
   * Build a {@link MetaBean} for a given id.
   * @param beanInfoId
   * @return MetaBean
   * @throws Exception if unable to build
   */
  public MetaBean buildForId(String beanInfoId) throws Exception {
    throw new IllegalArgumentException("MetaBean " + beanInfoId + " not found");
  }

/*
    *//* convenience method *//*
    public void addLastFactory(MetaBeanFactory metaBeanFactory) {
        if (factories == null) factories = new MetaBeanFactory[1];
        else {
            MetaBeanFactory[] facold = factories;
            factories = new MetaBeanFactory[facold.length + 1];
            System.arraycopy(facold, 0, factories, 0, facold.length);
        }
        factories[factories.length - 1] = metaBeanFactory;
        updateXmlFactory();
    }

    */

  /*
   * convenience method
   *//*
    public void addFirstFactory(MetaBeanFactory metaBeanFactory) {
        if (factories == null) factories = new MetaBeanFactory[1];
        else {
            MetaBeanFactory[] facold = factories;
            factories = new MetaBeanFactory[facold.length + 1];
            System.arraycopy(facold, 0, factories, 1, facold.length);
        }
        factories[0] = metaBeanFactory;
        updateXmlFactory();
    }*/

  /**
   * Build beans for all known ids.  Default implementation returns an empty map.
   * @return Map of String : MetaBean
   */
  public Map<String, MetaBean> buildAll() throws Exception {
    return new HashMap<String, MetaBean>();
  }

  /**
   * Find the named class.
   * @param className
   * @return Class found or null
   */
  protected Class<?> findLocalClass(String className) {
    if (className != null) {
      try {
        return ClassUtils.getClass(className);
      } catch (ClassNotFoundException e) {
        log.trace("class not found: " + className, e);
      }
    }
    return null;
  }

  /**
   * Build a MetaBean for the specified class.
   * @param clazz
   * @return MetaBean
   * @throws Exception
   */
  public MetaBean buildForClass(Class<?> clazz) throws Exception {
    MetaBean meta = new MetaBean();
    if (clazz != null) { // local class here?
      meta.setBeanClass(clazz);
      meta.setId(clazz.getName()); // default id = full class name!
    }
    for (MetaBeanFactory factory : factories) {
      factory.buildMetaBean(meta);
    }
    return meta;
  }

}

@@ -2434,945 +2434,8 @@
  %7D%0A%0A
-/*%0A    *//* convenience method *//*%0A    public void addLastFactory(MetaBeanFactory metaBeanFactory) %7B%0A        if (factories == null) factories = new MetaBeanFactory%5B1%5D;%0A        else %7B%0A            MetaBeanFactory%5B%5D facold = factories;%0A            factories = new MetaBeanFactory%5Bfacold.length + 1%5D;%0A            System.arraycopy(facold, 0, factories, 0, facold.length);%0A        %7D%0A        factories%5Bfactories.length - 1%5D = metaBeanFactory;%0A        updateXmlFactory();%0A    %7D%0A%0A    */%0A%0A  /*%0A   * convenience method%0A   *//*%0A    public void addFirstFactory(MetaBeanFactory metaBeanFactory) %7B%0A        if (factories == null) factories = new MetaBeanFactory%5B1%5D;%0A        else %7B%0A            MetaBeanFactory%5B%5D facold = factories;%0A            factories = new MetaBeanFactory%5Bfacold.length + 1%5D;%0A            System.arraycopy(facold, 0, factories, 1, facold.length);%0A        %7D%0A        factories%5B0%5D = metaBeanFactory;%0A        updateXmlFactory();%0A    %7D*/%0A%0A
   /*

delete commented methods