2626import com .artipie .rpm .pkg .HeaderTags ;
2727import com .artipie .rpm .pkg .Package ;
2828import java .io .IOException ;
29+ import java .util .Arrays ;
30+ import java .util .Set ;
31+ import java .util .stream .Collectors ;
2932import javax .xml .stream .XMLEventFactory ;
3033import javax .xml .stream .XMLEventWriter ;
3134import javax .xml .stream .XMLStreamException ;
3538 * Xml event to write to the output stream.
3639 * @since 1.5
3740 */
41+ @ SuppressWarnings ("PMD.AvoidDuplicateLiterals" )
3842public 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 }
0 commit comments