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

File WikiReader.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart7.png
64% of files have more coverage

Code metrics

20
30
4
1
149
101
18
0.6
7.5
4
4.5

Classes

Class Line # Actions
WikiReader 51 30 0% 18 17
0.685185268.5%
 

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.filter.xar.internal.input;
21   
22    import java.io.IOException;
23    import java.io.InputStream;
24   
25    import javax.inject.Inject;
26   
27    import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
28    import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
29    import org.apache.commons.lang3.exception.ExceptionUtils;
30    import org.slf4j.Logger;
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.component.annotation.InstantiationStrategy;
33    import org.xwiki.component.descriptor.ComponentInstantiationStrategy;
34    import org.xwiki.filter.FilterEventParameters;
35    import org.xwiki.filter.FilterException;
36    import org.xwiki.filter.event.model.WikiDocumentFilter;
37    import org.xwiki.filter.input.InputSource;
38    import org.xwiki.filter.input.InputStreamInputSource;
39    import org.xwiki.filter.xar.input.XARInputProperties;
40    import org.xwiki.logging.marker.TranslationMarker;
41    import org.xwiki.model.reference.EntityReference;
42    import org.xwiki.xar.XarPackage;
43    import org.xwiki.xar.internal.model.XarModel;
44   
45    /**
46    * @version $Id: c3b8bf4c97c0938409fc4acf2772f8342500ef68 $
47    * @since 6.2M1
48    */
49    @Component(roles = WikiReader.class)
50    @InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP)
 
51    public class WikiReader
52    {
53    private static final TranslationMarker LOG_DOCUMENT_SKIPPED = new TranslationMarker(
54    "filter.xar.log.document.skipped", WikiDocumentFilter.LOG_DOCUMENT_SKIPPED);
55   
56    private static final TranslationMarker LOG_DOCUMENT_FAILREAD = new TranslationMarker(
57    "filter.xar.log.document.failread", WikiDocumentFilter.LOG_DOCUMENT_ERROR);
58   
59    private static final TranslationMarker LOG_DESCRIPTOR_FAILREAD = new TranslationMarker(
60    "filter.xar.log.descriptor.failread");
61   
62    @Inject
63    private DocumentLocaleReader documentReader;
64   
65    @Inject
66    private Logger logger;
67   
68    private XARInputProperties properties;
69   
70    private XarPackage xarPackage = new XarPackage();
71   
 
72  7 toggle public void setProperties(XARInputProperties properties)
73    {
74  7 this.properties = properties;
75   
76  7 this.documentReader.setProperties(properties);
77    }
78   
 
79  0 toggle public XarPackage getXarPackage()
80    {
81  0 return this.xarPackage;
82    }
83   
 
84  7 toggle public void read(Object filter, XARInputFilter proxyFilter) throws IOException, FilterException
85    {
86  7 InputStream stream;
87   
88  7 InputSource source = this.properties.getSource();
89  7 if (source instanceof InputStreamInputSource) {
90  7 stream = ((InputStreamInputSource) source).getInputStream();
91    } else {
92  0 throw new FilterException("Unsupported source type [" + source.getClass() + "]");
93    }
94   
95  7 read(stream, filter, proxyFilter);
96   
97    // Close remaining opened spaces
98  7 if (this.documentReader.getSentSpaceReference() != null) {
99  14 for (EntityReference space = this.documentReader.getSentSpaceReference(); space != null; space =
100    space.getParent()) {
101  7 proxyFilter.endWikiSpace(space.getName(), FilterEventParameters.EMPTY);
102    }
103    }
104   
105    // Send extension event
106  7 if (this.xarPackage.getPackageExtensionId() != null) {
107  0 proxyFilter.beginExtension(this.xarPackage.getPackageExtensionId(), this.xarPackage.getPackageVersion(),
108    FilterEventParameters.EMPTY);
109  0 proxyFilter.endExtension(this.xarPackage.getPackageExtensionId(), this.xarPackage.getPackageVersion(),
110    FilterEventParameters.EMPTY);
111    }
112    }
113   
 
114  7 toggle public void read(InputStream stream, Object filter, XARInputFilter proxyFilter) throws IOException
115    {
116  7 ZipArchiveInputStream zis = new ZipArchiveInputStream(stream, "UTF-8", false);
117   
118  28 for (ZipArchiveEntry entry = zis.getNextZipEntry(); entry != null; entry = zis.getNextZipEntry()) {
119  21 if (entry.isDirectory() || entry.getName().startsWith("META-INF")) {
120    // The entry is either a directory or is something inside of the META-INF dir.
121    // (we use that directory to put meta data such as LICENSE/NOTICE files.)
122  3 continue;
123  18 } else if (entry.getName().equals(XarModel.PATH_PACKAGE)) {
124    // The entry is the manifest (package.xml). Read this differently.
125  7 try {
126  7 this.xarPackage.readDescriptor(zis);
127    } catch (Exception e) {
128  0 if (this.properties.isVerbose()) {
129  0 this.logger.warn(LOG_DESCRIPTOR_FAILREAD, "Failed to read XAR descriptor from entry [{}]: {}",
130    entry.getName(), ExceptionUtils.getRootCauseMessage(e));
131    }
132    }
133    } else {
134  11 try {
135  11 this.documentReader.read(zis, filter, proxyFilter);
136    } catch (SkipEntityException skip) {
137  2 if (this.properties.isVerbose()) {
138  2 this.logger.info(LOG_DOCUMENT_SKIPPED, "Skipped document [{}]", skip.getEntityReference());
139    }
140    } catch (Exception e) {
141  0 if (this.properties.isVerbose()) {
142  0 this.logger.warn(LOG_DOCUMENT_FAILREAD, "Failed to read XAR XML document from entry [{}]: {}",
143    entry.getName(), ExceptionUtils.getRootCauseMessage(e), e);
144    }
145    }
146    }
147    }
148    }
149    }