Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.tuscany.sca.implementation.java.injection;
 
 import java.util.List;
 

Version:
$Rev: 1036786 $ $Date: 2010-11-19 12:03:23 +0200 (Fri, 19 Nov 2010) $
 
     private Mediator mediator = null;
     private boolean isSimpleType;
 
         UtilityExtensionPoint utilityExtensionPoint = registry.getExtensionPoint(UtilityExtensionPoint.class);
         this. = utilityExtensionPoint.getUtility(Mediator.class);
         this. = utilityExtensionPoint.getUtility(SimpleTypeMapper.class);
     }
 
     public JavaPropertyValueObjectFactory(Mediator mediator) {
         this. = mediator;
     }
 
     public ObjectFactory createValueFactory(Property propertyObject propertyValueJavaElementImpl javaElement) {
         Document doc = (Document)propertyValue;
         List<Nodenodes = getValues(doc);
         Class<?> javaType = JavaIntrospectionHelper.getBaseType(javaElement.getType(), javaElement.getGenericType());
         if (property.isMany()) {
             if (javaElement.getType().isArray()) {
                 return new ArrayObjectFactoryImpl(propertynodesjavaType);
             } else {
                 return new ListObjectFactoryImpl(propertynodesjavaType);
             }
         } else {
             Object value = null;
             if (!nodes.isEmpty()) {
                 value = nodes.get(0);
             }
             return new ObjectFactoryImpl(propertyvaluejavaType);
 
         }
     }
 
     public ObjectFactory createValueFactory(Property propertyObject propertyValueClass<?> javaType) {
         Document doc = (Document)propertyValue;
         List<Nodenodes = getValues(doc);
         if (property.isMany()) {
             return new ListObjectFactoryImpl(propertynodesjavaType);
         } else {
             Object value = null;
             if (!nodes.isEmpty()) {
                 value = nodes.get(0);
             }
             return new ObjectFactoryImpl(propertyvaluejavaType);
         }
    }
    public <B> B createPropertyValue(ComponentProperty propertyClass<B> type) {
    	
    	validateTypes(propertytype);
    	
        ObjectFactory<B> factory = this.createValueFactory(propertyproperty.getValue(), type);
        return factory.getInstance();
    }
	private <B> void validateTypes(ComponentProperty propertyClass<B> type) {
		// JAXB seems to do some strange things with conversions, so
    	// we can't rely on the databinding conversion from Node->Java to catch
		// incompatible types. 
		DataType prop1 = property.getProperty().getDataType();
    	
		if ( (prop1 != null) && (type.isAssignableFrom(prop1.getPhysical())) )  {
			return;    			
else if ( .getXMLType(type) != null ) { 
    		if ( .getXMLType(type).getQName().equals(property.getXSDType())) 
    			return;    			    		
    	} else if ( isSimpleType(property) ) {
    		if ( type.isAssignableFrom(.getJavaType(property.getXSDType()))) 
    			return;    		
    	} 
    	
		throw new IllegalArgumentException("Property type " + prop1.getPhysical().getName() + " is not compatible with " + type.getName());
 
	}
    abstract class ObjectFactoryImplBase implements ObjectFactory {
        protected SimpleTypeMapper simpleTypeMapper = new SimpleTypeMapperImpl();
        protected Property property;
        protected Object propertyValue;
        protected Class<?> javaType;
        protected DataType<XMLTypesourceDataType;
        protected DataType<?> targetDataType;
        public ObjectFactoryImplBase(Property propertyObject propertyValueClass<?> javaType) {
            this. = property;
            this. = propertyValue;
            this. = javaType;
             =
                new DataTypeImpl<XMLType>(.Node.class,
                                          new XMLType(nullthis..getXSDType()));
            
             = property.getDataType();
            if ( == null) {
                TypeInfo typeInfo = null;
                if (this..getXSDType() != null) {
                    if (.isSimpleXSDType(this..getXSDType())) {
                        typeInfo = new TypeInfo(property.getXSDType(), truenull);
                    } else {
                        typeInfo = new TypeInfo(property.getXSDType(), falsenull);
                    }
                } else {
                    typeInfo = new TypeInfo(property.getXSDType(), falsenull);
                }
                XMLType xmlType = new XMLType(typeInfo);
                String dataBinding = null// (String)property.getExtensions().get(DataBinding.class.getName());
                if (dataBinding != null) {
                     = new DataTypeImpl<XMLType>(dataBindingjavaTypexmlType);
                } else {
                     = new DataTypeImpl<XMLType>(dataBindingjavaTypexmlType);
                    .getDataBindings().introspectType(null);
                }
            }
        }
    }
    class ObjectFactoryImpl extends ObjectFactoryImplBase {
        private Object result = null;
        
        public ObjectFactoryImpl(Property propertyObject propertyValueClass<?> javaType) {
            super(propertypropertyValuejavaType);
        }
        public Object getInstance() throws ObjectCreationException {
            // TUSCANY-3791
            if ( != null){
                return ;
            }
            
            if () {
                try {
                     = .toJavaObject(.getXSDType(), (String)null);
                } catch (NumberFormatException ex) {
                    throw new ObjectCreationException("Failed to create instance for property " + .getName()
                        + " with value "
                        + ex);
                } catch (IllegalArgumentException ex) {
                    throw new ObjectCreationException("Failed to create instance for property " + .getName()
                        + " with value "
                        + ex);
                }
            } else {
                 = .mediate(null);
            }
            return ;
        }
    }
        private List result = null;
        
        public ListObjectFactoryImpl(Property propertyList<?> propertyValuesClass<?> javaType) {
            super(propertypropertyValuesjavaType);
        }
        @SuppressWarnings("unchecked")
        public List<?> getInstance() throws ObjectCreationException {
            // TUSCANY-3791
            if ( != null){
                return ;
            }
            
            if () {
                List<Objectvalues = new ArrayList<Object>();
                for (String aValue : (List<String>)) {
                    try {
                        values.add(.toJavaObject(.getXSDType(), aValuenull));
                    } catch (NumberFormatException ex) {
                        throw new ObjectCreationException("Failed to create instance for property " + 
                            .getName()
                            + " with value "
                            + aValue
                            + " from value list of "
                            + ex);
                    } catch (IllegalArgumentException ex) {
                        throw new ObjectCreationException("Failed to create instance for property " + 
                            .getName()
                            + " with value "
                            + aValue
                            + " from value list of "
                            + ex);
                    }
                }
                 = values;
            } else {
                List instances = new ArrayList();
                for (Node aValue : (List<Node>)) {
                    instances.add(.mediate(aValuenull));
                }
                 = instances;
            }
            
            return ;
        }
    }
        private Object result = null;
        
        public ArrayObjectFactoryImpl(Property propertyList<?> propertyValuesClass<?> javaType) {
            super(propertypropertyValuesjavaType);
        }
        @SuppressWarnings("unchecked")
        public Object getInstance() throws ObjectCreationException {
            // TUSCANY-3791
            if ( != null){
                return ;
            }
            
            if () {
                int count = 0;
                Object values = Array.newInstance(, ((List<Object>)).size());
                for (String aValue : (List<String>)) {
                    try {
                        Array.set(valuescount++, .toJavaObject(.getXSDType(), aValuenull));
                    } catch (NumberFormatException ex) {
                        throw new ObjectCreationException("Failed to create instance for property " + 
                            .getName()
                            + " with value "
                            + aValue
                            + " from value list of "
                            + ex);
                    } catch (IllegalArgumentException ex) {
                        throw new ObjectCreationException("Failed to create instance for property " + 
                            .getName()
                            + " with value "
                            + aValue
                            + " from value list of "
                            + ex);
                    }
                }
                 = values;
            } else {
                Object instances = Array.newInstance(, ((List<Object>)).size());
                int count = 0;
                for (Node aValue : (List<Node>)) {
                    Array.set(instancescount++, .mediate(aValuenull));
                }
                 = instances;
            }
            
            return ;
        }
    }

    
