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

File XarFile.java

 

Coverage histogram

../../../img/srcFileCovDistChart5.png
74% of files have more coverage

Code metrics

6
14
9
1
128
58
12
0.86
1.56
9
1.33

Classes

Class Line # Actions
XarFile 35 14 0% 12 15
0.482758648.3%
 

Contributing tests

This file is covered by 9 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: 458af0038e6dfdd11f9db93589ca3c17c303f74c $
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  0 toggle public XarFile(File file) throws XarException, IOException
49    {
50  0 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  24 toggle public XarFile(File file, XarPackage xarPackage) throws XarException, IOException
71    {
72  24 this.file = file;
73  24 this.zipFile = new ZipFile(file);
74  24 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  119 toggle @Override
86    public void close() throws IOException
87    {
88  119 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  134 toggle public InputStream getInputStream(LocalDocumentReference reference) throws IOException
97    {
98  134 XarEntry entry = this.xarPackage.getEntry(reference);
99  134 if (entry == null) {
100  0 throw new IOException("Failed to find entry for referenc [" + reference + "]");
101    }
102   
103  134 return this.zipFile.getInputStream(this.zipFile.getEntry(entry.getEntryName()));
104    }
105   
106    /**
107    * @return the entries in the XAR file
108    */
 
109  0 toggle public Collection<XarEntry> getEntries()
110    {
111  0 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  134 toggle public XarEntry getEntry(LocalDocumentReference reference)
119    {
120  134 return this.xarPackage.getEntry(reference);
121    }
122   
 
123  0 toggle @Override
124    public String toString()
125    {
126  0 return this.file.toString();
127    }
128    }