1 package util
.response
.databases
;
3 import interfaces
.Constants
;
5 import java
.util
.ArrayList
;
6 import java
.util
.Iterator
;
14 public class Browse
extends Response
implements Iterable
<String
> {
16 private final Constants content
;
17 private final Listing elements
;
19 public static Browse
read(Reader reader
) {
21 Listing elements
= null;
22 Constants content
= null;
24 for (Constants code
: reader
) {
26 case daap_browseartistlisting
:
27 case daap_browsealbumlisting
:
28 case daap_browsegenrelisting
:
29 case daap_browsecomposerlisting
:
31 elements
= Listing
.read(reader
.nextComposite(code
), code
);
36 if (content
== null) throw new RuntimeException("element not found");
38 return new Browse(content
, elements
);
41 public Browse(Constants content
, List
<String
> elements
) {
42 super(Constants
.daap_databasebrowse
, Response
.OK
);
44 this.content
= content
;
45 this.elements
= new Listing(elements
, content
);
48 private Browse(Constants content
, Listing elements
) {
49 super(Constants
.daap_databasebrowse
, Response
.OK
);
51 this.content
= content
;
52 this.elements
= elements
;
55 public Constants
content() {
59 public Iterator
<String
> iterator() {
60 return elements
.list
.iterator();
64 return elements
.list
.size();
67 public void write(Writer writer
) {
70 writer
.appendByte(Constants
.dmap_updatetype
, (byte)0);
71 writer
.appendInteger(Constants
.dmap_specifiedtotalcount
, size());
72 writer
.appendInteger(Constants
.dmap_returnedcount
, size());
73 writer
.appendNode(elements
);
78 private static class Listing
implements Node
{
79 public final List
<String
> list
;
80 public final Constants type
;
82 public static Listing
read(Reader reader
, Constants type
) {
83 List
<String
> list
= new ArrayList
<String
>();
84 for (Constants code
: reader
) {
85 if (code
== Constants
.dmap_listingitem
) {
86 list
.add(reader
.nextString(code
));
89 return new Listing(list
, type
);
92 public Listing(List
<String
> list
, Constants type
) {
96 public Constants
type() {
99 public void write(Writer writer
) {
100 for (String s
: list
) {
101 writer
.appendString(Constants
.dmap_listingitem
, s
);
108 public static Node buildBrowseResponse(int code, List<String> artists) throws UnsupportedEncodingException {
110 Composite response = createResponse(DACPConstants.abro);
112 Composite list = createList(response, code, 0, artists.size());
114 for (String s: artists) {
115 list.append(new StringNode(DACPConstants.mlit, s));
118 response.append(buildIndex(artists));
123 private static Node buildIndex(List<String> items) throws UnsupportedEncodingException {
125 Composite index = new Composite(DACPConstants.mshl);
131 for (String s: items) {
133 if (s.charAt(0) == current) {
138 Composite item = new Composite(DACPConstants.mlit);
139 item.append(new StringNode(DACPConstants.mshc, "\0"+current));
140 item.append(new IntegerNode(DACPConstants.mshi, offset));
141 item.append(new IntegerNode(DACPConstants.mshn, count));
145 current = s.charAt(0);
152 Composite item = new Composite(DACPConstants.mlit);
153 item.append(new StringNode(DACPConstants.mshc, "\0"+current));
154 item.append(new IntegerNode(DACPConstants.mshi, offset));
155 item.append(new IntegerNode(DACPConstants.mshn, count));