shhs преди 5 години
ревизия
1de3f718fa

+ 18 - 0
.gitignore

@@ -0,0 +1,18 @@
+.DS_Store
+.idea/*
+.idea
+target
+target/
+.project
+.settings/
+logs
+*.iml
+
+
+# Editor directories and files
+.idea
+.vscode
+*.suo
+*.ntvs*
+*.njsproj
+*.sln

+ 84 - 0
pom.xml

@@ -0,0 +1,84 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>info.edu</groupId>
+  <artifactId>super</artifactId>
+  <version>1.0-SNAPSHOT</version>
+
+  <name>super</name>
+  <!-- FIXME change it to the project's website -->
+  <url>http://www.example.com</url>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    <maven.compiler.source>1.7</maven.compiler.source>
+    <maven.compiler.target>1.7</maven.compiler.target>
+  </properties>
+
+  <dependencies>
+    <!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
+    <dependency>
+      <groupId>dom4j</groupId>
+      <artifactId>dom4j</artifactId>
+      <version>1.6.1</version>
+    </dependency>
+
+    <!-- https://mvnrepository.com/artifact/jaxen/jaxen -->
+    <dependency>
+      <groupId>jaxen</groupId>
+      <artifactId>jaxen</artifactId>
+      <version>1.1.1</version>
+    </dependency>
+
+
+  </dependencies>
+
+  <build>
+    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
+      <plugins>
+        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
+        <plugin>
+          <artifactId>maven-clean-plugin</artifactId>
+          <version>3.1.0</version>
+        </plugin>
+        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
+        <plugin>
+          <artifactId>maven-resources-plugin</artifactId>
+          <version>3.0.2</version>
+        </plugin>
+        <plugin>
+          <artifactId>maven-compiler-plugin</artifactId>
+          <version>3.8.0</version>
+        </plugin>
+        <plugin>
+          <artifactId>maven-surefire-plugin</artifactId>
+          <version>2.22.1</version>
+        </plugin>
+        <plugin>
+          <artifactId>maven-jar-plugin</artifactId>
+          <version>3.0.2</version>
+        </plugin>
+        <plugin>
+          <artifactId>maven-install-plugin</artifactId>
+          <version>2.5.2</version>
+        </plugin>
+        <plugin>
+          <artifactId>maven-deploy-plugin</artifactId>
+          <version>2.8.2</version>
+        </plugin>
+        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
+        <plugin>
+          <artifactId>maven-site-plugin</artifactId>
+          <version>3.7.1</version>
+        </plugin>
+        <plugin>
+          <artifactId>maven-project-info-reports-plugin</artifactId>
+          <version>3.0.0</version>
+        </plugin>
+      </plugins>
+    </pluginManagement>
+  </build>
+</project>

+ 4 - 0
src/main/java/info/edu/BaseMain.java

@@ -0,0 +1,4 @@
+package info.edu;
+
+public class BaseMain {
+}

+ 13 - 0
src/main/java/info/edu/Main.java

@@ -0,0 +1,13 @@
+package info.edu;
+
+/**
+ * Hello world!
+ *
+ */
+public class Main extends BaseMain
+{
+    public static void main( String[] args )
+    {
+        System.out.println( "Hello World!" );
+    }
+}

+ 23 - 0
src/main/java/info/edu/model/Item.java

@@ -0,0 +1,23 @@
+package info.edu.model;
+
+public class Item {
+
+    private Long id;
+    private String name;
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+}

+ 50 - 0
src/main/java/info/edu/model/User.java

