View Javadoc

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;
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                   // 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[] args) {
122         String lib = "lib.xml";
123         try {
124             copyJars(lib, "c:/temp");
125         } catch (ParserConfigurationException e) {
126             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
127         } catch (SAXException 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 }