View Javadoc

1   /*
2    * Copyright [2007] [University Corporation for Advanced Internet Development, Inc.]
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package edu.internet2.middleware.shibboleth.common.config.attribute.resolver.attributeDefinition;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  import java.util.Map;
22  
23  import javax.xml.namespace.QName;
24  
25  import org.opensaml.xml.util.DatatypeHelper;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
29  import org.springframework.beans.factory.xml.ParserContext;
30  import org.w3c.dom.Element;
31  
32  /**
33   * Spring bean definition parser for scripted attribute configuration elements.
34   */
35  public class TemplateAttributeDefinitionBeanDefinitionParser extends BaseAttributeDefinitionBeanDefinitionParser {
36  
37      /** Schema type name. */
38      public static final QName TYPE_NAME = new QName(AttributeDefinitionNamespaceHandler.NAMESPACE, "Template");
39  
40      /** SourceValue element name. */
41      public static final QName TEMPLATE_ELEMENT_NAME = new QName(AttributeDefinitionNamespaceHandler.NAMESPACE,
42              "Template");
43  
44      /** SourceValue element name. */
45      public static final QName SOURCE_ATTRIBUTE_ELEMENT_NAME = new QName(AttributeDefinitionNamespaceHandler.NAMESPACE,
46              "SourceAttribute");
47  
48      /** Class logger. */
49      @SuppressWarnings("unused")
50      private final Logger log = LoggerFactory.getLogger(TemplateAttributeDefinitionBeanDefinitionParser.class);
51  
52      /** {@inheritDoc} */
53      protected Class getBeanClass(Element arg0) {
54          return TemplateAttributeDefinitionFactoryBean.class;
55      }
56  
57      /** {@inheritDoc} */
58      protected void doParse(String pluginId, Element pluginConfig, Map<QName, List<Element>> pluginConfigChildren,
59              BeanDefinitionBuilder pluginBuilder, ParserContext parserContext) {
60          super.doParse(pluginId, pluginConfig, pluginConfigChildren, pluginBuilder, parserContext);
61  
62          if (pluginConfigChildren.containsKey(TEMPLATE_ELEMENT_NAME)) {
63              Element templateElement = pluginConfigChildren.get(TEMPLATE_ELEMENT_NAME).get(0);
64              String attributeTemplate = DatatypeHelper.safeTrimOrNullString(templateElement.getTextContent());
65              pluginBuilder.addPropertyValue("attributeTemplate", attributeTemplate);
66          }
67  
68          List<String> sourceAttributes = new ArrayList<String>();
69          for (Element element : pluginConfigChildren.get(SOURCE_ATTRIBUTE_ELEMENT_NAME)) {
70              sourceAttributes.add(DatatypeHelper.safeTrimOrNullString(element.getTextContent()));
71          }
72          pluginBuilder.addPropertyValue("sourceAttributes", sourceAttributes);
73          
74          String velocityEngineRef = pluginConfig.getAttributeNS(null, "velocityEngine");
75          pluginBuilder.addPropertyReference("velocityEngine", velocityEngineRef);
76      }
77  }