Package org.salamandra.web.core.transformer.node.binder

Source Code of org.salamandra.web.core.transformer.node.binder.XmlBindingFactory

/**
* @(#)XmlBindingFactory.java  0.1 07/05/06
*
* Copyright 2007 Luigi Scarpato (scluigi@users.sourceforge.net). All rights reserved.
*
* 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 org.salamandra.web.core.transformer.node.binder;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.List;

import org.salamandra.web.core.property.Params;
import org.salamandra.web.core.property.PropertyUtils;
import org.salamandra.web.core.property.PropertyUtils.ISetProperty;
import org.salamandra.web.core.transformer.SAXTransformer;
import org.salamandra.web.core.transformer.node.binder.annotation.ABinder;
import org.salamandra.web.core.transformer.node.binder.annotation.AParam;
import org.salamandra.web.core.transformer.node.binder.impl.O2Binder;
import org.salamandra.web.core.transformer.node.tx.TrXFactory;
import org.salamandra.web.core.transformer.node.tx.TrXO;
import org.salamandra.web.core.transformer.node.tx.TrXFactory.Protocol;

/**
*
* @author  Luigi Scarpato
*/
public class XmlBindingFactory {
  private Class abstractBind;
  private List<ISetProperty> lstProp;

  private SAXTransformer st;

  public XmlBindingFactory(Class abstractBind, List<ISetProperty> lstProp) {
    this.lstProp = lstProp;
    this.abstractBind = abstractBind;
  }
 
  public XmlBindingFactory(List<ISetProperty> lstProp) {
    this(null, lstProp);
  }

  public void setSAXTransformer( SAXTransformer st ) {
    this.st = st;
  }
 
  @SuppressWarnings("unchecked")
  protected AbstractXmlBinding<Object> createXmlBinder(Protocol protocol, Object o) {
    try {
      Class<? extends AbstractXmlBinding> classBinding = null;
          Params params = new Params();

          for (ISetProperty property : lstProp) {
            params.addPropertyValue(property.getProperty(), property.getValue());
      }
         
      Annotation[] annotations = o.getClass().getAnnotations();

      for (Annotation annotation : annotations) {
        ABinder abstractXmlBinding = annotation.annotationType().getAnnotation( ABinder.class );
        if ( abstractXmlBinding != null ) {
                  classBinding = abstractXmlBinding.value();
                 
          Method[] mm = annotation.annotationType().getDeclaredMethods();
                  for( Method m : mm )
                  {
                        Class tClass = m.getReturnType();
                    
                        AParam aParam = (AParam) tClass.getComponentType().getAnnotation( AParam.class );
                       
                        if ( aParam!=null ) {
                          Annotation[] aValue = (Annotation[]) m.invoke(annotation);
                          for (Annotation ref : aValue) {
                           
                            Method mv = ref.annotationType().getDeclaredMethod("value");
                            Object v = mv.invoke(ref);
                           
                            params.addParameter(aParam.type(), v);
              }
                        }
                  }
        }
      }
     
      if (classBinding == null)
        classBinding = O2Binder.class;
      Constructor<? extends AbstractXmlBinding> constructor = classBinding
          .getConstructor(new Class[] { Object.class, TrXO.class });
     
      AbstractXmlBinding xmlBinding = constructor
          .newInstance(new Object[] { o, new TrXFactory().createTrXO(protocol, params, st ) });
           
      return xmlBinding;
    }
    catch (Exception e) {
      throw new IllegalArgumentException( "could not instantiate the Class of type AbstractXmlBinding", e );
    }
  }

  public AbstractXmlBinding create(Object o) {
    return create(Protocol.XSLT, o);
  }

  public AbstractXmlBinding create(Protocol protocol, Object o) {
   
    AbstractXmlBinding xb = null;
    try {
      xb = createXmlBinder(protocol, o);
     
      PropertyUtils.applyPropertyValues(lstProp, o);
    } catch (Exception e) {
    }

    return xb;
  }
}
TOP

Related Classes of org.salamandra.web.core.transformer.node.binder.XmlBindingFactory

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.