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

File AbstractSpacesConfigurationSource.java

 

Coverage histogram

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

Code metrics

12
50
14
3
209
148
20
0.4
3.57
4.67
1.43

Classes

Class Line # Actions
AbstractSpacesConfigurationSource 42 6 0% 3 0
1.0100%
AbstractSpacesConfigurationSource.SpaceIterator 47 10 0% 6 4
0.777777877.8%
AbstractSpacesConfigurationSource.SpaceConfigurationSource 90 34 0% 11 2
0.957446895.7%
 

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.configuration.internal;
21   
22    import java.util.Collections;
23    import java.util.Iterator;
24    import java.util.List;
25   
26    import javax.inject.Inject;
27    import javax.inject.Provider;
28   
29    import org.xwiki.configuration.ConfigurationSource;
30    import org.xwiki.model.reference.DocumentReference;
31    import org.xwiki.model.reference.SpaceReference;
32   
33    import com.xpn.xwiki.XWikiContext;
34    import com.xpn.xwiki.doc.XWikiDocument;
35   
36    /**
37    * Abstract composite configuration source that looks in the current space and all its parent spaces.
38    *
39    * @version $Id: 6a384e8002f767bc169ef896a538412e6330bf66 $
40    * @since 8.2RC1
41    */
 
42    public abstract class AbstractSpacesConfigurationSource extends AbstractCompositeConfigurationSource
43    {
44    @Inject
45    private Provider<XWikiContext> xcontextProvider;
46   
 
47    private class SpaceIterator implements Iterator<ConfigurationSource>
48    {
49    private SpaceReference reference;
50   
 
51  86902 toggle SpaceIterator(SpaceReference reference)
52    {
53  86893 this.reference = reference;
54    }
55   
 
56  183997 toggle @Override
57    public boolean hasNext()
58    {
59  183996 return this.reference != null;
60    }
61   
 
62  97090 toggle @Override
63    public ConfigurationSource next()
64    {
65  97102 SpaceReference next = this.reference;
66   
67  97113 if (this.reference != null) {
68    // Move reference to parent
69  97123 if (this.reference.getParent() instanceof SpaceReference) {
70  10231 this.reference = (SpaceReference) this.reference.getParent();
71    } else {
72  86893 this.reference = null;
73    }
74   
75    // Return wrapped space configuration associated to the space reference
76  97115 return new SpaceConfigurationSource(next);
77    }
78   
79  0 return null;
80    }
81   
 
82  0 toggle @Override
83    public void remove()
84    {
85  0 throw new UnsupportedOperationException();
86    }
87   
88    }
89   
 
90    private class SpaceConfigurationSource implements ConfigurationSource
91    {
92    private SpaceReference reference;
93   
94    private XWikiDocument document;
95   
 
96  97110 toggle SpaceConfigurationSource(SpaceReference reference)
97    {
98  97118 this.reference = reference;
99    }
100   
 
101  194269 toggle private XWikiDocument setCurrentDocument(XWikiDocument document)
102    {
103  194275 XWikiContext xcontext = xcontextProvider.get();
104  194300 if (xcontext != null) {
105  194289 XWikiDocument currentDocument = xcontext.getDoc();
106  194292 xcontext.setDoc(document);
107  194293 return currentDocument;
108    }
109   
110  0 return null;
111    }
112   
 
113  97104 toggle public XWikiDocument getDocument()
114    {
115  97114 if (this.document == null) {
116  97097 this.document = new XWikiDocument(
117    new DocumentReference(SpacePreferencesConfigurationSource.DOCUMENT_NAME, this.reference));
118    }
119   
120  97146 return this.document;
121    }
122   
 
123  2 toggle @Override
124    public <T> T getProperty(String key, T defaultValue)
125    {
126  2 XWikiDocument currentDocument = setCurrentDocument(getDocument());
127  2 try {
128  2 return getSpaceConfigurationSource().getProperty(key, defaultValue);
129    } finally {
130  2 setCurrentDocument(currentDocument);
131    }
132    }
133   
 
134  2 toggle @Override
135    public <T> T getProperty(String key, Class<T> valueClass)
136    {
137  2 XWikiDocument currentDocument = setCurrentDocument(getDocument());
138  2 try {
139  2 return getSpaceConfigurationSource().getProperty(key, valueClass);
140    } finally {
141  2 setCurrentDocument(currentDocument);
142    }
143    }
144   
 
145  5 toggle @Override
146    public <T> T getProperty(String key)
147    {
148  5 XWikiDocument currentDocument = setCurrentDocument(getDocument());
149  5 try {
150  5 return getSpaceConfigurationSource().getProperty(key);
151    } finally {
152  5 setCurrentDocument(currentDocument);
153    }
154    }
155   
 
156  3 toggle @Override
157    public List<String> getKeys()
158    {
159  3 XWikiDocument currentDocument = setCurrentDocument(getDocument());
160  3 try {
161  3 return getSpaceConfigurationSource().getKeys();
162    } finally {
163  3 setCurrentDocument(currentDocument);
164    }
165    }
166   
 
167  97097 toggle @Override
168    public boolean containsKey(String key)
169    {
170  97105 XWikiDocument currentDocument = setCurrentDocument(getDocument());
171  97118 try {
172  97127 return getSpaceConfigurationSource().containsKey(key);
173    } finally {
174  97129 setCurrentDocument(currentDocument);
175    }
176    }
177   
 
178  2 toggle @Override
179    public boolean isEmpty()
180    {
181  2 XWikiDocument currentDocument = setCurrentDocument(getDocument());
182  2 try {
183  2 return getSpaceConfigurationSource().isEmpty();
184    } finally {
185  2 setCurrentDocument(currentDocument);
186    }
187    }
188    }
189   
 
190  212646 toggle @Override
191    public Iterator<ConfigurationSource> iterator()
192    {
193  212628 XWikiContext xcontext = this.xcontextProvider.get();
194   
195  212634 if (xcontext != null) {
196  146055 XWikiDocument currentDocument = xcontext.getDoc();
197  146037 if (currentDocument != null) {
198  86897 return new SpaceIterator(currentDocument.getDocumentReference().getLastSpaceReference());
199    }
200    }
201   
202  125717 return Collections.<ConfigurationSource>emptyList().iterator();
203    }
204   
205    /**
206    * @return the space configuration source to use at each level
207    */
208    protected abstract ConfigurationSource getSpaceConfigurationSource();
209    }