KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > jaggenerator > LibCopier


1 /* Copyright (C) 2004 Finalist IT Group
2  *
3  * This file is part of JAG - the Java J2EE Application Generator
4  *
5  * JAG is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * JAG is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with JAG; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */

17
18
19 package com.finalist.jaggenerator;
20
21 import org.xml.sax.SAXException JavaDoc;
22 import org.w3c.dom.Document JavaDoc;
23 import org.w3c.dom.NodeList JavaDoc;
24 import org.w3c.dom.Node JavaDoc;
25 import org.w3c.dom.NamedNodeMap JavaDoc;
26
27 import javax.xml.parsers.ParserConfigurationException JavaDoc;
28 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
29 import javax.xml.parsers.DocumentBuilder JavaDoc;
30 import java.io.*;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.net.URL JavaDoc;
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 JavaDoc copyJars(String JavaDoc libXmlFile, String JavaDoc targetDir) throws ParserConfigurationException JavaDoc, SAXException JavaDoc, IOException {
55
56        File target = new File(targetDir);
57        HashMap JavaDoc failedCopies = new HashMap JavaDoc();
58        if (!target.exists()) {
59            System.out.println("Target directory to copy files to doesn't exist.");
60            return null;
61        }
62        ArrayList JavaDoc libs = new ArrayList JavaDoc();
63        DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
64        dbf.setValidating(true);
65        DocumentBuilder JavaDoc db = dbf.newDocumentBuilder();
66        Document JavaDoc doc = db.parse(libXmlFile);
67        NodeList JavaDoc nl = doc.getElementsByTagName("lib");
68        for (int i = 0; i < nl.getLength(); i++) {
69           Node JavaDoc node = nl.item(i);
70           NamedNodeMap JavaDoc atts = node.getAttributes();
71           String JavaDoc file;
72           Node JavaDoc url = atts.getNamedItem("url");
73           if (url != null) {
74              file = url.getNodeValue();
75              try {
76                  URL JavaDoc theUrl = new URL JavaDoc(file);
77                  String JavaDoc name = theUrl.getFile();
78                  if (name.lastIndexOf("/") != -1) {
79                      name = name.substring(name.lastIndexOf("/")+1);
80                  }
81                  libs.add(name);
82              } catch (Exception JavaDoc e) {
83                  e.printStackTrace();
84              }
85           }
86        }
87
88        for (Iterator JavaDoc iterator = libs.iterator(); iterator.hasNext();) {
89            String JavaDoc s = (String JavaDoc) 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                  // Only copy if the target file doesn't exist yet.
95
copy(sourceFile, targetFile);
96               } else {
97               }
98            } else {
99                failedCopies.put(s, sourceFile.getCanonicalPath());
100            }
101        }
102        return failedCopies;
103     }
104
105     // Copies src file to dst file.
106
// If the dst file does not exist, it is created
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         // Transfer bytes from in to out
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 JavaDoc[] args) {
122         String JavaDoc lib = "lib.xml";
123         try {
124             copyJars(lib, "c:/temp");
125         } catch (ParserConfigurationException JavaDoc e) {
126             e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
127
} catch (SAXException JavaDoc e) {
128             e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
129
} catch (IOException e) {
130             e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
131
}
132     }
133 }
134
Popular Tags