Class | Line # | Actions | |||||
---|---|---|---|---|---|---|---|
SolrFieldStringEntityReferenceSerializer | 50 | 6 | 0% | 2 | 0 |
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.search.solr.internal; | |
21 | ||
22 | import javax.inject.Named; | |
23 | import javax.inject.Singleton; | |
24 | ||
25 | import org.xwiki.component.annotation.Component; | |
26 | import org.xwiki.model.reference.EntityReference; | |
27 | import org.xwiki.model.reference.EntityReferenceSerializer; | |
28 | ||
29 | /** | |
30 | * Serializes an {@link EntityReference} to a {@link String} that can be included in a Solr field name. Examples: | |
31 | * <ul> | |
32 | * <li>Class reference 'Blog.BlogPostClass' produces 'Blog.BlogPostClass'</li> | |
33 | * <li>Class reference 'Bl\.og.BlogPos\.tClass' produces 'Bl..og.BlogPos..tClass'</li> | |
34 | * <li>Class property reference 'Blog.BlogPostClass^title' produces 'Blog.BlogPostClass.title'</li> | |
35 | * <li>Class property reference 'B\.log.BlogPost\.Class^titl\.e' produces 'B..log.BlogPost..Class.titl..e'</li> | |
36 | * </ul> | |
37 | * Note that we need this special serialization syntax because both '^' and '\' are special characters in the Solr query | |
38 | * syntax so it's not possible to use them in the field name. We chose '.' (dot) as the separator because it was already | |
39 | * used for separating the space and page name in the default {@link EntityReferenceSerializer} and because it was one | |
40 | * of the few non-alphanumeric characters allowed in the field name. Other options would have been '_' (but it appears | |
41 | * more often in the space/page/property name than dot), '$' (already used by the Solr field name encoder) and '-' | |
42 | * (which doesn't look natural). | |
43 | * | |
44 | * @version $Id: fc3711446def55068137fa113c7b5462edc4e11c $ | |
45 | * @since 5.3RC1 | |
46 | */ | |
47 | @Component | |
48 | @Named("solr") | |
49 | @Singleton | |
50 | public class SolrFieldStringEntityReferenceSerializer implements EntityReferenceSerializer<String> | |
51 | { | |
52 | /** | |
53 | * The string version of the {@link SolrFieldStringEntityReferenceResolver#SEPARATOR}. | |
54 | */ | |
55 | static final String SEPARATOR = String.valueOf(SolrFieldStringEntityReferenceResolver.SEPARATOR); | |
56 | ||
57 | /** | |
58 | * The escaped separator. | |
59 | */ | |
60 | static final String ESCAPED_SEPARATOR = SEPARATOR + SEPARATOR; | |
61 | ||
62 | 8693 | @Override |
63 | public String serialize(EntityReference reference, Object... parameters) | |
64 | { | |
65 | 8693 | if (reference == null) { |
66 | 1 | return null; |
67 | } | |
68 | ||
69 | 8692 | StringBuilder output = new StringBuilder(); |
70 | 8692 | for (EntityReference parent : reference.getReversedReferenceChain()) { |
71 | 21735 | output.append(SEPARATOR).append(parent.getName().replace(SEPARATOR, "..")); |
72 | } | |
73 | 8692 | return output.substring(1); |
74 | } | |
75 | } |