Skip to content

Commit 5f96763

Browse files
Created implementation of XmlEvent for filelists.xml (#396)
* Created implementation of `XmlEvent` to filelists.xml * code review * extra todo removed
1 parent 01ac6b9 commit 5f96763

File tree

3 files changed

+179
-10
lines changed

3 files changed

+179
-10
lines changed

src/main/java/com/artipie/rpm/meta/XmlEvent.java

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
import com.artipie.rpm.pkg.HeaderTags;
2727
import com.artipie.rpm.pkg.Package;
2828
import java.io.IOException;
29+
import java.util.Arrays;
30+
import java.util.Set;
31+
import java.util.stream.Collectors;
2932
import javax.xml.stream.XMLEventFactory;
3033
import javax.xml.stream.XMLEventWriter;
3134
import javax.xml.stream.XMLStreamException;
@@ -35,6 +38,7 @@
3538
* Xml event to write to the output stream.
3639
* @since 1.5
3740
*/
41+
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
3842
public interface XmlEvent {
3943

4044
/**
@@ -45,11 +49,10 @@ public interface XmlEvent {
4549
void add(Package.Meta meta) throws IOException;
4650

4751
/**
48-
* Implementation of {@link XmlEvent} to build event for {@link XmlPackage#OTHER} package.
52+
* Implementation of {@link XmlEvent} to build event for `package` and `version` tags.
4953
* @since 1.5
50-
* @checkstyle ExecutableStatementCountCheck (30 lines)
5154
*/
52-
class Others implements XmlEvent {
55+
final class PackageAndVersion implements XmlEvent {
5356

5457
/**
5558
* Where to write the event.
@@ -60,17 +63,17 @@ class Others implements XmlEvent {
6063
* Ctor.
6164
* @param writer Writer to write the event
6265
*/
63-
public Others(final XMLEventWriter writer) {
66+
public PackageAndVersion(final XMLEventWriter writer) {
6467
this.writer = writer;
6568
}
6669

6770
@Override
6871
public void add(final Package.Meta meta) throws IOException {
6972
final XMLEventFactory events = XMLEventFactory.newFactory();
7073
final HeaderTags tags = new HeaderTags(meta);
74+
final String pkg = "package";
75+
final String version = "version";
7176
try {
72-
final String pkg = "package";
73-
final String version = "version";
7477
this.writer.add(events.createStartElement("", "", pkg));
7578
this.writer.add(events.createAttribute("pkgid", meta.checksum().hex()));
7679
this.writer.add(events.createAttribute("name", tags.name()));
@@ -80,6 +83,37 @@ public void add(final Package.Meta meta) throws IOException {
8083
this.writer.add(events.createAttribute("ver", tags.version()));
8184
this.writer.add(events.createAttribute("rel", tags.release()));
8285
this.writer.add(events.createEndElement("", "", version));
86+
} catch (final XMLStreamException err) {
87+
throw new IOException(err);
88+
}
89+
}
90+
}
91+
92+
/**
93+
* Implementation of {@link XmlEvent} to build event for {@link XmlPackage#OTHER} package.
94+
* @since 1.5
95+
*/
96+
final class Other implements XmlEvent {
97+
98+
/**
99+
* Where to write the event.
100+
*/
101+
private final XMLEventWriter writer;
102+
103+
/**
104+
* Ctor.
105+
* @param writer Writer to write the event
106+
*/
107+
public Other(final XMLEventWriter writer) {
108+
this.writer = writer;
109+
}
110+
111+
@Override
112+
public void add(final Package.Meta meta) throws IOException {
113+
final XMLEventFactory events = XMLEventFactory.newFactory();
114+
final HeaderTags tags = new HeaderTags(meta);
115+
try {
116+
new PackageAndVersion(this.writer).add(meta);
83117
for (final String changelog : tags.changelog()) {
84118
final ChangelogEntry entry = new ChangelogEntry(changelog);
85119
final String tag = "changelog";
@@ -89,7 +123,59 @@ public void add(final Package.Meta meta) throws IOException {
89123
this.writer.add(events.createCharacters(entry.content()));
90124
this.writer.add(events.createEndElement("", "", tag));
91125
}
92-
this.writer.add(events.createEndElement("", "", pkg));
126+
this.writer.add(events.createEndElement("", "", "package"));
127+
} catch (final XMLStreamException err) {
128+
throw new IOException(err);
129+
}
130+
}
131+
}
132+
133+
/**
134+
* Implementation of {@link XmlEvent} to build event for {@link XmlPackage#FILELISTS} package.
135+
* @since 1.5
136+
*/
137+
final class Filelists implements XmlEvent {
138+
139+
/**
140+
* Where to write the event.
141+
*/
142+
private final XMLEventWriter writer;
143+
144+
/**
145+
* Ctor.
146+
* @param writer Writer to write the event
147+
*/
148+
public Filelists(final XMLEventWriter writer) {
149+
this.writer = writer;
150+
}
151+
152+
@Override
153+
public void add(final Package.Meta meta) throws IOException {
154+
final XMLEventFactory events = XMLEventFactory.newFactory();
155+
final HeaderTags tags = new HeaderTags(meta);
156+
try {
157+
new PackageAndVersion(this.writer).add(meta);
158+
final String[] files = tags.baseNames().toArray(new String[0]);
159+
final String[] dirs = tags.dirNames().toArray(new String[0]);
160+
final Set<String> dirset = Arrays.stream(dirs).collect(Collectors.toSet());
161+
final int[] did = tags.dirIndexes();
162+
for (int idx = 0; idx < files.length; idx += 1) {
163+
final String fle = files[idx];
164+
// @checkstyle MethodBodyCommentsCheck (2 lines)
165+
// @todo #388:30min This condition is not covered with unit test, extend
166+
// the test to check this case and make sure it works properly.
167+
if (fle.isEmpty() || fle.charAt(0) == '.') {
168+
continue;
169+
}
170+
final String path = String.format("%s%s", dirs[did[idx]], fle);
171+
this.writer.add(events.createStartElement("", "", "file"));
172+
if (dirset.contains(String.format("%s/", path))) {
173+
this.writer.add(events.createAttribute("type", "dir"));
174+
}
175+
this.writer.add(events.createCharacters(path));
176+
this.writer.add(events.createEndElement("", "", "file"));
177+
}
178+
this.writer.add(events.createEndElement("", "", "package"));
93179
} catch (final XMLStreamException err) {
94180
throw new IOException(err);
95181
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2020 artipie.com
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included
14+
* in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package com.artipie.rpm.meta;
25+
26+
import com.artipie.asto.test.TestResource;
27+
import com.artipie.rpm.Digest;
28+
import com.artipie.rpm.hm.IsXmlEqual;
29+
import com.artipie.rpm.pkg.FilePackage;
30+
import com.artipie.rpm.pkg.FilePackageHeader;
31+
import com.fasterxml.aalto.stax.OutputFactoryImpl;
32+
import java.io.IOException;
33+
import java.io.OutputStream;
34+
import java.nio.file.Files;
35+
import java.nio.file.Path;
36+
import javax.xml.stream.XMLEventWriter;
37+
import javax.xml.stream.XMLStreamException;
38+
import org.hamcrest.MatcherAssert;
39+
import org.junit.jupiter.api.Test;
40+
import org.junit.jupiter.api.io.TempDir;
41+
42+
/**
43+
* Test for {@link XmlEvent.Other}.
44+
* @since 1.5
45+
*/
46+
class XmlEventFilelistsTest {
47+
48+
/**
49+
* Temporary directory for all tests.
50+
* @checkstyle VisibilityModifierCheck (3 lines)
51+
*/
52+
@TempDir
53+
Path tmp;
54+
55+
@Test
56+
void writesPackageInfo() throws XMLStreamException, IOException {
57+
final Path res = Files.createTempFile(this.tmp, "filelists", ".xml");
58+
final Path file = new TestResource("libdeflt1_0-2020.03.27-25.1.armv7hl.rpm").asPath();
59+
try (OutputStream out = Files.newOutputStream(res)) {
60+
final XMLEventWriter writer = new OutputFactoryImpl().createXMLEventWriter(out);
61+
new XmlEvent.Filelists(writer).add(
62+
new FilePackage.Headers(new FilePackageHeader(file).header(), file, Digest.SHA256)
63+
);
64+
writer.close();
65+
}
66+
MatcherAssert.assertThat(
67+
res,
68+
new IsXmlEqual(
69+
String.join(
70+
"\n",
71+
//@checkstyle LineLengthCheck (1 line)
72+
"<package pkgid=\"47bbb8b2401e8853812e6340f4197252b92463c132f64a257e18c0c8c83ae462\" name=\"libdeflt1_0\" arch=\"armv7hl\">",
73+
"<version epoch=\"0\" ver=\"2020.03.27\" rel=\"25.1\"/>",
74+
"<file>/usr/lib/libdeflt.so.1.0</file>",
75+
"<file type=\"dir\">/usr/share/licenses/libdeflt1_0</file>",
76+
"<file>/usr/share/licenses/libdeflt1_0/CDDL.Schily.txt</file>",
77+
"</package>"
78+
)
79+
)
80+
);
81+
}
82+
83+
}

src/test/java/com/artipie/rpm/meta/XmlEventOthersTest.java renamed to src/test/java/com/artipie/rpm/meta/XmlEventOtherTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@
4040
import org.junit.jupiter.api.io.TempDir;
4141

4242
/**
43-
* Test for {@link XmlEvent.Others}.
43+
* Test for {@link XmlEvent.Other}.
4444
* @since 1.5
4545
*/
46-
class XmlEventOthersTest {
46+
class XmlEventOtherTest {
4747

4848
/**
4949
* Temporary directory for all tests.
@@ -58,7 +58,7 @@ void writesPackageInfo() throws XMLStreamException, IOException {
5858
final Path file = new TestResource("abc-1.01-26.git20200127.fc32.ppc64le.rpm").asPath();
5959
try (OutputStream out = Files.newOutputStream(res)) {
6060
final XMLEventWriter writer = new OutputFactoryImpl().createXMLEventWriter(out);
61-
new XmlEvent.Others(writer).add(
61+
new XmlEvent.Other(writer).add(
6262
new FilePackage.Headers(new FilePackageHeader(file).header(), file, Digest.SHA256)
6363
);
6464
writer.close();

0 commit comments

Comments
 (0)