1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.wysiwyg.server.wiki

File EntityReferenceConverter.java

 

Coverage histogram

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

Code metrics

16
46
7
1
182
110
16
0.35
6.57
7
2.29

Classes

Class Line # Actions
EntityReferenceConverter 48 46 0% 16 6
0.913043591.3%
 

Contributing tests

This file is covered by 1 test. .

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.wysiwyg.server.wiki;
21   
22    import java.util.HashMap;
23    import java.util.Map;
24   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27    import javax.inject.Singleton;
28   
29    import org.apache.commons.lang3.StringUtils;
30    import org.xwiki.component.annotation.Component;
31    import org.xwiki.gwt.wysiwyg.client.wiki.WikiPageReference;
32    import org.xwiki.model.EntityType;
33    import org.xwiki.model.reference.AttachmentReference;
34    import org.xwiki.model.reference.DocumentReference;
35    import org.xwiki.model.reference.EntityReference;
36    import org.xwiki.model.reference.EntityReferenceSerializer;
37    import org.xwiki.model.reference.SpaceReference;
38    import org.xwiki.model.reference.SpaceReferenceResolver;
39    import org.xwiki.model.reference.WikiReference;
40   
41    /**
42    * Converts between client-side entity references and server-side entity references.
43    *
44    * @version $Id: 6ab56143d8754b0c7ee407090c1166f28419e342 $
45    */
46    @Component(roles = EntityReferenceConverter.class)
47    @Singleton
 
