1 6 package fr.jayasoft.ivy.xml; 7 8 import java.io.File ; 9 import java.io.IOException ; 10 import java.text.ParseException ; 11 import java.util.ArrayList ; 12 import java.util.Date ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 import java.util.SortedMap ; 16 import java.util.TreeMap ; 17 18 import javax.xml.parsers.SAXParser ; 19 import javax.xml.parsers.SAXParserFactory ; 20 21 import org.xml.sax.Attributes ; 22 import org.xml.sax.SAXException ; 23 import org.xml.sax.helpers.DefaultHandler ; 24 25 import fr.jayasoft.ivy.Artifact; 26 import fr.jayasoft.ivy.DefaultArtifact; 27 import fr.jayasoft.ivy.Ivy; 28 import fr.jayasoft.ivy.ModuleId; 29 import fr.jayasoft.ivy.ModuleRevisionId; 30 import fr.jayasoft.ivy.extendable.ExtendableItemHelper; 31 import fr.jayasoft.ivy.report.XmlReportOutputter; 32 33 public class XmlReportParser { 34 private static class SaxXmlReportParser { 35 private List _mrids; 36 private List _defaultMrids; 37 private List _realMrids; 38 private List _artifacts; 39 private File _report; 40 SaxXmlReportParser(File report) { 41 _artifacts = new ArrayList (); 42 _mrids = new ArrayList (); 43 _defaultMrids = new ArrayList (); 44 _realMrids = new ArrayList (); 45 _report = report; 46 } 47 48 public void parse() throws Exception { 49 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); 50 saxParser.parse(_report, new DefaultHandler () { 51 private String _organisation; 52 private String _module; 53 private String _branch; 54 private String _revision; 55 private int _position; 56 private Date _pubdate; 57 private boolean _skip; 58 private ModuleRevisionId _mrid; 59 private boolean _default; 60 private SortedMap _revisionsMap = new TreeMap (); private List _revisionArtifacts = null; 62 private int _maxPos; 63 64 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 65 if ("module".equals(qName)) { 66 _organisation = attributes.getValue("organisation"); 67 _module = attributes.getValue("name"); 68 } else if ("revision".equals(qName)) { 69 _revisionArtifacts = new ArrayList (); 70 _branch = attributes.getValue("branch"); 71 _revision = attributes.getValue("name"); 72 _default = Boolean.valueOf(attributes.getValue("default")).booleanValue(); 73 String pos = attributes.getValue("position"); 76 _position = pos == null ? getMaxPos()+1 : Integer.valueOf(pos).intValue(); 77 if (attributes.getValue("error") != null || attributes.getValue("evicted") != null) { 78 _skip = true; 79 } else { 80 _revisionsMap.put(new Integer (_position), _revisionArtifacts); 81 _mrid = ModuleRevisionId.newInstance(_organisation, _module, _branch, _revision, 82 ExtendableItemHelper.getExtraAttributes(attributes, "extra-")); 83 _mrids.add(_mrid); 84 if (_default) { 85 _defaultMrids.add(_mrid); 86 } else { 87 _realMrids.add(_mrid); 88 } 89 try { 90 _pubdate = Ivy.DATE_FORMAT.parse(attributes.getValue("pubdate")); 91 _skip = false; 92 } catch (ParseException e) { 93 throw new IllegalArgumentException ("invalid publication date for "+_organisation+" "+_module+" "+_revision+": "+attributes.getValue("pubdate")); 94 } 95 } 96 } else if ("artifact".equals(qName)) { 97 if (_skip) { 98 return; 99 } 100 String status = attributes.getValue("status"); 101 if (status != null && "failed".equals(status)) { 102 return; 103 } 104 String artifactName = attributes.getValue("name"); 105 String type = attributes.getValue("type"); 106 String ext = attributes.getValue("ext"); 107 Artifact artifact = new DefaultArtifact(_mrid, _pubdate, artifactName, type, ext, ExtendableItemHelper.getExtraAttributes(attributes, "extra-")); 108 _revisionArtifacts.add(artifact); 109 } 110 } 111 112 public void endElement(String uri, String localName, String qname) throws SAXException { 113 if ("dependencies".equals(qname)) { 114 for (Iterator it = _revisionsMap.values().iterator(); it.hasNext(); ) { 116 List artifacts = (List ) it.next(); 117 _artifacts.addAll(artifacts); 118 } 119 } 120 } 121 122 private int getMaxPos() { 123 return _revisionsMap.isEmpty() ? -1 : ((Integer )_revisionsMap.keySet().toArray()[_revisionsMap.size()-1]).intValue(); 124 } 125 }); 126 } 127 128 public List getArtifacts() { 129 return _artifacts; 130 } 131 public List getModuleRevisionIds() { 132 return _mrids; 133 } 134 135 public List getRealModuleRevisionIds() { 136 return _realMrids; 137 } 138 } 139 140 public Artifact[] getArtifacts(ModuleId moduleId, String conf, File cache) throws ParseException , IOException { 141 return getArtifacts(getReportFile(moduleId, conf, cache)); 142 } 143 144 private Artifact[] getArtifacts(File report) throws ParseException { 145 try { 146 SaxXmlReportParser parser = new SaxXmlReportParser(report); 147 parser.parse(); 148 return (Artifact[])parser.getArtifacts().toArray(new Artifact[parser.getArtifacts().size()]); 149 } catch (Exception ex) { 150 ParseException pe = new ParseException ("failed to parse report: "+report+": "+ex.getMessage(), 0); 151 pe.initCause(ex); 152 throw pe; 153 } 154 } 155 156 public ModuleRevisionId[] getDependencyRevisionIds(ModuleId moduleId, String conf, File cache) throws ParseException , IOException { 157 return getDependencyRevisionIds(getReportFile(moduleId, conf, cache)); 158 } 159 160 private ModuleRevisionId[] getDependencyRevisionIds(File report) throws ParseException { 161 try { 162 SaxXmlReportParser parser = new SaxXmlReportParser(report); 163 parser.parse(); 164 return (ModuleRevisionId[])parser.getModuleRevisionIds().toArray(new ModuleRevisionId[parser.getModuleRevisionIds().size()]); 165 } catch (Exception ex) { 166 ParseException pe = new ParseException ("failed to parse report: "+report+": "+ex.getMessage(), 0); 167 pe.initCause(ex); 168 throw pe; 169 } 170 } 171 172 175 public ModuleRevisionId[] getRealDependencyRevisionIds(ModuleId moduleId, String conf, File cache) throws ParseException , IOException { 176 return getRealDependencyRevisionIds(getReportFile(moduleId, conf, cache)); 177 } 178 179 private ModuleRevisionId[] getRealDependencyRevisionIds(File report) throws ParseException { 180 try { 181 SaxXmlReportParser parser = new SaxXmlReportParser(report); 182 parser.parse(); 183 return (ModuleRevisionId[])parser.getRealModuleRevisionIds().toArray(new ModuleRevisionId[parser.getRealModuleRevisionIds().size()]); 184 } catch (Exception ex) { 185 ParseException pe = new ParseException ("failed to parse report: "+report+": "+ex.getMessage(), 0); 186 pe.initCause(ex); 187 throw pe; 188 } 189 } 190 191 private File getReportFile(ModuleId moduleId, String conf, File cache) { 192 File report = new File (cache, XmlReportOutputter.getReportFileName(moduleId, conf)); 193 if (!report.exists()) { 194 throw new IllegalStateException ("no report file found for "+moduleId+" "+conf+" in "+cache+": ivy was looking for "+report); 195 } 196 return report; 197 } 198 199 } 200 201 | Popular Tags |