@@ -0,0 +1,50 @@
+package info.edu.model;
+
+public class User {
+
+    private Long id;
+    private String account;
+    private String pwd;
+    private String name;
+    private Long created;
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public String getAccount() {
+        return account;
+    }
+
+    public void setAccount(String account) {
+        this.account = account;
+    }
+
+    public String getPwd() {
+        return pwd;
+    }
+
+    public void setPwd(String pwd) {
+        this.pwd = pwd;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public Long getCreated() {
+        return created;
+    }
+
+    public void setCreated(Long created) {
+        this.created = created;
+    }
+}

+ 27 - 0
src/main/java/info/edu/service/UserService.java

@@ -0,0 +1,27 @@
+package info.edu.service;
+
+import info.edu.model.Item;
+import info.edu.model.User;
+import info.edu.utils.IdGen;
+import info.edu.utils.XmlUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class UserService extends XmlUtils<User> {
+
+    public static void main(String[] args) throws Exception{
+        XmlUtils xmlService = new XmlUtils();
+        List<Item> users = new ArrayList<>();
+        Item user = new Item();
+        user.setId(IdGen.getId());
+//        user.setAccount("admin");
+        user.setName("ceshiss");
+//        user.setPwd("123456");
+//        user.setCreated(System.currentTimeMillis());
+        users.add(user);
+//        xmlService.writeInnerXml("user.xml",users);
+        System.out.println(xmlService.readInnerXmlByAccount("user.xml","admin"));
+
+    }
+}

+ 91 - 0
src/main/java/info/edu/utils/IdGen.java

@@ -0,0 +1,91 @@
+package info.edu.utils;
+
+public class IdGen
+{
+    private long workerId;
+    private long datacenterId;
+    private long sequence;
+    private long twepoch;
+    private long workerIdBits;
+    private long datacenterIdBits;
+    private long maxWorkerId;
+    private long maxDatacenterId;
+    private long sequenceBits;
+    private long workerIdShift;
+    private long datacenterIdShift;
+    private long timestampLeftShift;
+    private long sequenceMask;
+    private long lastTimestamp;
+
+    public static IdGen getInstance() {
+        return IdGenHolder.instance;
+    }
+
+    public IdGen() {
+        this(0L, 0L);
+    }
+
+    public IdGen(final long workerId, final long datacenterId) {
+        this.sequence = 0L;
+        this.twepoch = 1288834974657L;
+        this.workerIdBits = 5L;
+        this.datacenterIdBits = 5L;
+        this.maxWorkerId = (-1L ^ -1L << (int)this.workerIdBits);
+        this.maxDatacenterId = (-1L ^ -1L << (int)this.datacenterIdBits);
+        this.sequenceBits = 12L;
+        this.workerIdShift = this.sequenceBits;
+        this.datacenterIdShift = this.sequenceBits + this.workerIdBits;
+        this.timestampLeftShift = this.sequenceBits + this.workerIdBits + this.datacenterIdBits;
+        this.sequenceMask = (-1L ^ -1L << (int)this.sequenceBits);
+        this.lastTimestamp = -1L;
+        if (workerId > this.maxWorkerId || workerId < 0L) {
+            throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", this.maxWorkerId));
+        }
+        if (datacenterId > this.maxDatacenterId || datacenterId < 0L) {
+            throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", this.maxDatacenterId));
+        }
+        this.workerId = workerId;
+        this.datacenterId = datacenterId;
+    }
+
+    public synchronized long Id() {
+        long timestamp = this.timeGen();
+        if (timestamp < this.lastTimestamp) {
+            throw new RuntimeException(String.format("Clock moved backwards.  Refusing to generate id for %d milliseconds", this.lastTimestamp - timestamp));
+        }
+        if (this.lastTimestamp == timestamp) {
+            this.sequence = (this.sequence + 1L & this.sequenceMask);
+            if (this.sequence == 0L) {
+                timestamp = this.tilNextMillis(this.lastTimestamp);
+            }
+        }
+        else {
+            this.sequence = 0L;
+        }
+        this.lastTimestamp = timestamp;
+        return timestamp - this.twepoch << (int)this.timestampLeftShift | this.datacenterId << (int)this.datacenterIdShift | this.workerId << (int)this.workerIdShift | this.sequence;
+    }
+
+    protected long tilNextMillis(final long lastTimestamp) {
+        long timestamp;
+        for (timestamp = this.timeGen(); timestamp <= lastTimestamp; timestamp = this.timeGen()) {}
+        return timestamp;
+    }
+
+    protected long timeGen() {
+        return System.currentTimeMillis();
+    }
+
+    public static long getId() {
+        return getInstance().Id();
+    }
+
+    private static class IdGenHolder
+    {
+        private static final IdGen instance;
+
+        static {
+            instance = new IdGen();
+        }
+    }
+}

+ 45 - 0
src/main/java/info/edu/utils/UtilsForXML.java

@@ -0,0 +1,45 @@
+package info.edu.utils;
+
+import org.dom4j.Document;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.io.FileOutputStream;
+import java.io.OutputStreamWriter;
+import org.dom4j.io.OutputFormat;
+import org.dom4j.io.SAXReader;
+import org.dom4j.io.XMLWriter;
+
+    public class UtilsForXML {
+        /**
+         * 需要一个方法来创建DOM4j的XML解析器并返回一个document对象
+         * @throws Exception
+         */
+        public static Document getDocument(String xmlPath) throws Exception {
+            Reader reader = new InputStreamReader(new FileInputStream(new File(xmlPath)),"utf-8");
+            SAXReader saxReader = new SAXReader();
+            //将XML文件路径传给Document对象并返回其实例dom
+            Document dom = saxReader.read(reader);
+            return dom;
+        }
+
+        /**
+         * 需要一个方法来将更新后的document对象写入到XML文件中去
+         * add by hanwl
+         * @throws Exception
+         */
+        public static void writeToXML(Document dom ,String xmlPath) throws Exception{
+
+            //首先创建样式和输出流
+            OutputFormat format = new OutputFormat().createPrettyPrint();
+            OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(xmlPath),"utf-8");
+            //OutputStream out = new FileOutputStream(xmlPath);
+            XMLWriter writer = new XMLWriter(out,format);
+
+            //写入之后关闭流
+            writer.write(dom);
+            writer.close();
+        }
+}

