Browse Source

完善了部分Xml读取

huangluyu 1 year ago
parent
commit
a0d53863df

+ 1 - 0
BdmOrm.iml

@@ -18,5 +18,6 @@
       </library>
     </orderEntry>
     <orderEntry type="library" name="lib" level="project" />
+    <orderEntry type="library" name="mysql-connector-java-8.0.11" level="project" />
   </component>
 </module>

+ 1 - 0
src/com/huangluyu/bdmorm/bootstrap/scan/BaseFileScan.java

@@ -20,6 +20,7 @@ public class BaseFileScan {
     static {
         fileRegisters.add(new ModelRegister());
         fileRegisters.add(new ConfigureRegister());
+        fileRegisters.add(new XmlRegister());
     }
 
     /**

+ 1 - 1
src/com/huangluyu/bdmorm/bootstrap/scan/ConfigureRegister.java

@@ -39,6 +39,6 @@ public class ConfigureRegister implements BaseFileRegister {
     }
 
     private boolean isPropertiesFile(File file) {
-        return file.isFile() && file.getPath().endsWith(".properties");
+        return file.isFile() && file.getPath().endsWith(".configure");
     }
 }

+ 43 - 1
src/com/huangluyu/bdmorm/bootstrap/scan/XmlRegister.java

@@ -1,6 +1,15 @@
 package com.huangluyu.bdmorm.bootstrap.scan;
 
+import com.huangluyu.bdmorm.bootstrap.BdmWorkSpace;
+import org.w3c.dom.Document;
+import org.xml.sax.SAXException;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.xpath.*;
 import java.io.File;
+import java.io.IOException;
 
 /**
  * com.huangluyu.bdmorm.bootstrap.scan
@@ -10,13 +19,46 @@ import java.io.File;
  */
 public class XmlRegister implements BaseFileRegister {
 
+    private static String DATA_SOURCE = "/dataSource";
+
+    private static String DB_URL = "/connection/url";
+    private static String DB_USER = "/connection/user";
+    private static String DB_PASSWORD = "/connection/password";
+
     @Override
     public void handleFile(File file) {
-
+        if (file.getName().equals("bdm.xml")) {
+            try {
+                Document doc = getDocument(file.getPath());
+                XPathFactory factory = XPathFactory.newInstance();
+                XPath xpath = factory.newXPath();
+                String dbUrl = (String) xpath.compile(DATA_SOURCE + DB_URL).evaluate(doc, XPathConstants.STRING);
+                String dbUser = (String) xpath.compile(DATA_SOURCE + DB_USER).evaluate(doc, XPathConstants.STRING);
+                String dbPassword = (String) xpath.compile(DATA_SOURCE + DB_PASSWORD).evaluate(doc, XPathConstants.STRING);
+                BdmWorkSpace.configure.put("db.url", dbUrl);
+                BdmWorkSpace.configure.put("db.user", dbUser);
+                BdmWorkSpace.configure.put("db.password", dbPassword);
+            } catch (ParserConfigurationException e) {
+                e.printStackTrace();
+            } catch (IOException e) {
+                e.printStackTrace();
+            } catch (SAXException e) {
+                e.printStackTrace();
+            } catch (XPathExpressionException e) {
+                e.printStackTrace();
+            }
+        }
     }
 
     @Override
     public void register() {
 
     }
+
+    private Document getDocument(String filePath) throws ParserConfigurationException, IOException, SAXException {
+        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+        factory.setNamespaceAware(true); // never forget this!
+        DocumentBuilder builder = factory.newDocumentBuilder();
+        return builder.parse(filePath);
+    }
 }

src/com/huangluyu/test/properties/db.properties → src/com/huangluyu/test/configure/db.properties