/service/velocity/src/main/java/com/alibaba/citrus/service/velocity/impl/VelocityEngineDefinitionParser.java

https://github.com/webx/citrus · Java · 101 lines · 65 code · 20 blank · 16 comment · 11 complexity · 11ba4ed06adb15cedc959fe45009befa MD5 · raw file

  1. /*
  2. * Copyright (c) 2002-2012 Alibaba Group Holding Limited.
  3. * All rights reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.alibaba.citrus.service.velocity.impl;
  18. import static com.alibaba.citrus.springext.util.DomUtil.*;
  19. import static com.alibaba.citrus.springext.util.SpringExtUtil.*;
  20. import static com.alibaba.citrus.util.Assert.*;
  21. import static com.alibaba.citrus.util.StringUtil.*;
  22. import java.util.List;
  23. import java.util.Map;
  24. import com.alibaba.citrus.springext.ConfigurationPoint;
  25. import com.alibaba.citrus.springext.Contribution;
  26. import com.alibaba.citrus.springext.ContributionAware;
  27. import com.alibaba.citrus.springext.support.parser.AbstractSingleBeanDefinitionParser;
  28. import org.springframework.beans.factory.support.BeanDefinitionBuilder;
  29. import org.springframework.beans.factory.xml.ParserContext;
  30. import org.w3c.dom.Element;
  31. public class VelocityEngineDefinitionParser extends AbstractSingleBeanDefinitionParser<VelocityEngineImpl> implements
  32. ContributionAware {
  33. private ConfigurationPoint pluginsConfigurationPoint;
  34. public void setContribution(Contribution contrib) {
  35. this.pluginsConfigurationPoint = getSiblingConfigurationPoint("services/template/engines/velocity/plugins",
  36. contrib);
  37. }
  38. @Override
  39. protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
  40. attributesToProperties(element, "configuration.", builder, "path", "cacheEnabled", "modificationCheckInterval",
  41. "strictReference", "templateEncoding");
  42. ElementSelector globalMacros = and(sameNs(element), name("global-macros"));
  43. ElementSelector plugins = and(sameNs(element), name("plugins"));
  44. ElementSelector advancedProperties = and(sameNs(element), name("advanced-properties"));
  45. for (Element subElement : subElements(element)) {
  46. if (globalMacros.accept(subElement)) {
  47. parseGlobalMacros(subElement, parserContext, builder);
  48. } else if (plugins.accept(subElement)) {
  49. parsePlugins(subElement, parserContext, builder);
  50. } else if (advancedProperties.accept(subElement)) {
  51. parseAdvancedProperties(subElement, parserContext, builder);
  52. }
  53. }
  54. }
  55. private void parseGlobalMacros(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
  56. List<Object> macros = createManagedList(element, parserContext);
  57. for (Element subElement : subElements(element, and(sameNs(element), name("name")))) {
  58. String name = trimToNull(subElement.getTextContent());
  59. if (name != null) {
  60. macros.add(name);
  61. }
  62. }
  63. builder.addPropertyValue("configuration.globalMacros", macros);
  64. }
  65. private void parsePlugins(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
  66. List<Object> plugins = createManagedList(element, parserContext);
  67. for (Element subElement : subElements(element)) {
  68. plugins.add(parseConfigurationPointBean(subElement, pluginsConfigurationPoint, parserContext, builder));
  69. }
  70. builder.addPropertyValue("configuration.plugins", plugins);
  71. }
  72. private void parseAdvancedProperties(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
  73. Map<Object, Object> props = createManagedMap(element, parserContext);
  74. for (Element subElement : subElements(element, and(sameNs(element), name("property")))) {
  75. String name = assertNotNull(trimToNull(subElement.getAttribute("name")), "propertyName");
  76. String value = trimToEmpty(subElement.getAttribute("value"));
  77. props.put(name, value);
  78. }
  79. builder.addPropertyValue("configuration.advancedProperties", props);
  80. }
  81. }