Class | Line # | Actions | |||||
---|---|---|---|---|---|---|---|
DirectoryStreamFilterConverter | 51 | 7 | 0% | 3 | 10 |
1 | /* | |
2 | * See the NOTICE file distributed with this work for additional | |
3 | * information regarding copyright ownership. | |
4 | * | |
5 | * This is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU Lesser General Public License as | |
7 | * published by the Free Software Foundation; either version 2.1 of | |
8 | * the License, or (at your option) any later version. | |
9 | * | |
10 | * This software is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * Lesser General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU Lesser General Public | |
16 | * License along with this software; if not, write to the Free | |
17 | * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
18 | * 02110-1301 USA, or see the FSF site: http://www.fsf.org. | |
19 | */ | |
20 | package org.xwiki.vfs.internal.script; | |
21 | ||
22 | import java.lang.reflect.Type; | |
23 | import java.nio.file.DirectoryStream; | |
24 | ||
25 | import javax.inject.Inject; | |
26 | import javax.inject.Provider; | |
27 | import javax.inject.Singleton; | |
28 | ||
29 | import org.xwiki.component.annotation.Component; | |
30 | import org.xwiki.component.manager.ComponentManager; | |
31 | import org.xwiki.properties.converter.AbstractConverter; | |
32 | import org.xwiki.properties.converter.ConversionException; | |
33 | ||
34 | /** | |
35 | * Converts from {@link String} to {@link java.nio.file.DirectoryStream.Filter} by looking at Component implementations | |
36 | * of the {@link java.nio.file.DirectoryStream.Filter} role with the passed string as the component hint. This makes it | |
37 | * easy to call the VFS module scripting API from Velocity for example. | |
38 | * <p> | |
39 | * For example the following would list all entries in the referenced zip which are of type File: | |
40 | * <code><pre> | |
41 | * #set ($ds = $services.vfs.getPaths("attach:Sandbox.WebHome@my.zip", "/", "File")) | |
42 | * ... | |
43 | * $ds.close() | |
44 | * </pre></code> | |
45 | * | |
46 | * @version $Id: 786a794bec4d8bc56a0ec78f971c305b56602c29 $ | |
47 | * @since 7.4M2 | |
48 | */ | |
49 | @Component | |
50 | @Singleton | |
51 | public class DirectoryStreamFilterConverter extends AbstractConverter<DirectoryStream.Filter> | |
52 | { | |
53 | @Inject | |
54 | private Provider<ComponentManager> componentManagerProvider; | |
55 | ||
56 | 0 | ![]() |
57 | protected DirectoryStream.Filter convertToType(Type targetType, Object value) | |
58 | { | |
59 | 0 | if (value == null) { |
60 | 0 | return null; |
61 | } | |
62 | ||
63 | 0 | DirectoryStream.Filter filter; |
64 | ||
65 | 0 | try { |
66 | 0 | filter = this.componentManagerProvider.get().getInstance(DirectoryStream.Filter.class, value.toString()); |
67 | } catch (Exception e) { | |
68 | 0 | throw new ConversionException( |
69 | String.format("Failed to convert [%s] to [%s]", value, DirectoryStream.Filter.class.getName()), e); | |
70 | } | |
71 | ||
72 | 0 | return filter; |
73 | } | |
74 | } |