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 Content element.
049: *
050: * @author Neil Taylor
051: *
052: */
053: public class Content extends XmlElement implements
054: SwordElementInterface {
055: /**
056: * The identifier for the src attribute.
057: */
058: public static final String ATTR_SRC = "src";
059:
060: /**
061: * The identifier for the type attribute.
062: */
063: public static final String ATTR_TYPE = "type";
064:
065: /**
066: * The data for the type attribute.
067: */
068: private String type;
069:
070: /**
071: * The data for the source attribute.
072: */
073: private String source;
074:
075: /**
076: * Create a new instance and set the prefix to
077: * 'atom' and the local name to 'content'.
078: */
079: public Content() {
080: super ("atom", "content");
081: }
082:
083: /**
084: * Get the Source.
085: *
086: * @return The Source.
087: */
088: public String getSource() {
089: return source;
090: }
091:
092: /**
093: * Set the Source.
094: *
095: * @param source The source.
096: */
097: public void setSource(String source) {
098: this .source = source;
099: }
100:
101: /**
102: * Get the type.
103: *
104: * @return The type.
105: */
106: public String getType() {
107: return type;
108: }
109:
110: /**
111: * Set the type for the content. This should match the pattern
112: * ".* /.*" [Note, there is no space before the /, this has been added
113: * to allow this text to be written in a Java comment.].
114: *
115: * An example of the type is <code>application/zip</code>.
116: *
117: * @param type The specified type.
118: * @throws InvalidMediaTypeException If the specified type is null or
119: * it does not match the specified pattern.
120: */
121: public void setType(String type) throws InvalidMediaTypeException {
122: if (type == null || !type.matches(".*/.*")) {
123: throw new InvalidMediaTypeException("Type: '" + type
124: + "' does not match .*/.*");
125: }
126:
127: this .type = type;
128: }
129:
130: /**
131: * Marshall the data in this object to an Element object.
132: *
133: * @return A XOM Element that holds the data for this Content element.
134: */
135: public Element marshall() {
136: Element content = new Element(getQualifiedName(),
137: Namespaces.NS_ATOM);
138:
139: if (type != null) {
140: Attribute typeAttribute = new Attribute(ATTR_TYPE, type);
141: content.addAttribute(typeAttribute);
142: }
143:
144: if (source != null) {
145: Attribute typeAttribute = new Attribute(ATTR_SRC, source);
146: content.addAttribute(typeAttribute);
147: }
148:
149: return content;
150: }
151:
152: /**
153: * Unmarshall the content element into the data in this object.
154: *
155: * @throws UnmarshallException If the element does not contain a
156: * content element or if there are problems
157: * accessing the data.
158: */
159: public void unmarshall(Element content) throws UnmarshallException {
160: if (!isInstanceOf(content, localName, Namespaces.NS_ATOM)) {
161: throw new UnmarshallException(
162: "Element is not of the correct type");
163: }
164:
165: try {
166: // get the attributes
167: int attributeCount = content.getAttributeCount();
168: Attribute attribute = null;
169: for (int i = 0; i < attributeCount; i++) {
170: attribute = content.getAttribute(i);
171: String name = attribute.getQualifiedName();
172: if (ATTR_TYPE.equals(name)) {
173: type = attribute.getValue();
174: }
175:
176: if (ATTR_SRC.equals(name)) {
177: source = attribute.getValue();
178: }
179: }
180: } catch (Exception ex) {
181: InfoLogger.getLogger().writeError(
182: "Unable to parse an element in Content: "
183: + ex.getMessage());
184: throw new UnmarshallException("Error parsing Content", ex);
185: }
186: }
187:
188: }
|