| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package com.xpn.xwiki.internal.filter; |
| 21 |
|
|
| 22 |
|
import java.io.IOException; |
| 23 |
|
|
| 24 |
|
import javax.inject.Inject; |
| 25 |
|
import javax.inject.Named; |
| 26 |
|
import javax.inject.Singleton; |
| 27 |
|
|
| 28 |
|
import org.xwiki.component.annotation.Component; |
| 29 |
|
import org.xwiki.component.manager.ComponentLookupException; |
| 30 |
|
import org.xwiki.component.manager.ComponentManager; |
| 31 |
|
import org.xwiki.component.util.DefaultParameterizedType; |
| 32 |
|
import org.xwiki.filter.FilterEventParameters; |
| 33 |
|
import org.xwiki.filter.FilterException; |
| 34 |
|
import org.xwiki.filter.event.model.WikiDocumentFilter; |
| 35 |
|
import org.xwiki.filter.input.BeanInputFilterStream; |
| 36 |
|
import org.xwiki.filter.input.BeanInputFilterStreamFactory; |
| 37 |
|
import org.xwiki.filter.input.InputFilterStreamFactory; |
| 38 |
|
import org.xwiki.filter.input.InputSource; |
| 39 |
|
import org.xwiki.filter.instance.input.BeanEntityEventGenerator; |
| 40 |
|
import org.xwiki.filter.instance.input.DocumentInstanceInputProperties; |
| 41 |
|
import org.xwiki.filter.instance.input.EntityEventGenerator; |
| 42 |
|
import org.xwiki.filter.instance.output.DocumentInstanceOutputProperties; |
| 43 |
|
import org.xwiki.filter.output.BeanOutputFilterStream; |
| 44 |
|
import org.xwiki.filter.output.BeanOutputFilterStreamFactory; |
| 45 |
|
import org.xwiki.filter.output.OutputFilterStreamFactory; |
| 46 |
|
import org.xwiki.filter.output.OutputTarget; |
| 47 |
|
import org.xwiki.filter.output.StringWriterOutputTarget; |
| 48 |
|
import org.xwiki.filter.output.WriterOutputTarget; |
| 49 |
|
import org.xwiki.filter.xar.input.XARInputProperties; |
| 50 |
|
import org.xwiki.filter.xar.input.XARInputProperties.SourceType; |
| 51 |
|
import org.xwiki.filter.xar.internal.XARFilter; |
| 52 |
|
import org.xwiki.filter.xar.internal.XARFilterUtils; |
| 53 |
|
import org.xwiki.filter.xar.output.XAROutputProperties; |
| 54 |
|
import org.xwiki.model.reference.DocumentReference; |
| 55 |
|
import org.xwiki.model.reference.EntityReference; |
| 56 |
|
import org.xwiki.model.reference.SpaceReference; |
| 57 |
|
|
| 58 |
|
import com.xpn.xwiki.doc.XWikiAttachment; |
| 59 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
| 60 |
|
import com.xpn.xwiki.internal.filter.output.EntityOutputFilterStream; |
| 61 |
|
import com.xpn.xwiki.internal.filter.output.XWikiDocumentOutputFilterStream; |
| 62 |
|
import com.xpn.xwiki.objects.BaseObject; |
| 63 |
|
import com.xpn.xwiki.objects.BaseProperty; |
| 64 |
|
import com.xpn.xwiki.objects.classes.BaseClass; |
| 65 |
|
import com.xpn.xwiki.objects.classes.PropertyClass; |
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
@version |
| 71 |
|
@since |
| 72 |
|
|
| 73 |
|
@Component(roles = XWikiDocumentFilterUtils.class) |
| 74 |
|
@Singleton |
| |
|
| 70.9% |
Uncovered Elements: 37 (127) |
Complexity: 30 |
Complexity Density: 0.38 |
|
| 75 |
|
public class XWikiDocumentFilterUtils |
| 76 |
|
{ |
| 77 |
|
@Inject |
| 78 |
|
@Named(XARFilterUtils.ROLEHINT_CURRENT) |
| 79 |
|
private InputFilterStreamFactory xarInputFilterStreamFactory; |
| 80 |
|
|
| 81 |
|
@Inject |
| 82 |
|
@Named(XARFilterUtils.ROLEHINT_CURRENT) |
| 83 |
|
private OutputFilterStreamFactory xarOutputFilterStreamFactory; |
| 84 |
|
|
| 85 |
|
@Inject |
| 86 |
|
private ComponentManager componentManager; |
| 87 |
|
|
| 88 |
|
|
| 89 |
|
|
| |
|
| 48.4% |
Uncovered Elements: 16 (31) |
Complexity: 8 |
Complexity Density: 0.47 |
|
| 90 |
9338 |
private <T> Class<T> getClass(Object entity)... |
| 91 |
|
{ |
| 92 |
9338 |
Class<T> entityClass; |
| 93 |
|
|
| 94 |
9338 |
if (entity instanceof Class) { |
| 95 |
1 |
entityClass = (Class<T>) entity; |
| 96 |
9336 |
} else if (entity instanceof XWikiDocument) { |
| 97 |
6332 |
entityClass = (Class<T>) XWikiDocument.class; |
| 98 |
3005 |
} else if (entity instanceof XWikiAttachment) { |
| 99 |
0 |
entityClass = (Class<T>) XWikiAttachment.class; |
| 100 |
3005 |
} else if (entity instanceof BaseClass) { |
| 101 |
3005 |
entityClass = (Class<T>) BaseClass.class; |
| 102 |
0 |
} else if (entity instanceof BaseObject) { |
| 103 |
0 |
entityClass = (Class<T>) BaseObject.class; |
| 104 |
0 |
} else if (entity instanceof BaseProperty) { |
| 105 |
0 |
entityClass = (Class<T>) BaseProperty.class; |
| 106 |
0 |
} else if (entity instanceof PropertyClass) { |
| 107 |
0 |
entityClass = (Class<T>) PropertyClass.class; |
| 108 |
|
} else { |
| 109 |
0 |
entityClass = (Class<T>) entity.getClass(); |
| 110 |
|
} |
| 111 |
|
|
| 112 |
9338 |
return entityClass; |
| 113 |
|
} |
| 114 |
|
|
| |
|
| 40.7% |
Uncovered Elements: 16 (27) |
Complexity: 7 |
Complexity Density: 0.47 |
|
| 115 |
4268 |
private SourceType getSourceType(Class<?> entityClass) throws FilterException... |
| 116 |
|
{ |
| 117 |
4268 |
SourceType sourceType; |
| 118 |
|
|
| 119 |
4268 |
if (entityClass == XWikiDocument.class) { |
| 120 |
2523 |
sourceType = SourceType.DOCUMENT; |
| 121 |
1745 |
} else if (entityClass == XWikiAttachment.class) { |
| 122 |
0 |
sourceType = SourceType.ATTACHMENT; |
| 123 |
1745 |
} else if (entityClass == BaseClass.class) { |
| 124 |
1745 |
sourceType = SourceType.CLASS; |
| 125 |
0 |
} else if (entityClass == BaseObject.class) { |
| 126 |
0 |
sourceType = SourceType.OBJECT; |
| 127 |
0 |
} else if (entityClass == BaseProperty.class) { |
| 128 |
0 |
sourceType = SourceType.OBJECTPROPERTY; |
| 129 |
0 |
} else if (entityClass == PropertyClass.class) { |
| 130 |
0 |
sourceType = SourceType.CLASSPROPERTY; |
| 131 |
|
} else { |
| 132 |
0 |
throw new FilterException("Unsupported type [" + entityClass + "]"); |
| 133 |
|
} |
| 134 |
|
|
| 135 |
4268 |
return sourceType; |
| 136 |
|
} |
| 137 |
|
|
| 138 |
|
|
| 139 |
|
@param |
| 140 |
|
@param |
| 141 |
|
@return |
| 142 |
|
@throws |
| 143 |
|
@throws |
| 144 |
|
@throws |
| 145 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 146 |
1745 |
public <T> T importEntity(Object entity, InputSource source)... |
| 147 |
|
throws FilterException, IOException, ComponentLookupException |
| 148 |
|
{ |
| 149 |
|
|
| 150 |
1745 |
DocumentInstanceOutputProperties documentProperties = new DocumentInstanceOutputProperties(); |
| 151 |
|
|
| 152 |
|
|
| 153 |
1745 |
XARInputProperties xarProperties = new XARInputProperties(); |
| 154 |
|
|
| 155 |
1745 |
return importEntity(getClass(entity), entity instanceof Class ? null : (T) entity, source, xarProperties, |
| 156 |
|
documentProperties); |
| 157 |
|
} |
| 158 |
|
|
| 159 |
|
|
| 160 |
|
@param@link |
| 161 |
|
@param |
| 162 |
|
@param |
| 163 |
|
@param |
| 164 |
|
@param |
| 165 |
|
@return |
| 166 |
|
@throws |
| 167 |
|
@throws |
| 168 |
|
@throws |
| 169 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 2 |
Complexity Density: 0.18 |
|
| 170 |
4268 |
public <T> T importEntity(Class<T> entityClass, T entity, InputSource source, XARInputProperties xarProperties,... |
| 171 |
|
DocumentInstanceOutputProperties documentProperties) |
| 172 |
|
throws FilterException, IOException, ComponentLookupException |
| 173 |
|
{ |
| 174 |
|
|
| 175 |
4268 |
EntityOutputFilterStream<T> filterStream = this.componentManager |
| 176 |
|
.getInstance(new DefaultParameterizedType(null, EntityOutputFilterStream.class, entityClass)); |
| 177 |
4268 |
filterStream.setProperties(documentProperties); |
| 178 |
4268 |
filterStream.setEntity(entity); |
| 179 |
4268 |
if (filterStream instanceof XWikiDocumentOutputFilterStream) { |
| 180 |
2523 |
((XWikiDocumentOutputFilterStream) filterStream).disableRenderingEvents(); |
| 181 |
|
} |
| 182 |
|
|
| 183 |
|
|
| 184 |
4268 |
xarProperties.setSourceType(getSourceType(entityClass)); |
| 185 |
4267 |
xarProperties.setSource(source); |
| 186 |
4268 |
BeanInputFilterStream<XARInputProperties> xarReader = |
| 187 |
|
((BeanInputFilterStreamFactory<XARInputProperties>) this.xarInputFilterStreamFactory) |
| 188 |
|
.createInputFilterStream(xarProperties); |
| 189 |
|
|
| 190 |
|
|
| 191 |
4268 |
xarReader.read(filterStream.getFilter()); |
| 192 |
|
|
| 193 |
4268 |
xarReader.close(); |
| 194 |
|
|
| 195 |
4267 |
return filterStream.getEntity(); |
| 196 |
|
} |
| 197 |
|
|
| 198 |
|
|
| 199 |
|
@param |
| 200 |
|
@param |
| 201 |
|
@param |
| 202 |
|
@return |
| 203 |
|
@throws |
| 204 |
|
@throws |
| 205 |
|
@throws |
| 206 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 207 |
496 |
public XWikiDocument importDocument(InputSource source, XARInputProperties xarProperties,... |
| 208 |
|
DocumentInstanceOutputProperties documentProperties) |
| 209 |
|
throws FilterException, IOException, ComponentLookupException |
| 210 |
|
{ |
| 211 |
496 |
return importEntity(XWikiDocument.class, null, source, xarProperties, documentProperties); |
| 212 |
|
} |
| 213 |
|
|
| 214 |
|
|
| 215 |
|
|
| 216 |
|
|
| 217 |
|
@param |
| 218 |
|
@return |
| 219 |
|
@throws |
| 220 |
|
@throws |
| 221 |
|
@throws |
| 222 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 223 |
1 |
public String exportEntity(Object entity) throws ComponentLookupException, FilterException, IOException... |
| 224 |
|
{ |
| 225 |
1 |
return exportEntity(entity, new XAROutputProperties()); |
| 226 |
|
} |
| 227 |
|
|
| 228 |
|
|
| 229 |
|
@param |
| 230 |
|
@param |
| 231 |
|
@return |
| 232 |
|
@throws |
| 233 |
|
@throws |
| 234 |
|
@throws |
| 235 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 236 |
1261 |
public String exportEntity(Object entity, XAROutputProperties xarProperties)... |
| 237 |
|
throws ComponentLookupException, FilterException, IOException |
| 238 |
|
{ |
| 239 |
1261 |
return exportEntity(entity, xarProperties, new DocumentInstanceInputProperties()); |
| 240 |
|
} |
| 241 |
|
|
| 242 |
|
|
| 243 |
|
@param |
| 244 |
|
@param |
| 245 |
|
@throws |
| 246 |
|
@throws |
| 247 |
|
@throws |
| 248 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 249 |
0 |
public void exportEntity(Object entity, OutputTarget target)... |
| 250 |
|
throws ComponentLookupException, FilterException, IOException |
| 251 |
|
{ |
| 252 |
0 |
exportEntity(entity, target, new DocumentInstanceInputProperties()); |
| 253 |
|
} |
| 254 |
|
|
| 255 |
|
|
| 256 |
|
@param |
| 257 |
|
@param |
| 258 |
|
@param |
| 259 |
|
@throws |
| 260 |
|
@throws |
| 261 |
|
@throws |
| 262 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 263 |
0 |
public void exportEntity(Object entity, OutputTarget target, DocumentInstanceInputProperties documentProperties)... |
| 264 |
|
throws ComponentLookupException, FilterException, IOException |
| 265 |
|
{ |
| 266 |
0 |
exportEntity(entity, target, new XAROutputProperties(), documentProperties); |
| 267 |
|
} |
| 268 |
|
|
| 269 |
|
|
| 270 |
|
@param |
| 271 |
|
@param |
| 272 |
|
@param |
| 273 |
|
@return |
| 274 |
|
@throws |
| 275 |
|
@throws |
| 276 |
|
@throws |
| 277 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 278 |
1261 |
public String exportEntity(Object entity, XAROutputProperties xarProperties,... |
| 279 |
|
DocumentInstanceInputProperties documentProperties) |
| 280 |
|
throws ComponentLookupException, FilterException, IOException |
| 281 |
|
{ |
| 282 |
1261 |
WriterOutputTarget target = new StringWriterOutputTarget(); |
| 283 |
|
|
| 284 |
1261 |
exportEntity(entity, target, xarProperties, documentProperties); |
| 285 |
|
|
| 286 |
1261 |
return target.toString(); |
| 287 |
|
} |
| 288 |
|
|
| 289 |
|
|
| 290 |
|
@param |
| 291 |
|
@param |
| 292 |
|
@param |
| 293 |
|
@param |
| 294 |
|
@throws |
| 295 |
|
@throws |
| 296 |
|
@throws |
| 297 |
|
|
| |
|
| 96.9% |
Uncovered Elements: 1 (32) |
Complexity: 5 |
Complexity Density: 0.21 |
|
| 298 |
7593 |
public void exportEntity(Object entity, OutputTarget target, XAROutputProperties xarProperties,... |
| 299 |
|
DocumentInstanceInputProperties documentProperties) |
| 300 |
|
throws ComponentLookupException, FilterException, IOException |
| 301 |
|
{ |
| 302 |
|
|
| 303 |
7593 |
documentProperties.setVerbose(false); |
| 304 |
|
|
| 305 |
|
|
| 306 |
7593 |
xarProperties.setForceDocument(true); |
| 307 |
7593 |
if (target != null) { |
| 308 |
7593 |
xarProperties.setTarget(target); |
| 309 |
|
} |
| 310 |
7593 |
xarProperties.setVerbose(false); |
| 311 |
7593 |
BeanOutputFilterStream<XAROutputProperties> xarFilter = |
| 312 |
|
((BeanOutputFilterStreamFactory<XAROutputProperties>) this.xarOutputFilterStreamFactory) |
| 313 |
|
.createOutputFilterStream(xarProperties); |
| 314 |
7593 |
XARFilter filter = (XARFilter) xarFilter.getFilter(); |
| 315 |
|
|
| 316 |
7593 |
BeanEntityEventGenerator<Object, DocumentInstanceInputProperties> generator = this.componentManager |
| 317 |
|
.getInstance(new DefaultParameterizedType(null, EntityEventGenerator.class, getClass(entity))); |
| 318 |
|
|
| 319 |
|
|
| 320 |
7593 |
FilterEventParameters documentParameters = null; |
| 321 |
7593 |
DocumentReference documentReference = null; |
| 322 |
7593 |
if (entity instanceof XWikiDocument) { |
| 323 |
6332 |
documentReference = ((XWikiDocument) entity).getDocumentReference(); |
| 324 |
6332 |
for (SpaceReference spaceReference : documentReference.getSpaceReferences()) { |
| 325 |
6558 |
filter.beginWikiSpace(spaceReference.getName(), FilterEventParameters.EMPTY); |
| 326 |
|
} |
| 327 |
|
|
| 328 |
6332 |
documentParameters = new FilterEventParameters(); |
| 329 |
6332 |
documentParameters.put(WikiDocumentFilter.PARAMETER_LOCALE, ((XWikiDocument) entity).getDefaultLocale()); |
| 330 |
6332 |
filter.beginWikiDocument(documentReference.getName(), documentParameters); |
| 331 |
|
} |
| 332 |
|
|
| 333 |
|
|
| 334 |
7593 |
generator.write(entity, xarFilter, documentProperties); |
| 335 |
|
|
| 336 |
|
|
| 337 |
7593 |
if (documentParameters != null) { |
| 338 |
6332 |
filter.endWikiDocument(documentReference.getName(), documentParameters); |
| 339 |
|
|
| 340 |
6332 |
documentReference = ((XWikiDocument) entity).getDocumentReference(); |
| 341 |
6332 |
for (EntityReference reference = |
| 342 |
12890 |
documentReference.getParent(); reference instanceof SpaceReference; reference = reference.getParent()) { |
| 343 |
6558 |
filter.beginWikiSpace(reference.getName(), FilterEventParameters.EMPTY); |
| 344 |
|
} |
| 345 |
|
} |
| 346 |
|
|
| 347 |
7593 |
xarFilter.close(); |
| 348 |
|
} |
| 349 |
|
} |