Author: xavier
Date: Sun Mar 2 02:10:40 2008
New Revision: 632732
URL: http://svn.apache.org/viewvc?rev=632732&view=rev
Log:
fix style: extract anonymous inner class to reduce method length
Modified:
ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/report/XmlReportParser.java
Modified:
ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/report/XmlReportParser.java
URL:
http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/report/XmlReportParser.java?rev=632732&r1=632731&r2=632732&view=diff
==============================================================================
---
ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/report/XmlReportParser.java
(original)
+++
ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/report/XmlReportParser.java
Sun Mar 2 02:10:40 2008
@@ -46,6 +46,184 @@
public class XmlReportParser {
private static class SaxXmlReportParser {
+ private final class XmlReportParserHandler extends DefaultHandler {
+ private String organisation;
+
+ private String module;
+
+ private String branch;
+
+ private String revision;
+
+ private int position;
+
+ private Date pubdate;
+
+ private boolean skip;
+
+ private ModuleRevisionId mrid;
+
+ private boolean isDefault;
+
+ private SortedMap revisionsMap = new TreeMap(); // Use a TreeMap
to order by
+
+ private List revisionArtifacts = null;
+
+ public void startElement(String uri, String localName, String
qName,
+ Attributes attributes) throws SAXException {
+ if ("module".equals(qName)) {
+ organisation = attributes.getValue("organisation");
+ module = attributes.getValue("name");
+ } else if ("revision".equals(qName)) {
+ revisionArtifacts = new ArrayList();
+ branch = attributes.getValue("branch");
+ revision = attributes.getValue("name");
+ isDefault =
Boolean.valueOf(attributes.getValue("default")).booleanValue();
+ // retrieve position from file. If no position is found,
it may be an old
+ // report generated with a previous version,
+ // in which case, we put it at the last position
+ String pos = attributes.getValue("position");
+ position = pos == null ? getMaxPos() + 1 :
Integer.valueOf(pos).intValue();
+ if (attributes.getValue("error") != null
+ || attributes.getValue("evicted") != null) {
+ skip = true;
+ } else {
+ revisionsMap.put(new Integer(position),
revisionArtifacts);
+ mrid = ModuleRevisionId.newInstance(organisation,
module, branch,
+ revision,
ExtendableItemHelper.getExtraAttributes(attributes,
+ "extra-"));
+ mrids.add(mrid);
+ if (isDefault) {
+ defaultMrids.add(mrid);
+ } else {
+ Artifact metadataArtifact =
+ DefaultArtifact.newIvyArtifact(mrid, pubdate);
+ MetadataArtifactDownloadReport madr =
+ new
MetadataArtifactDownloadReport(metadataArtifact);
+ metadataReports.put(mrid, madr);
+ realMrids.add(mrid);
+ }
+ try {
+ pubdate =
Ivy.DATE_FORMAT.parse(attributes.getValue("pubdate"));
+ skip = false;
+ } catch (ParseException e) {
+ throw new IllegalArgumentException("invalid
publication date for "
+ + organisation + " " + module + " " +
revision + ": "
+ + attributes.getValue("pubdate"));
+ }
+ }
+ } else if ("metadata-artifact".equals(qName)) {
+ if (skip) {
+ return;
+ }
+ MetadataArtifactDownloadReport madr =
+ (MetadataArtifactDownloadReport)
metadataReports.get(mrid);
+ if (madr != null) {
+ madr.setDownloadStatus(
+
DownloadStatus.fromString(attributes.getValue("status")));
+
madr.setDownloadDetails(attributes.getValue("details"));
+
madr.setSize(Long.parseLong(attributes.getValue("size")));
+
madr.setDownloadTimeMillis(Long.parseLong(attributes.getValue("time")));
+
madr.setSearched(parseBoolean(attributes.getValue("searched")));
+ if (attributes.getValue("location") != null) {
+ madr.setLocalFile(new
File(attributes.getValue("location")));
+ }
+ if (attributes.getValue("original-local-location") !=
null) {
+ madr.setOriginalLocalFile(
+ new
File(attributes.getValue("original-local-location")));
+ }
+ if (attributes.getValue("origin-location") != null) {
+ if (ArtifactOrigin.UNKNOWN.getLocation().equals(
+ attributes.getValue("origin-location"))) {
+ madr.setArtifactOrigin(ArtifactOrigin.UNKNOWN);
+ } else {
+ madr.setArtifactOrigin(
+ new ArtifactOrigin(
+
parseBoolean(attributes.getValue("origin-is-local")),
+
attributes.getValue("origin-location")));
+ }
+ }
+ }
+ } else if ("artifact".equals(qName)) {
+ if (skip) {
+ return;
+ }
+ String status = attributes.getValue("status");
+ String artifactName = attributes.getValue("name");
+ String type = attributes.getValue("type");
+ String ext = attributes.getValue("ext");
+ Artifact artifact = new DefaultArtifact(mrid, pubdate,
artifactName,
+ type, ext,
ExtendableItemHelper.getExtraAttributes(attributes,
+ "extra-"));
+ ArtifactDownloadReport aReport = new
ArtifactDownloadReport(artifact);
+
aReport.setDownloadStatus(DownloadStatus.fromString(status));
+ aReport.setDownloadDetails(attributes.getValue("details"));
+
aReport.setSize(Long.parseLong(attributes.getValue("size")));
+
aReport.setDownloadTimeMillis(Long.parseLong(attributes.getValue("time")));
+ if (attributes.getValue("location") != null) {
+ aReport.setLocalFile(new
File(attributes.getValue("location")));
+ }
+ revisionArtifacts.add(aReport);
+ } else if ("origin-location".equals(qName)) {
+ if (skip) {
+ return;
+ }
+ ArtifactDownloadReport aReport = (ArtifactDownloadReport)
+ revisionArtifacts.get(revisionArtifacts.size() - 1);
+
+ if (ArtifactOrigin.UNKNOWN.getLocation().equals(
+ attributes.getValue("location"))) {
+ aReport.setArtifactOrigin(ArtifactOrigin.UNKNOWN);
+ } else {
+ aReport.setArtifactOrigin(
+ new ArtifactOrigin(
+ parseBoolean(attributes.getValue("is-local")),
+ attributes.getValue("location")));
+ }
+ } else if ("info".equals(qName)) {
+ String organisation = attributes.getValue("organisation");
+ String name = attributes.getValue("module");
+ String branch = attributes.getValue("branch");
+ String revision = attributes.getValue("revision");
+ Map extraAttributes = new HashMap();
+ for (int i = 0; i < attributes.getLength(); i++) {
+ String attName = attributes.getQName(i);
+ if (attName.startsWith("extra-")) {
+ String extraAttrName =
attName.substring("extra-".length());
+ String extraAttrValue = attributes.getValue(i);
+ extraAttributes.put(extraAttrName, extraAttrValue);
+ }
+ }
+ mRevisionId = ModuleRevisionId.newInstance(organisation,
name, branch,
+ revision, extraAttributes);
+ }
+ }
+
+ public void endElement(String uri, String localName, String qname)
+ throws SAXException {
+ if ("dependencies".equals(qname)) {
+ // add the artifacts in the correct order
+ for (Iterator it = revisionsMap.values().iterator();
it.hasNext();) {
+ List artifactReports = (List) it.next();
+
SaxXmlReportParser.this.artifactReports.addAll(artifactReports);
+ for (Iterator iter = artifactReports.iterator();
iter.hasNext();) {
+ ArtifactDownloadReport artifactReport
+ = (ArtifactDownloadReport) iter.next();
+ if (artifactReport.getDownloadStatus() !=
DownloadStatus.FAILED) {
+ artifacts.add(artifactReport.getArtifact());
+ }
+ }
+
+ }
+ }
+ }
+
+ private int getMaxPos() {
+ return revisionsMap.isEmpty() ? -1 : ((Integer)
revisionsMap.keySet()
+ .toArray()[revisionsMap.size() - 1]).intValue();
+ }
+ }
+
private List/*<ModuleRevisionId>*/ mrids;
private List/*<ModuleRevisionId>*/ defaultMrids;
@@ -74,185 +252,7 @@
public void parse() throws Exception {
SAXParser saxParser =
SAXParserFactory.newInstance().newSAXParser();
- saxParser.parse(report, new DefaultHandler() {
- private String organisation;
-
- private String module;
-
- private String branch;
-
- private String revision;
-
- private int position;
-
- private Date pubdate;
-
- private boolean skip;
-
- private ModuleRevisionId mrid;
-
- private boolean isDefault;
-
- private SortedMap revisionsMap = new TreeMap(); // Use a
TreeMap to order by
-
- // position (position = key)
-
- private List revisionArtifacts = null;
-
- public void startElement(String uri, String localName, String
qName,
- Attributes attributes) throws SAXException {
- if ("module".equals(qName)) {
- organisation = attributes.getValue("organisation");
- module = attributes.getValue("name");
- } else if ("revision".equals(qName)) {
- revisionArtifacts = new ArrayList();
- branch = attributes.getValue("branch");
- revision = attributes.getValue("name");
- isDefault =
Boolean.valueOf(attributes.getValue("default")).booleanValue();
- // retrieve position from file. If no position is
found, it may be an old
- // report generated with a previous version,
- // in which case, we put it at the last position
- String pos = attributes.getValue("position");
- position = pos == null ? getMaxPos() + 1 :
Integer.valueOf(pos).intValue();
- if (attributes.getValue("error") != null
- || attributes.getValue("evicted") != null) {
- skip = true;
- } else {
- revisionsMap.put(new Integer(position),
revisionArtifacts);
- mrid = ModuleRevisionId.newInstance(organisation,
module, branch,
- revision,
ExtendableItemHelper.getExtraAttributes(attributes,
- "extra-"));
- mrids.add(mrid);
- if (isDefault) {
- defaultMrids.add(mrid);
- } else {
- Artifact metadataArtifact =
- DefaultArtifact.newIvyArtifact(mrid,
pubdate);
- MetadataArtifactDownloadReport madr =
- new
MetadataArtifactDownloadReport(metadataArtifact);
- metadataReports.put(mrid, madr);
- realMrids.add(mrid);
- }
- try {
- pubdate =
Ivy.DATE_FORMAT.parse(attributes.getValue("pubdate"));
- skip = false;
- } catch (ParseException e) {
- throw new IllegalArgumentException("invalid
publication date for "
- + organisation + " " + module + " " +
revision + ": "
- + attributes.getValue("pubdate"));
- }
- }
- } else if ("metadata-artifact".equals(qName)) {
- if (skip) {
- return;
- }
- MetadataArtifactDownloadReport madr =
- (MetadataArtifactDownloadReport)
metadataReports.get(mrid);
- if (madr != null) {
- madr.setDownloadStatus(
-
DownloadStatus.fromString(attributes.getValue("status")));
-
madr.setDownloadDetails(attributes.getValue("details"));
-
madr.setSize(Long.parseLong(attributes.getValue("size")));
-
madr.setDownloadTimeMillis(Long.parseLong(attributes.getValue("time")));
-
madr.setSearched(parseBoolean(attributes.getValue("searched")));
- if (attributes.getValue("location") != null) {
- madr.setLocalFile(new
File(attributes.getValue("location")));
- }
- if (attributes.getValue("original-local-location")
!= null) {
- madr.setOriginalLocalFile(
- new
File(attributes.getValue("original-local-location")));
- }
- if (attributes.getValue("origin-location") !=
null) {
- if
(ArtifactOrigin.UNKNOWN.getLocation().equals(
-
attributes.getValue("origin-location"))) {
-
madr.setArtifactOrigin(ArtifactOrigin.UNKNOWN);
- } else {
- madr.setArtifactOrigin(
- new ArtifactOrigin(
-
parseBoolean(attributes.getValue("origin-is-local")),
-
attributes.getValue("origin-location")));
- }
- }
- }
- } else if ("artifact".equals(qName)) {
- if (skip) {
- return;
- }
- String status = attributes.getValue("status");
- String artifactName = attributes.getValue("name");
- String type = attributes.getValue("type");
- String ext = attributes.getValue("ext");
- Artifact artifact = new DefaultArtifact(mrid, pubdate,
artifactName,
- type, ext,
ExtendableItemHelper.getExtraAttributes(attributes,
- "extra-"));
- ArtifactDownloadReport aReport = new
ArtifactDownloadReport(artifact);
-
aReport.setDownloadStatus(DownloadStatus.fromString(status));
-
aReport.setDownloadDetails(attributes.getValue("details"));
-
aReport.setSize(Long.parseLong(attributes.getValue("size")));
-
aReport.setDownloadTimeMillis(Long.parseLong(attributes.getValue("time")));
- if (attributes.getValue("location") != null) {
- aReport.setLocalFile(new
File(attributes.getValue("location")));
- }
- revisionArtifacts.add(aReport);
- } else if ("origin-location".equals(qName)) {
- if (skip) {
- return;
- }
- ArtifactDownloadReport aReport =
(ArtifactDownloadReport)
- revisionArtifacts.get(revisionArtifacts.size() -
1);
-
- if (ArtifactOrigin.UNKNOWN.getLocation().equals(
- attributes.getValue("location"))) {
- aReport.setArtifactOrigin(ArtifactOrigin.UNKNOWN);
- } else {
- aReport.setArtifactOrigin(
- new ArtifactOrigin(
-
parseBoolean(attributes.getValue("is-local")),
- attributes.getValue("location")));
- }
- } else if ("info".equals(qName)) {
- String organisation =
attributes.getValue("organisation");
- String name = attributes.getValue("module");
- String branch = attributes.getValue("branch");
- String revision = attributes.getValue("revision");
- Map extraAttributes = new HashMap();
- for (int i = 0; i < attributes.getLength(); i++) {
- String attName = attributes.getQName(i);
- if (attName.startsWith("extra-")) {
- String extraAttrName =
attName.substring("extra-".length());
- String extraAttrValue = attributes.getValue(i);
- extraAttributes.put(extraAttrName,
extraAttrValue);
- }
- }
- mRevisionId =
ModuleRevisionId.newInstance(organisation, name, branch,
- revision, extraAttributes);
- }
- }
-
- public void endElement(String uri, String localName, String
qname)
- throws SAXException {
- if ("dependencies".equals(qname)) {
- // add the artifacts in the correct order
- for (Iterator it = revisionsMap.values().iterator();
it.hasNext();) {
- List artifactReports = (List) it.next();
-
SaxXmlReportParser.this.artifactReports.addAll(artifactReports);
- for (Iterator iter = artifactReports.iterator();
iter.hasNext();) {
- ArtifactDownloadReport artifactReport
- = (ArtifactDownloadReport) iter.next();
- if (artifactReport.getDownloadStatus() !=
DownloadStatus.FAILED) {
-
artifacts.add(artifactReport.getArtifact());
- }
- }
-
- }
- }
- }
-
- private int getMaxPos() {
- return revisionsMap.isEmpty() ? -1 : ((Integer)
revisionsMap.keySet()
- .toArray()[revisionsMap.size() - 1]).intValue();
- }
- });
+ saxParser.parse(report, new XmlReportParserHandler());
}
private static boolean parseBoolean(String str) {