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

File XARWikiWriter.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

8
50
7
1
175
114
19
0.38
7.14
7
2.71

Classes

Class Line # Actions
XARWikiWriter 45 50 0% 19 65
0.00%
 

Contributing tests

No tests hitting this source file were found.

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.output;
21   
22    import java.io.Closeable;
23    import java.io.File;
24    import java.io.IOException;
25    import java.io.OutputStream;
26    import java.util.Locale;
27   
28    import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
29    import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
30    import org.apache.commons.io.output.CloseShieldOutputStream;
31    import org.xwiki.filter.FilterException;
32    import org.xwiki.filter.output.FileOutputTarget;
33    import org.xwiki.filter.output.OutputStreamOutputTarget;
34    import org.xwiki.filter.output.OutputTarget;
35    import org.xwiki.filter.xar.output.XAROutputProperties;
36    import org.xwiki.model.EntityType;
37    import org.xwiki.model.reference.EntityReference;
38    import org.xwiki.model.reference.LocalDocumentReference;
39    import org.xwiki.xar.XarPackage;
40   
41    /**
42    * @version $Id: fc6520f11cba03ea1ae446c4b8be4b4a04a9d263 $
43    * @since 6.2M1
44    */
 
45    public class XARWikiWriter implements Closeable
46    {
47    private final String name;
48   
49    private final XAROutputProperties xarProperties;
50   
51    private final ZipArchiveOutputStream zipStream;
52   
53    private XarPackage xarPackage = new XarPackage();
54   
 
55  0 toggle public XARWikiWriter(String name, XAROutputProperties xarProperties) throws FilterException
56    {
57  0 this.name = name;
58  0 this.xarProperties = xarProperties;
59   
60  0 this.xarPackage = new XarPackage();
61   
62  0 this.xarPackage.setPackageName(this.name);
63  0 this.xarPackage.setPackageDescription(xarProperties.getPackageDescription());
64  0 this.xarPackage.setPackageLicense(xarProperties.getPackageLicense());
65  0 this.xarPackage.setPackageAuthor(xarProperties.getPackageAuthor());
66  0 this.xarPackage.setPackageVersion(xarProperties.getPackageVersion());
67  0 this.xarPackage.setPackageBackupPack(xarProperties.isPackageBackupPack());
68  0 this.xarPackage.setPreserveVersion(xarProperties.isPreserveVersion());
69  0 this.xarPackage.setPackageExtensionId(xarProperties.getPackageExtensionId());
70   
71  0 OutputTarget target = this.xarProperties.getTarget();
72   
73  0 try {
74  0 if (target instanceof FileOutputTarget && ((FileOutputTarget) target).getFile().isDirectory()) {
75  0 this.zipStream =
76    new ZipArchiveOutputStream(new File(((FileOutputTarget) target).getFile(), name + ".xar"));
77  0 } else if (target instanceof OutputStreamOutputTarget) {
78  0 this.zipStream =
79    new ZipArchiveOutputStream(new CloseShieldOutputStream(
80    ((OutputStreamOutputTarget) target).getOutputStream()));
81    } else {
82  0 throw new FilterException(String.format("Unsupported output target [%s]. Only [%s] is supported",
83    target, OutputStreamOutputTarget.class));
84    }
85    } catch (IOException e) {
86  0 throw new FilterException("Failed to create zip output stream", e);
87    }
88   
89  0 this.zipStream.setEncoding("UTF8");
90   
91    // By including the unicode extra fields, it is possible to extract XAR-files containing documents with
92    // non-ascii characters in the document name using InfoZIP, and the filenames will be correctly
93    // converted to the character set of the local file system.
94  0 this.zipStream.setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy.ALWAYS);
95    }
96   
 
97  0 toggle public String getName()
98    {
99  0 return this.name;
100    }
101   
 
102  0 toggle private void addSpacePath(StringBuilder path, EntityReference spaceReference)
103    {
104  0 EntityReference parent = spaceReference.getParent();
105  0 if (parent != null && parent.getType() == EntityType.SPACE) {
106  0 addSpacePath(path, parent);
107    }
108   
109  0 path.append(spaceReference.getName()).append('/');
110    }
111   
 
112  0 toggle public OutputStream newEntry(LocalDocumentReference reference) throws FilterException
113    {
114  0 StringBuilder path = new StringBuilder();
115   
116    // Add space path
117  0 addSpacePath(path, reference.getParent());
118   
119    // Add document name
120  0 path.append(reference.getName());
121   
122    // Add language
123  0 if (reference.getLocale() != null && !reference.getLocale().equals(Locale.ROOT)) {
124  0 path.append('.');
125  0 path.append(reference.getLocale());
126    }
127   
128    // Add extension
129  0 path.append(".xml");
130   
131  0 String entryName = path.toString();
132   
133  0 ZipArchiveEntry zipentry = new ZipArchiveEntry(entryName);
134  0 try {
135  0 this.zipStream.putArchiveEntry(zipentry);
136    } catch (IOException e) {
137  0 throw new FilterException("Failed to add a new zip entry for [" + path + "]", e);
138    }
139   
140  0 this.xarPackage.addEntry(reference, entryName);
141   
142  0 return this.zipStream;
143    }
144   
 
145  0 toggle public void closeEntry() throws FilterException
146    {
147  0 try {
148  0 this.zipStream.closeArchiveEntry();
149    } catch (IOException e) {
150  0 throw new FilterException("Failed to close zip archive entry", e);
151    }
152    }
153   
 
154  0 toggle private void writePackage() throws FilterException
155    {
156  0 try {
157  0 this.xarPackage.write(this.zipStream, this.xarProperties.getEncoding());
158    } catch (Exception e) {
159  0 throw new FilterException("Failed to write package.xml entry", e);
160    }
161    }
162   
 
163  0 toggle public void close() throws IOException
164    {
165    // Add package.xml descriptor
166  0 try {
167  0 writePackage();
168    } catch (FilterException e) {
169  0 throw new IOException("Failed to write package", e);
170    }
171   
172    // Close zip stream
173  0 this.zipStream.close();
174    }
175    }