1 package uk.ac.cam.spectra;
2
3 import java.io.IOException;
4 import java.text.ParseException;
5 import java.util.Calendar;
6 import java.util.Date;
7
8 import nu.xom.Attribute;
9 import nu.xom.Builder;
10 import nu.xom.Element;
11 import nu.xom.ParsingException;
12 import nu.xom.ValidityException;
13
14 import org.apache.log4j.Logger;
15
16 import com.hp.hpl.jena.rdf.model.Model;
17 import com.hp.hpl.jena.rdf.model.Resource;
18 import com.hp.hpl.jena.rdf.model.Statement;
19
20
21
22
23
24
25
26
27
28
29
30 public class EmbargoMetadataEncoder {
31 private static final Logger LOG = Logger
32 .getLogger(EmbargoMetadataEncoder.class);
33
34 private String closedLicenseURL;
35
36 private String closedLicenseDescription;
37
38 private String closedLicenseMachineReadable;
39
40 private String openLicenseURL;
41
42 private String openLicenseDescription;
43
44 private String openLicenseMachineReadable;
45
46
47
48
49
50
51
52
53
54
55
56 public Element asXML(Model m, String uri) {
57 Resource r = m.createResource(uri);
58 Statement relStmt = r.getProperty(m
59 .createProperty(SpectraMetadata.embargo_release.toURIString()));
60 if (relStmt == null) {
61 return null;
62 }
63 String release = relStmt.getLiteral().getLexicalForm();
64 Statement perStmt = r.getProperty(m
65 .createProperty(SpectraMetadata.embargo_period.toURIString()));
66 if (perStmt == null) {
67 throw new RuntimeException(
68 "Problem with metadata encoding, expected to find embargo "
69 + "period statement in metadata");
70 }
71 int period = perStmt.getLiteral().getInt();
72 if (period == 0) {
73 return null;
74 }
75 Statement startStmt = r.getProperty(m
76 .createProperty(SpectraMetadata.embargo_date.toURIString()));
77
78 Date startD = null;
79 String start = (startStmt != null) ? startStmt.getLiteral()
80 .getLexicalForm() : null;
81 try {
82 startD = start == null ? new Date()
83 : Constants.SIMPLE_ISO_DATE_FORMAT.parse(start);
84 if (start == null) {
85 start = Constants.SIMPLE_ISO_DATE_FORMAT.format(startD);
86 }
87 } catch (ParseException e) {
88 throw new RuntimeException("Metadata encoding problem, expected "
89 + start + " to parse as a date: " + e.getMessage(), e);
90 }
91
92 Element embargo = new Element("embargo", Constants.SPECTRA_NS);
93 Element cLic = new Element("embargoLicense", Constants.SPECTRA_NS);
94 embargo.appendChild(cLic);
95 Element cLicUrl = new Element("url", Constants.SPECTRA_NS);
96 cLic.appendChild(cLicUrl);
97 cLicUrl.appendChild(closedLicenseURL);
98 Element cLicDesc = new Element("description", Constants.SPECTRA_NS);
99 cLic.appendChild(cLicDesc);
100 cLicDesc.appendChild(closedLicenseDescription);
101
102 Builder builder = new Builder();
103 if (closedLicenseMachineReadable != null) {
104 try {
105 Element cLicMachine = builder.build(
106 closedLicenseMachineReadable, Constants.SPECTRA_NS)
107 .getRootElement();
108 cLic.appendChild(cLicMachine);
109 } catch (ValidityException e) {
110 LOG.warn(
111 "Could not parse configured machine readable license as XML: "
112 + e.getMessage(), e);
113 } catch (ParsingException e) {
114 LOG.warn(
115 "Could not parse configured machine readable license as XML: "
116 + e.getMessage(), e);
117 } catch (IOException e) {
118 LOG.warn(
119 "Could not parse configured machine readable license as XML: "
120 + e.getMessage(), e);
121 }
122 }
123
124 Element oLic = new Element("postEmbargoLicense", Constants.SPECTRA_NS);
125 embargo.appendChild(oLic);
126 Element oLicUrl = new Element("url", Constants.SPECTRA_NS);
127 oLic.appendChild(oLicUrl);
128 oLicUrl.appendChild(openLicenseURL);
129
130 Element oLicDesc = new Element("description", Constants.SPECTRA_NS);
131 oLic.appendChild(oLicDesc);
132 oLicDesc.appendChild(openLicenseDescription);
133
134 if (openLicenseMachineReadable != null) {
135 try {
136 Element cLicMachine = builder.build(openLicenseMachineReadable,
137 Constants.SPECTRA_NS).getRootElement();
138 oLic.appendChild(cLicMachine);
139 } catch (ValidityException e) {
140 LOG.warn(
141 "Could not parse configured machine readable license as XML: "
142 + e.getMessage(), e);
143 } catch (ParsingException e) {
144 LOG.warn(
145 "Could not parse configured machine readable license as XML: "
146 + e.getMessage(), e);
147 } catch (IOException e) {
148 LOG.warn(
149 "Could not parse configured machine readable license as XML: "
150 + e.getMessage(), e);
151 }
152 }
153 Element embargoStart = new Element("start", Constants.SPECTRA_NS);
154 embargo.appendChild(embargoStart);
155 embargoStart.addAttribute(new Attribute("xsi:type", Constants.XSI_NS,
156 "xsd:date"));
157 embargoStart.appendChild(start);
158 Calendar c = Calendar.getInstance();
159 c.setTime(startD);
160 c.add(Calendar.MONTH, period);
161 String end = Constants.SIMPLE_ISO_DATE_FORMAT.format(c.getTime());
162 Element embargoEnd = new Element("end", Constants.SPECTRA_NS);
163 embargo.appendChild(embargoEnd);
164 embargoEnd.addAttribute(new Attribute("xsi:type", Constants.XSI_NS,
165 "xsd:date"));
166 embargoEnd.appendChild(end);
167
168 Element releaseEl = new Element("release", Constants.SPECTRA_NS);
169 embargo.appendChild(releaseEl);
170 releaseEl.appendChild(release);
171 return embargo;
172 }
173
174 public String getClosedLicenseDescription() {
175 return closedLicenseDescription;
176 }
177
178 public void setClosedLicenseDescription(String closedLicenseDescription) {
179 this.closedLicenseDescription = closedLicenseDescription;
180 }
181
182 public String getClosedLicenseMachineReadable() {
183 return closedLicenseMachineReadable;
184 }
185
186 public void setClosedLicenseMachineReadable(
187 String closedLicenseMachineReadable) {
188 this.closedLicenseMachineReadable = closedLicenseMachineReadable;
189 }
190
191 public String getClosedLicenseURL() {
192 return closedLicenseURL;
193 }
194
195 public void setClosedLicenseURL(String closedLicenseURL) {
196 this.closedLicenseURL = closedLicenseURL;
197 }
198
199 public String getOpenLicenseDescription() {
200 return openLicenseDescription;
201 }
202
203 public void setOpenLicenseDescription(String openLicenseDescription) {
204 this.openLicenseDescription = openLicenseDescription;
205 }
206
207 public String getOpenLicenseMachineReadable() {
208 return openLicenseMachineReadable;
209 }
210
211 public void setOpenLicenseMachineReadable(String openLicenseMachineReadable) {
212 this.openLicenseMachineReadable = openLicenseMachineReadable;
213 }
214
215 public String getOpenLicenseURL() {
216 return openLicenseURL;
217 }
218
219 public void setOpenLicenseURL(String openLicenseURL) {
220 this.openLicenseURL = openLicenseURL;
221 }
222
223 }