Skip to content

Commit 875b2c7

Browse files
committed
add support for capability list documents
1 parent d9f9ba2 commit 875b2c7

File tree

5 files changed

+297
-6
lines changed

5 files changed

+297
-6
lines changed
Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,109 @@
11
package org.openarchives.resourcesync;
22

3+
import java.util.ArrayList;
34
import java.util.Date;
5+
import java.util.List;
46

57
public class CapabilityList extends UrlSet
68
{
9+
private List<String> allowedCapabilities = new ArrayList<String>();
10+
711
public CapabilityList()
812
{
9-
this(null);
13+
this(null, null);
1014
}
1115

12-
public CapabilityList(String describedBy)
16+
public CapabilityList(String describedBy, Date lastModified)
1317
{
1418
super();
1519
this.capability = "capabilitylist";
16-
this.lastModified = new Date();
20+
21+
this.allowedCapabilities.add(ResourceSync.CAPABILITY_RESOURCELIST);
22+
this.allowedCapabilities.add(ResourceSync.CAPABILITY_RESOURCEDUMP);
23+
this.allowedCapabilities.add(ResourceSync.CAPABILITY_CHANGELIST);
24+
this.allowedCapabilities.add(ResourceSync.CAPABILITY_CHANGEDUMP);
25+
26+
if (lastModified == null)
27+
{
28+
this.lastModified = new Date();
29+
}
30+
else
31+
{
32+
this.lastModified = lastModified;
33+
}
1734

1835
if (describedBy != null)
1936
{
2037
this.addLn(ResourceSync.REL_DESCRIBED_BY, describedBy);
2138
}
2239
}
2340

24-
public URL addUrl(String loc, String capability)
41+
public void addDescribedBy(String describedBy)
42+
{
43+
this.addLn(ResourceSync.REL_DESCRIBED_BY, describedBy);
44+
}
45+
46+
public void addCapableUrl(URL url)
47+
throws SpecComplianceException
48+
{
49+
// first check to see if this is an allowed capability
50+
String capability = url.getCapability();
51+
if (capability == null || !this.allowedCapabilities.contains(url.getCapability()))
52+
{
53+
throw new SpecComplianceException("Attempting to add capability " + url.getCapability() + " to CapabilityList - not permitted. " +
54+
"CapabilityList may only represent resourcelist, resourcedump, changelist, changedump");
55+
}
56+
57+
// now determine if a URL already exists with this capability
58+
List<ResourceSyncEntry> entries = this.getUrls();
59+
ResourceSyncEntry removable = null;
60+
for (ResourceSyncEntry entry : entries)
61+
{
62+
if (capability.equals(entry.getCapability()))
63+
{
64+
removable = entry;
65+
break; // have to break to avoid concurrent access/modification issues
66+
}
67+
}
68+
if (removable != null)
69+
{
70+
entries.remove(removable);
71+
}
72+
73+
this.addUrl(url);
74+
}
75+
76+
public URL addCapableUrl(String loc, String capability)
77+
throws SpecComplianceException
2578
{
2679
URL url = new URL();
2780
url.setLoc(loc);
2881
url.setCapability(capability);
29-
this.addEntry(url);
82+
this.addCapableUrl(url);
3083
return url;
3184
}
85+
86+
public URL setResourceList(String loc)
87+
throws SpecComplianceException
88+
{
89+
return this.addCapableUrl(loc, ResourceSync.CAPABILITY_RESOURCELIST);
90+
}
91+
92+
public URL setResourceDump(String loc)
93+
throws SpecComplianceException
94+
{
95+
return this.addCapableUrl(loc, ResourceSync.CAPABILITY_RESOURCEDUMP);
96+
}
97+
98+
public URL setChangeList(String loc)
99+
throws SpecComplianceException
100+
{
101+
return this.addCapableUrl(loc, ResourceSync.CAPABILITY_CHANGELIST);
102+
}
103+
104+
public URL setChangeDump(String loc)
105+
throws SpecComplianceException
106+
{
107+
return this.addCapableUrl(loc, ResourceSync.CAPABILITY_CHANGEDUMP);
108+
}
32109
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void setLoc(String url, Date lastModified)
4040

4141
public void setLoc(String url)
4242
{
43-
this.setLoc(url, new Date(), null);
43+
this.setLoc(url, null, null);
4444
}
4545

4646
public void setLastModified(Date lastModified)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.openarchives.resourcesync;
2+
3+
public class SpecComplianceException extends Exception
4+
{
5+
public SpecComplianceException()
6+
{
7+
super();
8+
}
9+
10+
public SpecComplianceException(String message)
11+
{
12+
super(message);
13+
}
14+
15+
public SpecComplianceException(String message, Throwable cause)
16+
{
17+
super(message, cause);
18+
}
19+
20+
public SpecComplianceException(Throwable cause)
21+
{
22+
super(cause);
23+
}
24+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
package org.openarchives.resourcesync;
22

3+
import java.util.List;
4+
35
public class UrlSet extends ResourceSyncDocument
46
{
57
public UrlSet()
68
{
79
this.root = "urlset";
810
}
11+
12+
public void addUrl(URL url)
13+
{
14+
this.addEntry(url);
15+
}
16+
17+
public List<ResourceSyncEntry> getUrls()
18+
{
19+
return this.getEntries();
20+
}
921
}
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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.CapabilityList;
7+
import org.openarchives.resourcesync.ResourceSync;
8+
import org.openarchives.resourcesync.ResourceSyncEntry;
9+
import org.openarchives.resourcesync.ResourceSyncLn;
10+
import org.openarchives.resourcesync.SpecComplianceException;
11+
import org.openarchives.resourcesync.URL;
12+
13+
import java.util.Date;
14+
import java.util.List;
15+
16+
@RunWith(JUnit4.class)
17+
public class TestCapabilityList
18+
{
19+
@Test
20+
public void blankConstructor()
21+
{
22+
Date now = new Date();
23+
CapabilityList cl = new CapabilityList();
24+
25+
assert cl.getCapability().equals(ResourceSync.CAPABILITY_CAPABILITYLIST);
26+
assert cl.getLastModified().getTime() >= now.getTime();
27+
28+
List<ResourceSyncLn> lns = cl.getLns();
29+
assert lns.size() == 0;
30+
}
31+
32+
@Test
33+
public void construction()
34+
{
35+
Date now = new Date();
36+
CapabilityList cl = new CapabilityList("http://describes", now);
37+
38+
assert cl.getCapability().equals(ResourceSync.CAPABILITY_CAPABILITYLIST);
39+
assert cl.getLastModified().equals(now);
40+
41+
List<ResourceSyncLn> lns = cl.getLns();
42+
assert lns.size() == 1;
43+
}
44+
45+
@Test
46+
public void methods()
47+
{
48+
Date now = new Date();
49+
CapabilityList cl = new CapabilityList();
50+
51+
cl.addDescribedBy("http://describes1");
52+
List<ResourceSyncLn> lns = cl.getLns();
53+
assert lns.size() == 1;
54+
55+
cl.addDescribedBy("http://describes2");
56+
lns = cl.getLns();
57+
assert lns.size() == 2;
58+
59+
URL url = new URL();
60+
url.setLoc("http://loc");
61+
url.setCapability(ResourceSync.CAPABILITY_CHANGELIST);
62+
try
63+
{
64+
cl.addCapableUrl(url);
65+
}
66+
catch (SpecComplianceException e)
67+
{
68+
assert false;
69+
}
70+
List<ResourceSyncEntry> urls = cl.getUrls();
71+
assert urls.size() == 1;
72+
assert urls.get(0).getLoc().equals("http://loc");
73+
74+
try
75+
{
76+
cl.addCapableUrl("http://otherloc", ResourceSync.CAPABILITY_RESOURCELIST);
77+
}
78+
catch (SpecComplianceException e)
79+
{
80+
assert false;
81+
}
82+
urls = cl.getUrls();
83+
assert urls.size() == 2;
84+
boolean resourcel = false;
85+
boolean changel = false;
86+
for (ResourceSyncEntry u : urls)
87+
{
88+
if (u.getCapability().equals(ResourceSync.CAPABILITY_RESOURCELIST))
89+
{
90+
assert u.getLoc().equals("http://otherloc");
91+
resourcel = true;
92+
}
93+
if (u.getCapability().equals(ResourceSync.CAPABILITY_CHANGELIST))
94+
{
95+
assert u.getLoc().equals("http://loc");
96+
changel = true;
97+
}
98+
}
99+
assert resourcel;
100+
assert changel;
101+
102+
try
103+
{
104+
cl.addCapableUrl("http://fail", ResourceSync.CAPABILITY_CHANGEDUMP_MANIFEST);
105+
assert false;
106+
}
107+
catch (SpecComplianceException e)
108+
{
109+
assert true;
110+
}
111+
112+
try
113+
{
114+
cl.setResourceList("http://resourcelist");
115+
cl.setResourceDump("http://resourcedump");
116+
cl.setChangeList("http://changelist");
117+
cl.setChangeDump("http://changedump");
118+
}
119+
catch (SpecComplianceException e)
120+
{
121+
assert false;
122+
}
123+
124+
urls = cl.getUrls();
125+
assert urls.size() == 4;
126+
boolean resourcelist = false;
127+
boolean resourcedump = false;
128+
boolean changelist = false;
129+
boolean changedump = false;
130+
for (ResourceSyncEntry u : urls)
131+
{
132+
if (u.getCapability().equals(ResourceSync.CAPABILITY_RESOURCELIST))
133+
{
134+
assert u.getLoc().equals("http://resourcelist");
135+
resourcelist = true;
136+
}
137+
if (u.getCapability().equals(ResourceSync.CAPABILITY_RESOURCEDUMP))
138+
{
139+
assert u.getLoc().equals("http://resourcedump");
140+
resourcedump = true;
141+
}
142+
if (u.getCapability().equals(ResourceSync.CAPABILITY_CHANGELIST))
143+
{
144+
assert u.getLoc().equals("http://changelist");
145+
changelist = true;
146+
}
147+
if (u.getCapability().equals(ResourceSync.CAPABILITY_CHANGEDUMP))
148+
{
149+
assert u.getLoc().equals("http://changedump");
150+
changedump = true;
151+
}
152+
}
153+
assert resourcelist;
154+
assert resourcedump;
155+
assert changelist;
156+
assert changedump;
157+
}
158+
159+
@Test
160+
public void manualCheck()
161+
{
162+
try
163+
{
164+
CapabilityList cl = new CapabilityList();
165+
cl.addDescribedBy("http://describedby");
166+
cl.setResourceList("http://resourcelist");
167+
cl.setResourceDump("http://resourcedump");
168+
cl.setChangeList("http://changelist");
169+
cl.setChangeDump("http://changedump");
170+
171+
System.out.println(cl.serialise());
172+
}
173+
catch (SpecComplianceException e)
174+
{
175+
e.printStackTrace();
176+
}
177+
}
178+
}

0 commit comments

Comments
 (0)