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:55 $
041: * Revision : $Revision: 1.2 $
042: * Name : $Name: $
043: */
044:
045: import java.io.ByteArrayOutputStream;
046: import java.io.IOException;
047: import java.util.Iterator;
048:
049: import nu.xom.Builder;
050: import nu.xom.Document;
051: import nu.xom.Element;
052: import nu.xom.ParsingException;
053: import nu.xom.Serializer;
054:
055: /**
056: * A representation of a SWORD Service Document.
057: *
058: * http://www.ukoln.ac.uk/repositories/digirep/index/SWORD_APP_Profile_0.5
059: *
060: * @author Stuart Lewis
061: * @author Neil Taylor
062: */
063: public class ServiceDocument {
064: /**
065: * The Service object that is held by this object.
066: */
067: private Service service;
068:
069: /**
070: * Create a new instance and set the initial service level to Zero.
071: */
072: public ServiceDocument() {
073: this (ServiceLevel.ZERO);
074: }
075:
076: /**
077: * Create a new instance and set the specified service level.
078: *
079: * @param complianceLevel The service compliance level.
080: */
081: public ServiceDocument(ServiceLevel complianceLevel) {
082: service = new Service(complianceLevel);
083: }
084:
085: /**
086: * Create a new instance and store the specified Service document.
087: *
088: * @param service The Service object.
089: */
090: public ServiceDocument(Service service) {
091: this .service = service;
092: }
093:
094: /**
095: * Instantiate a ServiceDocument
096: *
097: * @param complianceLevel The compliance level of this implementation
098: * @param noOp Whether or not the NOOP option is available
099: * @param verbose Whether or not the verbose option is available
100: * @param workspaceTitle The name of the workspace title
101: * @param workspaceCollections A Collection of workspaces
102: *
103: * @deprecated Please use the other constructors.
104: */
105: public ServiceDocument(ServiceLevel complianceLevel, boolean noOp,
106: boolean verbose, String workspaceTitle,
107: Collection workspaceCollections) {
108:
109: service = new Service(complianceLevel, noOp, verbose);
110: Workspace workspace = new Workspace(workspaceTitle);
111: workspace.addCollection(workspaceCollections); // FIXME - not quite right?
112: service.addWorkspace(workspace);
113: }
114:
115: /**
116: * Get the compliance level from this Service Document
117: *
118: * @return The compliance level
119: *
120: * @deprecated Please access the compliance level directly from the service.
121: */
122: public ServiceLevel getComplianceLevel() {
123: // Return the compliance level
124: return service.getComplianceLevel();
125: }
126:
127: /**
128: * Returns a boolean depending on whether or not the Service Document
129: * says the server supports the NOOP option
130: *
131: * @return The NOOP option status
132: *
133: * @deprecated Please access the value directly from the service.
134: */
135: public boolean supportsNoOp() {
136: // Return the NOOP option status
137: return service.isNoOp();
138: }
139:
140: /**
141: *
142: * @param noOp
143: *
144: * @deprecated Please access the value directly from the service.
145: */
146: public void setNoOp(boolean noOp) {
147: service.setNoOp(noOp);
148: }
149:
150: /**
151: * Returns a boolean depending on whether or not the Service Document
152: * says the server supports the verbose option
153: *
154: * @return The verbose option status
155: *
156: * @deprecated Please access the value directly from the service.
157: */
158: public boolean supportsVerbose() {
159: // Return the verbose option status
160: return service.isVerbose();
161: }
162:
163: /**
164: *
165: * @param verbose
166: *
167: * @deprecated Please access the value directly from the service.
168: */
169: public void setVerbose(boolean verbose) {
170: service.setVerbose(verbose);
171: }
172:
173: /**
174: * Returns the Collectinos in the workspace described by the Service Document
175: *
176: * @return The workspaces
177: *
178: * @deprecated Please access the value directly from the service.
179: */
180: public Iterator<Collection> getWorkspaceCollections() {
181: // Return the collections
182: return null; // FIXME service.getWorkspaces().collectionIterator();
183: }
184:
185: /**
186: * @deprecated Please access the value directly from the service.
187: */
188: public Iterator<Workspace> getWorkspaces() {
189: return service.getWorkspaces();
190: }
191:
192: /**
193: *
194: * @param workspace
195: *
196: * @deprecated Please access the value directly from the service.
197: */
198: public void addWorkspace(Workspace workspace) {
199: service.addWorkspace(workspace);
200: }
201:
202: /**
203: * Set the service object associated with this document.
204: *
205: * @param service The new Service object.
206: */
207: public void setService(Service service) {
208: this .service = service;
209: }
210:
211: /**
212: * Retrieve the Service object associated with this document.
213: *
214: * @return The Service object.
215: */
216: public Service getService() {
217: return service;
218: }
219:
220: /**
221: * Return the Service Document in it's XML form.
222: *
223: * @return The ServiceDocument
224: */
225: public String toString() {
226: return marshall();
227: }
228:
229: /**
230: * Marshall the data in the Service element and generate a String representation.
231: * The returned string is UTF-8 format.
232: *
233: * @return A string of XML, or <code>null</code> if there was an error
234: * marshalling the data.
235: */
236: public String marshall() {
237: try {
238: ByteArrayOutputStream stream = new ByteArrayOutputStream();
239: Serializer serializer = new Serializer(stream, "UTF-8");
240: serializer.setIndent(3);
241: serializer.setMaxLength(64);
242:
243: Document doc = new Document(service.marshall());
244: serializer.write(doc);
245:
246: return stream.toString();
247: } catch (IOException ex) {
248: System.err.println(ex);
249: }
250:
251: return null;
252: }
253:
254: /**
255: * Convert the specified XML string into a set of objects
256: * used within the service. A new Service object will be
257: * created and stored. This will dispose of any previous
258: * Service object associated with this object.
259: *
260: * @param xml The XML string.
261: * @throws UnmarshallException If there was a problem unmarshalling the
262: * data. This might be as a result of an
263: * error in parsing the XML string,
264: * extracting information.
265: */
266: public void unmarshall(String xml) throws UnmarshallException {
267: //
268: try {
269: Builder builder = new Builder();
270: Document doc = builder.build(xml,
271: "http://something.com/here");
272: Element root = doc.getRootElement();
273:
274: service = new Service();
275: service.unmarshall(root);
276:
277: } catch (ParsingException ex) {
278: throw new UnmarshallException("Unable to parse the XML", ex);
279: } catch (IOException ex) {
280: throw new UnmarshallException("Error acessing the file?",
281: ex);
282:
283: }
284: }
285: }
|