| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.wysiwyg.server.internal.plugin.macro; |
| 21 |
|
|
| 22 |
|
import java.lang.reflect.Type; |
| 23 |
|
import java.util.ArrayList; |
| 24 |
|
import java.util.Collections; |
| 25 |
|
import java.util.Comparator; |
| 26 |
|
import java.util.LinkedHashMap; |
| 27 |
|
import java.util.List; |
| 28 |
|
import java.util.Map; |
| 29 |
|
|
| 30 |
|
import javax.inject.Inject; |
| 31 |
|
import javax.inject.Provider; |
| 32 |
|
import javax.inject.Singleton; |
| 33 |
|
|
| 34 |
|
import org.apache.commons.lang3.StringUtils; |
| 35 |
|
import org.xwiki.component.annotation.Component; |
| 36 |
|
import org.xwiki.gwt.wysiwyg.client.plugin.macro.MacroDescriptor; |
| 37 |
|
import org.xwiki.gwt.wysiwyg.client.plugin.macro.MacroService; |
| 38 |
|
import org.xwiki.gwt.wysiwyg.client.plugin.macro.ParameterDescriptor; |
| 39 |
|
import org.xwiki.gwt.wysiwyg.client.plugin.macro.ParameterType; |
| 40 |
|
import org.xwiki.rendering.macro.Macro; |
| 41 |
|
import org.xwiki.rendering.macro.MacroCategoryManager; |
| 42 |
|
import org.xwiki.rendering.macro.MacroId; |
| 43 |
|
import org.xwiki.rendering.macro.MacroManager; |
| 44 |
|
import org.xwiki.rendering.syntax.Syntax; |
| 45 |
|
import org.xwiki.rendering.syntax.SyntaxFactory; |
| 46 |
|
import org.xwiki.wysiwyg.server.plugin.macro.MacroDescriptorTranslator; |
| 47 |
|
|
| 48 |
|
import com.xpn.xwiki.XWikiContext; |
| 49 |
|
|
| 50 |
|
|
| 51 |
|
@link |
| 52 |
|
|
| 53 |
|
@version |
| 54 |
|
|
| 55 |
|
@Component |
| 56 |
|
@Singleton |
| |
|
| 55.4% |
Uncovered Elements: 50 (112) |
Complexity: 22 |
Complexity Density: 0.28 |
|
| 57 |
|
public class XWikiMacroService implements MacroService |
| 58 |
|
{ |
| 59 |
|
|
| 60 |
|
@link |
| 61 |
|
|
| 62 |
|
@Inject |
| 63 |
|
private SyntaxFactory syntaxFactory; |
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
@Inject |
| 69 |
|
private MacroManager macroManager; |
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
@Inject |
| 75 |
|
private MacroCategoryManager categoryManager; |
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
@Inject |
| 81 |
|
private MacroDescriptorTranslator macroDescriptorTranslator; |
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
@Inject |
| 87 |
|
private Provider<XWikiContext> xcontextProvider; |
| 88 |
|
|
| |
|
| 70.6% |
Uncovered Elements: 5 (17) |
Complexity: 4 |
Complexity Density: 0.36 |
|
| 89 |
1 |
@Override... |
| 90 |
|
public MacroDescriptor getMacroDescriptor(String macroId, String syntaxId, String wikiId) |
| 91 |
|
{ |
| 92 |
1 |
XWikiContext xcontext = xcontextProvider.get(); |
| 93 |
|
|
| 94 |
1 |
String newWikiId = wikiId; |
| 95 |
1 |
if (newWikiId == null) { |
| 96 |
1 |
newWikiId = xcontext.getWikiId(); |
| 97 |
|
} |
| 98 |
|
|
| 99 |
1 |
String oldWikiId = xcontext.getWikiId(); |
| 100 |
1 |
if (oldWikiId != newWikiId) { |
| 101 |
|
|
| 102 |
0 |
xcontext.setWikiId(newWikiId); |
| 103 |
|
} |
| 104 |
|
|
| 105 |
1 |
try { |
| 106 |
1 |
return macroDescriptorTranslator.translate(getUntranslatedMacroDescriptor(macroId, syntaxId)); |
| 107 |
|
} finally { |
| 108 |
|
|
| 109 |
1 |
if (oldWikiId != newWikiId) { |
| 110 |
0 |
xcontext.setWikiId(oldWikiId); |
| 111 |
|
} |
| 112 |
|
} |
| 113 |
|
} |
| 114 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 115 |
1 |
@Override... |
| 116 |
|
public MacroDescriptor getMacroDescriptor(String macroId, String syntaxId) |
| 117 |
|
{ |
| 118 |
1 |
return getMacroDescriptor(macroId, syntaxId, null); |
| 119 |
|
} |
| 120 |
|
|
| 121 |
|
|
| 122 |
|
@param |
| 123 |
|
@param |
| 124 |
|
@return |
| 125 |
|
|
| |
|
| 92.3% |
Uncovered Elements: 2 (26) |
Complexity: 3 |
Complexity Density: 0.12 |
|
| 126 |
1 |
private MacroDescriptor getUntranslatedMacroDescriptor(String macroId, String syntaxId)... |
| 127 |
|
{ |
| 128 |
1 |
try { |
| 129 |
1 |
MacroId macroIdObject = new MacroId(macroId, syntaxFactory.createSyntaxFromIdString(syntaxId)); |
| 130 |
1 |
Macro<?> macro = macroManager.getMacro(macroIdObject); |
| 131 |
1 |
org.xwiki.rendering.macro.descriptor.MacroDescriptor descriptor = macro.getDescriptor(); |
| 132 |
|
|
| 133 |
1 |
ParameterDescriptor contentDescriptor = null; |
| 134 |
1 |
if (descriptor.getContentDescriptor() != null) { |
| 135 |
1 |
contentDescriptor = new ParameterDescriptor(); |
| 136 |
1 |
contentDescriptor.setId("content"); |
| 137 |
1 |
contentDescriptor.setName("Content"); |
| 138 |
1 |
contentDescriptor.setDescription(descriptor.getContentDescriptor().getDescription()); |
| 139 |
|
|
| 140 |
1 |
contentDescriptor.setType(createMacroParameterType(StringBuffer.class)); |
| 141 |
1 |
contentDescriptor.setMandatory(descriptor.getContentDescriptor().isMandatory()); |
| 142 |
|
} |
| 143 |
|
|
| 144 |
|
|
| 145 |
1 |
Map<String, ParameterDescriptor> parameterDescriptorMap = new LinkedHashMap<String, ParameterDescriptor>(); |
| 146 |
1 |
for (Map.Entry<String, org.xwiki.rendering.macro.descriptor.ParameterDescriptor> entry : descriptor |
| 147 |
|
.getParameterDescriptorMap().entrySet()) { |
| 148 |
1 |
parameterDescriptorMap.put(entry.getKey(), createMacroParameterDescriptor(entry.getValue())); |
| 149 |
|
} |
| 150 |
|
|
| 151 |
1 |
MacroDescriptor result = new MacroDescriptor(); |
| 152 |
1 |
result.setId(macroIdObject.getId()); |
| 153 |
1 |
result.setName(descriptor.getName()); |
| 154 |
1 |
result.setDescription(descriptor.getDescription()); |
| 155 |
1 |
result.setSupportingInlineMode(macro.supportsInlineMode()); |
| 156 |
|
|
| 157 |
1 |
result.setContentDescriptor(contentDescriptor); |
| 158 |
1 |
result.setParameterDescriptorMap(parameterDescriptorMap); |
| 159 |
|
|
| 160 |
1 |
return result; |
| 161 |
|
} catch (Exception e) { |
| 162 |
0 |
throw new RuntimeException("Exception while retrieving macro descriptor.", e); |
| 163 |
|
} |
| 164 |
|
} |
| 165 |
|
|
| 166 |
|
|
| 167 |
|
@link@link |
| 168 |
|
|
| 169 |
|
@param |
| 170 |
|
@return |
| 171 |
|
|
| |
|
| 85.7% |
Uncovered Elements: 2 (14) |
Complexity: 3 |
Complexity Density: 0.3 |
|
| 172 |
1 |
private ParameterDescriptor createMacroParameterDescriptor(... |
| 173 |
|
org.xwiki.rendering.macro.descriptor.ParameterDescriptor descriptor) |
| 174 |
|
{ |
| 175 |
1 |
ParameterDescriptor result = new ParameterDescriptor(); |
| 176 |
1 |
result.setId(descriptor.getId()); |
| 177 |
|
|
| 178 |
|
|
| 179 |
1 |
result.setName(StringUtils.isBlank(descriptor.getName()) ? descriptor.getId() : descriptor.getName()); |
| 180 |
1 |
result.setDescription(descriptor.getDescription()); |
| 181 |
1 |
result.setType(createMacroParameterType(descriptor.getParameterType())); |
| 182 |
1 |
Object defaultValue = descriptor.getDefaultValue(); |
| 183 |
1 |
if (defaultValue != null) { |
| 184 |
1 |
result.setDefaultValue(String.valueOf(defaultValue)); |
| 185 |
|
} |
| 186 |
1 |
result.setMandatory(descriptor.isMandatory()); |
| 187 |
1 |
return result; |
| 188 |
|
} |
| 189 |
|
|
| 190 |
|
|
| 191 |
|
@link |
| 192 |
|
@link |
| 193 |
|
|
| 194 |
|
@param |
| 195 |
|
@return |
| 196 |
|
|
| |
|
| 44.4% |
Uncovered Elements: 10 (18) |
Complexity: 4 |
Complexity Density: 0.33 |
|
| 197 |
2 |
private ParameterType createMacroParameterType(Type type)... |
| 198 |
|
{ |
| 199 |
2 |
ParameterType parameterType = new ParameterType(); |
| 200 |
2 |
if (type instanceof Class) { |
| 201 |
2 |
Class<?> parameterClass = (Class<?>) type; |
| 202 |
2 |
parameterType.setName(parameterClass.getName()); |
| 203 |
2 |
if (parameterClass.isEnum()) { |
| 204 |
0 |
Object[] parameterClassConstants = parameterClass.getEnumConstants(); |
| 205 |
0 |
Map<String, String> parameterTypeConstants = new LinkedHashMap<String, String>(); |
| 206 |
0 |
for (int i = 0; i < parameterClassConstants.length; i++) { |
| 207 |
0 |
String constant = String.valueOf(parameterClassConstants[i]); |
| 208 |
|
|
| 209 |
0 |
parameterTypeConstants.put(constant, constant); |
| 210 |
|
} |
| 211 |
0 |
parameterType.setEnumConstants(parameterTypeConstants); |
| 212 |
|
} |
| 213 |
|
} |
| 214 |
2 |
return parameterType; |
| 215 |
|
} |
| 216 |
|
|
| |
|
| 0% |
Uncovered Elements: 26 (26) |
Complexity: 5 |
Complexity Density: 0.25 |
|
| 217 |
0 |
@Override... |
| 218 |
|
public List<MacroDescriptor> getMacroDescriptors(String syntaxId, String wikiId) |
| 219 |
|
{ |
| 220 |
0 |
XWikiContext xcontext = xcontextProvider.get(); |
| 221 |
|
|
| 222 |
0 |
String newWikiId = wikiId; |
| 223 |
0 |
if (newWikiId == null) { |
| 224 |
0 |
newWikiId = xcontext.getWikiId(); |
| 225 |
|
} |
| 226 |
|
|
| 227 |
0 |
String oldWikiId = xcontext.getWikiId(); |
| 228 |
0 |
if (oldWikiId != newWikiId) { |
| 229 |
|
|
| 230 |
0 |
xcontext.setWikiId(newWikiId); |
| 231 |
|
} |
| 232 |
|
|
| 233 |
0 |
try { |
| 234 |
0 |
Syntax syntax = syntaxFactory.createSyntaxFromIdString(syntaxId); |
| 235 |
0 |
List<MacroDescriptor> descriptors = new ArrayList<MacroDescriptor>(); |
| 236 |
0 |
for (String category : categoryManager.getMacroCategories(syntax)) { |
| 237 |
0 |
for (MacroId macroId : categoryManager.getMacroIds(category, syntax)) { |
| 238 |
0 |
MacroDescriptor descriptor = getUntranslatedMacroDescriptor(macroId.getId(), syntaxId); |
| 239 |
0 |
descriptor.setCategory(category); |
| 240 |
0 |
descriptors.add(macroDescriptorTranslator.translate(descriptor)); |
| 241 |
|
} |
| 242 |
|
} |
| 243 |
|
|
| 244 |
0 |
Collections.sort(descriptors, new Comparator<MacroDescriptor>() |
| 245 |
|
{ |
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 246 |
0 |
public int compare(MacroDescriptor alice, MacroDescriptor bob)... |
| 247 |
|
{ |
| 248 |
0 |
return alice.getName().compareTo(bob.getName()); |
| 249 |
|
} |
| 250 |
|
}); |
| 251 |
|
|
| 252 |
0 |
return descriptors; |
| 253 |
|
} catch (Exception e) { |
| 254 |
0 |
throw new RuntimeException("Exception while retrieving the list of macro descriptors for syntax [" |
| 255 |
|
+ syntaxId + "].", e); |
| 256 |
|
} finally { |
| 257 |
|
|
| 258 |
0 |
if (oldWikiId != newWikiId) { |
| 259 |
0 |
xcontext.setWikiId(oldWikiId); |
| 260 |
|
} |
| 261 |
|
} |
| 262 |
|
} |
| 263 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 264 |
0 |
@Override... |
| 265 |
|
public List<MacroDescriptor> getMacroDescriptors(String syntaxId) |
| 266 |
|
{ |
| 267 |
0 |
return getMacroDescriptors(syntaxId, null); |
| 268 |
|
} |
| 269 |
|
} |