Moved MPRIS to a new project
[stereo.git] / DAAPLib / src / util / response / databases / Browse.java
blobe116bcc30654ec2380ac9f3d789e4c5f2cc0087c
1 package util.response.databases;
3 import interfaces.Constants;
5 import java.util.ArrayList;
6 import java.util.Iterator;
7 import java.util.List;
9 import api.Node;
10 import api.Reader;
11 import api.Response;
12 import api.Writer;
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) {
25 switch (code) {
26 case daap_browseartistlisting:
27 case daap_browsealbumlisting:
28 case daap_browsegenrelisting:
29 case daap_browsecomposerlisting:
30 content = code;
31 elements = Listing.read(reader.nextComposite(code), code);
32 break;
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() {
56 return content;
59 public Iterator<String> iterator() {
60 return elements.list.iterator();
63 public int size() {
64 return elements.list.size();
67 public void write(Writer writer) {
68 super.write(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);
75 //TODO append index
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) {
93 this.list = list;
94 this.type = type;
96 public Constants type() {
97 return 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));
120 return response;
123 private static Node buildIndex(List<String> items) throws UnsupportedEncodingException {
125 Composite index = new Composite(DACPConstants.mshl);
127 char current = 0;
128 int count = 0;
129 int offset = 0;
131 for (String s: items) {
133 if (s.charAt(0) == current) {
134 count++;
136 else {
137 if (current != 0) {
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));
142 index.append(item);
145 current = s.charAt(0);
146 offset += count;
147 count = 0;
151 if (current != 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));
156 index.append(item);
159 return index;