48    public class EntityReferenceConverter
49    {
50    /**
51    * Maps entity types to client reference component names.
52    */
53    private static final Map<EntityType, String> REFERENCE_COMPONENT_NAME;
54   
55    @Inject
56    private SpaceReferenceResolver<String> spaceResolver;
57   
58    @Inject
59    @Named("local")
60    private EntityReferenceSerializer<String> localEntityReferenceSerializer;
61   
 
62  1 toggle static {
63  1 REFERENCE_COMPONENT_NAME = new HashMap<EntityType, String>();
64  1 REFERENCE_COMPONENT_NAME.put(EntityType.WIKI, WikiPageReference.WIKI_NAME);
65  1 REFERENCE_COMPONENT_NAME.put(EntityType.SPACE, WikiPageReference.SPACE_NAME);
66  1 REFERENCE_COMPONENT_NAME.put(EntityType.DOCUMENT, WikiPageReference.PAGE_NAME);
67  1 REFERENCE_COMPONENT_NAME.put(EntityType.ATTACHMENT,
68    org.xwiki.gwt.wysiwyg.client.wiki.AttachmentReference.FILE_NAME);
69    }
70   
71    /**
72    * Converts an entity reference received from the client to an entity reference to be used on the server.
73    *
74    * @param clientEntityReference a client-side entity reference
75    * @return a server-side entity reference
76    */
 
77  2 toggle public EntityReference convert(org.xwiki.gwt.wysiwyg.client.wiki.EntityReference clientEntityReference)
78    {
79  2 if (clientEntityReference == null) {
80  1 return null;
81    }
82  1 EntityReference serverEntityReference = null;
83  1 String wikiName = clientEntityReference.getComponent(REFERENCE_COMPONENT_NAME.get(EntityType.WIKI));
84  1 if (!StringUtils.isEmpty(wikiName)) {
85  1 serverEntityReference = new EntityReference(wikiName, EntityType.WIKI);
86    }
87  1 String localSpaceReference = clientEntityReference.getComponent(REFERENCE_COMPONENT_NAME.get(EntityType.SPACE));
88  1 if (!StringUtils.isEmpty(localSpaceReference)) {
89  1 serverEntityReference = this.spaceResolver.resolve(localSpaceReference, serverEntityReference);
90    }
91  1 String pageName = clientEntityReference.getComponent(REFERENCE_COMPONENT_NAME.get(EntityType.DOCUMENT));
92  1 if (!StringUtils.isEmpty(pageName)) {
93  1 serverEntityReference = new EntityReference(pageName, EntityType.DOCUMENT, serverEntityReference);
94    }
95  1 String fileName = clientEntityReference.getComponent(REFERENCE_COMPONENT_NAME.get(EntityType.ATTACHMENT));
96  1 if (!StringUtils.isEmpty(fileName)) {
97  1 serverEntityReference = new EntityReference(fileName, EntityType.ATTACHMENT, serverEntityReference);
98    }
99  1 return serverEntityReference;
100    }
101   
102    /**
103    * Converts an entity reference used on the server side to an entity reference to be sent to the client.
104    *
105    * @param serverEntityReference a server-side entity reference
106    * @return the corresponding client-side entity reference
107    */
 
108  2 toggle public org.xwiki.gwt.wysiwyg.client.wiki.EntityReference convert(EntityReference serverEntityReference)
109    {
110  2 org.xwiki.gwt.wysiwyg.client.wiki.EntityReference clientEntityReference =
111    new org.xwiki.gwt.wysiwyg.client.wiki.EntityReference();
112  2 try {
113  2 clientEntityReference.setType(org.xwiki.gwt.wysiwyg.client.wiki.EntityReference.EntityType
114    .valueOf(serverEntityReference.getType().toString()));
115    } catch (Exception e) {
116  1 return null;
117    }
118   
119  1 EntityReference child = serverEntityReference;
120   
121    // The client side doesn't support nested spaces yet so we 'collapse' all the nested spaces.
122  1 EntityReference spaceReference = serverEntityReference.extractReference(EntityType.SPACE);
123  1 if (spaceReference != null) {
124  1 String localSpaceReference = this.localEntityReferenceSerializer.serialize(spaceReference);
125  1 EntityReference wikiReference = serverEntityReference.extractReference(EntityType.WIKI);
126  1 EntityReference newSpaceReference = new SpaceReference(localSpaceReference, wikiReference);
127  1 child = serverEntityReference.replaceParent(spaceReference, newSpaceReference);
128    }
129   
130  5 while (child != null) {
131  4 String componentName = REFERENCE_COMPONENT_NAME.get(child.getType());
132  4 if (componentName != null) {
133  4 clientEntityReference.setComponent(componentName, child.getName());
134    }
135  4 child = child.getParent();
136    }
137  1 return clientEntityReference;
138    }
139   
140    /**
141    * @param documentReference a document reference
142    * @return the corresponding wiki page reference
143    */
 
144  1 toggle public WikiPageReference convert(DocumentReference documentReference)
145    {
146  1 String wikiName = documentReference.getWikiReference().getName();
147  1 String spaceName = this.localEntityReferenceSerializer.serialize(documentReference.getLastSpaceReference());
148  1 String pageName = documentReference.getName();
149  1 return new WikiPageReference(wikiName, spaceName, pageName);
150    }
151   
152    /**
153    * @param reference a wiki page reference
154    * @return the corresponding document reference
155    */
 
156  1 toggle public DocumentReference convert(WikiPageReference reference)
157    {
158  1 SpaceReference spaceReference =
159    this.spaceResolver.resolve(reference.getSpaceName(), new WikiReference(reference.getWikiName()));
160  1 return new DocumentReference(reference.getPageName(), spaceReference);
161    }
162   
163    /**
164    * @param attachmentReference an attachment reference
165    * @return the corresponding client attachment reference
166    */
 
167  1 toggle public org.xwiki.gwt.wysiwyg.client.wiki.AttachmentReference convert(AttachmentReference attachmentReference)
168    {
169  1 return new org.xwiki.gwt.wysiwyg.client.wiki.AttachmentReference(attachmentReference.getName(),
170    convert(attachmentReference.getDocumentReference()));
171    }
172   
173    /**
174    * @param clientAttachmentReference a client attachment reference
175    * @return the corresponding server-side attachment reference
176    */
 
177  1 toggle public AttachmentReference convert(org.xwiki.gwt.wysiwyg.client.wiki.AttachmentReference clientAttachmentReference)
178    {
179  1 return new AttachmentReference(clientAttachmentReference.getFileName(),
180    convert(clientAttachmentReference.getWikiPageReference()));
181    }
182    }