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 a text construct in the ATOM elements. This is a superclass of
049: * several elements within this implementation.
050: *
051: * @author Neil Taylor
052: */
053: public class TextConstruct extends XmlElement implements
054: SwordElementInterface {
055: /**
056: * The content in the element.
057: */
058: private String content;
059:
060: /**
061: * The type of the element.
062: */
063: private ContentType type;
064:
065: /**
066: * Create a new instance, specifying the prefix and local name.
067: *
068: * @param prefix The prefix.
069: * @param name The local name.
070: */
071: public TextConstruct(String prefix, String name) {
072: super (prefix, name);
073: }
074:
075: /**
076: * Create a new instance. Set the default type to TextConstructType.TEXT.
077: *
078: * @param name The name that will be applied.
079: */
080: public TextConstruct(String name) {
081: super (name);
082: this .type = ContentType.TEXT;
083: }
084:
085: /**
086: * Marshall the data in this object to an Element object.
087: *
088: * @return The data expressed in an Element.
089: */
090: public Element marshall() {
091: Element element = new Element(getQualifiedName(),
092: Namespaces.NS_ATOM);
093: if (type != null) {
094: Attribute typeAttribute = new Attribute("type", type
095: .toString());
096: element.addAttribute(typeAttribute);
097: }
098:
099: if (content != null) {
100: element.appendChild(content);
101: }
102: return element;
103: }
104:
105: /**
106: * Unmarshall the text element into this object.
107: *
108: * This unmarshaller only handles plain text content, although it can
109: * recognise the three different type elements of text, html and xhtml. This
110: * is an area that can be improved in a future implementation, if necessary.
111: *
112: * @param text The text element.
113: *
114: * @throws UnmarshallException If the specified element is not of
115: * the correct type, where the localname is used
116: * to specify the valid name. Also thrown
117: * if there is an issue accessing the data.
118: */
119: public void unmarshall(Element text) throws UnmarshallException {
120: if (!isInstanceOf(text, localName, Namespaces.NS_ATOM)) {
121: throw new UnmarshallException("Not a " + getQualifiedName()
122: + " element");
123: }
124: try {
125: // get the attributes
126: int attributeCount = text.getAttributeCount();
127: Attribute attribute = null;
128: for (int i = 0; i < attributeCount; i++) {
129: attribute = text.getAttribute(i);
130: if ("type".equals(attribute.getQualifiedName())) {
131: String value = attribute.getValue();
132: if (ContentType.TEXT.toString().equals(value)) {
133: type = ContentType.TEXT;
134: } else if (ContentType.HTML.toString()
135: .equals(value)) {
136: type = ContentType.HTML;
137: } else if (ContentType.XHTML.toString().equals(
138: value)) {
139: type = ContentType.XHTML;
140: } else {
141: InfoLogger.getLogger().writeError(
142: "Unable to parse extract type in "
143: + getQualifiedName());
144: // FIXME - check error handling here
145: }
146: }
147: }
148:
149: // retrieve all of the sub-elements
150: int length = text.getChildCount();
151: if (length > 0) {
152: content = unmarshallString(text);
153: }
154: // FIXME - the above only handles plain text content.
155: } catch (Exception ex) {
156: InfoLogger.getLogger().writeError(
157: "Unable to parse an element in "
158: + getQualifiedName() + ": "
159: + ex.getMessage());
160: throw new UnmarshallException(
161: "Unable to parse an element in "
162: + getQualifiedName(), ex);
163: }
164: }
165:
166: /**
167: * Get the content in this TextConstruct.
168: *
169: * @return The content, expressed as a string.
170: */
171: public String getContent() {
172: return content;
173: }
174:
175: /**
176: * Set the content. This only supports text content.
177: *
178: * @param content The content.
179: */
180: public void setContent(String content) {
181: this .content = content;
182: }
183:
184: /**
185: * Get the type.
186: *
187: * @return The type.
188: */
189: public ContentType getType() {
190: return type;
191: }
192:
193: /**
194: * Set the type.
195: *
196: * @param type The type.
197: */
198: public void setType(ContentType type) {
199: this.type = type;
200: }
201: }
|