package com.jqmobile.core.utils.xml.impl; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URISyntaxException; import java.net.URL; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.jar.JarEntry; import java.util.jar.JarFile; import com.jqmobile.core.utils.xml.IReadXML; import com.jqmobile.core.utils.xml.IXMLParse; /** * xml文件查找 * @author scc * */ public class ReadXML implements IReadXML{ private final static String DefaultXmlName = "mod.xml"; private String xmlName; @Override public List<IXMLParse> readModXML() throws IOException { return readXML(DefaultXmlName); } @Override public List<IXMLParse> readXML(String xmlName) throws IOException { this.xmlName = xmlName; return getXMLFiles(); } public List<IXMLParse> getXMLFiles() throws IOException{ List<InputStream> fileList=new ArrayList<InputStream>(); List<IXMLParse> xps=new ArrayList<IXMLParse>(); // String filePath=Thread.currentThread().getContextClassLoader() // .getResource("").getPath(); URL url = Thread.currentThread().getContextClassLoader().getResource(""); // String str = filePath; // System.out.println(filePath); // //以tomcat目录下webapps为参照找出项目根路径file对象 // if(filePath.contains("wtpwebapps")){ //// file=new File(filePath); //// while(!file.getParentFile().getName().equals("webapps")){ //// file=file.getParentFile(); //// } // str = filePath.substring(0, filePath.lastIndexOf("wtpwebapps")); // }else if(filePath.contains("target")){ //// file.getParentFile().toString().endsWith("jiuqi\\mobile") // str = filePath.substring(0, filePath.lastIndexOf("target")); // }else if(filePath.contains("webapps")){ // str = filePath.substring(0, filePath.lastIndexOf("webapps")); // }else if(filePath.contains("pubtime")){ // str = filePath.substring(0, filePath.lastIndexOf("pubtime")); // } // if(null == str){ // throw new RuntimeException("当前运行环境错误"); // } // System.out.println(str); File file; try { file = new File(url.toURI()); List<InputStream> streams=getfiles(fileList, file);//file.getAbsolutePath()); for(InputStream is:streams){ xps.add(XMLFactory.getByInputStream(is)); } } catch (Throwable e) { e.printStackTrace(); } return xps; } /** * 递归目录查找文件 * @param filelList 文件对象集合 * @param root 根目录 * @return * @throws IOException */ private List<InputStream> getfiles(List<InputStream> filelList,File root) throws IOException{ // File root = new File(root); File[] files = root.listFiles(); if(null != files) for (File file : files) { if (file.isDirectory()) { getfiles(filelList,file); } else { if(file.getName().equals(xmlName)){ filelList.add(new FileInputStream(file)); System.out.println("file:"+file.getAbsolutePath()); }else if(file.getName().endsWith(".jar")){ getInputStream(file); } } } return filelList; } /** * 转换成流的形式 * @param file * @return * @throws IOException */ private InputStream getInputStream(File file) throws IOException { JarFile jf = new JarFile(file); InputStream is=null; Enumeration<JarEntry> e = jf.entries(); while (e.hasMoreElements()) { JarEntry je = e.nextElement(); if (je.getName().equals(xmlName)) { System.out.println("jar:"+file.getName()); is=jf.getInputStream(je); } } jf.close(); return is; } }