/odps-sdk/odps-sdk-core/src/main/java/com/aliyun/odps/simpleframework/xml/core/PrimitiveValue.java

https://github.com/aliyun/aliyun-odps-java-sdk · Java · 332 lines · 119 code · 29 blank · 184 comment · 31 complexity · dda0636e0a9e71a7650beef834497d84 MD5 · raw file

  1. /*
  2. * PrimitiveValue.java July 2007
  3. *
  4. * Copyright (C) 2007, Niall Gallagher <niallg@users.sf.net>
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  15. * implied. See the License for the specific language governing
  16. * permissions and limitations under the License.
  17. */
  18. package com.aliyun.odps.simpleframework.xml.core;
  19. import com.aliyun.odps.simpleframework.xml.strategy.Type;
  20. import com.aliyun.odps.simpleframework.xml.stream.InputNode;
  21. import com.aliyun.odps.simpleframework.xml.stream.OutputNode;
  22. import com.aliyun.odps.simpleframework.xml.stream.Style;
  23. /**
  24. * The <code>PrimitiveValue</code> is used to serialize a primitive
  25. * value to and from a node. If a value name is provided in the
  26. * annotation then this will serialize and deserialize that value
  27. * with the given name, if the value is primitive and no name is
  28. * specified then the value is written inline, that is without any
  29. * enclosing XML element.
  30. * <pre>
  31. *
  32. * &lt;entry key="one"&gt;example one&lt;/entry&gt;
  33. * &lt;entry key="two"&gt;example two&lt;/entry&gt;
  34. * &lt;entry key="three"&gt;example three&lt;/entry&gt;
  35. *
  36. * </pre>
  37. * Allowing the value to be written as either an XML element or an
  38. * inline text value enables a more flexible means for representing
  39. * the value. The only condition for having an inline value is that
  40. * the key is specified as an attribute in the annotation.
  41. *
  42. * @author Niall Gallagher
  43. *
  44. * @see CompositeMap
  45. */
  46. class PrimitiveValue implements Converter {
  47. /**
  48. * The primitive factory used to resolve the primitive to a string.
  49. */
  50. private final PrimitiveFactory factory;
  51. /**
  52. * This is the context used to support the serialization process.
  53. */
  54. private final Context context;
  55. /**
  56. * The primitive converter used to read the value from the node.
  57. */
  58. private final Primitive root;
  59. /**
  60. * This is the style used to style the XML names for the value.
  61. */
  62. private final Style style;
  63. /**
  64. * The entry object contains the details on how to write the value.
  65. */
  66. private final Entry entry;
  67. /**
  68. * Represents the primitive type the value is serialized to and from.
  69. */
  70. private final Type type;
  71. /**
  72. * Constructor for the <code>PrimitiveValue</code> object. This is
  73. * used to create the value object which converts the map value to
  74. * an instance of the value type. This can also resolve references.
  75. *
  76. * @param context this is the context object used for serialization
  77. * @param entry this is the entry object that describes entries
  78. * @param type this is the type that this converter deals with
  79. */
  80. public PrimitiveValue(Context context, Entry entry, Type type) {
  81. this.factory = new PrimitiveFactory(context, type);
  82. this.root = new Primitive(context, type);
  83. this.style = context.getStyle();
  84. this.context = context;
  85. this.entry = entry;
  86. this.type = type;
  87. }
  88. /**
  89. * This method is used to read the value value from the node. The
  90. * value read from the node is resolved using the template filter.
  91. * If the value value can not be found according to the annotation
  92. * attributes then an exception is thrown.
  93. *
  94. * @param node this is the node to read the value object from
  95. *
  96. * @return this returns the value deserialized from the node
  97. */
  98. public Object read(InputNode node) throws Exception {
  99. Class expect = type.getType();
  100. String name = entry.getValue();
  101. if(!entry.isInline()) {
  102. if(name == null) {
  103. name = context.getName(expect);
  104. }
  105. return readElement(node, name);
  106. }
  107. return readAttribute(node, name);
  108. }
  109. /**
  110. * This method is used to read the value value from the node. The
  111. * value read from the node is resolved using the template filter.
  112. * If the value value can not be found according to the annotation
  113. * attributes then an exception is thrown.
  114. *
  115. * @param node this is the node to read the value object from
  116. * @param value this is the value to deserialize in to
  117. *
  118. * @return this returns the value deserialized from the node
  119. *
  120. * @throws Exception if value is not null an exception is thrown
  121. */
  122. public Object read(InputNode node, Object value) throws Exception {
  123. Class expect = type.getType();
  124. if(value != null) {
  125. throw new PersistenceException("Can not read value of %s for %s", expect, entry);
  126. }
  127. return read(node);
  128. }
  129. /**
  130. * This method is used to read the element value from the node. The
  131. * value read from the node is resolved using the template filter.
  132. * If the value value can not be found according to the annotation
  133. * attributes then null is assumed and returned.
  134. *
  135. * @param node this is the node to read the value object from
  136. * @param key this is the name of the value XML element
  137. *
  138. * @return this returns the value deserialized from the node
  139. */
  140. private Object readElement(InputNode node, String key) throws Exception {
  141. String name = style.getAttribute(key);
  142. InputNode child = node.getNext(name);
  143. if(child == null) {
  144. return null;
  145. }
  146. return root.read(child);
  147. }
  148. /**
  149. * This method is used to read the text value from the node. The
  150. * value read from the node is resolved using the template filter.
  151. * If the value value can not be found according to the annotation
  152. * attributes then null is assumed and returned.
  153. *
  154. * @param node this is the node to read the value object from
  155. * @param name this is the name of the value XML attribute, if any
  156. *
  157. * @return this returns the value deserialized from the node
  158. */
  159. private Object readAttribute(InputNode node, String name) throws Exception {
  160. if(name != null) {
  161. name = style.getAttribute(name);
  162. node = node.getAttribute(name);
  163. }
  164. if(node == null) {
  165. return null;
  166. }
  167. return root.read(node);
  168. }
  169. /**
  170. * This method is used to validate the value from the node. The
  171. * value read from the node is resolved using the template filter.
  172. * If the value value can not be found according to the annotation
  173. * attributes then null is assumed and the node is valid.
  174. *
  175. * @param node this is the node to read the value object from
  176. *
  177. * @return this returns true if the primitive key is valid
  178. */
  179. public boolean validate(InputNode node) throws Exception {
  180. Class expect = type.getType();
  181. String name = entry.getValue();
  182. if(!entry.isInline()) {
  183. if(name == null) {
  184. name = context.getName(expect);
  185. }
  186. return validateElement(node, name);
  187. }
  188. return validateAttribute(node, name);
  189. }
  190. /**
  191. * This method is used to validate the value from the node. The
  192. * value read from the node is resolved using the template filter.
  193. * If the value value can not be found according to the annotation
  194. * attributes then null is assumed and the node is valid.
  195. *
  196. * @param node this is the node to read the value object from
  197. * @param key this is the name of the node to be validated
  198. *
  199. * @return this returns true if the primitive key is valid
  200. */
  201. private boolean validateElement(InputNode node, String key) throws Exception {
  202. String name = style.getAttribute(key);
  203. InputNode child = node.getNext(name);
  204. if(child == null) {
  205. return true;
  206. }
  207. return root.validate(node);
  208. }
  209. /**
  210. * This method is used to validate the value from the node. The
  211. * value read from the node is resolved using the template filter.
  212. * If the value value can not be found according to the annotation
  213. * attributes then null is assumed and the node is valid.
  214. *
  215. * @param node this is the node to read the value object from
  216. * @param key this is the name of the node to be validated
  217. *
  218. * @return this returns true if the primitive key is valid
  219. */
  220. private boolean validateAttribute(InputNode node, String key) throws Exception {
  221. if(key != null) {
  222. key = style.getAttribute(key);
  223. node = node.getNext(key);
  224. }
  225. if(node == null) {
  226. return true;
  227. }
  228. return root.validate(node);
  229. }
  230. /**
  231. * This method is used to write the value to the specified node.
  232. * The value written to the node can be an attribute or an element
  233. * depending on the annotation attribute values. This method will
  234. * maintain references for serialized elements.
  235. *
  236. * @param node this is the node that the value is written to
  237. * @param item this is the item that is to be written
  238. */
  239. public void write(OutputNode node, Object item) throws Exception {
  240. Class expect = type.getType();
  241. String name = entry.getValue();
  242. if(!entry.isInline()) {
  243. if(name == null) {
  244. name = context.getName(expect);
  245. }
  246. writeElement(node, item, name);
  247. } else {
  248. writeAttribute(node, item, name);
  249. }
  250. }
  251. /**
  252. * This method is used to write the value to the specified node.
  253. * The value written to the node can be an attribute or an element
  254. * depending on the annotation attribute values. This method will
  255. * maintain references for serialized elements.
  256. *
  257. * @param node this is the node that the value is written to
  258. * @param item this is the item that is to be written
  259. * @param key this is the name of the element to be created
  260. */
  261. private void writeElement(OutputNode node, Object item, String key) throws Exception {
  262. String name = style.getAttribute(key);
  263. OutputNode child = node.getChild(name);
  264. if(item != null) {
  265. if(!isOverridden(child, item)) {
  266. root.write(child, item);
  267. }
  268. }
  269. }
  270. /**
  271. * This method is used to write the value to the specified node.
  272. * The value written to the node can be an attribute or an element
  273. * depending on the annotation attribute values. This method will
  274. * maintain references for serialized elements.
  275. *
  276. * @param node this is the node that the value is written to
  277. * @param item this is the item that is to be written
  278. * @param key this is the name of the attribute to be created
  279. */
  280. private void writeAttribute(OutputNode node, Object item, String key) throws Exception {
  281. if(item != null) {
  282. if(key != null) {
  283. key = style.getAttribute(key);
  284. node = node.setAttribute(key, null);
  285. }
  286. root.write(node, item);
  287. }
  288. }
  289. /**
  290. * This is used to determine whether the specified value has been
  291. * overridden by the strategy. If the item has been overridden
  292. * then no more serialization is require for that value, this is
  293. * effectively telling the serialization process to stop writing.
  294. *
  295. * @param node the node that a potential override is written to
  296. * @param value this is the object instance to be serialized
  297. *
  298. * @return returns true if the strategy overrides the object
  299. */
  300. private boolean isOverridden(OutputNode node, Object value) throws Exception{
  301. return factory.setOverride(type, value, node);
  302. }
  303. }