1 17 18 19 package com.finalist.jaggenerator; 20 21 import org.xml.sax.SAXException ; 22 import org.w3c.dom.Document ; 23 import org.w3c.dom.NodeList ; 24 import org.w3c.dom.Node ; 25 import org.w3c.dom.NamedNodeMap ; 26 27 import javax.xml.parsers.ParserConfigurationException ; 28 import javax.xml.parsers.DocumentBuilderFactory ; 29 import javax.xml.parsers.DocumentBuilder ; 30 import java.io.*; 31 import java.util.ArrayList ; 32 import java.util.Iterator ; 33 import java.util.HashMap ; 34 import java.net.URL ; 35 36 43 public class LibCopier { 44 45 54 public static HashMap copyJars(String libXmlFile, String targetDir) throws ParserConfigurationException , SAXException , IOException { 55 56 File target = new File(targetDir); 57 HashMap failedCopies = new HashMap (); 58 if (!target.exists()) { 59 System.out.println("Target directory to copy files to doesn't exist."); 60 return null; 61 } 62 ArrayList libs = new ArrayList (); 63 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 64 dbf.setValidating(true); 65 DocumentBuilder db = dbf.newDocumentBuilder(); 66 Document doc = db.parse(libXmlFile); 67 NodeList nl = doc.getElementsByTagName("lib"); 68 for (int i = 0; i < nl.getLength(); i++) { 69 Node node = nl.item(i); 70 NamedNodeMap atts = node.getAttributes(); 71 String file; 72 Node url = atts.getNamedItem("url"); 73 if (url != null) { 74 file = url.getNodeValue(); 75 try { 76 URL theUrl = new URL (file); 77 String name = theUrl.getFile(); 78 if (name.lastIndexOf("/") != -1) { 79 name = name.substring(name.lastIndexOf("/")+1); 80 } 81 libs.add(name); 82 } catch (Exception e) { 83 e.printStackTrace(); 84 } 85 } 86 } 87 88 for (Iterator iterator = libs.iterator(); iterator.hasNext();) { 89 String s = (String ) iterator.next(); 90 File sourceFile = new File(".."+File.separator+"lib"+File.separator+s); 91 File targetFile = new File(targetDir+File.separator+s); 92 if (sourceFile.exists()) { 93 if (!targetFile.exists()) { 94 copy(sourceFile, targetFile); 96 } else { 97 } 98 } else { 99 failedCopies.put(s, sourceFile.getCanonicalPath()); 100 } 101 } 102 return failedCopies; 103 } 104 105 private static void copy(File src, File dst) throws IOException { 108 InputStream in = new FileInputStream(src); 109 OutputStream out = new FileOutputStream(dst); 110 111 byte[] buf = new byte[1024]; 113 int len; 114 while ((len = in.read(buf)) > 0) { 115 out.write(buf, 0, len); 116 } 117 in.close(); 118 out.close(); 119 } 120 121 public static void main(String [] args) { 122 String lib = "lib.xml"; 123 try { 124 copyJars(lib, "c:/temp"); 125 } catch (ParserConfigurationException e) { 126 e.printStackTrace(); } catch (SAXException e) { 128 e.printStackTrace(); } catch (IOException e) { 130 e.printStackTrace(); } 132 } 133 } 134 | Popular Tags |