+ 241 - 0
src/main/java/info/edu/utils/XmlUtils.java

@@ -0,0 +1,241 @@
+package info.edu.utils;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.lang.reflect.Field;
+import java.util.*;
+
+import info.edu.model.User;
+import org.dom4j.Document;
+import org.dom4j.DocumentHelper;
+import org.dom4j.Element;
+import org.dom4j.io.SAXReader;
+import org.dom4j.io.XMLWriter;
+
+/*
+ * Created:20191218
+ * Xml Generic Tool Service
+ * */
+public class XmlUtils<T> {
+
+    // XML 存放路径
+    private final String defaultPath = "src/xml/";
+
+    /*
+     * 查询全部xml数据
+     * */
+    public List<T> readInnerXml(String path) throws Exception{
+        File dir = new File(defaultPath+path);
+        if (!dir.exists()) {
+            dir.createNewFile();
+            Document dom = DocumentHelper.createDocument();
+            Element root = dom.addElement("table");
+            String dirpath = dir+"";
+            UtilsForXML.writeToXML(dom, dirpath);
+        }
+        String dirPath = dir+"";
+        Document dom = UtilsForXML.getDocument(dirPath);
+        Element root = dom.getRootElement();
+        List<T> tables = new ArrayList<>();
+        List list = root.elements();
+        //处理root节点数据
+        for (int i = 0; i < list.size(); i++) {
+            Element person = (Element) list.get(i);
+            Map<String,Object> res = new HashMap<>();
+            //获取子节点数据
+            for (Iterator iterator = person.elementIterator(); iterator.hasNext();) {
+                Element type = (Element) iterator.next();
+                res.put(type.getName(),type.getStringValue());
+            }
+            tables.add((T)res);
+        }
+        return tables;
+    }
+
+    /*
+     * 根据item的属性id查询xml
+     * */
+    public T readInnerXmlById(String path,Long id) throws Exception{
+        File dir = new File(defaultPath+path);
+        String dirPath = dir+"";
+        Document dom = UtilsForXML.getDocument(dirPath);
+        Element root = dom.getRootElement();
+        Element beQuery = (Element)root.selectSingleNode("//item[@id="+id.toString()+"]");
+        Map<String,Object> res = new HashMap<>();
+        if(beQuery!=null){
+            for (Iterator iterator = beQuery.elementIterator(); iterator.hasNext();) {
+                Element type = (Element) iterator.next();
+                res.put(type.getName(),type.getStringValue());
+            }
+        }
+        return (T)res;
+    }
+
+    /*
+     * 根据item的属性id查询xml
+     * */
+    public T readInnerXmlByAccount(String path,String account) throws Exception{
+        File dir = new File(defaultPath+path);
+        String dirPath = dir+"";
+        Document dom = UtilsForXML.getDocument(dirPath);
+        Element root = dom.getRootElement();
+        Element beQuery = (Element)root.selectSingleNode("//item[@account="+account+"]");
+        Map<String,Object> res = new HashMap<>();
+        if(beQuery!=null){
+            for (Iterator iterator = beQuery.elementIterator(); iterator.hasNext();) {
+                Element type = (Element) iterator.next();
+                res.put(type.getName(),type.getStringValue());
+            }
+        }
+        return (T)res;
+    }
+
+    /*
+     * 增加xml数据
+     * */
+    public int writeInnerXml(String path,List<T> lists) throws Exception{
+        File dir = new File(defaultPath+path);
+        if (!dir.exists()) {
+            dir.createNewFile();
+        }
+        int id = 1;
+        String dirPath = dir+"";
+        Document dom = UtilsForXML.getDocument(dirPath);
+        Element root = dom.getRootElement();
+        for (T list : lists) {
+            Field[] fields = list.getClass().getDeclaredFields();
+            Element newPerson = root.addElement("item");
+            for(int i=0; i<fields.length; i++){
+                Field f = fields[i];
+                f.setAccessible(true);
+                if(f.getName() == "id"){
+                    newPerson.addAttribute("id", f.get(list).toString());
+                }
+                if(f.getName() == "account"){
+                    newPerson.addAttribute("account", f.get(list).toString());
+                }
+                Element element = newPerson.addElement(f.getName());
+                element.setText(f.get(list).toString());
+            }
+        }
+        UtilsForXML.writeToXML(dom, dirPath);
+        return id;
+    }
+
+    /*
+     * 根据person属性id修改xml数据
+     * */
+    public static int UpdateClinetLoginUser(int id,String appDir,String publisherName,String serverUrl,String userName,String passWord) throws Exception{
+        File dir = new File(appDir+"\\persons.xml");
+        String dirPath = dir+"";
+        Document dom = UtilsForXML.getDocument(dirPath);
+        Element root = dom.getRootElement();
+        Element beQuery = (Element)root.selectSingleNode("//person[@id="+id+"]");
+        beQuery.element("publishername").setText(publisherName);
+        beQuery.element("serverurl").setText(serverUrl);
+        beQuery.element("username").setText(userName);
+        beQuery.element("password").setText(passWord);
+        UtilsForXML.writeToXML(dom, dirPath);
+        return id;
+    }
+    /*
+     * 根据person属性id删除xml数据
+     * */
+    public static int DeleteClinetLoginUser(int id,String appDir) throws Exception{
+        File dir = new File(appDir+"\\persons.xml");
+        String dirPath = dir+"";
+        Document dom = UtilsForXML.getDocument(dirPath);
+        Element root = dom.getRootElement();
+        Element beQuery = (Element)root.selectSingleNode("//person[@id="+id+"]");
+        beQuery.getParent().remove(beQuery);
+        UtilsForXML.writeToXML(dom, dirPath);
+        return id;
+    }
+
+    public void checkXml(String path){
+        File file = new File(defaultPath+path);
+        if(file.exists()){
+            System.out.println("file success found");
+        }else {
+            System.out.println("fils not found ");
+            createdXml(file);
+        }
+    }
+
+    public void createdXml(File file){
+        try {
+            Document doc = DocumentHelper.createDocument();
+            FileWriter os = new FileWriter(file);
+            doc.write(os);
+            os.flush();
+            os.close();
+        }catch (Exception e){
+            System.out.println("文件创建失败");
+        }
+
+    }
+
+    public void test(List<T> lists) throws Exception{
+
+        for (T list : lists) {
+            Field[] fields = list.getClass().getDeclaredFields();
+            for(int i=0; i<fields.length; i++){
+                Field f = fields[i];
+                f.setAccessible(true);
+                System.out.println("属性名:" + f.getName() + " 属性值:" + f.get(list));
+            }
+        }
+    }
+
+
+//    public void  writeInnerXml(String path, List<T> lists) throws Exception{
+//
+//        List<T> oldRes = new ArrayList<>();
+//        // 提取原有数据
+//        oldRes = readInnerXml(path);
+//        if (oldRes == null){
+//            System.out.println("not relast");
+//        }
+//        Document doc = DocumentHelper.createDocument();
+//        Element ele = doc.s("table");
+//
+//        for (T list : lists) {
+//            Field[] fields = list.getClass().getDeclaredFields();
+//            for(int i=0; i<fields.length; i++){
+//                Field f = fields[i];
+//                f.setAccessible(true);
+//                ele.setA("id", "1");
+//                Element element = ele.addElement(f.getName());
+//                element.setText(f.get(list).toString());
+//            }
+//        }
+//        try {
+//            XMLWriter writer = new XMLWriter(new FileWriter(new File(path)));
+//            writer.write(doc);
+//            writer.close();
+//        } catch (Exception e) {
+//            // TODO: handle exception
+//        }
+//    }
+
+
+//    public List<T> readInnerXml(String path) {
+//        try {
+//            File file = new File(path);
+//            SAXReader reader = new SAXReader();
+//            Document doc = reader.read(file);
+//            Element root = doc.getRootElement();
+//            List<T> lists = new ArrayList<>();
+////            root.
+//            for (Iterator iterator = root.elementIterator(); iterator.hasNext();) {
+//                Map<String,Object> res = new HashMap<>();
+//                Element type = (Element) iterator.next();
+//                res.put(type.getName(),type.getStringValue());
+//                lists.add((T)res);
+//            }
+//            return lists;
+//        } catch (Exception e) {
+//            return null;
+//        }
+//    }
+}

