Просмотр исходного кода

解析jar报中组件文件处理

wuxw лет назад: 7
Родитель
Сommit
aa2d1c17ff

+ 25 - 16
WebService/src/main/java/com/java110/web/core/PackageScanner.java

@@ -1,8 +1,11 @@
+
 package com.java110.web.core;
 package com.java110.web.core;
 
 
 import org.springframework.util.StringUtils;
 import org.springframework.util.StringUtils;
 
 
-import java.io.*;
+import java.io.File;
+import java.io.FileFilter;
+import java.io.IOException;
 import java.net.JarURLConnection;
 import java.net.JarURLConnection;
 import java.net.URISyntaxException;
 import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.URL;
@@ -16,11 +19,11 @@ import java.util.jar.JarFile;
 
 
 public abstract class PackageScanner {
 public abstract class PackageScanner {
 
 
-    public void packageScanner(Class<?> klass,String suffix) {
+    public void packageScanner(Class<?> klass, String suffix) {
         packageScanner(klass.getPackage().getName(), suffix);
         packageScanner(klass.getPackage().getName(), suffix);
     }
     }
 
 
-    public void packageScanner(String packageName,String suffix) {
+    public void packageScanner(String packageName, String suffix) {
         String packagePath = packageName.replace(".", File.separator);
         String packagePath = packageName.replace(".", File.separator);
 
 
         ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
         ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
@@ -28,14 +31,15 @@ public abstract class PackageScanner {
             Enumeration<URL> resources = classLoader.getResources(packagePath);
             Enumeration<URL> resources = classLoader.getResources(packagePath);
             while (resources.hasMoreElements()) {
             while (resources.hasMoreElements()) {
                 URL url = resources.nextElement();
                 URL url = resources.nextElement();
+                System.out.println("一层循环" + url);
                 if (url.getProtocol().equals("jar")) {
                 if (url.getProtocol().equals("jar")) {
-                    scanPackage(url,suffix);
+                    scanPackage(packageName, url, suffix);
                 } else {
                 } else {
                     File file = new File(url.toURI());
                     File file = new File(url.toURI());
                     if (!file.exists()) {
                     if (!file.exists()) {
                         continue;
                         continue;
                     }
                     }
-                    scanPackage(packageName, file,suffix);
+                    scanPackage(packageName, file, suffix);
                 }
                 }
             }
             }
         } catch (IOException e) {
         } catch (IOException e) {
@@ -45,23 +49,28 @@ public abstract class PackageScanner {
         }
         }
     }
     }
 
 
-    private void scanPackage(URL url,String suffix) throws IOException {
-        JarURLConnection jarUrlConnection =  (JarURLConnection) url.openConnection();
+    private void scanPackage(String packageName, URL url, String suffix) throws IOException {
+        JarURLConnection jarUrlConnection = (JarURLConnection) url.openConnection();
         JarFile jarFile = jarUrlConnection.getJarFile();
         JarFile jarFile = jarUrlConnection.getJarFile();
         Enumeration<JarEntry> jarEntries = jarFile.entries();
         Enumeration<JarEntry> jarEntries = jarFile.entries();
+        System.out.println("二层循环" + url + " : " + jarUrlConnection.getJarFileURL());
+
 
 
         while (jarEntries.hasMoreElements()) {
         while (jarEntries.hasMoreElements()) {
+
             JarEntry jarEntry = jarEntries.nextElement();
             JarEntry jarEntry = jarEntries.nextElement();
             String jarName = jarEntry.getName();
             String jarName = jarEntry.getName();
-            if (jarEntry.isDirectory() || !jarName.endsWith(suffix)) {
+            if (jarEntry.isDirectory() || !jarName.endsWith(suffix) || !jarName.startsWith(packageName)) {
                 continue;
                 continue;
             }
             }
-            String className = jarName.replace(suffix, "");
-            handleResource(className);
+            //String className = jarName.replace(suffix, "");
+            System.out.println("二层循环" + jarName);
+
+            handleResource(jarName);
         }
         }
     }
     }
 
 
-    private void scanPackage(String packageName, final File currentfile,final String suffix) {
+    private void scanPackage(String packageName, final File currentfile, final String suffix) {
         File[] files = currentfile.listFiles(new FileFilter() {
         File[] files = currentfile.listFiles(new FileFilter() {
             @Override
             @Override
             public boolean accept(File pathname) {
             public boolean accept(File pathname) {
@@ -73,11 +82,11 @@ public abstract class PackageScanner {
         });
         });
         for (File file : files) {
         for (File file : files) {
             if (file.isDirectory()) {
             if (file.isDirectory()) {
-                scanPackage(packageName + "." + file.getName(), file,suffix);
+                scanPackage(packageName + "." + file.getName(), file, suffix);
             } else {
             } else {
-                packageName = packageName.replace(".",File.separator);
-                String fileName = packageName + File.separator +  file.getName();
-                if(StringUtils.isEmpty(fileName) || !fileName.endsWith(suffix)){
+                packageName = packageName.replace(".", File.separator);
+                String fileName = packageName + File.separator + file.getName();
+                if (StringUtils.isEmpty(fileName) || !fileName.endsWith(suffix)) {
                     continue;
                     continue;
                 }
                 }
                 handleResource(fileName);
                 handleResource(fileName);
@@ -94,7 +103,7 @@ public abstract class PackageScanner {
             protected void handleResource(String filePath) {
             protected void handleResource(String filePath) {
 
 
             }
             }
-        }.packageScanner("components","js");
+        }.packageScanner("components", "js");
     }
     }
 
 
 }
 }

+ 9 - 2
WebService/src/main/java/com/java110/web/core/VueComponentTemplate.java

@@ -1,8 +1,13 @@
 package com.java110.web.core;
 package com.java110.web.core;
 
 
+import org.springframework.core.io.ClassPathResource;
 import org.springframework.util.StringUtils;
 import org.springframework.util.StringUtils;
 
 
-import java.io.*;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
 import java.util.HashMap;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Map;
 
 
@@ -57,6 +62,7 @@ public class VueComponentTemplate extends PackageScanner {
      * @return
      * @return
      */
      */
     public static String findTemplateByComponentCode(String componentCode) {
     public static String findTemplateByComponentCode(String componentCode) {
+        System.out.println("componentTemplate" + componentTemplate);
         if (componentTemplate.containsKey(componentCode)) {
         if (componentTemplate.containsKey(componentCode)) {
             return componentTemplate.get(componentCode);
             return componentTemplate.get(componentCode);
         }
         }
@@ -74,7 +80,8 @@ public class VueComponentTemplate extends PackageScanner {
         Reader reader = null;
         Reader reader = null;
         String sb = "";
         String sb = "";
         try {
         try {
-            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(File.separator + filePath);
+            InputStream inputStream = new ClassPathResource(filePath).getInputStream();
+            //InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(File.separator + filePath);
             reader = new InputStreamReader(inputStream, "UTF-8");
             reader = new InputStreamReader(inputStream, "UTF-8");
             int tempChar;
             int tempChar;
             StringBuffer b = new StringBuffer();
             StringBuffer b = new StringBuffer();

+ 21 - 0
WebService/src/test/java/com/java110/web/core/VueComponentTemplateTest.java

@@ -0,0 +1,21 @@
+package com.java110.web.core;
+
+import org.junit.Test;
+
+import java.io.File;
+
+import static org.junit.Assert.*;
+
+public class VueComponentTemplateTest {
+
+    @Test
+    public void handleResource() {
+    }
+
+    @Test
+    public void sp(){
+        String path = "components/privilege/privilege.html";
+        System.out.println(File.separator);
+        System.out.println( path.substring(path.lastIndexOf(File.separator) + 1, path.length()));
+    }
+}