Skip to content

Commit af1a87a

Browse files
committed
Add support for changelistarchive document format, and introduce support in the base classes for ordered url/sitemap elements
1 parent 440190b commit af1a87a

File tree

6 files changed

+254
-7
lines changed

6 files changed

+254
-7
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.openarchives.resourcesync;
2+
3+
import java.util.Date;
4+
5+
public class ChangeListArchive extends SitemapIndex
6+
{
7+
public ChangeListArchive()
8+
{
9+
this(null, null);
10+
}
11+
12+
public ChangeListArchive(Date lastMod)
13+
{
14+
this(lastMod, null);
15+
}
16+
17+
public ChangeListArchive(Date lastMod, String capabilityList)
18+
{
19+
this.capability = ResourceSync.CAPABILITY_CHANGELIST;
20+
21+
if (lastMod != null)
22+
{
23+
this.setLastModified(lastMod);
24+
}
25+
else
26+
{
27+
this.setLastModified(new Date());
28+
}
29+
30+
if (capabilityList != null)
31+
{
32+
this.addLn(ResourceSync.REL_RESOURCESYNC, capabilityList);
33+
}
34+
}
35+
36+
public void addChangeList(Sitemap sitemap)
37+
{
38+
this.addSitemap(sitemap);
39+
}
40+
41+
public Sitemap addChangeList(String loc, Date lastMod)
42+
{
43+
Sitemap sitemap = new Sitemap();
44+
sitemap.setLoc(loc);
45+
sitemap.setLastModified(lastMod);
46+
this.addChangeList(sitemap);
47+
return sitemap;
48+
}
49+
}

src/main/java/org/openarchives/resourcesync/ResourceSyncDocument.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.HashMap;
1010
import java.util.List;
1111
import java.util.Map;
12+
import java.util.TreeMap;
1213

1314
public abstract class ResourceSyncDocument
1415
{
@@ -18,7 +19,8 @@ public abstract class ResourceSyncDocument
1819

1920
// these options can be accessed using getters and setters
2021
protected Date lastModified;
21-
protected List<ResourceSyncEntry> entries = new ArrayList<ResourceSyncEntry>();
22+
protected List<ResourceSyncEntry> unorderedEntries = new ArrayList<ResourceSyncEntry>();
23+
protected TreeMap<Date, List<ResourceSyncEntry>> orderedEntries = new TreeMap<Date, List<ResourceSyncEntry>>();
2224
protected List<ResourceSyncLn> lns = new ArrayList<ResourceSyncLn>();
2325

2426
public ResourceSyncLn addLn(String rel, String href)
@@ -38,12 +40,21 @@ public void addLn(ResourceSyncLn ln)
3840

3941
public void addEntry(ResourceSyncEntry entry)
4042
{
41-
this.entries.add(entry);
43+
Date key = entry.getLastModified() == null ? new Date(0) : entry.getLastModified();
44+
if (!this.orderedEntries.containsKey(key))
45+
{
46+
this.orderedEntries.put(key, new ArrayList<ResourceSyncEntry>());
47+
}
48+
this.orderedEntries.get(key).add(entry);
49+
50+
// FIXME: are there any serious concerns about storing the entry in two locations?
51+
// it's all by-reference, right?
52+
this.unorderedEntries.add(entry);
4253
}
4354

4455
public List<ResourceSyncEntry> getEntries()
4556
{
46-
return entries;
57+
return this.unorderedEntries;
4758
}
4859

4960
public List<ResourceSyncLn> getLns()
@@ -75,7 +86,10 @@ public Element getElement()
7586
// set the capability of the document in the rs:md
7687
Element md = new Element("md", ResourceSync.NS_RS);
7788
md.setAttribute("capability", this.capability, ResourceSync.NS_RS);
78-
md.setAttribute("modified", ResourceSync.DATE_FORMAT.format(this.lastModified), ResourceSync.NS_ATOM);
89+
if (this.lastModified != null)
90+
{
91+
md.setAttribute("modified", ResourceSync.DATE_FORMAT.format(this.lastModified), ResourceSync.NS_ATOM);
92+
}
7993
root.addContent(md);
8094

8195
// serialise the rs:ln elements
@@ -87,10 +101,14 @@ public Element getElement()
87101
root.addContent(lnEl);
88102
}
89103

90-
for (ResourceSyncEntry entry : this.entries)
104+
for (Date date : this.orderedEntries.keySet())
91105
{
92-
Element entryElement = entry.getElement();
93-
root.addContent(entryElement);
106+
List<ResourceSyncEntry> entries = this.orderedEntries.get(date);
107+
for (ResourceSyncEntry entry : entries)
108+
{
109+
Element entryElement = entry.getElement();
110+
root.addContent(entryElement);
111+
}
94112
}
95113

96114
return root;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.openarchives.resourcesync;
2+
3+
public class Sitemap extends ResourceSyncEntry
4+
{
5+
public Sitemap()
6+
{
7+
this.root = "sitemap";
8+
}
9+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.openarchives.resourcesync;
2+
3+
import java.util.List;
4+
5+
public class SitemapIndex extends ResourceSyncDocument
6+
{
7+
public SitemapIndex()
8+
{
9+
this.root = "sitemapindex";
10+
}
11+
12+
public void addSitemap(Sitemap sitemap)
13+
{
14+
this.addEntry(sitemap);
15+
}
16+
17+
public List<ResourceSyncEntry> getSitemaps()
18+
{
19+
return this.getEntries();
20+
}
21+
}

src/test/java/org/openarchives/resourcesync/test/TestBaseClasses.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,65 @@ public void resourceSyncDocument()
365365
System.out.println(serial);
366366
}
367367