Utility methods


    

Parameters:
property
Returns:
    private boolean isSimpleType(Property property) {
        if (property.getXSDType() != null) {
            return .isSimpleXSDType(property.getXSDType());
        } else {
            if (property instanceof Document) {
                Document doc = (Document)property;
                Element element = doc.getDocumentElement();
                if (element.getChildNodes().getLength() == 1 && element.getChildNodes().item(0).getNodeType() == .) {
                    return true;
                }
            }
        }
        return false;
    }

    
Retrieve list of simple property values

Parameters:
concatenatedValue
javaType
Returns:
    private static List<StringgetSimplePropertyValues(String concatenatedValueClass<?> javaType) {
        List<StringpropValues = new ArrayList<String>();
        StringTokenizer st = null;
        if (javaType.getName().equals("java.lang.String")) {
            st = new StringTokenizer(concatenatedValue"\"");
        } else {
            st = new StringTokenizer(concatenatedValue);
        }
        String aToken = null;
        while (st.hasMoreTokens()) {
            aToken = st.nextToken();
            if (aToken.trim().length() > 0) {
                propValues.add(aToken);
            }
        }
        return propValues;
    }

    
Retrieve the list of complex property values

Parameters:
document
Returns:
    private static List<NodegetValues(Document document) {
    	 List<NodepropValues = new ArrayList<Node>();
    	 if ( document == null )
    		 return propValues;
        // The root is the property element
        Element rootElement = document.getDocumentElement();
       
        NodeList nodes = rootElement.getChildNodes();
        for (int count = 0; count < nodes.getLength(); ++count) {
            Node node = nodes.item(count);
            if (node.getNodeType() == .) {
                propValues.add(DOMHelper.promote(node));
            }
        }
        return propValues;
    }
New to GrepCode? Check out our FAQ X