You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DesignResourceConverter.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.ui.declarative.converters;
  17. import java.io.File;
  18. import java.io.Serializable;
  19. import java.util.HashMap;
  20. import java.util.Locale;
  21. import java.util.Map;
  22. import com.vaadin.data.Result;
  23. import com.vaadin.data.util.converter.Converter;
  24. import com.vaadin.server.ExternalResource;
  25. import com.vaadin.server.FileResource;
  26. import com.vaadin.server.FontAwesome;
  27. import com.vaadin.server.FontIcon;
  28. import com.vaadin.server.GenericFontIcon;
  29. import com.vaadin.server.Resource;
  30. import com.vaadin.server.ResourceReference;
  31. import com.vaadin.server.ThemeResource;
  32. import com.vaadin.ui.declarative.DesignAttributeHandler;
  33. import com.vaadin.v7.data.util.converter.Converter.ConversionException;
  34. /**
  35. * A converter for {@link Resource} implementations supported by
  36. * {@link DesignAttributeHandler}.
  37. *
  38. * @since 7.4
  39. * @author Vaadin Ltd
  40. */
  41. @SuppressWarnings("serial")
  42. public class DesignResourceConverter implements Converter<String, Resource> {
  43. @Override
  44. public Result<Resource> convertToModel(String value, Locale locale) {
  45. if (!value.contains("://")) {
  46. // assume it'is "file://" protocol, one that is used to access a
  47. // file on a given path on the server, this will later be striped
  48. // out anyway
  49. value = "file://" + value;
  50. }
  51. String protocol = value.split("://")[0];
  52. try {
  53. ResourceConverterByProtocol converter = ResourceConverterByProtocol
  54. .valueOf(protocol.toUpperCase(Locale.ENGLISH));
  55. return Result.ok(converter.parse(value));
  56. } catch (IllegalArgumentException iae) {
  57. return Result.error("Unrecognized protocol: " + protocol);
  58. }
  59. }
  60. @Override
  61. public String convertToPresentation(Resource value, Locale locale) {
  62. ResourceConverterByProtocol byType = ResourceConverterByProtocol
  63. .byType(value.getClass());
  64. if (byType != null) {
  65. return byType.format(value);
  66. } else {
  67. throw new IllegalArgumentException(
  68. "unknown Resource type - " + value.getClass().getName());
  69. }
  70. }
  71. private static interface ProtocolResourceConverter extends Serializable {
  72. public String format(Resource value);
  73. public Resource parse(String value);
  74. }
  75. private static enum ResourceConverterByProtocol
  76. implements ProtocolResourceConverter {
  77. HTTP, HTTPS, FTP, FTPS, THEME {
  78. @Override
  79. public Resource parse(String value) {
  80. // strip "theme://" from the url, use the rest as the resource
  81. // id
  82. return new ThemeResource(value.substring(8));
  83. }
  84. @Override
  85. public String format(Resource value) {
  86. return new ResourceReference(value, null, null).getURL();
  87. }
  88. },
  89. FONTICON {
  90. @Override
  91. public Resource parse(String value) {
  92. final String address = (value.split("://", 2))[1];
  93. final String[] familyAndCode = address.split("/", 2);
  94. final int codepoint = Integer.valueOf(familyAndCode[1], 16);
  95. if (FontAwesome.FONT_FAMILY.equals(familyAndCode[0])) {
  96. try {
  97. return FontAwesome.fromCodepoint(codepoint);
  98. } catch (IllegalArgumentException iae) {
  99. throw new ConversionException(
  100. "Unknown codepoint in FontAwesome: "
  101. + codepoint,
  102. iae);
  103. }
  104. }
  105. FontIcon generic = new GenericFontIcon(familyAndCode[0],
  106. codepoint);
  107. return generic;
  108. }
  109. @Override
  110. public String format(Resource value) {
  111. FontIcon icon = (FontIcon) value;
  112. return new ResourceReference(icon, null, null).getURL();
  113. }
  114. },
  115. @Deprecated
  116. FONT {
  117. @Override
  118. public Resource parse(String value) {
  119. // Deprecated, 7.4 syntax is
  120. // font://"+FontAwesome.valueOf(foo) eg. "font://AMBULANCE"
  121. final String iconName = (value.split("://", 2))[1];
  122. try {
  123. return FontAwesome.valueOf(iconName);
  124. } catch (IllegalArgumentException iae) {
  125. throw new ConversionException(
  126. "Unknown FontIcon constant: " + iconName, iae);
  127. }
  128. }
  129. @Override
  130. public String format(Resource value) {
  131. throw new UnsupportedOperationException(
  132. "Use " + ResourceConverterByProtocol.FONTICON.toString()
  133. + " instead");
  134. }
  135. },
  136. FILE {
  137. @Override
  138. public Resource parse(String value) {
  139. return new FileResource(new File(value.split("://")[1]));
  140. }
  141. @Override
  142. public String format(Resource value) {
  143. String path = ((FileResource) value).getSourceFile().getPath();
  144. if (File.separatorChar != '/') {
  145. // make sure we use '/' as file separator in templates
  146. return path.replace(File.separatorChar, '/');
  147. } else {
  148. return path;
  149. }
  150. }
  151. };
  152. @Override
  153. public Resource parse(String value) {
  154. // default behavior for HTTP, HTTPS, FTP and FTPS
  155. return new ExternalResource(value);
  156. }
  157. @Override
  158. public String format(Resource value) {
  159. // default behavior for HTTP, HTTPS, FTP and FTPS
  160. return ((ExternalResource) value).getURL();
  161. }
  162. private static Map<Class<? extends Resource>, ResourceConverterByProtocol> typeToConverter = new HashMap<Class<? extends Resource>, ResourceConverterByProtocol>();
  163. static {
  164. typeToConverter.put(ExternalResource.class, HTTP);
  165. // ^ any of non-specialized would actually work
  166. typeToConverter.put(ThemeResource.class, THEME);
  167. typeToConverter.put(FontIcon.class, FONTICON);
  168. typeToConverter.put(FileResource.class, FILE);
  169. }
  170. public static ResourceConverterByProtocol byType(
  171. Class<? extends Resource> resourceType) {
  172. for (Class<?> type : typeToConverter.keySet()) {
  173. if (type.isAssignableFrom(resourceType)) {
  174. return typeToConverter.get(type);
  175. }
  176. }
  177. return null;
  178. }
  179. }
  180. }