1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.xar

File XarFile.java

 

Coverage histogram

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

Code metrics

6
15
10
1
137
62
13
0.87
1.5
10
1.3

Classes

Class Line # Actions
XarFile 35 15 0% 13 10
0.6774193667.7%
 

Contributing tests

This file is covered by 10 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.xar;
21   
22    import java.io.Closeable;
23    import java.io.File;
24    import java.io.IOException;
25    import java.io.InputStream;
26    import java.util.Collection;
27   
28    import org.apache.commons.compress.archivers.zip.ZipFile;
29    import org.xwiki.model.reference.LocalDocumentReference;
30   
31    /**
32    * @version $Id: 17f58ed100c882ef30be7b6020e67ccea987a5a4 $
33    * @since 5.4RC1
34    */
 
35    public class XarFile implements Closeable
36    {
37    private File file;
38   
39    private ZipFile zipFile;
40   
41    private XarPackage xarPackage;
42   
43    /**
44    * @param file the XAR file
45    * @throws XarException when failing parse the file (for example if it's not a valid XAR package)
46    * @throws IOException when failing to read file
47    */
 
48  1 toggle public XarFile(File file) throws XarException, IOException
49    {
50  1 this(file, (XarPackage) null);
51    }
52   
53    /**
54    * @param file the XAR file
55    * @param pages the pages in the XAR package
56    * @throws XarException when failing parse the file (for example if it's not a valid XAR package)
57    * @throws IOException when failing to read file
58    */
 
59  0 toggle public XarFile(File file, Collection<XarEntry> pages) throws XarException, IOException
60    {
61  0 this(file, pages != null ? new XarPackage(pages) : null);
62    }
63   
64    /**
65    * @param file the XAR file
66    * @param xarPackage the descriptor of the XAR package
67    * @throws XarException when failing parse the file (for example if it's not a valid XAR package)
68    * @throws IOException when failing to read file
69    */
 
70  126 toggle public XarFile(File file, XarPackage xarPackage) throws XarException, IOException
71    {
72  126 this.file = file;
73  126 this.zipFile = new ZipFile(file);
74  126 this.xarPackage = xarPackage != null ? xarPackage : new XarPackage(this.zipFile);
75    }
76   
77    /**
78    * @return the XAR file
79    */
 
80  0 toggle public File getFile()
81    {
82  0 return this.file;
83    }
84   
 
85  1695 toggle @Override
86    public void close() throws IOException
87    {
88  1695 this.zipFile.close();
89    }
90   
91    /**
92    * @param reference the reference of the page
93    * @return an input stream to the page XML
94    * @throws IOException when failing to open a stream to the page XML
95    */
 
96  1710 toggle public InputStream getInputStream(LocalDocumentReference reference) throws IOException
97    {
98  1710 XarEntry entry = this.xarPackage.getEntry(reference);
99  1710 if (entry == null) {
100  0 throw new IOException("Failed to find entry for referenc [" + reference + "]");
101    }
102   
103  1710 return this.zipFile.getInputStream(this.zipFile.getEntry(entry.getEntryName()));
104    }
105   
106    /**
107    * @return the entries in the XAR file
108    */
 
109  1 toggle public Collection<XarEntry> getEntries()
110    {
111  1 return this.xarPackage.getEntries();
112    }
113   
114    /**
115    * @param reference the reference of the document
116    * @return the XAR entry associated to a wiki document
117    */
 
118  1716 toggle public XarEntry getEntry(LocalDocumentReference reference)
119    {
120  1716 return this.xarPackage.getEntry(reference);
121    }
122   
 
123  0 toggle @Override
124    public String toString()
125    {
126  0 return this.file.toString();
127    }
128   
129    /**
130    * @return the {@link XarPackage}
131    * @since 10.9
132    */
 
133  1 toggle public XarPackage getXarPackage()
134    {
135  1 return this.xarPackage;
136    }
137    }