Skip to content

Commit d9f9ba2

Browse files
committed
base classes for creating resourcesync documents
0 parents  commit d9f9ba2

File tree

11 files changed

+984
-0
lines changed

11 files changed

+984
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea/
2+
resourcesync.iml
3+
target/
4+

README.md

Whitespace-only changes.

pom.xml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.openarchives</groupId>
8+
<artifactId>resourcesync</artifactId>
9+
<version>0.5-SNAPSHOT</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>org.jdom</groupId>
14+
<artifactId>jdom</artifactId>
15+
<version>2.0.2</version>
16+
</dependency>
17+
18+
<dependency>
19+
<groupId>junit</groupId>
20+
<artifactId>junit</artifactId>
21+
<version>4.8.2</version>
22+
</dependency>
23+
</dependencies>
24+
25+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.openarchives.resourcesync;
2+
3+
import java.util.Date;
4+
5+
public class CapabilityList extends UrlSet
6+
{
7+
public CapabilityList()
8+
{
9+
this(null);
10+
}
11+
12+
public CapabilityList(String describedBy)
13+
{
14+
super();
15+
this.capability = "capabilitylist";
16+
this.lastModified = new Date();
17+
18+
if (describedBy != null)
19+
{
20+
this.addLn(ResourceSync.REL_DESCRIBED_BY, describedBy);
21+
}
22+
}
23+
24+
public URL addUrl(String loc, String capability)
25+
{
26+
URL url = new URL();
27+
url.setLoc(loc);
28+
url.setCapability(capability);
29+
this.addEntry(url);
30+
return url;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.openarchives.resourcesync;
2+
3+
import org.jdom2.Namespace;
4+
5+
import java.text.SimpleDateFormat;
6+
7+
public class ResourceSync
8+
{
9+
// namespaces
10+
public static Namespace NS_SITEMAP = Namespace.getNamespace("sm", "http://www.sitemaps.org/schemas/sitemap/0.9");
11+
public static Namespace NS_RS = Namespace.getNamespace("rs", "http://www.openarchives.org/rs/terms/");
12+
public static Namespace NS_ATOM = Namespace.getNamespace("atom", "http://www.w3.org/2005/Atom");
13+
14+
// date format
15+
public static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
16+
17+
// rel values
18+
public static String REL_DESCRIBED_BY = "describedby";
19+
public static String REL_DESCRIBES = "describes";
20+
public static String REL_COLLECTION = "collection";
21+
22+
// capabilities
23+
public static String CAPABILITY_RESOURCELIST = "resourcelist";
24+
public static String CAPABILITY_CHANGELIST = "changelist";
25+
public static String CAPABILITY_RESOURCEDUMP = "resourcedump";
26+
public static String CAPABILITY_CHANGEDUMP = "changedump";
27+
public static String CAPABILITY_RESOURCEDUMP_MANIFEST = "resourcedump-manifest";
28+
public static String CAPABILITY_CHANGEDUMP_MANIFEST = "changedump-manifest";
29+
public static String CAPABILITY_CAPABILITYLIST = "capabilitylist";
30+
31+
// change frequency defined options (although technically you can use others if you want)
32+
public static String FREQ_ALWAYS = "always";
33+
public static String FREQ_HOURLY = "hourly";
34+
public static String FREQ_DAILY = "daily";
35+
public static String FREQ_WEEKLY = "weekly";
36+
public static String FREQ_YEARLY = "yearly";
37+
public static String FREQ_NEVER = "never";
38+
39+
// change types
40+
public static String CHANGE_CREATED = "created";
41+
public static String CHANGE_UPDATED = "updated";
42+
public static String CHANGE_DELETED = "deleted";
43+
44+
// supported hashes
45+
public static String HASH_MD5 = "md5";
46+
public static String HASH_SHA_256 = "sha-256";
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package org.openarchives.resourcesync;
2+
3+
import org.jdom2.Document;
4+
import org.jdom2.Element;
5+
import org.jdom2.output.XMLOutputter;
6+
7+
import java.util.ArrayList;
8+
import java.util.Date;
9+
import java.util.HashMap;
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
public abstract class ResourceSyncDocument
14+
{
15+
// these options should be provided by the extending class
16+
protected String capability;
17+
protected String root;
18+
19+
// these options can be accessed using getters and setters
20+
protected Date lastModified;
21+
protected List<ResourceSyncEntry> entries = new ArrayList<ResourceSyncEntry>();
22+
protected List<ResourceSyncLn> lns = new ArrayList<ResourceSyncLn>();
23+
24+
public ResourceSyncLn addLn(String rel, String href)
25+
{
26+
// rs:ln elements are repeatable and can have multiple ones with the same rel
27+
ResourceSyncLn ln = new ResourceSyncLn();
28+
ln.setRel(rel);
29+
ln.setHref(href);
30+
this.lns.add(ln);
31+
return ln;
32+
}
33+
34+
public void addLn(ResourceSyncLn ln)
35+
{
36+
this.lns.add(ln);
37+
}
38+
39+
public void addEntry(ResourceSyncEntry entry)
40+
{
41+
this.entries.add(entry);
42+
}
43+
44+
public List<ResourceSyncEntry> getEntries()
45+
{
46+
return entries;
47+
}
48+
49+
public List<ResourceSyncLn> getLns()
50+
{
51+
return lns;
52+
}
53+
54+
public Date getLastModified()
55+
{
56+
return lastModified;
57+
}
58+
59+
public void setLastModified(Date lastModified)
60+
{
61+
this.lastModified = lastModified;
62+
}
63+
64+
public String getCapability()
65+
{
66+
return capability;
67+
}
68+
69+
public Element getElement()
70+
{
71+
Element root = new Element(this.root, ResourceSync.NS_SITEMAP);
72+
root.addNamespaceDeclaration(ResourceSync.NS_ATOM);
73+
root.addNamespaceDeclaration(ResourceSync.NS_RS);
74+
75+
// set the capability of the document in the rs:md
76+
Element md = new Element("md", ResourceSync.NS_RS);
77+
md.setAttribute("capability", this.capability, ResourceSync.NS_RS);
78+
md.setAttribute("modified", ResourceSync.DATE_FORMAT.format(this.lastModified), ResourceSync.NS_ATOM);
79+
root.addContent(md);
80+
81+
// serialise the rs:ln elements
82+
for (ResourceSyncLn ln : this.lns)
83+
{
84+
Element lnEl = new Element("ln", ResourceSync.NS_RS);
85+
lnEl.setAttribute("rel", ln.getRel(), ResourceSync.NS_ATOM);
86+
lnEl.setAttribute("href", ln.getHref(), ResourceSync.NS_ATOM);
87+
root.addContent(lnEl);
88+
}
89+
90+
for (ResourceSyncEntry entry : this.entries)
91+
{
92+
Element entryElement = entry.getElement();
93+
root.addContent(entryElement);
94+
}
95+
96+
return root;
97+
}
98+
99+
public String serialise()
100+
{
101+
Element element = this.getElement();
102+
// element = element.detach();
103+
Document doc = new Document(element);
104+
// doc.setRootElement(element);
105+
XMLOutputter out = new XMLOutputter();
106+
return out.outputString(doc);
107+
}
108+
}

0 commit comments

Comments
 (0)