Debian Codegraph @

src/test/java/com/fasterxml/jackson/module/jaxb/introspect/Content.java

raw
1 package com.fasterxml.jackson.module.jaxb.introspect;
2
3 import java.net.URI;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8
9 import javax.ws.rs.core.MediaType;
10 import javax.xml.bind.annotation.*;
11
12 import org.w3c.dom.Element;
13
14 /**
15 * <p>Represents an atom:content element.</p>
16 * <p/>
17 * <p>Per RFC4287:</p>
18 * <p/>
19 * <pre>
20 * The "atom:content" element either contains or links to the content of
21 * the entry. The content of atom:content is Language-Sensitive.
22 * <p/>
23 * atomInlineTextContent =
24 * element atom:content {
25 * atomCommonAttributes,
26 * attribute type { "text" | "html" }?,
27 * (text)*
28 * }
29 * <p/>
30 * atomInlineXHTMLContent =
31 * element atom:content {
32 * atomCommonAttributes,
33 * attribute type { "xhtml" },
34 * xhtmlDiv
35 * }
36 * atomInlineOtherContent =
37 * element atom:content {
38 * atomCommonAttributes,
39 * attribute type { atomMediaType }?,
40 * (text|anyElement)*
41 * }
42 * <p/>
43 * atomOutOfLineContent =
44 * element atom:content {
45 * atomCommonAttributes,
46 * attribute type { atomMediaType }?,
47 * attribute src { atomUri },
48 * empty
49 * }
50 * <p/>
51 * atomContent = atomInlineTextContent
52 * | atomInlineXHTMLContent
53 * | atomInlineOtherContent
54 * | atomOutOfLineContent
55 * <p/>
56 * </pre>
57 *
58 * @author <a href="mailto:[email protected]">Bill Burke</a>
59 * @version $Revision: 1666 $
60 */
61 @XmlRootElement(name = "content")
62 @XmlAccessorType(XmlAccessType.PROPERTY)
63 class Content extends CommonAttributes
64 {
65 private String type;
66 private MediaType mediaType;
67 private String text;
68 private Element element;
69 private URI src;
70 private List<Object> value;
71 protected Object jaxbObject;
72
73 @XmlAnyElement
74 @XmlMixed
75 public List<Object> getValue() { return value; }
76
77 public void setValue(List<Object> value) { this.value = value; }
78
79 @XmlAttribute
80 public URI getSrc() { return src; }
81
82 public void setSrc(URI src) { this.src = src; }
83
84 /**
85 * Mime type of the content
86 */
87 @XmlTransient
88 public MediaType getType()
89 {
90 if (mediaType == null)
91 {
92 if (type.equals("html")) mediaType = MediaType.TEXT_HTML_TYPE;
93 else if (type.equals("text")) mediaType = MediaType.TEXT_PLAIN_TYPE;
94 else if (type.equals("xhtml")) mediaType = MediaType.APPLICATION_XHTML_XML_TYPE;
95 else mediaType = MediaType.valueOf(type);
96 }
97 return mediaType;
98 }
99
100 public void setType(MediaType type)
101 {
102 mediaType = type;
103 if (type.equals(MediaType.TEXT_PLAIN_TYPE)) this.type = "text";
104 else if (type.equals(MediaType.TEXT_HTML_TYPE)) this.type = "html";
105 else if (type.equals(MediaType.APPLICATION_XHTML_XML_TYPE)) this.type = "xhtml";
106 else this.type = type.toString();
107 }
108
109 @XmlAttribute(name = "type")
110 public String getRawType() { return type; }
111
112
113 public void setRawType(String type) { this.type = type; }
114
115 @XmlTransient
116 public String getText()
117 {
118 if (value == null) return null;
119 if (value.size() == 0) return null;
120 if (text != null) return text;
121 StringBuffer buf = new StringBuffer();
122 for (Object obj : value)
123 {
124 if (obj instanceof String) buf.append(obj.toString());
125 }
126 text = buf.toString();
127 return text;
128 }
129
130 public void setText(String text)
131 {
132 if (value == null) value = new ArrayList<Object>();
133 if (this.text != null && value != null) value.clear();
134 this.text = text;
135 value.add(text);
136 }
137
138 /**
139 * Get content as an XML Element if the content is XML. Otherwise, this will just return null.
140 */
141 @XmlTransient
142 public Element getElement()
143 {
144 if (value == null) return null;
145 if (element != null) return element;
146 for (Object obj : value)
147 {
148 if (obj instanceof Element)
149 {
150 element = (Element) obj;
151 return element;
152 }
153 }
154 return null;
155 }
156
157 /**
158 * Set the content to an XML Element
159 *
160 * @param element
161 */
162 public void setElement(Element element)
163 {
164 if (value == null) value = new ArrayList<Object>();
165 if (this.element != null && value != null) value.clear();
166 this.element = element;
167 value.add(element);
168 }
169 }
170
171 @XmlAccessorType(XmlAccessType.PROPERTY)
172 class CommonAttributes
173 {
174 private String language;
175 private URI base;
176
177 private Map<?,?> extensionAttributes = new HashMap<String,String>();
178
179 @XmlAttribute(name = "lang", namespace = "http://www.w3.org/XML/1998/namespace")
180 public String getLanguage() {
181 return language;
182 }
183
184 public void setLanguage(String language) {
185 this.language = language;
186 }
187
188 @XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
189 public URI getBase() {
190 return base;
191 }
192
193 public void setBase(URI base) {
194 this.base = base;
195 }
196
197 @XmlAnyAttribute
198 public Map<?,?> getExtensionAttributes() {
199 return extensionAttributes;
200 }
201 }