1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
43 |
|
@since |
44 |
|
|
|
|
| 0% |
Uncovered Elements: 65 (65) |
Complexity: 19 |
Complexity Density: 0.38 |
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 25 (25) |
Complexity: 5 |
Complexity Density: 0.24 |
|
55 |
0 |
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 |
|
|
92 |
|
|
93 |
|
|
94 |
0 |
this.zipStream.setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy.ALWAYS); |
95 |
|
} |
96 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
97 |
0 |
public String getName()... |
98 |
|
{ |
99 |
0 |
return this.name; |
100 |
|
} |
101 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
102 |
0 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 4 |
Complexity Density: 0.29 |
|
112 |
0 |
public OutputStream newEntry(LocalDocumentReference reference) throws FilterException... |
113 |
|
{ |
114 |
0 |
StringBuilder path = new StringBuilder(); |
115 |
|
|
116 |
|
|
117 |
0 |
addSpacePath(path, reference.getParent()); |
118 |
|
|
119 |
|
|
120 |
0 |
path.append(reference.getName()); |
121 |
|
|
122 |
|
|
123 |
0 |
if (reference.getLocale() != null && !reference.getLocale().equals(Locale.ROOT)) { |
124 |
0 |
path.append('.'); |
125 |
0 |
path.append(reference.getLocale()); |
126 |
|
} |
127 |
|
|
128 |
|
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
145 |
0 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
154 |
0 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
163 |
0 |
public void close() throws IOException... |
164 |
|
{ |
165 |
|
|
166 |
0 |
try { |
167 |
0 |
writePackage(); |
168 |
|
} catch (FilterException e) { |
169 |
0 |
throw new IOException("Failed to write package", e); |
170 |
|
} |
171 |
|
|
172 |
|
|
173 |
0 |
this.zipStream.close(); |
174 |
|
} |
175 |
|
} |