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.3 $
042: * Name : $Name: $
043: */
044:
045: import java.io.ByteArrayOutputStream;
046: import java.io.IOException;
047:
048: import javax.servlet.http.HttpServletResponse;
049:
050: import nu.xom.Builder;
051: import nu.xom.Document;
052: import nu.xom.Element;
053: import nu.xom.ParsingException;
054: import nu.xom.Serializer;
055:
056: /**
057: *
058: * @author Stuart Lewis
059: * @author Neil Taylor
060: *
061: */
062: public class DepositResponse {
063: private SWORDEntry entry;
064:
065: private int httpResponse;
066:
067: public DepositResponse(int httpResponse) {
068: entry = new SWORDEntry();
069: this .httpResponse = httpResponse;
070: }
071:
072: /**
073: *
074: * @param entry
075: */
076: public void setEntry(SWORDEntry entry) {
077: this .entry = entry;
078: }
079:
080: /**
081: *
082: * @param entry
083: */
084: public SWORDEntry getEntry() {
085: return entry;
086: }
087:
088: public int getHttpResponse() {
089: return httpResponse;
090: }
091:
092: public void setHttpResponse(int httpResponse) {
093: this .httpResponse = httpResponse;
094: }
095:
096: /**
097: *
098: * @return
099: */
100: public String marshall() {
101: try {
102: ByteArrayOutputStream stream = new ByteArrayOutputStream();
103: Serializer serializer = new Serializer(stream, "UTF-8");
104: serializer.setIndent(3);
105: serializer.setMaxLength(64);
106:
107: if (entry != null) {
108: Document doc = new Document(entry.marshall());
109: serializer.write(doc);
110: System.out.println(stream.toString());
111: return stream.toString();
112: }
113: } catch (IOException ex) {
114: System.err.println(ex);
115: }
116:
117: return null; // default return value.
118: }
119:
120: /**
121: *
122: * @param xml
123: * @throws UnmarshallException
124: */
125: public void unmarshall(String xml) throws UnmarshallException {
126: try {
127: Builder builder = new Builder();
128: Document doc = builder.build(xml,
129: "http://something.com/here");
130: Element root = doc.getRootElement();
131:
132: entry = new SWORDEntry();
133: entry.unmarshall(root);
134:
135: } catch (ParsingException ex) {
136: throw new UnmarshallException("Unable to parse the XML", ex);
137: } catch (IOException ex) {
138: throw new UnmarshallException("Error acessing the file?",
139: ex);
140: }
141: }
142:
143: public String toString() {
144: return marshall();
145: }
146:
147: }
|