/* sort entries by year -> month -> day -> lastModified */
Object[] sortedList = filteredDocs.toArray();
Arrays.sort(sortedList, new Comparator() {
public int compare(Object o1, Object o2) {
Document d1,d2;
int year1,month1,day1;
int year2,month2,day2;
d1 = (Document)o1;
d2 = (Document)o2;
String[] patterns;
try {
patterns = d1.getPath().split("/");
} catch (DocumentException e) {
throw new RuntimeException(e);
}
year1 = Integer.parseInt(patterns[2]);
month1 = Integer.parseInt(patterns[3]);
day1 = Integer.parseInt(patterns[4]);
try {
patterns = d2.getPath().split("/");
} catch (DocumentException e) {
throw new RuntimeException(e);
}
year2 = Integer.parseInt(patterns[2]);
month2 = Integer.parseInt(patterns[3]);
day2 = Integer.parseInt(patterns[4]);
if (year1 > year2) {
return 1;
} else if (year1 == year2) {
if (month1 > month2) {
return 1;
} else if (month1 == month2) {
if (day1 > day2) {
return 1;
} else if (day1 == day2) {
/* newest first */
try {
Date date1 = new Date(d1.getLastModified());
Date date2 = new Date(d2.getLastModified());
return date2.compareTo(date1);
} catch (DocumentException e) {
throw new RuntimeException(e);
}
} else {
return -1;
}
} else {
return -1;
}
} else {
return -1;
}
}
});
/* group entries by year -> month -> day */
/* works because the list is sorted =) */
int currentYear = 0;
int currentMonth = 0;
int currentDay = 0;
boolean yearOpen = false;
boolean monthOpen = false;
boolean dayOpen = false;
for (int i=0; i<sortedList.length; i++) {
Document doc = ((Document)sortedList[i]);
String[] patterns = doc.getPath().split("/");
int year = Integer.parseInt(patterns[2]);
int month = Integer.parseInt(patterns[3]);
int day = Integer.parseInt(patterns[4]);
if (year != currentYear) {
if (dayOpen) {
dayOpen = false;
this.contentHandler.endElement(URI, DAY_NODE_NAME, PREFIX + ':'
+ DAY_NODE_NAME);
}
if (monthOpen) {
monthOpen = false;
this.contentHandler.endElement(URI, MONTH_NODE_NAME, PREFIX + ':'
+ MONTH_NODE_NAME);
}
if (yearOpen) {
this.contentHandler.endElement(URI, YEAR_NODE_NAME, PREFIX + ':'
+ YEAR_NODE_NAME);
}
this.attributes.clear();
attributes.addAttribute("", ID_ATTR_NAME,
ID_ATTR_NAME, "CDATA", String.valueOf(year));
this.contentHandler.startElement(URI, YEAR_NODE_NAME, PREFIX + ':'
+ YEAR_NODE_NAME, attributes);
yearOpen = true;
currentYear = year;
currentMonth = 0;
currentDay = 0;
}
if (month != currentMonth) {
if (dayOpen) {
dayOpen = false;
this.contentHandler.endElement(URI, DAY_NODE_NAME, PREFIX + ':'
+ DAY_NODE_NAME);
}
if (monthOpen) {
this.contentHandler.endElement(URI, MONTH_NODE_NAME, PREFIX + ':'
+ MONTH_NODE_NAME);
}
this.attributes.clear();
attributes.addAttribute("", ID_ATTR_NAME,
ID_ATTR_NAME, "CDATA", String.valueOf(month));
this.contentHandler.startElement(URI, MONTH_NODE_NAME, PREFIX + ':'
+ MONTH_NODE_NAME, attributes);
monthOpen = true;
currentMonth = month;
currentDay = 0;
}
if (day != currentDay) {
if (dayOpen) {
this.contentHandler.endElement(URI, DAY_NODE_NAME, PREFIX + ':'
+ DAY_NODE_NAME);
}
this.attributes.clear();
attributes.addAttribute("", ID_ATTR_NAME,
ID_ATTR_NAME, "CDATA", String.valueOf(day));
this.contentHandler.startElement(URI, DAY_NODE_NAME, PREFIX + ':'
+ DAY_NODE_NAME, attributes);
dayOpen = true;
currentDay = day;
}
if (structure == null) {
attributes.clear();
attributes.addAttribute("", PATH_ATTR_NAME,
PATH_ATTR_NAME, "CDATA", doc.getPath());
attributes.addAttribute("", URL_ATTR_NAME,
URL_ATTR_NAME, "CDATA", doc.getCanonicalWebappURL());
org.w3c.dom.Document docDOM = DocumentHelper.readDocument(doc.getInputStream());
Element parent = docDOM.getDocumentElement();
Element element = (Element) XPathAPI.selectSingleNode(parent,
"/*[local-name() = 'entry']/*[local-name() = 'title']");
attributes.addAttribute("", TITLE_ATTR_NAME,
TITLE_ATTR_NAME, "CDATA",DocumentHelper.getSimpleElementText(element));
attributes.addAttribute("", LASTMOD_ATTR_NAME,
LASTMOD_ATTR_NAME, "CDATA", String.valueOf(doc.getLastModified()));
DocumentHelper.getSimpleElementText(element);
this.contentHandler.startElement(URI, ENTRY_NODE_NAME,
PREFIX + ':' + ENTRY_NODE_NAME, attributes);
this.contentHandler.endElement(URI, ENTRY_NODE_NAME, PREFIX
+ ':' + ENTRY_NODE_NAME);