KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > xml > XmlReportParser


1 /*
2  * This file is subject to the license found in LICENCE.TXT in the root directory of the project.
3  *
4  * #SNAPSHOT#
5  */

6 package fr.jayasoft.ivy.xml;
7
8 import java.io.File JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.text.ParseException JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.Date JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.SortedMap JavaDoc;
16 import java.util.TreeMap JavaDoc;
17
18 import javax.xml.parsers.SAXParser JavaDoc;
19 import javax.xml.parsers.SAXParserFactory JavaDoc;
20
21 import org.xml.sax.Attributes JavaDoc;
22 import org.xml.sax.SAXException JavaDoc;
23 import org.xml.sax.helpers.DefaultHandler JavaDoc;
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 JavaDoc _mrids;
36         private List JavaDoc _defaultMrids;
37         private List JavaDoc _realMrids;
38         private List JavaDoc _artifacts;
39         private File JavaDoc _report;
40         SaxXmlReportParser(File JavaDoc report) {
41             _artifacts = new ArrayList JavaDoc();
42             _mrids = new ArrayList JavaDoc();
43             _defaultMrids = new ArrayList JavaDoc();
44             _realMrids = new ArrayList JavaDoc();
45             _report = report;
46         }
47         
48         public void parse() throws Exception JavaDoc {
49             SAXParser JavaDoc saxParser = SAXParserFactory.newInstance().newSAXParser();
50             saxParser.parse(_report, new DefaultHandler JavaDoc() {
51                 private String JavaDoc _organisation;
52                 private String JavaDoc _module;
53                 private String JavaDoc _branch;
54                 private String JavaDoc _revision;
55                 private int _position;
56                 private Date JavaDoc _pubdate;
57                 private boolean _skip;
58                 private ModuleRevisionId _mrid;
59                 private boolean _default;
60                 private SortedMap JavaDoc _revisionsMap = new TreeMap JavaDoc(); // Use a TreeMap to order by position (position = key)
61
private List JavaDoc _revisionArtifacts = null;
62                 private int _maxPos;
63                 
64                 public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
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 JavaDoc();
70                         _branch = attributes.getValue("branch");
71                         _revision = attributes.getValue("name");
72                         _default = Boolean.valueOf(attributes.getValue("default")).booleanValue();
73                         // retrieve position from file. If no position is found, it may be an old report generated with a previous version,
74
// in which case, we put it at the last position
75
String JavaDoc 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 JavaDoc(_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 JavaDoc e) {
93                                 throw new IllegalArgumentException JavaDoc("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 JavaDoc status = attributes.getValue("status");
101                         if (status != null && "failed".equals(status)) {
102                             return;
103                         }
104                         String JavaDoc artifactName = attributes.getValue("name");
105                         String JavaDoc type = attributes.getValue("type");
106                         String JavaDoc 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 JavaDoc uri, String JavaDoc localName, String JavaDoc qname) throws SAXException JavaDoc {
113                     if ("dependencies".equals(qname)) {
114                         // add the artifacts in the correct order
115
for (Iterator JavaDoc it = _revisionsMap.values().iterator(); it.hasNext(); ) {
116                             List JavaDoc artifacts = (List JavaDoc) it.next();
117                             _artifacts.addAll(artifacts);
118                         }
119                     }
120                 }
121                 
122                 private int getMaxPos() {
123                     return _revisionsMap.isEmpty() ? -1 : ((Integer JavaDoc)_revisionsMap.keySet().toArray()[_revisionsMap.size()-1]).intValue();
124                 }
125             });
126         }
127
128         public List JavaDoc getArtifacts() {
129             return _artifacts;
130         }
131         public List JavaDoc getModuleRevisionIds() {
132             return _mrids;
133         }
134
135         public List JavaDoc getRealModuleRevisionIds() {
136             return _realMrids;
137         }
138     }
139
140     public Artifact[] getArtifacts(ModuleId moduleId, String JavaDoc conf, File JavaDoc cache) throws ParseException JavaDoc, IOException JavaDoc {
141         return getArtifacts(getReportFile(moduleId, conf, cache));
142     }
143
144     private Artifact[] getArtifacts(File JavaDoc report) throws ParseException JavaDoc {
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 JavaDoc ex) {
150             ParseException JavaDoc pe = new ParseException JavaDoc("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 JavaDoc conf, File JavaDoc cache) throws ParseException JavaDoc, IOException JavaDoc {
157         return getDependencyRevisionIds(getReportFile(moduleId, conf, cache));
158     }
159
160     private ModuleRevisionId[] getDependencyRevisionIds(File JavaDoc report) throws ParseException JavaDoc {
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 JavaDoc ex) {
166             ParseException JavaDoc pe = new ParseException JavaDoc("failed to parse report: "+report+": "+ex.getMessage(), 0);
167             pe.initCause(ex);
168             throw pe;
169         }
170     }
171         
172     /**
173      * Returns all the mrids of the dependencies which have a real module descriptor, i.e. not a default one
174      */

175     public ModuleRevisionId[] getRealDependencyRevisionIds(ModuleId moduleId, String JavaDoc conf, File JavaDoc cache) throws ParseException JavaDoc, IOException JavaDoc {
176         return getRealDependencyRevisionIds(getReportFile(moduleId, conf, cache));
177     }
178
179     private ModuleRevisionId[] getRealDependencyRevisionIds(File JavaDoc report) throws ParseException JavaDoc {
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 JavaDoc ex) {
185             ParseException JavaDoc pe = new ParseException JavaDoc("failed to parse report: "+report+": "+ex.getMessage(), 0);
186             pe.initCause(ex);
187             throw pe;
188         }
189     }
190         
191     private File JavaDoc getReportFile(ModuleId moduleId, String JavaDoc conf, File JavaDoc cache) {
192         File JavaDoc report = new File JavaDoc(cache, XmlReportOutputter.getReportFileName(moduleId, conf));
193         if (!report.exists()) {
194             throw new IllegalStateException JavaDoc("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