1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.web.sx; |
21 |
|
|
22 |
|
import java.io.StringWriter; |
23 |
|
import java.util.List; |
24 |
|
|
25 |
|
import org.apache.commons.lang3.StringUtils; |
26 |
|
import org.apache.commons.lang3.exception.ExceptionUtils; |
27 |
|
import org.apache.velocity.VelocityContext; |
28 |
|
import org.slf4j.Logger; |
29 |
|
import org.slf4j.LoggerFactory; |
30 |
|
import org.xwiki.lesscss.compiler.LESSCompiler; |
31 |
|
import org.xwiki.lesscss.compiler.LESSCompilerException; |
32 |
|
import org.xwiki.lesscss.resources.LESSResourceReference; |
33 |
|
import org.xwiki.lesscss.resources.LESSResourceReferenceFactory; |
34 |
|
import org.xwiki.model.reference.ObjectPropertyReference; |
35 |
|
import org.xwiki.velocity.VelocityManager; |
36 |
|
import org.xwiki.velocity.XWikiVelocityException; |
37 |
|
|
38 |
|
import com.xpn.xwiki.XWikiContext; |
39 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
40 |
|
import com.xpn.xwiki.objects.BaseObject; |
41 |
|
import com.xpn.xwiki.objects.BaseObjectReference; |
42 |
|
import com.xpn.xwiki.web.Utils; |
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
@version |
49 |
|
@since |
50 |
|
|
|
|
| 88.1% |
Uncovered Elements: 7 (59) |
Complexity: 14 |
Complexity Density: 0.34 |
|
51 |
|
public class SxDocumentSource implements SxSource |
52 |
|
{ |
53 |
|
|
54 |
|
private static final String CONTENT_PROPERTY_NAME = "code"; |
55 |
|
|
56 |
|
|
57 |
|
private static final String CONTENT_TYPE_PROPERTY_NAME = "contentType"; |
58 |
|
|
59 |
|
|
60 |
|
private static final String PARSE_CONTENT_PROPERTY_NAME = "parse"; |
61 |
|
|
62 |
|
|
63 |
|
private static final String CACHE_POLICY_PROPERTY_NAME = "cache"; |
64 |
|
|
65 |
|
|
66 |
|
private static final String NAME_PROPERTY_NAME = "name"; |
67 |
|
|
68 |
|
|
69 |
|
private static final Logger LOGGER = LoggerFactory.getLogger(SxDocumentSource.class); |
70 |
|
|
71 |
|
|
72 |
|
private XWikiDocument document; |
73 |
|
|
74 |
|
|
75 |
|
private XWikiContext context; |
76 |
|
|
77 |
|
|
78 |
|
private Extension extension; |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
@param |
84 |
|
@param |
85 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
86 |
489 |
public SxDocumentSource(XWikiContext context, Extension extension)... |
87 |
|
{ |
88 |
488 |
this.context = context; |
89 |
486 |
this.document = context.getDoc(); |
90 |
487 |
this.extension = extension; |
91 |
|
} |
92 |
|
|
|
|
| 82.4% |
Uncovered Elements: 3 (17) |
Complexity: 5 |
Complexity Density: 0.45 |
|
93 |
489 |
@Override... |
94 |
|
public CachePolicy getCachePolicy() |
95 |
|
{ |
96 |
489 |
CachePolicy finalCache = CachePolicy.LONG; |
97 |
|
|
98 |
489 |
if (this.document.getObjects(this.extension.getClassName()) != null) { |
99 |
488 |
for (BaseObject sxObj : this.document.getObjects(this.extension.getClassName())) { |
100 |
502 |
if (sxObj == null) { |
101 |
0 |
continue; |
102 |
|
} |
103 |
502 |
try { |
104 |
502 |
CachePolicy cache = |
105 |
|
CachePolicy.valueOf(StringUtils.upperCase(StringUtils.defaultIfEmpty(sxObj |
106 |
|
.getStringValue(CACHE_POLICY_PROPERTY_NAME), "LONG"))); |
107 |
502 |
if (cache.compareTo(finalCache) > 0) { |
108 |
314 |
finalCache = cache; |
109 |
|
} |
110 |
|
} catch (Exception ex) { |
111 |
0 |
LOGGER.warn("SX object [{}#{}] has an invalid cache policy: [{}]", |
112 |
|
new Object[]{this.document.getFullName(), sxObj.getStringValue(NAME_PROPERTY_NAME), |
113 |
|
sxObj.getStringValue(CACHE_POLICY_PROPERTY_NAME)}); |
114 |
|
} |
115 |
|
} |
116 |
|
} |
117 |
489 |
return finalCache; |
118 |
|
} |
119 |
|
|
|
|
| 88.2% |
Uncovered Elements: 4 (34) |
Complexity: 7 |
Complexity Density: 0.27 |
|
120 |
487 |
@Override... |
121 |
|
public String getContent() |
122 |
|
{ |
123 |
488 |
StringBuilder resultBuilder = new StringBuilder(); |
124 |
|
|
125 |
488 |
List<BaseObject> objects = this.document.getObjects(this.extension.getClassName()); |
126 |
487 |
if (objects != null) { |
127 |
486 |
for (BaseObject sxObj : objects) { |
128 |
500 |
if (sxObj == null) { |
129 |
0 |
continue; |
130 |
|
} |
131 |
501 |
String sxContent = sxObj.getLargeStringValue(CONTENT_PROPERTY_NAME); |
132 |
501 |
int parse = sxObj.getIntValue(PARSE_CONTENT_PROPERTY_NAME); |
133 |
502 |
if ("LESS".equals(sxObj.getStringValue(CONTENT_TYPE_PROPERTY_NAME))) { |
134 |
1 |
LESSCompiler lessCompiler = Utils.getComponent(LESSCompiler.class); |
135 |
1 |
LESSResourceReferenceFactory lessResourceReferenceFactory = |
136 |
|
Utils.getComponent(LESSResourceReferenceFactory.class); |
137 |
1 |
ObjectPropertyReference objectPropertyReference = |
138 |
|
new ObjectPropertyReference(CONTENT_PROPERTY_NAME, new BaseObjectReference( |
139 |
|
sxObj.getXClassReference(), sxObj.getNumber(), sxObj.getDocumentReference())); |
140 |
1 |
LESSResourceReference lessResourceReference = |
141 |
|
lessResourceReferenceFactory.createReferenceForXObjectProperty(objectPropertyReference); |
142 |
1 |
try { |
143 |
1 |
sxContent = lessCompiler.compile(lessResourceReference, true, (parse == 1), false); |
144 |
|
} catch (LESSCompilerException e) { |
145 |
|
|
146 |
|
|
147 |
0 |
sxContent = String.format("/* LESS errors while parsing skin extension [%s]. */\n/* %s */", |
148 |
|
sxObj.getStringValue(NAME_PROPERTY_NAME), ExceptionUtils.getRootCauseMessage(e)); |
149 |
|
} |
150 |
500 |
} else if (parse == 1) { |
151 |
426 |
try { |
152 |
426 |
StringWriter writer = new StringWriter(); |
153 |
424 |
VelocityManager velocityManager = Utils.getComponent(VelocityManager.class); |
154 |
426 |
VelocityContext vcontext = velocityManager.getVelocityContext(); |
155 |
425 |
velocityManager.getVelocityEngine().evaluate(vcontext, writer, |
156 |
|
this.document.getPrefixedFullName(), sxContent); |
157 |
426 |
sxContent = writer.toString(); |
158 |
|
} catch (XWikiVelocityException ex) { |
159 |
0 |
LOGGER.warn("Velocity errors while parsing skin extension [{}] with content [{}]: ", |
160 |
|
this.document.getPrefixedFullName(), sxContent, ExceptionUtils.getRootCauseMessage(ex)); |
161 |
|
} |
162 |
|
} |
163 |
|
|
164 |
|
|
165 |
502 |
resultBuilder.append(sxContent + "\n"); |
166 |
|
} |
167 |
|
} |
168 |
489 |
return resultBuilder.toString(); |
169 |
|
} |
170 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
171 |
978 |
@Override... |
172 |
|
public long getLastModifiedDate() |
173 |
|
{ |
174 |
978 |
return this.document.getDate().getTime(); |
175 |
|
} |
176 |
|
|
177 |
|
} |