Code example for AbstractBeanDefinition

Methods: setBeanClassvalidate

0
public final class XQueryParserUtils { 
 
 
 
	private XQueryParserUtils() { 
		//prevent instantiation 
		throw new AssertionError("Cannot instantiate a utility class");
	} 
 
	/** 
	 * Create the instance of the {@link XQueryExecutor} 
	 * @param element 
	 * @return 
	 */ 
	public static final AbstractBeanDefinition getXQueryExecutor(Element element) {
		BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(XQueryExecutor.class);
		IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "converter");
		IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "data-source","xQDataSource");
		IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "format-output");
		setXQueryInBuilder(element, builder);
		//lets get the parameter nodes 
		setXQueryParameters(element, builder);
 
		return builder.getBeanDefinition();
	} 
 
	/** 
	 * The provided xquery may have one or more 'xquery-parameter' child elements, this private helper method sets the parameters in 
	 * the builder for {@link XQueryExecutor} being constructed 
	 * 
	 * @param element 
	 * @param builder 
	 */ 
	private static void setXQueryParameters(Element element,
			BeanDefinitionBuilder builder) {
		NodeList parameters = element.getElementsByTagNameNS(element.getNamespaceURI(), "xquery-parameter");
		if(parameters != null && parameters.getLength() > 0) {
			ManagedList<AbstractBeanDefinition> params = new ManagedList<AbstractBeanDefinition>();
			for(int i = 0;i < parameters.getLength();i++) {
				Node node = parameters.item(i);
				NamedNodeMap attrs = node.getAttributes();
				Assert.isTrue(attrs.getLength() > 1,
						"One of ref, value or expression should be present with the name attribute"); 
				Attr nameAttr = (Attr)attrs.getNamedItem("name");
 
				//TODO No check for the mutually exclusivity of these attributes, needed? 
 
				//create a new XQueryParameter instance 
				BeanDefinitionBuilder paramBuilder =
					BeanDefinitionBuilder.genericBeanDefinition(XQueryParameter.class);
				paramBuilder.addConstructorArgValue(nameAttr.getTextContent());
				Attr attr;
				//add the value if present 
				if(attrs.getNamedItem("value") != null) {
					attr = (Attr)attrs.getNamedItem("value");
					paramBuilder.addPropertyValue("parameterValue",attr.getTextContent());
				} 
				else if(attrs.getNamedItem("ref") != null) {
					attr = (Attr)attrs.getNamedItem("ref");
					paramBuilder.addPropertyReference("parameterValue", attr.getTextContent());
				} 
				else if(attrs.getNamedItem("expression") != null) {
					attr = (Attr)attrs.getNamedItem("expression");
					paramBuilder.addPropertyValue("expression", attr.getTextContent());
				} 
				params.add(paramBuilder.getBeanDefinition());
			} 
			builder.addPropertyValue("xQueryParameters", params);
		} 
	} 
 
	/** 
	 * Private helper method that is used to set the xquery in the builder. The XQuery can be provided 
	 * using wither the xquery attribute, xquery sub element or the resource containing the xquery. 
	 * These attributes/child node are mutually exclusive to each other, the method checks for this mutual 
	 * exclusivity and sets in the builder for {@link XQueryExecutor} the appripriate attribute. 
	 * 
	 * @param element 
	 * @param builder 
	 */ 
	private static void setXQueryInBuilder(Element element,
			BeanDefinitionBuilder builder) {
		NodeList list = element.getElementsByTagNameNS(element.getNamespaceURI(), "xquery");
		Attr xQueryAttribute = element.getAttributeNode("xquery");
		Attr xQueryResource = element.getAttributeNode("xquery-file-resource");
 
		Assert.isTrue(!(xQueryAttribute != null && xQueryResource != null),
				"Only one of xquery or xquery-file-resource may be specified"); 
 
		Assert.isTrue(!(xQueryAttribute != null && list != null && list.getLength() > 0),
				"At most one of the xquery attribute " + 
				"or the xquery child element should to be provided"); 
 
		Assert.isTrue(!(xQueryResource != null && list != null && list.getLength() > 0),
				"At most one of the xquery-file-resource attribute " + 
				"or the xquery child element should to be provided"); 
 
		Assert.isTrue(xQueryResource != null || xQueryAttribute != null || (list != null && list.getLength() > 0),
				"At least one of xquery, xquery-file-resource attributes or the xquery child element needs to be provided"); 
 
		if(xQueryResource != null) {
			//resource specified 
			String textContent = xQueryResource.getTextContent();
			Assert.isTrue(StringUtils.hasText(textContent),"Non empty, non null resource path should be provided");
			Resource resource;
			if(textContent.startsWith(ResourceUtils.CLASSPATH_URL_PREFIX)) {
				resource = new ClassPathResource(textContent.substring(ResourceUtils.CLASSPATH_URL_PREFIX.length()));
			}