001: /* Copyright (C) 2004 Finalist IT Group
002: *
003: * This file is part of JAG - the Java J2EE Application Generator
004: *
005: * JAG is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: * JAG is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: * You should have received a copy of the GNU General Public License
014: * along with JAG; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
016: */
017:
018: package com.finalist.jaggenerator;
019:
020: import org.xml.sax.SAXException;
021: import org.w3c.dom.Document;
022: import org.w3c.dom.NodeList;
023: import org.w3c.dom.Node;
024: import org.w3c.dom.NamedNodeMap;
025:
026: import javax.xml.parsers.ParserConfigurationException;
027: import javax.xml.parsers.DocumentBuilderFactory;
028: import javax.xml.parsers.DocumentBuilder;
029: import java.io.*;
030: import java.util.ArrayList;
031: import java.util.Iterator;
032: import java.util.HashMap;
033: import java.net.URL;
034:
035: /**
036: * Helper class for copying jar files to the generated build file to prevent unnecessary downloads.
037: *
038: * User: Rudie Ekkelenkamp
039: * Date: Apr 16, 2004
040: * Time: 9:13:55 PM
041: */
042: public class LibCopier {
043:
044: /**
045: * Reads the libraries from the JAG generated libXmlFile and copy them if needed
046: * from the local lib directory to the generated lib directory.
047: * @throws javax.xml.parsers.ParserConfigurationException Indicates a serious configuration error.
048: * @throws org.xml.sax.SAXException Encapsulate a general SAX error or warning.
049: * @throws java.io.IOException Signals that an I/O exception of some sort has occurred
050: * @return a Hashmap with filename/URL pairs as Strings that couldn't be copied .
051: *
052: */
053: public static HashMap copyJars(String libXmlFile, String targetDir)
054: throws ParserConfigurationException, SAXException,
055: IOException {
056:
057: File target = new File(targetDir);
058: HashMap failedCopies = new HashMap();
059: if (!target.exists()) {
060: System.out
061: .println("Target directory to copy files to doesn't exist.");
062: return null;
063: }
064: ArrayList libs = new ArrayList();
065: DocumentBuilderFactory dbf = DocumentBuilderFactory
066: .newInstance();
067: dbf.setValidating(true);
068: DocumentBuilder db = dbf.newDocumentBuilder();
069: Document doc = db.parse(libXmlFile);
070: NodeList nl = doc.getElementsByTagName("lib");
071: for (int i = 0; i < nl.getLength(); i++) {
072: Node node = nl.item(i);
073: NamedNodeMap atts = node.getAttributes();
074: String file;
075: Node url = atts.getNamedItem("url");
076: if (url != null) {
077: file = url.getNodeValue();
078: try {
079: URL theUrl = new URL(file);
080: String name = theUrl.getFile();
081: if (name.lastIndexOf("/") != -1) {
082: name = name
083: .substring(name.lastIndexOf("/") + 1);
084: }
085: libs.add(name);
086: } catch (Exception e) {
087: e.printStackTrace();
088: }
089: }
090: }
091:
092: for (Iterator iterator = libs.iterator(); iterator.hasNext();) {
093: String s = (String) iterator.next();
094: File sourceFile = new File(".." + File.separator + "lib"
095: + File.separator + s);
096: File targetFile = new File(targetDir + File.separator + s);
097: if (sourceFile.exists()) {
098: if (!targetFile.exists()) {
099: // Only copy if the target file doesn't exist yet.
100: copy(sourceFile, targetFile);
101: } else {
102: }
103: } else {
104: failedCopies.put(s, sourceFile.getCanonicalPath());
105: }
106: }
107: return failedCopies;
108: }
109:
110: // Copies src file to dst file.
111: // If the dst file does not exist, it is created
112: private static void copy(File src, File dst) throws IOException {
113: InputStream in = new FileInputStream(src);
114: OutputStream out = new FileOutputStream(dst);
115:
116: // Transfer bytes from in to out
117: byte[] buf = new byte[1024];
118: int len;
119: while ((len = in.read(buf)) > 0) {
120: out.write(buf, 0, len);
121: }
122: in.close();
123: out.close();
124: }
125:
126: public static void main(String[] args) {
127: String lib = "lib.xml";
128: try {
129: copyJars(lib, "c:/temp");
130: } catch (ParserConfigurationException e) {
131: e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
132: } catch (SAXException e) {
133: e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
134: } catch (IOException e) {
135: e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
136: }
137: }
138: }
|