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: import java.util.Iterator;
048:
049: import org.purl.sword.base.Namespaces;
050:
051: import nu.xom.Element;
052: import nu.xom.Elements;
053:
054: /**
055: * Represents an Atom Publishing Protocol Service element, with
056: * SWORD extensions.
057: *
058: * @author Neil Taylor
059: */
060: public class Service extends XmlElement implements
061: SwordElementInterface {
062: /**
063: * The local name for the element.
064: */
065: private static final String ELEMENT_NAME = "service";
066:
067: /**
068: * The service compliance level.
069: */
070: private ServiceLevel complianceLevel;
071:
072: /**
073: * The noOp value.
074: */
075: private boolean noOp;
076:
077: /**
078: * Flag to record if the noOp value has been set by a user of the class.
079: */
080: private boolean isNoOp;
081:
082: /**
083: * The verbose value.
084: */
085: private boolean verbose;
086:
087: /**
088: * Flag to record if the verbose value has been set by a user of the class.
089: */
090: private boolean isVerbose;
091:
092: /**
093: * List of Workspaces.
094: */
095: private List<Workspace> workspaces;
096:
097: /**
098: * Create a new instance.
099: */
100: public Service() {
101: super ("app");
102:
103: isVerbose = false;
104: isNoOp = false;
105: workspaces = new ArrayList<Workspace>();
106: complianceLevel = ServiceLevel.UNDEFINED;
107: }
108:
109: /**
110: * Create a new instance.
111: *
112: * @param complianceLevel The service compliance level.
113: */
114: public Service(ServiceLevel complianceLevel) {
115: this ();
116: this .complianceLevel = complianceLevel;
117: }
118:
119: /**
120: * Create a new instance with the specified compliance level, noOp and
121: * verbose values.
122: *
123: * @param complianceLevel The service compliance level.
124: * @param noOp The noOp.
125: * @param verbose The verbose element.
126: */
127: public Service(ServiceLevel complianceLevel, boolean noOp,
128: boolean verbose) {
129: this ();
130: this .complianceLevel = complianceLevel;
131: setNoOp(noOp);
132: setVerbose(verbose);
133: }
134:
135: /**
136: * Get the service compliance level.
137: *
138: * @return The compliance level.
139: */
140: public ServiceLevel getComplianceLevel() {
141: return complianceLevel;
142: }
143:
144: /**
145: * Set the service compliance level.
146: *
147: * @param The compliance level.
148: */
149: public void setComplianceLevel(ServiceLevel complianceLevel) {
150: this .complianceLevel = complianceLevel;
151: }
152:
153: /**
154: * Get the NoOp value.
155: *
156: * @return The value.
157: */
158: public boolean isNoOp() {
159: return noOp;
160: }
161:
162: /**
163: * Set the NoOp value.
164: *
165: * @param noOp The value.
166: */
167: public void setNoOp(boolean noOp) {
168: this .noOp = noOp;
169: isNoOp = true;
170: }
171:
172: /**
173: * Determine if the NoOp value has been set. This should be called to
174: * check if an item has been programatically set and does not have a
175: * default value.
176: *
177: * @return True if it has been set programmatically. Otherwise, false.
178: */
179: public boolean isNoOpSet() {
180: return isNoOp;
181: }
182:
183: /**
184: * Get the Verbose setting.
185: *
186: * @return The value.
187: */
188: public boolean isVerbose() {
189: return verbose;
190: }
191:
192: /**
193: * Set the Verbose value.
194: *
195: * @param verbose The value.
196: */
197: public void setVerbose(boolean verbose) {
198: this .verbose = verbose;
199: isVerbose = true;
200: }
201:
202: /**
203: * Determine if the Verbose value has been set. This should be called to
204: * check if an item has been programatically set and does not have a
205: * default value.
206: *
207: * @return True if it has been set programmatically. Otherwise, false.
208: */
209: public boolean isVerboseSet() {
210: return isVerbose;
211: }
212:
213: /**
214: * Get an Iterator over the workspaces.
215: *
216: * @return The workspace.
217: */
218: public Iterator<Workspace> getWorkspaces() {
219: return workspaces.iterator();
220: }
221:
222: /**
223: * Get a List of workspaces
224: *
225: * @return The workspaces in a List
226: */
227: public List<Workspace> getWorkspacesList() {
228: return workspaces;
229: }
230:
231: /**
232: * Add a workspace.
233: *
234: * @param workspace The workspace.
235: */
236: public void addWorkspace(Workspace workspace) {
237: this .workspaces.add(workspace);
238: }
239:
240: /**
241: * Clear the list of workspaces.
242: */
243: public void clearWorkspaces() {
244: this .workspaces.clear();
245: }
246:
247: /**
248: * Marshall the data in this object to an Element object.
249: *
250: * @return A XOM Element that holds the data for this Content element.
251: */
252: public Element marshall() {
253: Element service = new Element(ELEMENT_NAME, Namespaces.NS_APP);
254: service.addNamespaceDeclaration("atom", Namespaces.NS_ATOM);
255: service.addNamespaceDeclaration("dcterms",
256: Namespaces.NS_DC_TERMS);
257: service.addNamespaceDeclaration("sword", Namespaces.NS_SWORD);
258:
259: if (complianceLevel != ServiceLevel.UNDEFINED) {
260: //System.out.println("The compliance level is: " + complianceLevel );
261: Element compliance = new Element("sword:level",
262: Namespaces.NS_SWORD);
263: compliance.appendChild(Integer.toString(complianceLevel
264: .number()));
265: service.appendChild(compliance);
266: }
267:
268: if (isVerboseSet()) {
269: Element verboseElement = new Element("sword:verbose",
270: Namespaces.NS_SWORD);
271: verboseElement.appendChild(Boolean.toString(verbose));
272: service.appendChild(verboseElement);
273: }
274:
275: if (isNoOpSet()) {
276: Element noOpElement = new Element("sword:noOp",
277: Namespaces.NS_SWORD);
278: noOpElement.appendChild(Boolean.toString(noOp));
279: service.appendChild(noOpElement);
280: }
281:
282: for (Workspace item : workspaces) {
283: service.appendChild(item.marshall());
284: }
285:
286: return service;
287: }
288:
289: /**
290: * Get a service level that corresponds to the specified value. Used
291: * during the unmarshall process.
292: *
293: * @param level The integer version of a service level.
294: *
295: * @return If the parameter matches one of the defined levels,
296: * a ServiceLevel of ONE or ZERO is returned. Otherwise,
297: * ServiceLevel.UNDEFINED is returned.
298: */
299: private ServiceLevel getServiceLevel(int level) {
300: ServiceLevel theLevel = ServiceLevel.UNDEFINED;
301:
302: switch (level) {
303: case 0:
304: theLevel = ServiceLevel.ZERO;
305: break;
306:
307: case 1:
308: theLevel = ServiceLevel.ONE;
309: break;
310:
311: default:
312: // FIXME - should this default to ZERO instead?
313: theLevel = ServiceLevel.UNDEFINED;
314: InfoLogger.getLogger().writeError(
315: "Invalid value for sword:level");
316: break;
317: }
318:
319: return theLevel;
320:
321: }
322:
323: /**
324: * Unmarshall the content element into the data in this object.
325: *
326: * @throws UnmarshallException If the element does not contain a
327: * content element or if there are problems
328: * accessing the data.
329: */
330: public void unmarshall(Element service) throws UnmarshallException {
331: if (!isInstanceOf(service, "service", Namespaces.NS_APP)) {
332: throw new UnmarshallException("Not an app:service element");
333: }
334:
335: try {
336: workspaces.clear();
337:
338: // retrieve all of the sub-elements
339: Elements elements = service.getChildElements();
340: Element element = null;
341: int length = elements.size();
342:
343: for (int i = 0; i < length; i++) {
344: element = elements.get(i);
345:
346: if (isInstanceOf(element, "level", Namespaces.NS_SWORD)) {
347: int level = unmarshallInteger(element);
348: complianceLevel = getServiceLevel(level);
349: } else if (isInstanceOf(element, "verbose",
350: Namespaces.NS_SWORD)) {
351: setVerbose(unmarshallBoolean(element));
352: } else if (isInstanceOf(element, "noOp",
353: Namespaces.NS_SWORD)) {
354: setNoOp(unmarshallBoolean(element));
355: } else if (isInstanceOf(element, "workspace",
356: Namespaces.NS_APP)) {
357: Workspace workspace = new Workspace();
358: workspace.unmarshall(element);
359: workspaces.add(workspace);
360: }
361: }
362: } catch (Exception ex) {
363: InfoLogger.getLogger().writeError(
364: "Unable to parse an element in Service: "
365: + ex.getMessage());
366: throw new UnmarshallException(
367: "Unable to parse element in Service", ex);
368: }
369: }
370: }
|