1 4 package com.inversoft.savant; 5 6 7 import java.io.BufferedInputStream ; 8 import java.io.BufferedWriter ; 9 import java.io.File ; 10 import java.io.FileInputStream ; 11 import java.io.FileWriter ; 12 import java.io.IOException ; 13 import java.util.Iterator ; 14 import java.util.Set ; 15 import javax.xml.parsers.DocumentBuilder ; 16 import javax.xml.parsers.DocumentBuilderFactory ; 17 import javax.xml.parsers.ParserConfigurationException ; 18 19 import org.w3c.dom.Document ; 20 import org.w3c.dom.NamedNodeMap ; 21 import org.w3c.dom.Node ; 22 import org.w3c.dom.NodeList ; 23 import org.xml.sax.SAXException ; 24 25 26 33 public class ArtifactTools { 34 35 public static final String OPEN_TAG = "<dependencies>\n"; 36 public static final String CLOSE_TAG = "</dependencies>"; 37 38 39 48 public static Document resolveArtifactDependencies(Artifact artifact, 49 File file) 50 throws SavantException { 51 try { 52 BufferedInputStream bis = new BufferedInputStream (new FileInputStream (file)); 53 DocumentBuilder xmlBuilder = 54 DocumentBuilderFactory.newInstance().newDocumentBuilder(); 55 Document doc = xmlBuilder.parse(bis); 56 NodeList list = doc.getElementsByTagName("artifact"); 57 for (int i = 0; i < list.getLength(); i++) { 58 Node node = list.item(i); 59 NamedNodeMap attrs = node.getAttributes(); 60 Node 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 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 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 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 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 ioe) { 98 throw new SavantException("Unable to parse artifact dependency XML " + 99 "for artifact [" + artifact + "]", ioe); 100 } catch (ParserConfigurationException pce) { 101 throw new SavantException("Unable to parse artifact dependency XML " + 102 "for artifact [" + artifact + "]", pce); 103 } catch (SAXException se) { 104 throw new SavantException("Unable to parse artifact dependency XML " + 105 "for artifact [" + artifact + "]", se); 106 } 107 } 108 109 118 public static final File generateXML(Set artifacts) throws SavantException { 119 BufferedWriter bos = null; 120 try { 121 File tmp = File.createTempFile("savant", "deps"); 122 bos = new BufferedWriter (new FileWriter (tmp)); 123 124 bos.write(OPEN_TAG); 126 for (Iterator 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 ioe) { 137 throw new SavantException(ioe); 138 } finally { 139 if (bos != null) { 140 try { 141 bos.close(); 142 } catch (IOException ioe) { 143 throw new SavantException(ioe); 144 } 145 } 146 } 147 } 148 }
| Popular Tags
|