// Copyright 1999-2005 California Institute of Technology. ALL RIGHTS
// RESERVED. U.S. Government Sponsorship acknowledged.
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Stack;
import jpl.eda.profile.EnumeratedProfileElement;
import jpl.eda.profile.Profile;
import jpl.eda.profile.ProfileAttributes;
import jpl.eda.profile.ResourceAttributes;
import jpl.eda.profile.handlers.ProfileHandler;
import jpl.eda.xmlquery.QueryElement;
import jpl.eda.xmlquery.XMLQuery;
public class MusicHandler implements ProfileHandler {
private static final List EL = Collections.EMPTY_LIST;
private static List describe(Set tracks) {
List profiles = new ArrayList();
for (Iterator i = tracks.iterator(); i.hasNext();) {
Track t = (Track) i.next();
String id = t.getID().toString();
String trackName = t.getName();
String albumName = t.getAlbum().getName();
String artistName = t.getArtist().getName();
Profile p = createProfile(id, trackName,
albumName, artistName);
profiles.add(p);
}
return profiles;
}
private static Profile createProfile(String id,
String trackName, String albumName,
String artistName) {
Profile p = new Profile();
ProfileAttributes pa=new ProfileAttributes(id, "1.0", "profile", "active", "unclassified", /*parent*/null,
/*children*/EL, "1.3.6.1.4.1.7655", /*revNotes*/EL);
p.setProfileAttributes(pa);
ResourceAttributes ra=new ResourceAttributes(p, id, trackName, Collections.singletonList("audio/mpeg"), /*desc*/null,
Collections.singletonList(artistName), /*subjects*/EL, /*pubs*/EL, /*contrib*/EL, /*dates*/EL, /*types*/EL,
/*sources*/EL, /*langs*/EL, /*relations*/EL, /*covs*/EL, /*rights*/EL,
Collections.singletonList("Tutorial.Music"), "granule", "system.productServer",
Collections.singletonList("urn:eda:rmi:MyProductServer"));
p.setResourceAttributes(ra);
EnumeratedProfileElement artistElem =
new EnumeratedProfileElement(p, "artist", "artist", "Name of the artist of a work", "string", "name",
/*syns*/EL, /*ob*/true, /*maxOccur*/1, /*comment*/null, Collections.singletonList(artistName));
p.getProfileElements().put("artist",
artistElem);
EnumeratedProfileElement albumElem = new EnumeratedProfileElement(p, "album", "album",
"Name of album where track occurs", "string", "name", /*syns*/EL, /*ob*/true, /*maxOccur*/1, /*comment*/null,
Collections.singletonList(albumName));
p.getProfileElements().put("album", albumElem);
return p;
}
private static Expr transform(XMLQuery q) {
Stack stack = new Stack();
for (Iterator i = q.getWhereElementSet().iterator(); i.hasNext();) {
QueryElement e = (QueryElement) i.next();
String keyword = e.getValue();
String type = e.getRole();
if ("elemName".equals(type))
stack.push(keyword);
else if ("LITERAL".equals(type))
stack.push(keyword);
else if ("RELOP".equals(type))
addRelational(keyword, (String)stack.pop(), (String)stack.pop(), stack);
else if ("LOGOP".equals(type))
addLogical(keyword, stack);
else throw new IllegalArgumentException("Unknown query " + type + " type");
}
if (stack.size() == 0)
return new Constant(true);
else if (stack.size() > 1)
throw new IllegalArgumentException("Unbalanced query");
else return (Expr) stack.pop();
}
private static void addRelational(String op,
String value, String kind, Stack stack) {
if ("artist".equals(kind))
stack.push(new ArtistExpr(op, value));
else if ("album".equals(kind))
stack.push(new AlbumExpr(op, value));
else if ("track".equals(kind))
stack.push(new TrackExpr(op, value));
else throw new
IllegalArgumentException("Unknown profile element " + kind);
}
private static void addLogical(String op,
Stack stack) {
if ("AND".equals(op))
stack.push(new And((Expr)stack.pop(), (Expr) stack.pop()));
else if ("OR".equals(op))
stack.push(new Or((Expr)stack.pop(), (Expr) stack.pop()));
else if ("NOT".equals(op))
stack.push(new Not((Expr)stack.pop()));
else throw new
IllegalArgumentException("Illegal operator " + op);
}
public List findProfiles(XMLQuery q) {
Expr expr = transform(q);
Set matches = expr.evaluate();
List profiles = describe(matches);
return profiles;
}
public Profile get(String id) {
URI uri = URI.create(id);
for (Iterator i = DB.TRACKS.iterator(); i.hasNext();) {
Track t = (Track) i.next();
if (t.getID().equals(uri))
return createProfile(t.getID().toString(), t.getName(), t.getAlbum().getName(),
t.getArtist().getName());
}
return null;
}
}