PageRenderTime 23ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/org.mwc.asset.comms/docs/restlet_src/org.restlet/org/restlet/engine/converter/ConverterUtils.java

https://bitbucket.org/haris_peco/debrief
Java | 192 lines | 84 code | 21 blank | 87 comment | 17 complexity | aa0e1dcff3398f163715b2ccef0b0ab0 MD5 | raw file
  1. /**
  2. * Copyright 2005-2010 Noelios Technologies.
  3. *
  4. * The contents of this file are subject to the terms of one of the following
  5. * open source licenses: LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL 1.0 (the
  6. * "Licenses"). You can select the license that you prefer but you may not use
  7. * this file except in compliance with one of these Licenses.
  8. *
  9. * You can obtain a copy of the LGPL 3.0 license at
  10. * http://www.opensource.org/licenses/lgpl-3.0.html
  11. *
  12. * You can obtain a copy of the LGPL 2.1 license at
  13. * http://www.opensource.org/licenses/lgpl-2.1.php
  14. *
  15. * You can obtain a copy of the CDDL 1.0 license at
  16. * http://www.opensource.org/licenses/cddl1.php
  17. *
  18. * You can obtain a copy of the EPL 1.0 license at
  19. * http://www.opensource.org/licenses/eclipse-1.0.php
  20. *
  21. * See the Licenses for the specific language governing permissions and
  22. * limitations under the Licenses.
  23. *
  24. * Alternatively, you can obtain a royalty free commercial license with less
  25. * limitations, transferable or non-transferable, directly at
  26. * http://www.noelios.com/products/restlet-engine
  27. *
  28. * Restlet is a registered trademark of Noelios Technologies.
  29. */
  30. package org.restlet.engine.converter;
  31. import java.util.ArrayList;
  32. import java.util.List;
  33. import java.util.logging.Level;
  34. import org.restlet.Context;
  35. import org.restlet.engine.Engine;
  36. import org.restlet.engine.resource.VariantInfo;
  37. import org.restlet.representation.Representation;
  38. import org.restlet.representation.Variant;
  39. import org.restlet.resource.UniformResource;
  40. /**
  41. * Utilities for the converter service.
  42. *
  43. * @author Jerome Louvel
  44. */
  45. public class ConverterUtils {
  46. /**
  47. * Adds a variant to the given list.
  48. *
  49. * @param variants
  50. * The list to update.
  51. * @param variant
  52. * The variant info to add.
  53. * @return The updated list.
  54. */
  55. protected static List<VariantInfo> addVariant(List<VariantInfo> variants,
  56. VariantInfo variant) {
  57. List<VariantInfo> result = variants;
  58. if (result == null) {
  59. result = new ArrayList<VariantInfo>();
  60. }
  61. if (!result.contains(variant)) {
  62. result.add(variant);
  63. }
  64. return result;
  65. }
  66. /**
  67. * Returns the list of variants that can be converted from a given object
  68. * class.
  69. *
  70. * @param sourceClass
  71. * The source class.
  72. * @param targetVariant
  73. * The expected representation metadata.
  74. * @return The list of variants that can be converted.
  75. */
  76. public static List<VariantInfo> getVariants(Class<?> sourceClass,
  77. Variant targetVariant) {
  78. List<VariantInfo> result = null;
  79. List<VariantInfo> helperVariants = null;
  80. for (ConverterHelper ch : Engine.getInstance()
  81. .getRegisteredConverters()) {
  82. // List of variants that can be converted from the source class
  83. helperVariants = ch.getVariants(sourceClass);
  84. if (helperVariants != null) {
  85. // Loop over the variants list
  86. for (VariantInfo helperVariant : helperVariants) {
  87. if (targetVariant == null) {
  88. result = addVariant(result, helperVariant);
  89. } else if (helperVariant.includes(targetVariant)) {
  90. // Detected a more generic variant, but still consider
  91. // the conversion is possible to the target variant.
  92. result = addVariant(result, new VariantInfo(
  93. targetVariant.getMediaType()));
  94. } else if (targetVariant.includes(helperVariant)) {
  95. // Detected a more specific variant, but still consider
  96. // the conversion is possible to the target variant.
  97. result = addVariant(result, helperVariant);
  98. }
  99. }
  100. }
  101. }
  102. return result;
  103. }
  104. /**
  105. * Returns the best converter helper matching the given parameters.
  106. *
  107. * @param source
  108. * The object to convert to a representation.
  109. * @param target
  110. * The target representation variant.
  111. * @param resource
  112. * The optional parent resource.
  113. * @return The matched converter helper or null.
  114. */
  115. public static ConverterHelper getBestHelper(Object source, Variant target,
  116. UniformResource resource) {
  117. ConverterHelper result = null;
  118. float bestScore = -1.0F;
  119. float currentScore;
  120. for (ConverterHelper ch : Engine.getInstance()
  121. .getRegisteredConverters()) {
  122. try {
  123. currentScore = ch.score(source, target, resource);
  124. if (currentScore > bestScore) {
  125. bestScore = currentScore;
  126. result = ch;
  127. }
  128. } catch (Exception e) {
  129. Context.getCurrentLogger().log(
  130. Level.SEVERE,
  131. "Unable get the score of the " + ch
  132. + " converter helper.", e);
  133. }
  134. }
  135. return result;
  136. }
  137. /**
  138. * Returns the best converter helper matching the given parameters.
  139. *
  140. * @param <T>
  141. * The target class.
  142. * @param source
  143. * The source representation variant.
  144. * @param target
  145. * The target class.
  146. * @param resource
  147. * The parent resource.
  148. * @return The matched converter helper or null.
  149. */
  150. public static <T> ConverterHelper getBestHelper(Representation source,
  151. Class<T> target, UniformResource resource) {
  152. ConverterHelper result = null;
  153. float bestScore = -1.0F;
  154. float currentScore;
  155. for (ConverterHelper ch : Engine.getInstance()
  156. .getRegisteredConverters()) {
  157. currentScore = ch.score(source, target, resource);
  158. if (currentScore > bestScore) {
  159. bestScore = currentScore;
  160. result = ch;
  161. }
  162. }
  163. return result;
  164. }
  165. /**
  166. * Private constructor to ensure that the class acts as a true utility class
  167. * i.e. it isn't instantiable and extensible.
  168. */
  169. private ConverterUtils() {
  170. }
  171. }