Skip to content

Commit 440190b

Browse files
committed
add support for resource list documents
1 parent 875b2c7 commit 440190b

File tree

6 files changed

+213
-5
lines changed

6 files changed

+213
-5
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public CapabilityList()
1616
public CapabilityList(String describedBy, Date lastModified)
1717
{
1818
super();
19-
this.capability = "capabilitylist";
19+
this.capability = ResourceSync.CAPABILITY_CAPABILITYLIST;
2020

2121
this.allowedCapabilities.add(ResourceSync.CAPABILITY_RESOURCELIST);
2222
this.allowedCapabilities.add(ResourceSync.CAPABILITY_RESOURCEDUMP);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.openarchives.resourcesync;
2+
3+
import java.util.Date;
4+
5+
public class ResourceList extends UrlSet
6+
{
7+
public ResourceList()
8+
{
9+
this(null, null);
10+
}
11+
12+
public ResourceList(Date lastModified)
13+
{
14+
this(lastModified, null);
15+
}
16+
17+
public ResourceList(Date lastMod, String capabilityList)
18+
{
19+
super();
20+
this.capability = ResourceSync.CAPABILITY_RESOURCELIST;
21+
22+
if (lastMod == null)
23+
{
24+
this.lastModified = new Date();
25+
}
26+
else
27+
{
28+
this.lastModified = lastMod;
29+
}
30+
31+
if (capabilityList != null)
32+
{
33+
this.addLn(ResourceSync.REL_RESOURCESYNC, capabilityList);
34+
}
35+
}
36+
37+
public void addResource(URL resource)
38+
{
39+
this.addEntry(resource);
40+
}
41+
42+
public URL addResource(String loc)
43+
{
44+
return this.addResource(loc, null, null);
45+
}
46+
47+
public URL addResource(String loc, Date lastMod)
48+
{
49+
return this.addResource(loc, lastMod, null);
50+
}
51+
52+
public URL addResource(String loc, Date lastMod, String changeFreq)
53+
{
54+
URL resource = new URL();
55+
resource.setLoc(loc);
56+
resource.setLastModified(lastMod);
57+
resource.setChangeFreq(changeFreq);
58+
this.addResource(resource);
59+
return resource;
60+
}
61+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class ResourceSync
1818
public static String REL_DESCRIBED_BY = "describedby";
1919
public static String REL_DESCRIBES = "describes";
2020
public static String REL_COLLECTION = "collection";
21+
public static String REL_RESOURCESYNC = "resourcesync";
2122

2223
// capabilities
2324
public static String CAPABILITY_RESOURCELIST = "resourcelist";

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,7 @@ public Element getElement()
9999
public String serialise()
100100
{
101101
Element element = this.getElement();
102-
// element = element.detach();
103102
Document doc = new Document(element);
104-
// doc.setRootElement(element);
105103
XMLOutputter out = new XMLOutputter();
106104
return out.outputString(doc);
107105
}

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,71 +179,93 @@ public Element getElement()
179179

180180
// set the metadata element
181181
Element md = new Element("md", ResourceSync.NS_RS);
182+
boolean trip = false;
182183
if (this.capability != null)
183184
{
184185
md.setAttribute("capability", this.capability, ResourceSync.NS_RS);
186+
trip = true;
185187
}
186188
if (this.change != null)
187189
{
188190
md.setAttribute("change", this.change, ResourceSync.NS_RS);
191+
trip = true;
189192
}
190193
String hashAttr = this.getHashAttr(this.hashes);
191194
if (!"".equals(hashAttr))
192195
{
193196
md.setAttribute("hash", hashAttr, ResourceSync.NS_ATOM);
197+
trip = true;
194198
}
195199
if (this.length > -1)
196200
{
197201
md.setAttribute("length", Integer.toString(this.length), ResourceSync.NS_ATOM);
202+
trip = true;
198203
}
199204
if (this.path != null)
200205
{
201206
md.setAttribute("path", this.path, ResourceSync.NS_RS);
207+
trip = true;
202208
}
203209
if (this.type != null)
204210
{
205211
md.setAttribute("type", this.type, ResourceSync.NS_ATOM);
212+
trip = true;
213+
}
214+
if (trip)
215+
{
216+
root.addContent(md);
206217
}
207-
root.addContent(md);
208218

209219
// set the link elements
210220
for (ResourceSyncLn ln : this.lns)
211221
{
222+
trip = false;
212223
Element link = new Element("ln", ResourceSync.NS_RS);
213224
String lnHash = this.getHashAttr(ln.getHashes());
214225
if (!"".equals(lnHash))
215226
{
216227
link.setAttribute("hash", lnHash, ResourceSync.NS_ATOM);
228+
trip = true;
217229
}
218230
if (ln.getHref() != null)
219231
{
220232
link.setAttribute("href", ln.getHref(), ResourceSync.NS_ATOM);
233+
trip = true;
221234
}
222235
if (ln.getLength() > -1)
223236
{
224237
link.setAttribute("length", Integer.toString(ln.getLength()), ResourceSync.NS_ATOM);
238+
trip = true;
225239
}
226240
if (ln.getModified() != null)
227241
{
228242
link.setAttribute("modified", ResourceSync.DATE_FORMAT.format(ln.getModified()), ResourceSync.NS_ATOM);
243+
trip = true;
229244
}
230245
if (ln.getPath() != null)
231246
{
232247
link.setAttribute("path", ln.getPath(), ResourceSync.NS_RS);
248+
trip = true;
233249
}
234250
if (ln.getRel() != null)
235251
{
236252
link.setAttribute("rel", ln.getRel(), ResourceSync.NS_ATOM);
253+
trip = true;
237254
}
238255
if (ln.getPri() > 0)
239256
{
240257
link.setAttribute("pri", Integer.toString(ln.getPri())); // FIXME: namespace?
258+
trip = true;
241259
}
242260
if (ln.getType() != null)
243261
{
244262
link.setAttribute("type", ln.getType(), ResourceSync.NS_ATOM);
263+
trip = true;
264+
}
265+
if (trip)
266+
{
267+
root.addContent(link);
245268
}
246-
root.addContent(link);
247269
}
248270

249271
return root;
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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.ResourceList;
7+
import org.openarchives.resourcesync.ResourceSync;
8+
import org.openarchives.resourcesync.ResourceSyncEntry;
9+
import org.openarchives.resourcesync.ResourceSyncLn;
10+
import org.openarchives.resourcesync.URL;
11+
12+
import java.util.Date;
13+
import java.util.List;
14+
import java.util.Map;
15+
16+
@RunWith(JUnit4.class)
17+
public class TestResourceList
18+
{
19+
@Test
20+
public void blankConstructor()
21+
{
22+
Date now = new Date();
23+
ResourceList rl = new ResourceList();
24+
25+
assert rl.getCapability().equals(ResourceSync.CAPABILITY_RESOURCELIST);
26+
assert rl.getLastModified().getTime() >= now.getTime();
27+
28+
List<ResourceSyncLn> lns = rl.getLns();
29+
assert lns.size() == 0;
30+
}
31+
32+
@Test
33+
public void construction()
34+
{
35+
Date now = new Date();
36+
ResourceList rl = new ResourceList(now, "http://capabilitylist");
37+
38+
assert rl.getCapability().equals(ResourceSync.CAPABILITY_RESOURCELIST);
39+
assert rl.getLastModified().equals(now);
40+
41+
List<ResourceSyncLn> lns = rl.getLns();
42+
assert lns.size() == 1;
43+
}
44+
45+
@Test
46+
public void methods()
47+
{
48+
Date now = new Date();
49+
ResourceList rl = new ResourceList();
50+
51+
URL resource = new URL();
52+
resource.setLoc("http://url1");
53+
resource.setChange(ResourceSync.CHANGE_UPDATED);
54+
rl.addResource(resource);
55+
56+
rl.addResource("http://url2", now, ResourceSync.FREQ_WEEKLY);
57+
rl.addResource("http://url3", now);
58+
59+
URL res4 = rl.addResource("http://url4");
60+
res4.setLastModified(now);
61+
res4.addHash(ResourceSync.HASH_MD5, "123456789");
62+
res4.setLength(100);
63+
res4.setType("application/xml");
64+
65+
List<ResourceSyncEntry> urls = rl.getUrls();
66+
boolean url1 = false;
67+
boolean url2 = false;
68+
boolean url3 = false;
69+
boolean url4 = false;
70+
for (ResourceSyncEntry url : urls)
71+
{
72+
if (url.getLoc().equals("http://url1"))
73+
{
74+
assert url.getChange().equals(ResourceSync.CHANGE_UPDATED);
75+
url1 = true;
76+
}
77+
if (url.getLoc().equals("http://url2"))
78+
{
79+
assert url.getLastModified().equals(now);
80+
assert url.getChangeFreq().equals(ResourceSync.FREQ_WEEKLY);
81+
url2 = true;
82+
}
83+
if (url.getLoc().equals("http://url3"))
84+
{
85+
assert url.getLastModified().equals(now);
86+
url3 = true;
87+
}
88+
if (url.getLoc().equals("http://url4"))
89+
{
90+
assert url.getLastModified().equals(now);
91+
assert url.getLength() == 100;
92+
assert url.getType().equals("application/xml");
93+
Map<String, String> hashes = url.getHashes();
94+
assert hashes.get(ResourceSync.HASH_MD5).equals("123456789");
95+
url4 = true;
96+
}
97+
}
98+
assert url1;
99+
assert url2;
100+
assert url3;
101+
assert url4;
102+
}
103+
104+
@Test
105+
public void manualCheck()
106+
{
107+
Date now = new Date();
108+
ResourceList rl = new ResourceList(now, "http://capabilitylist");
109+
110+
URL resource = new URL();
111+
resource.setLoc("http://url1");
112+
resource.setChange(ResourceSync.CHANGE_UPDATED);
113+
rl.addResource(resource);
114+
115+
rl.addResource("http://url2", now, ResourceSync.FREQ_WEEKLY);
116+
rl.addResource("http://url3", now);
117+
118+
URL res4 = rl.addResource("http://url4");
119+
res4.setLastModified(now);
120+
res4.addHash(ResourceSync.HASH_MD5, "123456789");
121+
res4.setLength(100);
122+
res4.setType("application/xml");
123+
124+
System.out.println(rl.serialise());
125+
}
126+
}

0 commit comments

Comments
 (0)