+ 48 - 0
src/xml/item.xml

@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<table> 
+  <item id="1"> 
+    <id>1</id>  
+    <name>ceshi</name> 
+  </item>  
+  <item id="1"> 
+    <id>1</id>  
+    <name>ceshi</name> 
+  </item>  
+  <item id="1"> 
+    <id>1</id>  
+    <name>ceshiss</name> 
+  </item>  
+  <item id="1"> 
+    <id>1</id>  
+    <name>ceshiss</name> 
+  </item>  
+  <item id="1"> 
+    <id>1207227379462701056</id>  
+    <name>ceshiss</name> 
+  </item>  
+  <item id="1"> 
+    <id>1207228367980462080</id>  
+    <name>ceshiss</name> 
+  </item>  
+  <item id="1207228527720529920"> 
+    <id>1207228527720529920</id>  
+    <name>ceshiss</name> 
+  </item>  
+  <item id="1207228568648548352"> 
+    <id>1207228568648548352</id>  
+    <name>ceshiss</name> 
+  </item>  
+  <item id="1207228583332806656"> 
+    <id>1207228583332806656</id>  
+    <name>ceshiss</name> 
+  </item>  
+  <item id="1207234846737301504"> 
+    <id>1207234846737301504</id>  
+    <name>ceshiss</name> 
+  </item>  
+  <item id="1207234908804612096">
+    <id>1207234908804612096</id>
+    <name>ceshiss</name>
+  </item>
+</table>

+ 11 - 0
src/xml/user.xml

@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<table>
+  <item id="1207233501422682112" account="admin">
+    <id>1207233501422682112</id>
+    <account>admin</account>
+    <pwd>123456</pwd>
+    <name>ceshiss</name>
+    <created>1576661870710</created>
+  </item>
+</table>