001: /**
002: * Copyright (c) 2007, Aberystwyth University
003: *
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the
012: * following disclaimer.
013: *
014: * - Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * - Neither the name of the Centre for Advanced Software and
020: * Intelligent Systems (CASIS) nor the names of its
021: * contributors may be used to endorse or promote products derived
022: * from this software without specific prior written permission.
023: *
024: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
025: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
026: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
027: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
028: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
029: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
030: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
031: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
032: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
033: * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
034: * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
035: * SUCH DAMAGE.
036: */package org.purl.sword.base;
037:
038: /**
039: * Author : $Author: nst $
040: * Date : $Date: 2007/09/21 15:18:54 $
041: * Revision : $Revision: 1.4 $
042: * Name : $Name: $
043: */
044:
045: import java.util.ArrayList;
046: import java.util.List;
047:
048: import org.purl.sword.base.Namespaces;
049: import org.w3.atom.ContentType;
050: import org.w3.atom.Title;
051:
052: import nu.xom.Attribute;
053: import nu.xom.Elements;
054: import nu.xom.Element;
055:
056: /**
057: * A representation of a SWORD Collection.
058: *
059: * http://www.ukoln.ac.uk/repositories/digirep/index/SWORD_APP_Profile_0.5
060: *
061: * @author Stuart Lewis
062: * @author Neil Taylor
063: */
064: public class Collection extends XmlElement implements
065: SwordElementInterface {
066: /**
067: * The element name.
068: */
069: public static final String ELEMENT_NAME = "collection";
070:
071: /**
072: * Collection location, expressed as a URL.
073: */
074: private String location;
075:
076: /**
077: * Holds the ATOM Title for the collection.
078: */
079: private Title title;
080:
081: /**
082: * List of the APP:Accept elements.
083: */
084: private List<String> accepts;
085:
086: /**
087: * Holds the SWORD Collection policy.
088: */
089: private String collectionPolicy;
090:
091: /**
092: * The SWORD mediation value. Indicates if mediation is allowed.
093: */
094: private boolean mediation;
095:
096: /**
097: * Internal value to track if the mediation value has been
098: * set programmatically.
099: */
100: private boolean mediationSet;
101:
102: /**
103: * The SWORD treatment value.
104: */
105: private String treatment;
106:
107: /**
108: * The SWORD namespace.
109: */
110: private String namespace;
111:
112: /**
113: * The DC Terms Abstract details.
114: */
115: private String dcAbstract;
116:
117: /**
118: * Create a new instance.
119: */
120: public Collection() {
121: super (null);
122: accepts = new ArrayList<String>();
123: mediationSet = false;
124: }
125:
126: /**
127: * Create a new instance and set the initial location for the collection.
128: *
129: * @param location The initial location, expressed as a URL.
130: */
131: public Collection(String location) {
132: super (null);
133: this .location = location;
134: }
135:
136: /**
137: * Retrieve an array that holds all of the Accept details.
138: *
139: * @return An array of strings. Each string represents an
140: * individual accept element. The array will have a length
141: * of 0 if no accepts elements are stored in this collection.
142: */
143: public String[] getAccepts() {
144: String[] values = new String[this .accepts.size()];
145: return (String[]) accepts.toArray(values);
146: }
147:
148: /**
149: * Retrieve an array that holds all of the Accept details.
150: *
151: * @return An array of strings. Each string represents an
152: * individual accept element. The array will have a length
153: * of 0 if no accepts elements are stored in this collection.
154: */
155: public List<String> getAcceptsList() {
156: return accepts;
157: }
158:
159: /**
160: * Add an accepts entry.
161: *
162: * @param accepts The accepts value.
163: */
164: public void addAccepts(String accepts) {
165: this .accepts.add(accepts);
166: }
167:
168: /**
169: * Remove all of the accepts associated with this Collection.
170: */
171: public void clearAccepts() {
172: this .accepts.clear();
173: }
174:
175: /**
176: * Get the collection policy.
177: *
178: * @return The SWORD collectionPolicy.
179: */
180: public String getCollectionPolicy() {
181: return collectionPolicy;
182: }
183:
184: /**
185: * Set the collection policy.
186: *
187: * @param collectionPolicy The collection policy.
188: */
189: public void setCollectionPolicy(String collectionPolicy) {
190: this .collectionPolicy = collectionPolicy;
191: }
192:
193: /**
194: * Get the location.
195: *
196: * @return TShe location
197: */
198: public String getLocation() {
199: return location;
200: }
201:
202: /**
203: * Set the location.
204: *
205: * @param location The location.
206: */
207: public void setLocation(String location) {
208: this .location = location;
209: }
210:
211: /**
212: * Get the mediation value.
213: *
214: * @return The mediation
215: */
216: public boolean getMediation() {
217: return mediation;
218: }
219:
220: /**
221: * Set the mediation value.
222: *
223: * @param mediation The mediation value.
224: */
225: public void setMediation(boolean mediation) {
226: this .mediation = mediation;
227: mediationSet = true;
228: }
229:
230: /**
231: * See getFormatNamespace.
232: * @return the namespace
233: * @deprecated Use getFormatNamespace()
234: */
235: public String getNamespace() {
236: return getFormatNamespace();
237: }
238:
239: /**
240: * Get the format namespace.
241: *
242: * @return The format namespace.
243: */
244: public String getFormatNamespace() {
245: return namespace;
246: }
247:
248: /**
249: * See setFormatNamespace.
250: * @param namespace the namespace to set
251: * @deprecated Use setFormatNamespace
252: */
253: public void setNamespace(String namespace) {
254: setFormatNamespace(namespace);
255: }
256:
257: /**
258: * Set the format namespace.
259: *
260: * @param namespace The namespace.
261: */
262: public void setFormatNamespace(String namespace) {
263: this .namespace = namespace;
264: }
265:
266: /**
267: * Get the DC Term abstract.
268: *
269: * @return The abstract.
270: */
271: public String getAbstract() {
272: return dcAbstract;
273: }
274:
275: /**
276: * Set the abstract.
277: *
278: * @param abstractString The abstract.
279: */
280: public void setAbstract(String abstractString) {
281: this .dcAbstract = abstractString;
282: }
283:
284: /**
285: * Set the title. This will set the title type to ContentType.TEXT.
286: *
287: * @param title The title.
288: */
289: public void setTitle(String title) {
290: if (this .title == null) {
291: this .title = new Title();
292: }
293: this .title.setContent(title);
294: this .title.setType(ContentType.TEXT);
295: }
296:
297: /**
298: * Get the title.
299: *
300: * @return The title, or <code>null</code> if no title has been set.
301: */
302: public String getTitle() {
303: if (title == null) {
304: return null;
305: }
306: return title.getContent();
307: }
308:
309: /**
310: * Get the treatment value.
311: *
312: * @return The treatment.
313: */
314: public String getTreatment() {
315: return treatment;
316: }
317:
318: /**
319: * Set the treatment.
320: *
321: * @param treatment The treatment.
322: */
323: public void setTreatment(String treatment) {
324: this .treatment = treatment;
325: }
326:
327: /**
328: * Get a string representation of this object. This is
329: * equivalent to calling marshall().toString().
330: */
331: public String toString() {
332: Element element = marshall();
333: return element.toString();
334: }
335:
336: /**
337: * Marshall the data in this object to an Element object.
338: *
339: * @return A XOM Element that holds the data for this Content element.
340: */
341: public Element marshall() {
342: // convert data into XOM elements and return the 'root', i.e. the one
343: // that represents the collection.
344: Element collection = new Element(ELEMENT_NAME,
345: Namespaces.NS_APP);
346: Attribute href = new Attribute("href", location);
347: collection.addAttribute(href);
348:
349: //title = new Title();
350: collection.appendChild(title.marshall());
351:
352: Element acceptsElement = null;
353: for (String item : accepts) {
354: acceptsElement = new Element("accepts", Namespaces.NS_APP);
355: acceptsElement.appendChild(item);
356: collection.appendChild(acceptsElement);
357: }
358:
359: if (collectionPolicy != null) {
360: Element colPolicyElement = new Element(
361: "sword:collectionPolicy", Namespaces.NS_SWORD);
362: colPolicyElement.appendChild(collectionPolicy);
363: collection.appendChild(colPolicyElement);
364: }
365:
366: if (dcAbstract != null) {
367: Element dcAbstractElement = new Element("dcterms:abstract",
368: Namespaces.NS_DC_TERMS);
369: dcAbstractElement.appendChild(dcAbstract);
370: collection.appendChild(dcAbstractElement);
371: }
372:
373: if (mediationSet) {
374: Element mediationElement = new Element("sword:mediation",
375: Namespaces.NS_SWORD);
376: mediationElement.appendChild(Boolean.toString(mediation));
377: collection.appendChild(mediationElement);
378: }
379:
380: // treatment
381: if (treatment != null) {
382: Element treatmentElement = new Element("sword:treatment",
383: Namespaces.NS_SWORD);
384: treatmentElement.appendChild(treatment);
385: collection.appendChild(treatmentElement);
386: }
387:
388: // namespace
389: if (namespace != null) {
390: Element namespaceElement = new Element("sword:namespace",
391: Namespaces.NS_SWORD);
392: namespaceElement.appendChild(namespace);
393: collection.appendChild(namespaceElement);
394: }
395:
396: return collection;
397: }
398:
399: /**
400: * Unmarshall the content element into the data in this object.
401: *
402: * @throws UnmarshallException If the element does not contain a
403: * content element or if there are problems
404: * accessing the data.
405: */
406: public void unmarshall(Element collection)
407: throws UnmarshallException {
408: if (!isInstanceOf(collection, "collection", Namespaces.NS_APP)) {
409: throw new UnmarshallException(
410: "Not an app:collection element");
411: }
412:
413: try {
414: // retrieve the attributes
415: int count = collection.getAttributeCount();
416: Attribute a = null;
417: for (int i = 0; i < count; i++) {
418: a = collection.getAttribute(i);
419: if ("href".equals(a.getQualifiedName())) {
420: location = a.getValue();
421: }
422: }
423:
424: accepts.clear();
425:
426: // retrieve all of the sub-elements
427: Elements elements = collection.getChildElements();
428: Element element = null;
429: int length = elements.size();
430:
431: for (int i = 0; i < length; i++) {
432: element = elements.get(i);
433: // FIXME - atom assumes that it has been defined. not correct.
434: if (isInstanceOf(element, "title", Namespaces.NS_ATOM)) {
435: title = new Title();
436: title.unmarshall(element);
437: } else if (isInstanceOf(element, "accepts",
438: Namespaces.NS_APP)) {
439: accepts.add(unmarshallString(element));
440: } else if (isInstanceOf(element, "collectionPolicy",
441: Namespaces.NS_SWORD)) {
442: collectionPolicy = unmarshallString(element);
443: } else if (isInstanceOf(element, "abstract",
444: Namespaces.NS_DC_TERMS)) {
445: dcAbstract = unmarshallString(element);
446: } else if (isInstanceOf(element, "mediation",
447: Namespaces.NS_SWORD)) {
448: setMediation(unmarshallBoolean(element));
449: } else if (isInstanceOf(element, "treatment",
450: Namespaces.NS_SWORD)) {
451: treatment = unmarshallString(element);
452: } else if (isInstanceOf(element, "namespace",
453: Namespaces.NS_SWORD)) {
454: namespace = unmarshallString(element);
455: }
456: }
457: } catch (Exception ex) {
458: InfoLogger.getLogger().writeError(
459: "Unable to parse an element in Collection: "
460: + ex.getMessage());
461: throw new UnmarshallException(
462: "Unable to parse an element in Collection", ex);
463: }
464:
465: }
466: }
|