XMLConfigParser用来解析全局配置文件

    1. package com.example.aninbatis.config;
    2. import java.io.InputStream;
    3. import java.util.List;
    4. import java.util.Properties;
    5. import com.example.aninbatis.utils.DocumentUtils;
    6. import com.example.aninbatis.utils.Resources;
    7. import org.apache.commons.dbcp.BasicDataSource;
    8. import org.dom4j.Document;
    9. import org.dom4j.Element;
    10. /**
    11. * 用来解析全局配置文件
    12. */
    13. public class XMLConfigParser {
    14. /**
    15. * 全局配置封装
    16. */
    17. private Configuration configuration;
    18. public XMLConfigParser() {
    19. configuration = new Configuration();
    20. }
    21. /**
    22. *
    23. * @param rootElement
    24. * <configuration>
    25. * @return
    26. */
    27. public Configuration parse(Element rootElement) {
    28. parseEnvironments(rootElement.element("environments"));
    29. parseMappers(rootElement.element("mappers"));
    30. return configuration;
    31. }
    32. /**
    33. * 解析mappers子标签,最终该标签会去解析每个映射文件
    34. *
    35. * @param element
    36. */
    37. @SuppressWarnings("unchecked")
    38. private void parseMappers(Element element) {
    39. List<Element> elements = element.elements("mapper");
    40. for (Element mapperElement : elements) {
    41. parseMapper(mapperElement);
    42. }
    43. }
    44. private void parseMapper(Element mapperElement) {
    45. // 获取映射文件的路径
    46. String resource = mapperElement.attributeValue("resource");
    47. // 获取指定路径的IO流
    48. InputStream inputStream = Resources.getResourceAsStream(resource);
    49. // 获取映射文件对应的Document对象
    50. Document document = DocumentUtils.readDocument(inputStream);
    51. // 按照mapper标签语义去解析Document
    52. XMLMapperParser mapperParser = new XMLMapperParser(configuration);
    53. mapperParser.parse(document.getRootElement());
    54. }
    55. /**
    56. *
    57. * @param element
    58. * <environments>
    59. */
    60. @SuppressWarnings("unchecked")
    61. private void parseEnvironments(Element element) {
    62. String defaultEnvId = element.attributeValue("default");
    63. if (defaultEnvId == null || "".equals(defaultEnvId)) {
    64. return;
    65. }
    66. List<Element> elements = element.elements("environment");
    67. for (Element envElement : elements) {
    68. String envId = envElement.attributeValue("id");
    69. // 判断defaultEnvId和envId是否一致,一致再继续解析
    70. if (defaultEnvId.equals(envId)) {
    71. parseEnvironment(envElement);
    72. }
    73. }
    74. }
    75. /**
    76. *
    77. * @param envElement
    78. * <environment>
    79. */
    80. private void parseEnvironment(Element envElement) {
    81. Element dataSourceEnv = envElement.element("dataSource");
    82. String type = dataSourceEnv.attributeValue("type");
    83. type = type == null || type.equals("") ? "DBCP" : type;
    84. if ("DBCP".equals(type)) {
    85. Properties properties = parseProperty(dataSourceEnv);
    86. BasicDataSource dataSource = new BasicDataSource();
    87. dataSource.setDriverClassName(properties.getProperty("driver"));
    88. dataSource.setUrl(properties.getProperty("url"));
    89. dataSource.setUsername(properties.getProperty("username"));
    90. dataSource.setPassword(properties.getProperty("password"));
    91. // 将解析出来的DataSource对象,封装到Configuration对象中
    92. configuration.setDataSource(dataSource);
    93. }
    94. }
    95. @SuppressWarnings("unchecked")
    96. private Properties parseProperty(Element dataSourceEnv) {
    97. Properties properties = new Properties();
    98. List<Element> elements = dataSourceEnv.elements("property");
    99. for (Element element : elements) {
    100. String name = element.attributeValue("name");
    101. String value = element.attributeValue("value");
    102. properties.put(name, value);
    103. }
    104. return properties;
    105. }
    106. }