From 917c7cae1533746fec04a35d068f8d031c2a1560 Mon Sep 17 00:00:00 2001 From: zho <5061827@qq.com> Date: Thu, 7 Jun 2018 17:21:16 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 52 +++++ .../com/mec/util/ConfigNotFontException.java | 25 +++ .../com/mec/util/DriverDirectoryFile.java | 108 +++++++++++ src/main/java/com/mec/util/FileProcess.java | 57 ++++++ src/main/java/com/mec/util/KeyAdpter.java | 20 ++ .../java/com/mec/util/PackageScanner.java | 136 +++++++++++++ ...ingApplicationContextNotLoadException.java | 25 +++ .../com/mec/util/SpringContextHolder.java | 39 ++++ src/main/java/com/mec/util/ThreadLooker.java | 21 ++ .../com/mec/util/TypeTooSimpleException.java | 24 +++ .../java/com/mec/util/ValidationRule.java | 73 +++++++ .../ValidationRuleNotDefinedException.java | 25 +++ .../util/XMLFilePathNotExistException.java | 25 +++ src/main/java/com/mec/util/XMLReader.java | 108 +++++++++++ src/main/java/com/mec/util/test/Test.java | 8 + .../java/com/mec/util/xml/model/FileInfo.java | 82 ++++++++ .../java/com/mec/util/xml/model/FileList.java | 67 +++++++ .../java/com/mec/util/xml/model/TagModel.java | 113 +++++++++++ .../java/com/mec/util/xml/model/XMLModel.java | 179 ++++++++++++++++++ 19 files changed, 1187 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/com/mec/util/ConfigNotFontException.java create mode 100644 src/main/java/com/mec/util/DriverDirectoryFile.java create mode 100644 src/main/java/com/mec/util/FileProcess.java create mode 100644 src/main/java/com/mec/util/KeyAdpter.java create mode 100644 src/main/java/com/mec/util/PackageScanner.java create mode 100644 src/main/java/com/mec/util/SpringApplicationContextNotLoadException.java create mode 100644 src/main/java/com/mec/util/SpringContextHolder.java create mode 100644 src/main/java/com/mec/util/ThreadLooker.java create mode 100644 src/main/java/com/mec/util/TypeTooSimpleException.java create mode 100644 src/main/java/com/mec/util/ValidationRule.java create mode 100644 src/main/java/com/mec/util/ValidationRuleNotDefinedException.java create mode 100644 src/main/java/com/mec/util/XMLFilePathNotExistException.java create mode 100644 src/main/java/com/mec/util/XMLReader.java create mode 100644 src/main/java/com/mec/util/test/Test.java create mode 100644 src/main/java/com/mec/util/xml/model/FileInfo.java create mode 100644 src/main/java/com/mec/util/xml/model/FileList.java create mode 100644 src/main/java/com/mec/util/xml/model/TagModel.java create mode 100644 src/main/java/com/mec/util/xml/model/XMLModel.java diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..a6800f4 --- /dev/null +++ b/pom.xml @@ -0,0 +1,52 @@ + + + 4.0.0 + + com.mec + MecUtils + 1.0-SNAPSHOT + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.6.1 + + 1.8 + 1.8 + + + + + + + + + org.springframework + spring-core + 4.3.13.RELEASE + + + + org.springframework + spring-context + 4.3.13.RELEASE + + + + org.springframework + spring-beans + 4.3.13.RELEASE + + + + org.springframework + spring-context-support + 4.3.13.RELEASE + + + + \ No newline at end of file diff --git a/src/main/java/com/mec/util/ConfigNotFontException.java b/src/main/java/com/mec/util/ConfigNotFontException.java new file mode 100644 index 0000000..4a5862d --- /dev/null +++ b/src/main/java/com/mec/util/ConfigNotFontException.java @@ -0,0 +1,25 @@ +package com.mec.util; + +public class ConfigNotFontException extends Exception { + private static final long serialVersionUID = -5133801915010084919L; + + public ConfigNotFontException() { + } + + public ConfigNotFontException(String message) { + super(message); + } + + public ConfigNotFontException(Throwable cause) { + super(cause); + } + + public ConfigNotFontException(String message, Throwable cause) { + super(message, cause); + } + + public ConfigNotFontException(String message, Throwable cause, boolean enableSuppression, + boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/main/java/com/mec/util/DriverDirectoryFile.java b/src/main/java/com/mec/util/DriverDirectoryFile.java new file mode 100644 index 0000000..36817ef --- /dev/null +++ b/src/main/java/com/mec/util/DriverDirectoryFile.java @@ -0,0 +1,108 @@ +package com.mec.util; + +import java.io.File; + +import com.mec.util.xml.model.TagModel; +import com.mec.util.xml.model.XMLModel; + +public class DriverDirectoryFile { + public static final String TAG_DRIVERS = "drivers"; + public static final String TAG_DRIVER = "driver"; + public static final String TAG_DIRECTORY = "dir"; + public static final String TAG_ATTRIBUTE_TYPE = "type"; + public static final String TAG_ATTRIBUTE_NAME = "name"; + + public static final String TAG_TYPE_DRIVER = "driver"; + public static final String TAG_TYPE_DIRECTORY = "directory"; + public static final String TAG_TYPE_FILE = "file"; + + public static String getRootXString(boolean prettyFormat) { + XMLModel root = new XMLModel(TAG_DRIVERS); + + File[] drivers = File.listRoots(); + for (File driver : drivers) { + String absolutePath = driver.getAbsolutePath(); + absolutePath = absolutePath.substring(0, absolutePath.length()-1); + + TagModel tag = new TagModel(TAG_DRIVER) + .addAttribute(TAG_ATTRIBUTE_TYPE, TAG_TYPE_DRIVER) + .addAttribute(TAG_ATTRIBUTE_NAME, absolutePath); + root.addTag(tag); + } + + return root.writeToString(prettyFormat); + } + + public static String getRootXString() { + return getRootXString(false); + } + + public static String getDriverRootList(String driver, boolean prettyFormat) { + if (!driver.substring(1).startsWith(":")) { + return null; + } + + File file = new File(driver); + XMLModel CDriver = new XMLModel(TAG_DRIVER) + .addRootTagAttribute(TAG_ATTRIBUTE_NAME, driver) + .addRootTagAttribute(TAG_ATTRIBUTE_TYPE, TAG_TYPE_DRIVER); + + File[] dirs = file.listFiles(); + for (File dir : dirs) { + String dirPath = dir.getName(); + boolean isFile = dir.isFile(); + boolean isDir = dir.isDirectory(); + if(dir.isHidden()) { + continue; + } + TagModel tag = new TagModel("file") + .addAttribute("type", isFile ? "file" : (isDir ? "dir" : "unknow")) + .addAttribute("name", dirPath); + CDriver.addTag(tag); + } + + return CDriver.writeToString(prettyFormat); + } + + public static String getDriverRootList(String driver) { + return getDriverRootList(driver, false); + } + + public static String getFileList(String path, boolean prettyFormat) { + File file = new File(path); + String type = file.isFile() ? TAG_TYPE_FILE + : (file.isDirectory() ? TAG_TYPE_DIRECTORY : "UK"); + XMLModel CDriver = new XMLModel(type) + .addRootTagAttribute(TAG_ATTRIBUTE_NAME, path) + .addRootTagAttribute(TAG_ATTRIBUTE_TYPE, TAG_TYPE_DRIVER); + + File[] dirs = file.listFiles(); + for (File dir : dirs) { + String dirPath = dir.getName(); + boolean isFile = dir.isFile(); + boolean isDir = dir.isDirectory(); + if(dir.isHidden()) { + continue; + } + TagModel tag = new TagModel("file") + .addAttribute("type", isFile ? "file" : (isDir ? "dir" : "unknow")) + .addAttribute("name", dirPath); + CDriver.addTag(tag); + } + + return CDriver.writeToString(prettyFormat); + } + + public static String getFileList(String path) { + return getFileList(path, false); + } + + public static String pathToString(String path) { + return path.replaceAll("\\\\", "*"); + } + + public static String stringToPath(String path) { + return path.replaceAll("\\*", "\\\\"); + } + +} diff --git a/src/main/java/com/mec/util/FileProcess.java b/src/main/java/com/mec/util/FileProcess.java new file mode 100644 index 0000000..8015291 --- /dev/null +++ b/src/main/java/com/mec/util/FileProcess.java @@ -0,0 +1,57 @@ +package com.mec.util; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.channels.FileChannel; + +public class FileProcess { + public static final int DEFAULT_BUFFER_SIZE = 1 << 22; + + public static boolean fileCopy(String src, String tag) { + File in = new File(src); + if(!in.exists()) { + return false; + } + File out = new File(tag); + return fileCopy(in, out); + } + + public static boolean fileCopy(File srcFile, File tagFile) { + FileInputStream fis = null; + FileOutputStream fos = null; + FileChannel inFileChannel = null; + FileChannel outFileChannel = null; + int restSize = (int) srcFile.length(); + + try { + fis = new FileInputStream(srcFile); + fos = new FileOutputStream(tagFile); + + inFileChannel = fis.getChannel(); + outFileChannel = fos.getChannel(); + + inFileChannel.transferTo(0, restSize, outFileChannel); + } catch (FileNotFoundException e) { + return false; + } catch (IOException e) { + return false; + } finally { + if(fis != null) { + try { + fis.close(); + } catch (IOException e) { } + } + if(fos != null) { + try { + fos.close(); + } catch (IOException e) { } + } + } + + + return true; + } +} diff --git a/src/main/java/com/mec/util/KeyAdpter.java b/src/main/java/com/mec/util/KeyAdpter.java new file mode 100644 index 0000000..0bb7d1c --- /dev/null +++ b/src/main/java/com/mec/util/KeyAdpter.java @@ -0,0 +1,20 @@ +package com.mec.util; + +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; + +public class KeyAdpter implements KeyListener { + + @Override + public void keyTyped(KeyEvent e) { + } + + @Override + public void keyPressed(KeyEvent e) { + } + + @Override + public void keyReleased(KeyEvent e) { + } + +} diff --git a/src/main/java/com/mec/util/PackageScanner.java b/src/main/java/com/mec/util/PackageScanner.java new file mode 100644 index 0000000..c320f8e --- /dev/null +++ b/src/main/java/com/mec/util/PackageScanner.java @@ -0,0 +1,136 @@ +package com.mec.util; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.JarURLConnection; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.Enumeration; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +public abstract class PackageScanner { + private ClassLoader classLoader; + + public PackageScanner() { + } + + public abstract void dealClass(Class klass); + + private void scanPackage(String path, String packageName) { + File curFile = new File(path); + if(!curFile.exists()) { + return; + } + File[] files = curFile.listFiles(); + for(File file : files) { + if(file.isDirectory()) { + scanPackage(file.getAbsolutePath(), + packageName + "." + file.getName()); + continue; + } + if(file.isFile() && file.getName().endsWith(".class")) { + String fileName = file.getName(); + int dotInde = fileName.indexOf(".class"); + fileName = fileName.substring(0, dotInde); + String className = packageName + "." + fileName; + try { + Class klass = Class.forName(className); + dealClass(klass); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } + } + } + + private void scanPackage(URL url) { + try { + JarURLConnection jarURLConnection = + (JarURLConnection) url.openConnection(); + JarFile jarFile = jarURLConnection.getJarFile(); + Enumeration jarEntries = jarFile.entries(); + while(jarEntries.hasMoreElements()) { + JarEntry jarEntry = jarEntries.nextElement(); + if(jarEntry.isDirectory() || + !jarEntry.getName().endsWith(".class")) { + continue; + } + String className = jarEntry.getName(); + int dotIndex = className.indexOf(".class"); + className = className.substring(0, dotIndex).replace("/", "."); + if(!className.startsWith("com.mec")) { + continue; + } + try { + Class klass = Class.forName(className); + dealClass(klass); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void packageScanner(Class clazz) { + packageScanner(clazz.getPackage().getName()); + } + + public void packageScannerByXml(String xmlPath) + throws XMLFilePathNotExistException { + if(xmlPath == null) { + System.out.println("XML路径不能为空!"); + } + + InputStream is = Class.class.getResourceAsStream(xmlPath); + if(is == null) { + throw new XMLFilePathNotExistException("XML文件[]不存在!"); + } + Document document = XMLReader.openDocument(is); + new XMLReader() { + + @Override + public void dealElement(Element element, int index) { + String packageName = element.getAttribute("package"); + packageScanner(packageName); + } + }.dealElementByTagName(document, "askfor"); + + try { + is.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void packageScanner(String rootPackage) { + String rootPath = rootPackage.replace(".", "/"); + this.classLoader = Thread.currentThread().getContextClassLoader(); + Enumeration urls; + try { + urls = classLoader.getResources(rootPath); + while(urls.hasMoreElements()) { + URL url = urls.nextElement(); + String jarProtocol = url.getProtocol(); + if(jarProtocol.equals("file")) { + try { + File file = new File(url.toURI()); + scanPackage(file.getAbsolutePath(), rootPackage); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + } else if(jarProtocol.equals("jar")) { + scanPackage(url); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/com/mec/util/SpringApplicationContextNotLoadException.java b/src/main/java/com/mec/util/SpringApplicationContextNotLoadException.java new file mode 100644 index 0000000..d896596 --- /dev/null +++ b/src/main/java/com/mec/util/SpringApplicationContextNotLoadException.java @@ -0,0 +1,25 @@ +package com.mec.util; + +public class SpringApplicationContextNotLoadException extends RuntimeException { + private static final long serialVersionUID = -5997168462288626954L; + + public SpringApplicationContextNotLoadException() { + } + + public SpringApplicationContextNotLoadException(String message) { + super(message); + } + + public SpringApplicationContextNotLoadException(Throwable cause) { + super(cause); + } + + public SpringApplicationContextNotLoadException(String message, Throwable cause) { + super(message, cause); + } + + public SpringApplicationContextNotLoadException(String message, Throwable cause, boolean enableSuppression, + boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/main/java/com/mec/util/SpringContextHolder.java b/src/main/java/com/mec/util/SpringContextHolder.java new file mode 100644 index 0000000..d6b3cf4 --- /dev/null +++ b/src/main/java/com/mec/util/SpringContextHolder.java @@ -0,0 +1,39 @@ +package com.mec.util; + +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class SpringContextHolder { + private static volatile ClassPathXmlApplicationContext context; + + public SpringContextHolder(String springConfigXmlPath) { + if(context == null) { + synchronized (SpringContextHolder.class) { + if(context == null) { + context = new ClassPathXmlApplicationContext(springConfigXmlPath); + context.start(); + } + } + } + } + + public SpringContextHolder() { + if(context == null) { + throw new SpringApplicationContextNotLoadException("Spring上下文没有初始化!"); + } + } + + public void closeContext() { + context.close(); + context = null; + } + + @SuppressWarnings("unchecked") + public T getBean(Class clazz) { + return (T) context.getBean(clazz); + } + + @SuppressWarnings("unchecked") + public T getBean(String beanName) { + return (T) context.getBean(beanName); + } +} diff --git a/src/main/java/com/mec/util/ThreadLooker.java b/src/main/java/com/mec/util/ThreadLooker.java new file mode 100644 index 0000000..b426447 --- /dev/null +++ b/src/main/java/com/mec/util/ThreadLooker.java @@ -0,0 +1,21 @@ +package com.mec.util; + +public class ThreadLooker { + + public ThreadLooker() { + } + + public static void lookThreads() { + ThreadGroup thGroup = Thread.currentThread() + .getThreadGroup().getParent(); + + Thread[] threads = new Thread[thGroup.activeCount()]; + thGroup.enumerate(threads); + for(int i = 0; i < threads.length; i++) { + Thread thread = threads[i]; + System.out.println("[" + thread.getId() + "]" + thread.getName() + + ", " + thread.getState()); + } + } + +} diff --git a/src/main/java/com/mec/util/TypeTooSimpleException.java b/src/main/java/com/mec/util/TypeTooSimpleException.java new file mode 100644 index 0000000..5e4cea3 --- /dev/null +++ b/src/main/java/com/mec/util/TypeTooSimpleException.java @@ -0,0 +1,24 @@ +package com.mec.util; + +public class TypeTooSimpleException extends RuntimeException { + private static final long serialVersionUID = 7037074730406711821L; + + public TypeTooSimpleException() { + } + + public TypeTooSimpleException(String arg0) { + super(arg0); + } + + public TypeTooSimpleException(Throwable arg0) { + super(arg0); + } + + public TypeTooSimpleException(String arg0, Throwable arg1) { + super(arg0, arg1); + } + + public TypeTooSimpleException(String arg0, Throwable arg1, boolean arg2, boolean arg3) { + super(arg0, arg1, arg2, arg3); + } +} diff --git a/src/main/java/com/mec/util/ValidationRule.java b/src/main/java/com/mec/util/ValidationRule.java new file mode 100644 index 0000000..91dd00b --- /dev/null +++ b/src/main/java/com/mec/util/ValidationRule.java @@ -0,0 +1,73 @@ +package com.mec.util; + +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Pattern; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +public class ValidationRule { + private static volatile Map validationRuleMap; + + private void init(String xmlPath) { + InputStream is = Class.class.getResourceAsStream(xmlPath); + try { + if(is == null) { + throw new ConfigNotFontException("配置文件:config.xml不存在!"); + } + + Document document = XMLReader.openDocument(is); + if(document == null) { + throw new ConfigNotFontException("配置文件:config.xml不存在!"); + } + + new XMLReader() { + @Override + public void dealElement(Element type, int index) { + String name = type.getAttribute("name"); + String rule = type.getAttribute("rule"); + validationRuleMap.put(name, rule); + } + }.dealElementByTagName(document, "rule"); + } catch (ConfigNotFontException e) { + e.printStackTrace(); + } finally { + try { + is.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + public ValidationRule(String xmlPath) { + if(validationRuleMap == null) { + synchronized (ValidationRule.class) { + if(validationRuleMap == null) { + validationRuleMap = new HashMap<>(); + init(xmlPath); + } + } + } + } + + public ValidationRule() { + } + + public static boolean checkString(String ruleType, String string) { + if(validationRuleMap == null) { + throw new ValidationRuleNotDefinedException("有效性规则尚未定义!"); + } + String rule = validationRuleMap.get(ruleType); + if(rule == null) { + System.out.println("规则:[" + ruleType + "]尚未定义!"); + return false; + } + + Pattern pattern = Pattern.compile(rule); + return pattern.matcher(string).matches(); + } +} diff --git a/src/main/java/com/mec/util/ValidationRuleNotDefinedException.java b/src/main/java/com/mec/util/ValidationRuleNotDefinedException.java new file mode 100644 index 0000000..729739e --- /dev/null +++ b/src/main/java/com/mec/util/ValidationRuleNotDefinedException.java @@ -0,0 +1,25 @@ +package com.mec.util; + +public class ValidationRuleNotDefinedException extends RuntimeException { + private static final long serialVersionUID = 4125527012429522903L; + + public ValidationRuleNotDefinedException() { + } + + public ValidationRuleNotDefinedException(String message) { + super(message); + } + + public ValidationRuleNotDefinedException(Throwable cause) { + super(cause); + } + + public ValidationRuleNotDefinedException(String message, Throwable cause) { + super(message, cause); + } + + public ValidationRuleNotDefinedException(String message, Throwable cause, boolean enableSuppression, + boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/main/java/com/mec/util/XMLFilePathNotExistException.java b/src/main/java/com/mec/util/XMLFilePathNotExistException.java new file mode 100644 index 0000000..075c90d --- /dev/null +++ b/src/main/java/com/mec/util/XMLFilePathNotExistException.java @@ -0,0 +1,25 @@ +package com.mec.util; + +public class XMLFilePathNotExistException extends Exception { + private static final long serialVersionUID = -5419215660656868618L; + + public XMLFilePathNotExistException() { + } + + public XMLFilePathNotExistException(String message) { + super(message); + } + + public XMLFilePathNotExistException(Throwable cause) { + super(cause); + } + + public XMLFilePathNotExistException(String message, Throwable cause) { + super(message, cause); + } + + public XMLFilePathNotExistException(String message, Throwable cause, boolean enableSuppression, + boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/main/java/com/mec/util/XMLReader.java b/src/main/java/com/mec/util/XMLReader.java new file mode 100644 index 0000000..37ff80e --- /dev/null +++ b/src/main/java/com/mec/util/XMLReader.java @@ -0,0 +1,108 @@ +package com.mec.util; + +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +public abstract class XMLReader { + private static DocumentBuilder documentBuilder; + + static { + try { + documentBuilder = DocumentBuilderFactory + .newInstance() + .newDocumentBuilder(); + } catch (ParserConfigurationException e) { + e.printStackTrace(); + } + } + + public XMLReader() { + } + + public static InputStream stringToStream(String string) { + if (string == null || string.trim().length() <= 0) { + return null; + } + + return new ByteArrayInputStream(string.getBytes()); + } + + public static String streamToString(InputStream in) { + if (in == null) { + return null; + } + + BufferedReader bufferReader = new BufferedReader(new InputStreamReader(in)); + StringBuffer result = new StringBuffer(); + String line = new String(""); + try { + while((line = bufferReader.readLine()) != null) { + result.append(line); + } + } catch (IOException e) { + e.printStackTrace(); + } + + return result.toString(); + } + + public static Document openDocument(InputStream is) { + if(is == null) { + return null; + } + + Document document = null; + try { + document = documentBuilder.parse(is); + } catch (SAXException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + return document; + } + + public abstract void dealElement(Element element, int index); + + public void dealElementByTagName(Document document, String tagName) { + NodeList nodeList = document.getElementsByTagName(tagName); + for(int index = 0; index < nodeList.getLength(); index++) { + Element node = (Element) nodeList.item(index); + dealElement(node, index); + } + } + + public void dealElementByTagName(Element element, String tagName) { + NodeList nodeList = element.getElementsByTagName(tagName); + for(int index = 0; index < nodeList.getLength(); index++) { + Element node = (Element) nodeList.item(index); + dealElement(node, index); + } + } + + public XMLReader parseTag(Element element) throws Exception { + NodeList nodeList = element.getChildNodes(); + for(int index = 0; index < nodeList.getLength(); index++) { + Node node = nodeList.item(index); + if(node instanceof Element) { + dealElement((Element) node, index); + } + } + + return this; + } +} diff --git a/src/main/java/com/mec/util/test/Test.java b/src/main/java/com/mec/util/test/Test.java new file mode 100644 index 0000000..7b5e623 --- /dev/null +++ b/src/main/java/com/mec/util/test/Test.java @@ -0,0 +1,8 @@ +package com.mec.util.test; + +public class Test { + + public static void main(String[] args) { + } + +} diff --git a/src/main/java/com/mec/util/xml/model/FileInfo.java b/src/main/java/com/mec/util/xml/model/FileInfo.java new file mode 100644 index 0000000..293f4c6 --- /dev/null +++ b/src/main/java/com/mec/util/xml/model/FileInfo.java @@ -0,0 +1,82 @@ +package com.mec.util.xml.model; + +import java.io.File; + +public class FileInfo { + protected String name; + protected boolean file; + protected boolean dir; + protected boolean hidden; + protected String absolutePath; + + public FileInfo() { + } + + public FileInfo(String name) { + this.name = name; + this.absolutePath = ""; + this.file = false; + this.dir = false; + this.hidden = true; + } + + public FileInfo(File file) { + this.name = file.getName(); + this.file = file.isFile(); + this.dir = file.isDirectory(); + this.hidden = file.isHidden(); + this.absolutePath = file.getAbsolutePath(); + } + + protected void setName(String name) { + this.name = name; + } + + public FileInfo setFileInfo(File file) { + this.name = file.getName(); + this.file = file.isFile(); + this.dir = file.isDirectory(); + this.hidden = file.isHidden(); + this.absolutePath = file.getAbsolutePath(); + + return this; + } + + public String getName() { + return name; + } + + public boolean isFile() { + return file; + } + + public boolean isDir() { + return dir; + } + + public boolean isHidden() { + return hidden; + } + + public String getAbsolutePath() { + return absolutePath; + } + + public String getDetailInfo() { + StringBuffer result = new StringBuffer(); + + result.append(isDir() ? "d" : (isFile() ? "f" : "u")) + .append(isHidden() ? "H" : "X") + .append(" ").append(getName()) + .append("(").append(getAbsolutePath()).append(")") + .append("\n"); + + return result.toString(); + } + + @Override + public String toString() { + return name; + } + +} diff --git a/src/main/java/com/mec/util/xml/model/FileList.java b/src/main/java/com/mec/util/xml/model/FileList.java new file mode 100644 index 0000000..7a1d963 --- /dev/null +++ b/src/main/java/com/mec/util/xml/model/FileList.java @@ -0,0 +1,67 @@ +package com.mec.util.xml.model; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +public class FileList { + protected FileInfo root; + protected List fileList; + + public FileList() { + + } + + public FileList(File file) { + root = new FileInfo(file); + if (!root.isDir()) { + return; + } + + fileList = new ArrayList(); + + File[] files = file.listFiles(); + for (File f : files) { + fileList.add(new FileInfo(f)); + } + } + + public static List getRootDrivers() { + File[] roots = File.listRoots(); + if (roots.length <= 0) { + return null; + } + + List rootList = new ArrayList(); + for (File root : roots) { + FileInfo fileInfo = new FileInfo(root); + fileInfo.setName(root.getAbsolutePath()); + rootList.add(fileInfo); + } + + return rootList; + } + + public FileInfo getRoot() { + return root; + } + + public List getFileList() { + return fileList; + } + + @Override + public String toString() { + StringBuffer result = new StringBuffer(); + + result.append(root); + if (fileList != null) { + for (FileInfo fileInfo : fileList) { + result.append(fileInfo); + } + } + + return result.toString(); + } + +} diff --git a/src/main/java/com/mec/util/xml/model/TagModel.java b/src/main/java/com/mec/util/xml/model/TagModel.java new file mode 100644 index 0000000..79bc89c --- /dev/null +++ b/src/main/java/com/mec/util/xml/model/TagModel.java @@ -0,0 +1,113 @@ +package com.mec.util.xml.model; + +import java.util.HashMap; +import java.util.Map; + +public class TagModel { + private String tagName; + private Map attributeMap; + private String textContent; + private TagModel tag; + + public TagModel() { + attributeMap = new HashMap(); + } + + public TagModel(String tagName) { + attributeMap = new HashMap(); + this.tagName = tagName; + } + + public TagModel(TagModel tag) { + this(); + this.tagName = tag.getTagName(); + } + + public TagModel(TagModel tag, Map attributeMap) { + this(); + this.tagName = tag.getTagName(); + this.attributeMap = attributeMap; + } + + public TagModel(TagModel tag, String textContent) { + this(); + this.tagName = tag.getTagName(); + this.textContent = textContent; + } + + public TagModel(TagModel tag, Map attributeMap, String textContent) { + this(); + this.tagName = tag.getTagName(); + this.attributeMap = attributeMap; + this.textContent = textContent; + } + + public TagModel getTag() { + return tag; + } + + public TagModel setTag(TagModel tag) { + this.tag = tag; + + return this; + } + + public String getTagName() { + return tagName; + } + + public TagModel setTagName(String tagName) { + this.tagName = tagName; + + return this; + } + + public String getTextContent() { + return textContent; + } + + public TagModel setTextContent(String textContent) { + this.textContent = textContent; + + return this; + } + + public TagModel addAttribute(String key, String value) { + attributeMap.put(key, value); + + return this; + } + + public void setAttributeMap(Map attributeMap) { + this.attributeMap = attributeMap; + } + + public Map getAttributeMap() { + return attributeMap; + } + + @Override + public String toString() { + StringBuffer res = new StringBuffer("标签:"); + res.append(tagName).append(":("); + boolean first = true; + for (String key : attributeMap.keySet()) { + String value = attributeMap.get(key); + res.append(first ? "" : ", "); + first = false; + res.append(key).append(" -> ").append(value); + } + res.append(")"); + if (textContent != null) { + res.append(" [").append(textContent).append("]"); + } + res.append("\n"); + + if (tag != null) { + res.append(tag); + } + + return res.toString(); + } + +} diff --git a/src/main/java/com/mec/util/xml/model/XMLModel.java b/src/main/java/com/mec/util/xml/model/XMLModel.java new file mode 100644 index 0000000..61f4949 --- /dev/null +++ b/src/main/java/com/mec/util/xml/model/XMLModel.java @@ -0,0 +1,179 @@ +package com.mec.util.xml.model; + +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.DOMConfiguration; +import org.w3c.dom.DOMImplementation; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.ls.DOMImplementationLS; +import org.w3c.dom.ls.LSOutput; +import org.w3c.dom.ls.LSSerializer; + +public class XMLModel { + private TagModel rootTag; + private List tagList; + + public XMLModel(String rootTagName) { + rootTag = new TagModel(); + rootTag.setTagName(rootTagName); + + tagList = new ArrayList(); + } + + public XMLModel setRootTagAttribute(String key, String value) { + Map rootTagAttributeMap = rootTag.getAttributeMap(); + rootTagAttributeMap.clear(); + rootTagAttributeMap.put(key, value); + + return this; + } + + public XMLModel addRootTagAttribute(String key, String value) { + rootTag.addAttribute(key, value); + + return this; + } + + public XMLModel setRootTagAttribute(Map attributeMap) { + rootTag.setAttributeMap(attributeMap); + + return this; + } + + public XMLModel addTag(TagModel tag) { + tagList.add(tag); + + return this; + } + + public String getRootTagName() { + return rootTag.getTagName(); + } + + public Map getRootTagAttribute() { + return rootTag.getAttributeMap(); + } + + public List getTagList() { + return tagList; + } + + public void setTagList(List tagList) { + this.tagList = tagList; + } + + private void setChildElement(Document document, Element parent, TagModel tag) { + if(tag == null) { + return; + } + + Element element = document.createElement(tag.getTagName()); + Map tagAttributeMap = tag.getAttributeMap(); + for (String key : tagAttributeMap.keySet()) { + element.setAttribute(key, tagAttributeMap.get(key)); + } + element.setTextContent(tag.getTextContent()); + + setChildElement(document, element, tag.getTag()); + + parent.appendChild(element); + } + + private Document makeDoc() { + Document document = null; + + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + try { + DocumentBuilder db = dbf.newDocumentBuilder(); + document = db.newDocument(); + + Element root = document.createElement(rootTag.getTagName()); + Map rootAttributeMap = rootTag.getAttributeMap(); + for (String key : rootAttributeMap.keySet()) { + String value = rootAttributeMap.get(key); + root.setAttribute(key, value); + } + String textContent = rootTag.getTextContent(); + root.setTextContent(textContent == null ? "" : textContent); + + for(TagModel tag : tagList) { + setChildElement(document, root, tag); + } + + document.appendChild(root); + } catch (ParserConfigurationException e) { + e.printStackTrace(); + } + + return document; + } + + public String writeToString(boolean prettyFormat) { + Document document = makeDoc(); + if(document == null) { + return null; + } + + DOMImplementation domImplementation = document.getImplementation(); + DOMImplementationLS domImplementationLS = + (DOMImplementationLS) domImplementation.getFeature("LS", "3.0"); + LSSerializer lsSerializer = domImplementationLS.createLSSerializer(); + LSOutput lsOutput = domImplementationLS.createLSOutput(); + lsOutput.setEncoding("UTF-8"); + + DOMConfiguration domConfiguration = lsSerializer.getDomConfig(); + if (domConfiguration.canSetParameter("xml-declaration", true)) { + lsSerializer.getDomConfig().setParameter("xml-declaration", false); + } + domConfiguration.setParameter("format-pretty-print", prettyFormat); + + StringWriter stringWriter = new StringWriter(); + lsOutput.setCharacterStream(stringWriter); + lsSerializer.write(document, lsOutput); + + return stringWriter.toString(); + } + + public void writeToXML(String xmlPath) { + Document document = makeDoc(); + if(document == null) { + return ; + } + + DOMImplementation domImp = document.getImplementation(); + DOMImplementationLS domLs = (DOMImplementationLS) domImp.getFeature("LS", "3.0"); + LSSerializer ls = domLs.createLSSerializer(); + ls.getDomConfig().setParameter("format-pretty-print", true); + + LSOutput out = domLs.createLSOutput(); + out.setEncoding("UTF-8"); + try { + out.setByteStream(new FileOutputStream(xmlPath)); + ls.write(document, out); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + + @Override + public String toString() { + StringBuffer res = new StringBuffer(); + res.append(rootTag); + for (TagModel tag : tagList) { + res.append(tag); + } + + return res.toString(); + } + +} -- Gitee