1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 /***
37 * Helper class for copying jar files to the generated build file to prevent unnecessary downloads.
38 *
39 * User: Rudie Ekkelenkamp
40 * Date: Apr 16, 2004
41 * Time: 9:13:55 PM
42 */
43 public class LibCopier {
44
45 /***
46 * Reads the libraries from the JAG generated libXmlFile and copy them if needed
47 * from the local lib directory to the generated lib directory.
48 * @throws javax.xml.parsers.ParserConfigurationException Indicates a serious configuration error.
49 * @throws org.xml.sax.SAXException Encapsulate a general SAX error or warning.
50 * @throws java.io.IOException Signals that an I/O exception of some sort has occurred
51 * @return a Hashmap with filename/URL pairs as Strings that couldn't be copied .
52 *
53 */
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
95 copy(sourceFile, targetFile);
96 } else {
97 }
98 } else {
99 failedCopies.put(s, sourceFile.getCanonicalPath());
100 }
101 }
102 return failedCopies;
103 }
104
105
106
107 private static void copy(File src, File dst) throws IOException {
108 InputStream in = new FileInputStream(src);
109 OutputStream out = new FileOutputStream(dst);
110
111
112 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();
127 } catch (SAXException e) {
128 e.printStackTrace();
129 } catch (IOException e) {
130 e.printStackTrace();
131 }
132 }
133 }