1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.model.internal.reference.converter

File EntityReferenceConverter.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

8
13
3
1
90
52
7
0.54
4.33
3
2.33

Classes

Class Line # Actions
EntityReferenceConverter 47 13 0% 7 2
0.916666791.7%
 

Contributing tests

This file is covered by 5 tests. .

Source view

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.model.internal.reference.converter;
21   
22    import java.lang.reflect.Type;
23   
24    import javax.inject.Inject;
25    import javax.inject.Named;
26    import javax.inject.Singleton;
27   
28    import org.apache.commons.lang3.EnumUtils;
29    import org.xwiki.component.annotation.Component;
30    import org.xwiki.component.namespace.Namespace;
31    import org.xwiki.component.namespace.NamespaceUtils;
32    import org.xwiki.model.EntityType;
33    import org.xwiki.model.reference.EntityReference;
34    import org.xwiki.model.reference.EntityReferenceResolver;
35    import org.xwiki.model.reference.EntityReferenceSerializer;
36    import org.xwiki.properties.converter.AbstractConverter;
37   
38    /**
39    * Converter that converts a value into an {@link EntityReference} object. Reference are not resolved to created
40    * complete reference but kept exactly as indicated in the value.
41    *
42    * @version $Id: decc5462eeb79ccb7efdee47d746fb9052828abe $
43    * @since 5.2RC1
44    */
45    @Component
46    @Singleton
 
47    public class EntityReferenceConverter extends AbstractConverter<EntityReference>
48    {
49    @Inject
50    @Named("relative")
51    private EntityReferenceResolver<String> stringResolver;
52   
53    @Inject
54    private EntityReferenceSerializer<String> serialier;
55   
 
56  75 toggle @Override
57    protected EntityReference convertToType(Type type, Object value)
58    {
59  75 if (value == null) {
60  1 return null;
61    }
62   
63  74 return convertToType(type, value.toString());
64    }
65   
 
66  74 toggle private EntityReference convertToType(Type type, String value)
67    {
68  74 Namespace namespace = NamespaceUtils.toNamespace(value);
69   
70  74 EntityType entityType = namespace.getType() != null
71    ? EnumUtils.getEnum(EntityType.class, namespace.getType().toUpperCase()) : null;
72  74 String entityReference = namespace.getValue();
73  74 if (entityType == null) {
74  68 entityType = EntityType.DOCUMENT;
75  68 entityReference = value;
76    }
77   
78  74 return this.stringResolver.resolve(entityReference, entityType);
79    }
80   
 
81  1 toggle @Override
82    protected String convertToString(EntityReference value)
83    {
84  1 if (value == null) {
85  0 return null;
86    }
87   
88  1 return new Namespace(value.getType().getLowerCase(), this.serialier.serialize(value)).toString();
89    }
90    }