KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > savant > ArtifactTools


1 /*
2  * Copyright (c) 2003-2004, Inversoft, All Rights Reserved
3  */

4 package com.inversoft.savant;
5
6
7 import java.io.BufferedInputStream JavaDoc;
8 import java.io.BufferedWriter JavaDoc;
9 import java.io.File JavaDoc;
10 import java.io.FileInputStream JavaDoc;
11 import java.io.FileWriter JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Set JavaDoc;
15 import javax.xml.parsers.DocumentBuilder JavaDoc;
16 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
17 import javax.xml.parsers.ParserConfigurationException JavaDoc;
18
19 import org.w3c.dom.Document JavaDoc;
20 import org.w3c.dom.NamedNodeMap JavaDoc;
21 import org.w3c.dom.Node JavaDoc;
22 import org.w3c.dom.NodeList JavaDoc;
23 import org.xml.sax.SAXException JavaDoc;
24
25
26 /**
27  * <p>
28  * This class is a toolkit for handling artifact operations.
29  * </p>
30  *
31  * @author Brian Pontarelli
32  */

33 public class ArtifactTools {
34
35     public static final String JavaDoc OPEN_TAG = "<dependencies>\n";
36     public static final String JavaDoc CLOSE_TAG = "</dependencies>";
37
38
39     /**
40      * Updates the given artifact with its dependencies by parsing the XML
41      * dependency file (if one exists).
42      *
43      * @param artifact The artifact to update
44      * @param file The File to read the XML dependency information from.
45      * @throws SavantException If building, parsing or content failed or was
46      * invalid
47      */

48     public static Document JavaDoc resolveArtifactDependencies(Artifact artifact,
49             File JavaDoc file)
50     throws SavantException {
51         try {
52             BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file));
53             DocumentBuilder JavaDoc xmlBuilder =
54                 DocumentBuilderFactory.newInstance().newDocumentBuilder();
55             Document JavaDoc doc = xmlBuilder.parse(bis);
56             NodeList JavaDoc list = doc.getElementsByTagName("artifact");
57             for (int i = 0; i < list.getLength(); i++) {
58                 Node JavaDoc node = list.item(i);
59                 NamedNodeMap JavaDoc attrs = node.getAttributes();
60                 Node JavaDoc group = attrs.getNamedItem("group");
61                 if (group == null) {
62                     throw new SavantException("Artifact deps XML missing " +
63                         "group attribute [" + artifact.getName() + "]");
64                 }
65
66                 Node JavaDoc name = attrs.getNamedItem("name");
67                 if (name == null) {
68                     throw new SavantException("Artifact deps XML missing " +
69                         "name attribute [" + artifact.getName() + "]");
70                 }
71
72                 Node JavaDoc projName = attrs.getNamedItem("projectname");
73                 if (projName == null) {
74                     throw new SavantException("Artifact deps XML missing " +
75                         "projectname attribute [" + artifact.getName() + "]");
76                 }
77
78                 Node JavaDoc type = attrs.getNamedItem("type");
79                 if (type == null) {
80                     throw new SavantException("Artifact deps XML missing " +
81                         "type attribute [" + artifact.getName() + "]");
82                 }
83
84                 Node JavaDoc version = attrs.getNamedItem("version");
85
86                 Artifact dep = new Artifact();
87                 dep.setGroup(group.getNodeValue());
88                 dep.setName(name.getNodeValue());
89                 dep.setProjectname(projName.getNodeValue());
90                 dep.setType(type.getNodeValue());
91                 dep.setVersion( (version == null ? null : version.getNodeValue()) );
92
93                 artifact.addDependency(dep);
94             }
95
96             return doc;
97         } catch (IOException JavaDoc ioe) {
98             throw new SavantException("Unable to parse artifact dependency XML " +
99                 "for artifact [" + artifact + "]", ioe);
100         } catch (ParserConfigurationException JavaDoc pce) {
101             throw new SavantException("Unable to parse artifact dependency XML " +
102                 "for artifact [" + artifact + "]", pce);
103         } catch (SAXException JavaDoc se) {
104             throw new SavantException("Unable to parse artifact dependency XML " +
105                 "for artifact [" + artifact + "]", se);
106         }
107     }
108
109     /**
110      * Generates a temporary file that contains dependency XML which includes all
111      * of the artifacts given.
112      *
113      * @param artifacts The set of artifacts to include in the XML.
114      * @return The temp file and never null.
115      * @throws SavantException If the temp could not be created, or the XML could
116      * not be written.
117      */

118     public static final File JavaDoc generateXML(Set JavaDoc artifacts) throws SavantException {
119         BufferedWriter JavaDoc bos = null;
120         try {
121             File JavaDoc tmp = File.createTempFile("savant", "deps");
122             bos = new BufferedWriter JavaDoc(new FileWriter JavaDoc(tmp));
123
124             // Write the open tag
125
bos.write(OPEN_TAG);
126             for (Iterator JavaDoc iterator = artifacts.iterator(); iterator.hasNext();) {
127                 Artifact artifact = (Artifact) iterator.next();
128                 bos.write(" <artifact group=\"" + artifact.getGroup() +
129                     "\" projectname=\"" + artifact.getProjectname() + "\" name=\"" +
130                     artifact.getName() + "\" version=\"" + artifact.getVersion() +
131                     "\" type=\"" + artifact.getType() + "\"/>\n");
132             }
133
134             bos.write(CLOSE_TAG);
135             return tmp;
136         } catch (IOException JavaDoc ioe) {
137             throw new SavantException(ioe);
138         } finally {
139             if (bos != null) {
140                 try {
141                     bos.close();
142                 } catch (IOException JavaDoc ioe) {
143                     throw new SavantException(ioe);
144                 }
145             }
146         }
147     }
148 }
Popular Tags