368+
@Test
369+
public void ordering()
370+
{
371+
ResourceSyncDocument doc = new TestResourceSyncDocument();
372+
373+
ResourceSyncEntry entry1 = new TestResourceSyncEntry();
374+
entry1.setLoc("http://entry1");
375+
entry1.setLastModified(new Date(1000));
376+
377+
ResourceSyncEntry entry2 = new TestResourceSyncEntry();
378+
entry2.setLoc("http://entry2");
379+
entry2.setLastModified(new Date(10000));
380+
381+
ResourceSyncEntry entry3 = new TestResourceSyncEntry();
382+
entry3.setLoc("http://entry3");
383+
entry3.setLastModified(new Date(5000));
384+
385+
ResourceSyncEntry entry4 = new TestResourceSyncEntry();
386+
entry4.setLoc("http://entry4");
387+
388+
doc.addEntry(entry1);
389+
doc.addEntry(entry2);
390+
doc.addEntry(entry3);
391+
doc.addEntry(entry4);
392+
393+
Element element = doc.getElement();
394+
List<Element> entries = element.getChildren("url", ResourceSync.NS_SITEMAP);
395+
assert entries.size() == 4;
396+
int i = 0;
397+
for (Element entry : entries)
398+
{
399+
// these should all be in order, so this test will check that what we get
400+
// comes in the following order (oldest first):
401+
// 0s - http://entry4
402+
// 1000s - http://entry1
403+
// 5000s - http//entry3
404+
// 10000s - http://entry4
405+
if (i == 0)
406+
{
407+
assert entry.getChild("loc", ResourceSync.NS_SITEMAP).getText().equals("http://entry4");
408+
}
409+
if (i == 1)
410+
{
411+
assert entry.getChild("loc", ResourceSync.NS_SITEMAP).getText().equals("http://entry1");
412+
}
413+
if (i == 2)
414+
{
415+
assert entry.getChild("loc", ResourceSync.NS_SITEMAP).getText().equals("http://entry3");
416+
}
417+
if (i == 3)
418+
{
419+
assert entry.getChild("loc", ResourceSync.NS_SITEMAP).getText().equals("http://entry2");
420+
}
421+
422+
// increment our counter
423+
i++;
424+
}
425+
}
426+
368427
class TestResourceSyncEntry extends ResourceSyncEntry
369428
{
370429
public TestResourceSyncEntry()
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package org.openarchives.resourcesync.test;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.junit.runners.JUnit4;
6+
import org.openarchives.resourcesync.ChangeListArchive;
7+
import org.openarchives.resourcesync.ResourceSync;
8+
import org.openarchives.resourcesync.ResourceSyncEntry;
9+
import org.openarchives.resourcesync.ResourceSyncLn;
10+
import org.openarchives.resourcesync.Sitemap;
11+
12+
import java.util.Date;
13+
import java.util.List;
14+
15+
@RunWith(JUnit4.class)
16+
public class TestChangeListArchive
17+
{
18+
@Test
19+
public void blankConstructor()
20+
{
21+
Date now = new Date();
22+
ChangeListArchive cla = new ChangeListArchive();
23+
24+
assert cla.getCapability().equals(ResourceSync.CAPABILITY_CHANGELIST);
25+
assert cla.getLastModified().getTime() >= now.getTime();
26+
27+
List<ResourceSyncLn> lns = cla.getLns();
28+
assert lns.size() == 0;
29+
}
30+
31+
@Test
32+
public void construction()
33+
{
34+
Date now = new Date();
35+
ChangeListArchive cla = new ChangeListArchive(now, "http://capabilitylist");
36+
37+
assert cla.getCapability().equals(ResourceSync.CAPABILITY_CHANGELIST);
38+
assert cla.getLastModified().equals(now);
39+
40+
List<ResourceSyncLn> lns = cla.getLns();
41+
assert lns.size() == 1;
42+
}
43+
44+
@Test
45+
public void methods()
46+
{
47+
ChangeListArchive cla = new ChangeListArchive();
48+
49+
Sitemap sm = new Sitemap();
50+
sm.setLoc("http://changelist1");
51+
sm.setLastModified(new Date(1000));
52+
cla.addChangeList(sm);
53+
54+
Sitemap sm2 = cla.addChangeList("http://changelist2", new Date(10000));
55+
sm2.setType("application/xml");
56+
57+
List<ResourceSyncEntry> entries = cla.getEntries();
58+
boolean change1 = false;
59+
boolean change2 = false;
60+
for (ResourceSyncEntry entry : entries)
61+
{
62+
if (entry.getLoc().equals("http://changelist1"))
63+
{
64+
assert entry.getLastModified().equals(new Date(1000));
65+
change1 = true;
66+
}
67+
if (entry.getLoc().equals("http://changelist2"))
68+
{
69+
assert entry.getLastModified().equals(new Date(10000));
70+
assert entry.getType().equals("application/xml");
71+
change2 = true;
72+
}
73+
}
74+
assert change1;
75+
assert change2;
76+
}
77+
@Test
78+
public void manualCheck()
79+
{
80+
Date now = new Date();
81+
ChangeListArchive cla = new ChangeListArchive(now, "http://capabilitylist");
82+
83+
cla.addChangeList("http://changelist1", new Date(10000));
84+
cla.addChangeList("http://changelist2", new Date(1000));
85+
cla.addChangeList("http://changelist3", new Date(50000));
86+
cla.addChangeList("http://changelist4", new Date(5000));
87+
88+
System.out.println(cla.serialise());
89+
}
90+
91+
}

0 commit comments

Comments
 (0)