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.w3.atom;
037:
038: import nu.xom.Element;
039: import nu.xom.Elements;
040:
041: import org.purl.sword.base.InfoLogger;
042: import org.purl.sword.base.Namespaces;
043: import org.purl.sword.base.SwordElementInterface;
044: import org.purl.sword.base.UnmarshallException;
045: import org.purl.sword.base.XmlElement;
046:
047: /**
048: * Represents an Author type, as used in ATOM. This class is used as the
049: * base class for the different areas of ATOM that represent information
050: * about people. This includes the atom:author and atom:contributor
051: * elements.
052: *
053: * @author Neil Taylor
054: */
055: public class Author extends XmlElement implements SwordElementInterface {
056: /**
057: * The author's name.
058: */
059: private String name;
060:
061: /**
062: * The author's URI.
063: */
064: private String uri;
065:
066: /**
067: * The author's email.
068: */
069: private String email;
070:
071: /**
072: * Create a new instance and set the prefix to
073: * 'atom' and the local name to 'author'.
074: */
075: public Author() {
076: this ("atom", "author");
077: }
078:
079: /**
080: * Create a new instance and set the element name.
081: *
082: * @param prefix The prefix to use when marshalling the data.
083: * @param localName The localName to use when marshalling the data.
084: */
085: public Author(String prefix, String localName) {
086: super (prefix, localName);
087: }
088:
089: /**
090: * Marshall the data in this object to a XOM Element. The element
091: * will have the full name that is specified in the constructor.
092: *
093: * @return A XOM Element.
094: */
095: public Element marshall() {
096: Element element = new Element(getQualifiedName(),
097: Namespaces.NS_ATOM);
098:
099: if (name != null) {
100: Element nameElement = new Element(getQualifiedName("name"),
101: Namespaces.NS_ATOM);
102: nameElement.appendChild(name);
103: element.appendChild(nameElement);
104: }
105:
106: if (uri != null) {
107: Element uriElement = new Element(getQualifiedName("uri"),
108: Namespaces.NS_ATOM);
109: uriElement.appendChild(uri);
110: element.appendChild(uriElement);
111: }
112:
113: if (email != null) {
114: Element emailElement = new Element(
115: getQualifiedName("email"), Namespaces.NS_ATOM);
116: emailElement.appendChild(email);
117: element.appendChild(emailElement);
118: }
119:
120: return element;
121: }
122:
123: /**
124: * Unmarshall the author details from the specified element. The element
125: * is a XOM element.
126: *
127: */
128: public void unmarshall(Element author) throws UnmarshallException {
129: if (!isInstanceOf(author, localName, Namespaces.NS_ATOM)) {
130: throw new UnmarshallException(
131: "Element is not of the correct type");
132: }
133:
134: try {
135: // retrieve all of the sub-elements
136: Elements elements = author.getChildElements();
137: Element element = null;
138: int length = elements.size();
139:
140: for (int i = 0; i < length; i++) {
141: element = elements.get(i);
142:
143: if (isInstanceOf(element, "name", Namespaces.NS_ATOM)) {
144: name = unmarshallString(element);
145: }
146: if (isInstanceOf(element, "uri", Namespaces.NS_ATOM)) {
147: uri = unmarshallString(element);
148: }
149: if (isInstanceOf(element, "email", Namespaces.NS_ATOM)) {
150: email = unmarshallString(element);
151: } else {
152: // unknown element type
153: //counter.other++;
154: }
155: } // for
156: } catch (UnmarshallException ex) {
157: InfoLogger.getLogger().writeError(
158: "Unable to parse an element in "
159: + getQualifiedName() + ": "
160: + ex.getMessage());
161: throw ex;
162: }
163:
164: }
165:
166: /**
167: * Retrieve the author name.
168: *
169: * @return The name.
170: */
171: public String getName() {
172: return name;
173: }
174:
175: /**
176: * Set the author name.
177: *
178: * @param name The name.
179: */
180: public void setName(String name) {
181: this .name = name;
182: }
183:
184: /**
185: * Get the author URI.
186: *
187: * @return The URI.
188: */
189: public String getUri() {
190: return uri;
191: }
192:
193: /**
194: * Set the author URI.
195: *
196: * @param uri the URI.
197: */
198: public void setUri(String uri) {
199: this .uri = uri;
200: }
201:
202: /**
203: * Get the author email.
204: *
205: * @return The email.
206: */
207: public String getEmail() {
208: return email;
209: }
210:
211: /**
212: * Set the author email.
213: *
214: * @param email The email.
215: */
216: public void setEmail(String email) {
217: this.email = email;
218: }
219:
220: }
|