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: import nu.xom.Elements;
041:
042: import org.purl.sword.base.InfoLogger;
043: import org.purl.sword.base.Namespaces;
044: import org.purl.sword.base.SwordElementInterface;
045: import org.purl.sword.base.UnmarshallException;
046: import org.purl.sword.base.XmlElement;
047:
048: /**
049: * Represents an ATOM Link element.
050: *
051: * @author Neil Taylor
052: */
053: public class Link extends XmlElement implements SwordElementInterface {
054: /**
055: * Stores the href.
056: */
057: private String href;
058:
059: /**
060: * Stores the Rel attribute.
061: */
062: private String rel;
063:
064: /**
065: * Stores the type.
066: */
067: private String type;
068:
069: /**
070: * Stores the HREF lang.
071: */
072: private String hreflang;
073:
074: /**
075: * Stores the title.
076: */
077: private String title;
078:
079: /**
080: * Stores the length.
081: */
082: private String length;
083:
084: /**
085: * Stores the content.
086: */
087: private String content;
088:
089: /**
090: * Create a new instance and set prefix and local name to 'atom' and 'link',
091: * respectively.
092: */
093: public Link() {
094: super ("atom", "link");
095: }
096:
097: /**
098: * Mashall the data stored in this object into Element objects.
099: *
100: * @return An element that holds the data associated with this object.
101: */
102: public Element marshall() {
103: Element element = new Element(getQualifiedName(),
104: Namespaces.NS_ATOM);
105:
106: if (content != null) {
107: element.appendChild(content);
108: }
109:
110: if (href != null) {
111: Attribute hrefAttribute = new Attribute("href", href);
112: element.addAttribute(hrefAttribute);
113: }
114:
115: if (rel != null) {
116: Attribute relAttribute = new Attribute("rel", rel);
117: element.addAttribute(relAttribute);
118: }
119:
120: if (type != null) {
121: Attribute typeAttribute = new Attribute("type", type);
122: element.addAttribute(typeAttribute);
123: }
124:
125: if (hreflang != null) {
126: Attribute hreflangAttribute = new Attribute("hreflang",
127: hreflang);
128: element.addAttribute(hreflangAttribute);
129: }
130:
131: if (title != null) {
132: Attribute titleAttribute = new Attribute("title", title);
133: element.addAttribute(titleAttribute);
134: }
135:
136: if (length != null) {
137: Attribute lengthAttribute = new Attribute("length", length);
138: element.addAttribute(lengthAttribute);
139: }
140:
141: return element;
142: }
143:
144: /**
145: * Unmarshall the contents of the Link element into the internal data objects
146: * in this object.
147: *
148: * @param link The Link element to process.
149: *
150: * @throws UnmarshallException If the element does not contain an ATOM link
151: * element, or if there is a problem processing the element or any
152: * subelements.
153: */
154: public void unmarshall(Element link) throws UnmarshallException {
155: if (!isInstanceOf(link, localName, Namespaces.NS_ATOM)) {
156: throw new UnmarshallException("Not an atom:link element");
157: }
158:
159: try {
160: // get the attributes
161: int attributeCount = link.getAttributeCount();
162: Attribute attribute = null;
163: for (int i = 0; i < attributeCount; i++) {
164: attribute = link.getAttribute(i);
165: if ("href".equals(attribute.getQualifiedName())) {
166: href = attribute.getValue();
167: } else if ("rel".equals(attribute.getQualifiedName())) {
168: rel = attribute.getValue();
169: } else if ("type".equals(attribute.getQualifiedName())) {
170: type = attribute.getValue();
171: } else if ("hreflang".equals(attribute
172: .getQualifiedName())) {
173: // FIXME - is this the correct element?
174: hreflang = attribute.getValue();
175: } else if ("title".equals(attribute.getQualifiedName())) {
176: title = attribute.getValue();
177: } else if ("length"
178: .equals(attribute.getQualifiedName())) {
179: length = attribute.getValue();
180: }
181: }
182:
183: // retrieve all of the sub-elements
184: Elements elements = link.getChildElements();
185: Element element = null;
186: int length = elements.size();
187:
188: for (int i = 0; i < length; i++) {
189: element = elements.get(i);
190: content = unmarshallString(element);
191: // FIXME - is this correct?
192:
193: } // for
194: } catch (Exception ex) {
195: InfoLogger.getLogger().writeError(
196: "Unable to parse an element in Link: "
197: + ex.getMessage());
198: throw new UnmarshallException(
199: "Unable to parse element in link", ex);
200: }
201: }
202:
203: /**
204: * Get the HREF attribute.
205: *
206: * @return The HREF.
207: */
208: public String getHref() {
209: return href;
210: }
211:
212: /**
213: * Set the HREF attribute.
214: *
215: * @param href The href.
216: */
217: public void setHref(String href) {
218: this .href = href;
219: }
220:
221: /**
222: * Get the Rel attribute.
223: *
224: * @return The Rel.
225: */
226: public String getRel() {
227: return rel;
228: }
229:
230: /**
231: * Set the Rel attribute.
232: *
233: * @param rel The Rel.
234: */
235: public void setRel(String rel) {
236: this .rel = rel;
237: }
238:
239: /**
240: * Get the type.
241: *
242: * @return The type.
243: */
244: public String getType() {
245: return type;
246: }
247:
248: /**
249: * Set the type.
250: * @param type The type.
251: */
252: public void setType(String type) {
253: this .type = type;
254: }
255:
256: /**
257: * Get the HREF Lang attribute.
258: *
259: * @return The HREF Lang.
260: */
261: public String getHreflang() {
262: return hreflang;
263: }
264:
265: /**
266: * Set the HREF Lang attribute.
267: *
268: * @param hreflang The HREF Lang.
269: */
270: public void setHreflang(String hreflang) {
271: this .hreflang = hreflang;
272: }
273:
274: /**
275: * Get the title.
276: *
277: * @return The title.
278: */
279: public String getTitle() {
280: return title;
281: }
282:
283: /**
284: * Set the title.
285: *
286: * @param title The title.
287: */
288: public void setTitle(String title) {
289: this .title = title;
290: }
291:
292: /**
293: * Get the length.
294: *
295: * @return The length.
296: */
297: public String getLength() {
298: return length;
299: }
300:
301: /**
302: * Set the length.
303: *
304: * @param length The length.
305: */
306: public void setLength(String length) {
307: this .length = length;
308: }
309:
310: /**
311: * Get the content.
312: *
313: * @return The content.
314: */
315: public String getContent() {
316: return content;
317: }
318:
319: /**
320: * Set the content.
321: *
322: * @param content The content.
323: */
324: public void setContent(String content) {
325: this.content = content;
326: }
327:
328: }
|