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

File AbstractTypedStringEntityReferenceResolver.java

 

Coverage histogram

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

Code metrics

4
11
2
1
90
30
4
0.36
5.5
2
2

Classes

Class Line # Actions
AbstractTypedStringEntityReferenceResolver 37 11 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 11 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.annotation.reference.internal;
21   
22    import org.xwiki.annotation.reference.TypedStringEntityReferenceResolver;
23    import org.xwiki.model.EntityType;
24    import org.xwiki.model.reference.EntityReference;
25    import org.xwiki.model.reference.EntityReferenceResolver;
26   
27    /**
28    * Resolves references serialized as string that gets its type from the string serialization, in the form {@code
29    * type://} and, if no such type specification is found, it uses the default passed type. <br>
30    * For example, something like: {@code DOCUMENT://XWiki.TagClass[0]#tags} will result in parsing {@code
31    * XWiki.TagClass[0]#tags} as a document reference, while {OBJECT_PROPERTY://XWiki.TagClass[0]#tags} will result in
32    * parsing {@code XWiki.TagClass[0]#tags} as an object property reference.
33    *
34    * @version $Id: dbdfb85227d1c8c14fe380ae4eca5b90f50772ce $
35    * @since 2.3M1
36    */
 
37    public abstract class AbstractTypedStringEntityReferenceResolver implements TypedStringEntityReferenceResolver
38    {
39    /**
40    * {@inheritDoc}
41    * <p>
42    * Override to allow detecting the type of the reference from the prefix specified in the beginning of the
43    * serialized reference (e.g. DOCUMENT://Page). In case no type is specified, the second part parameter will be used
44    * as the default type.
45    * </p>
46    *
47    * @see TypedStringEntityReferenceResolver#resolve(String, org.xwiki.model.EntityType)
48    */
 
49  92 toggle @Override
50    public EntityReference resolve(String entityReferenceRepresentation, EntityType defaultType)
51    {
52  92 String refToParse = entityReferenceRepresentation;
53  92 EntityType type = defaultType;
54    // get the type specified at the beginning
55  92 EntityType specifiedType = getSerializedType(refToParse);
56    // if such type is specified
57  92 if (specifiedType != null) {
58    // this will be used to parse...
59  14 type = specifiedType;
60    // ...what's left of the reference after the protocol part
61  14 refToParse = refToParse.substring(type.toString().length() + 3);
62    }
63   
64    // use the super resolver to resolve the stripped reference and the resolved type
65  92 return getResolver().resolve(refToParse, type);
66    }
67   
68    /**
69    * Helper function to get the type of the serialized entity from its serialization as a protocol at the beginning of
70    * the serialization (e.g. DOCUMENT://Page).
71    *
72    * @param entityReferenceRepresentation the string representation of the entity to resolve
73    * @return the entity type specified as a prefix or null if no entity such exists
74    */
 
75  92 toggle private EntityType getSerializedType(String entityReferenceRepresentation)
76    {
77  92 for (EntityType type : EntityType.values()) {
78  688 if (entityReferenceRepresentation.startsWith(type + "://")) {
79  14 return type;
80    }
81    }
82  78 return null;
83    }
84   
85    /**
86    * @return the resolver to be used by this typed resolver to delegate resolving the actual reference, for the
87    * extracted type. Override to apply a specific strategy for the missing fields, for example.
88    */
89    protected abstract EntityReferenceResolver<String> getResolver();
90    }