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

File SSXListener.java

 

Coverage histogram

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

Code metrics

6
15
3
1
110
72
7
0.47
5
3
2.33

Classes

Class Line # Actions
SSXListener 56 15 0% 7 0
1.0100%
 

Contributing tests

This file is covered by 3 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.lesscss.internal.listeners;
21   
22    import java.util.Arrays;
23    import java.util.List;
24   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27    import javax.inject.Singleton;
28   
29    import org.xwiki.bridge.event.DocumentCreatedEvent;
30    import org.xwiki.bridge.event.DocumentDeletedEvent;
31    import org.xwiki.bridge.event.DocumentUpdatedEvent;
32    import org.xwiki.component.annotation.Component;
33    import org.xwiki.lesscss.internal.cache.ColorThemeCache;
34    import org.xwiki.lesscss.internal.cache.LESSResourcesCache;
35    import org.xwiki.lesscss.resources.LESSResourceReference;
36    import org.xwiki.lesscss.resources.LESSResourceReferenceFactory;
37    import org.xwiki.model.reference.DocumentReference;
38    import org.xwiki.model.reference.ObjectPropertyReference;
39    import org.xwiki.observation.EventListener;
40    import org.xwiki.observation.event.Event;
41    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
42   
43    import com.xpn.xwiki.doc.XWikiDocument;
44    import com.xpn.xwiki.objects.BaseObject;
45    import com.xpn.xwiki.objects.BaseObjectReference;
46   
47    /**
48    * Listener that clears the cache of compiled LESS Skin resources when a skin extension object (SSX) is saved.
49    *
50    * @since 6.4M2
51    * @version $Id: 5b47b4621a8ecb4120d937686291d6b5a641d870 $
52    */
53    @Component
54    @Named("lessSSX")
55    @Singleton
 
56    public class SSXListener implements EventListener
57    {
58    @Inject
59    private LESSResourcesCache lessResourcesCache;
60   
61    @Inject
62    private ColorThemeCache colorThemeCache;
63   
64    @Inject
65    private WikiDescriptorManager wikiDescriptorManager;
66   
67    @Inject
68    private LESSResourceReferenceFactory lessResourceReferenceFactory;
69   
 
70  481 toggle @Override
71    public String getName()
72    {
73  481 return "LESS SSX objects listener";
74    }
75   
 
76  61 toggle @Override
77    public List<Event> getEvents()
78    {
79  61 return Arrays.<Event>asList(
80    new DocumentCreatedEvent(),
81    new DocumentUpdatedEvent(),
82    new DocumentDeletedEvent());
83    }
84   
 
85  3967 toggle @Override
86    public void onEvent(Event event, Object source, Object data)
87    {
88  3967 XWikiDocument document = (XWikiDocument) source;
89  3967 String currentWiki = wikiDescriptorManager.getCurrentWikiId();
90   
91  3967 DocumentReference ssxDocRef = new DocumentReference(currentWiki, "XWiki", "StyleSheetExtension");
92   
93  3967 List<BaseObject> ssxObjects = document.getXObjects(ssxDocRef);
94  3967 if (ssxObjects != null && !ssxObjects.isEmpty()) {
95  160 for (BaseObject obj : ssxObjects) {
96  171 if (obj == null) {
97  1 continue;
98    }
99  170 if ("LESS".equals(obj.getStringValue("contentType"))) {
100  2 ObjectPropertyReference objectPropertyReference = new ObjectPropertyReference("code",
101    new BaseObjectReference(ssxDocRef, obj.getNumber(), document.getDocumentReference()));
102  2 LESSResourceReference lessResourceReference =
103    lessResourceReferenceFactory.createReferenceForXObjectProperty(objectPropertyReference);
104  2 lessResourcesCache.clearFromLESSResource(lessResourceReference);
105  2 colorThemeCache.clearFromLESSResource(lessResourceReference);
106    }
107    }
108    }
109    }
110    }