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.Attribute;
039: import nu.xom.Element;
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 ATOM Generator element.
049: *
050: * @author Neil Taylor
051: */
052: public class Generator extends XmlElement implements
053: SwordElementInterface {
054: /**
055: * The content for the element.
056: */
057: private String content;
058:
059: /**
060: * The URI attribute.
061: */
062: private String uri;
063:
064: /**
065: * The version attribute.
066: */
067: private String version;
068:
069: /**
070: * Create a new instance and set the prefix to
071: * 'atom' and the local name to 'generator'.
072: */
073: public Generator() {
074: super ("atom", "generator");
075: }
076:
077: /**
078: * Marshall the data in the object to an Element object.
079: *
080: * @return The element.
081: */
082: public Element marshall() {
083: Element element = new Element(getQualifiedName(),
084: Namespaces.NS_ATOM);
085:
086: if (content != null) {
087: element.appendChild(content);
088: }
089:
090: if (uri != null) {
091: Attribute uriAttribute = new Attribute("uri", uri);
092: element.addAttribute(uriAttribute);
093: }
094:
095: if (version != null) {
096: Attribute versionAttribute = new Attribute("version",
097: version);
098: element.addAttribute(versionAttribute);
099: }
100:
101: return element;
102: }
103:
104: /**
105: * Unmarshall the specified Generator element into the data in this object.
106: *
107: * @param generator The generator element.
108: *
109: * @throws UnmarshallException If the specified element is not an atom:generator
110: * element, or if there is an error accessing the data.
111: */
112: public void unmarshall(Element generator)
113: throws UnmarshallException {
114: if (!isInstanceOf(generator, localName, Namespaces.NS_ATOM)) {
115: throw new UnmarshallException(
116: "Not an atom:generator element");
117: }
118:
119: try {
120: // get the attributes
121: int attributeCount = generator.getAttributeCount();
122: Attribute attribute = null;
123: for (int i = 0; i < attributeCount; i++) {
124: attribute = generator.getAttribute(i);
125: if ("uri".equals(attribute.getQualifiedName())) {
126: uri = attribute.getValue();
127: } else if ("version".equals(attribute
128: .getQualifiedName())) {
129: version = attribute.getValue();
130: }
131: }
132:
133: int length = generator.getChildCount();
134: if (length > 0) {
135: content = unmarshallString(generator);
136: }
137: } catch (Exception ex) {
138: InfoLogger.getLogger().writeError(
139: "Unable to parse an element in Generator: "
140: + ex.getMessage());
141: throw new UnmarshallException(
142: "Unable to parse element in Generator", ex);
143: }
144: }
145:
146: /**
147: * Get the content.
148: *
149: * @return The content.
150: */
151: public String getContent() {
152: return content;
153: }
154:
155: /**
156: * Set the content.
157: *
158: * @param content The content.
159: */
160: public void setContent(String content) {
161: this .content = content;
162: }
163:
164: /**
165: * Get the URI.
166: *
167: * @return The URI.
168: */
169: public String getUri() {
170: return uri;
171: }
172:
173: /**
174: * Set the URI.
175: *
176: * @param uri The URI.
177: */
178: public void setUri(String uri) {
179: this .uri = uri;
180: }
181:
182: /**
183: * Get the version.
184: *
185: * @return The version.
186: */
187: public String getVersion() {
188: return version;
189: }
190:
191: /**
192: * Set the version.
193: *
194: * @param version The version.
195: */
196: public void setVersion(String version) {
197: this.version = version;
198: }
199:
200: }
|