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 ATOM Generator element.
049: *
050: * @author Neil Taylor
051: */
052: public class Source extends XmlElement implements SwordElementInterface {
053: /**
054: * The generator data for this object.
055: */
056: private Generator generator;
057:
058: /**
059: * Create a new instance and set the prefix to
060: * 'atom' and the local name to 'source'.
061: */
062: public Source() {
063: super ("atom", "source");
064: }
065:
066: /**
067: * Marshall the data stored in this object into Element objects.
068: *
069: * @return An element that holds the data associated with this object.
070: */
071: public Element marshall() {
072: Element source = new Element(getQualifiedName(),
073: Namespaces.NS_ATOM);
074:
075: if (generator != null) {
076: source.appendChild(generator.marshall());
077: }
078:
079: return source;
080: }
081:
082: /**
083: * Unmarshall the contents of the source element into the internal data objects
084: * in this object.
085: *
086: * @param source The Source element to process.
087: *
088: * @throws UnmarshallException If the element does not contain an ATOM Source
089: * element, or if there is a problem processing the element or any
090: * sub-elements.
091: */
092: public void unmarshall(Element source) throws UnmarshallException {
093: if (!isInstanceOf(source, localName, Namespaces.NS_ATOM)) {
094: throw new UnmarshallException("Not an atom:source element");
095: }
096:
097: try {
098: // retrieve all of the sub-elements
099: Elements elements = source.getChildElements();
100: Element element = null;
101: int length = elements.size();
102:
103: for (int i = 0; i < length; i++) {
104: element = elements.get(i);
105: if (isInstanceOf(element, "generator",
106: Namespaces.NS_ATOM)) {
107: generator = new Generator();
108: generator.unmarshall(element);
109: }
110: }
111: } catch (Exception ex) {
112: InfoLogger.getLogger().writeError(
113: "Unable to parse an element in Source: "
114: + ex.getMessage());
115: ex.printStackTrace();
116: throw new UnmarshallException(
117: "Unable to parse an element in Source", ex);
118: }
119: }
120:
121: /**
122: * Get the generator.
123: *
124: * @return The generator.
125: */
126: public Generator getGenerator() {
127: return generator;
128: }
129:
130: /**
131: * Set the generator.
132: *
133: * @param generator The generator.
134: */
135: public void setGenerator(Generator generator) {
136: this.generator = generator;
137: }
138: }
|