use of org.apache.tapestry5.integration.app1.data.Track in project tapestry-5 by apache.
the class MusicLibraryParser method parseTracks.
public List<Track> parseTracks(URL resource) {
logger.info(format("Parsing music library %s", resource));
long start = System.currentTimeMillis();
Handler handler = new Handler();
try {
XMLReader reader = XMLReaderFactory.createXMLReader();
reader.setContentHandler(handler);
reader.setEntityResolver(handler);
InputSource source = new InputSource(resource.openStream());
reader.parse(source);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
List<Track> result = handler.getTracks();
long elapsed = System.currentTimeMillis() - start;
logger.info(format("Parsed %d tracks in %d ms", result.size(), elapsed));
return result;
}
use of org.apache.tapestry5.integration.app1.data.Track in project tapestry-5 by apache.
the class ExceptionAnalyzerImpl method extractData.
private ExceptionData extractData(Throwable t) {
Map<String, Object> properties = CollectionFactory.newMap();
ClassPropertyAdapter adapter = propertyAccess.getAdapter(t);
Throwable cause = null;
for (String name : adapter.getPropertyNames()) {
PropertyAdapter pa = adapter.getPropertyAdapter(name);
if (!pa.isRead())
continue;
if (cause == null && Throwable.class.isAssignableFrom(pa.getType())) {
// Ignore the property, but track it as the cause.
Throwable nestedException = (Throwable) pa.get(t);
// Handle the case where an exception is its own cause (avoid endless loop!)
if (t != nestedException)
cause = nestedException;
continue;
}
if (throwableProperties.contains(name))
continue;
Object value = pa.get(t);
if (value == null)
continue;
// An interesting property, let's save it for the analysis.
properties.put(name, value);
}
// Provide the stack trace only at the deepest exception.
List<StackTraceElement> stackTrace = Collections.emptyList();
if (cause == null)
stackTrace = Arrays.asList(t.getStackTrace());
ExceptionInfo info = new ExceptionInfoImpl(t, properties, stackTrace);
return new ExceptionData(info, cause);
}
use of org.apache.tapestry5.integration.app1.data.Track in project tapestry-5 by apache.
the class AutocompleteDemo method onProvideCompletionsFromTitle.
List onProvideCompletionsFromTitle(String partialTitle) throws Exception {
boolean roundabout = false;
List<Track> matches = library.findByMatchingTitle(partialTitle);
List<String> result = CollectionFactory.newList();
for (Track t : matches) {
result.add(t.getTitle());
roundabout |= t.getTitle().equals("Roundabout");
}
if (roundabout) {
alertManager.info("Completions include 'Roundabout'.");
}
return result;
}
use of org.apache.tapestry5.integration.app1.data.Track in project tapestry-5 by apache.
the class AppModule method buildMusicLibrary.
public MusicLibrary buildMusicLibrary(Logger log) {
URL library = getClass().getResource("iTunes.xml");
final List<Track> tracks = new MusicLibraryParser(log).parseTracks(library);
final Map<Long, Track> idToTrack = CollectionFactory.newMap();
for (Track t : tracks) {
idToTrack.put(t.getId(), t);
}
return new MusicLibrary() {
public Track getById(long id) {
Track result = idToTrack.get(id);
if (result != null)
return result;
throw new IllegalArgumentException(String.format("No track with id #%d.", id));
}
public List<Track> getTracks() {
return tracks;
}
public List<Track> findByMatchingTitle(String title) {
String titleLower = title.toLowerCase();
List<Track> result = CollectionFactory.newList();
for (Track t : tracks) {
if (t.getTitle().toLowerCase().contains(titleLower))
result.add(t);
}
return result;
}
};
}
use of org.apache.tapestry5.integration.app1.data.Track in project tapestry-5 by apache.
the class AppModule method contributeValueEncoderSource.
public static void contributeValueEncoderSource(MappedConfiguration<Class, ValueEncoderFactory> configuration, final MusicLibrary library, final ToDoDatabase todoDatabase) {
ValueEncoder<Track> trackEncoder = new ValueEncoder<Track>() {
public String toClient(Track value) {
return Long.toString(value.getId());
}
public Track toValue(String clientValue) {
long id = Long.parseLong(clientValue);
return library.getById(id);
}
};
configuration.add(Track.class, GenericValueEncoderFactory.create(trackEncoder));
ValueEncoder<ToDoItem> todoEncoder = new ValueEncoder<ToDoItem>() {
public String toClient(ToDoItem value) {
return String.valueOf(value.getId());
}
public ToDoItem toValue(String clientValue) {
long id = Long.parseLong(clientValue);
return todoDatabase.get(id);
}
};
configuration.add(ToDoItem.class, GenericValueEncoderFactory.create(todoEncoder));
final ValueEncoder<Entity> encoder = new ValueEncoder<Entity>() {
public String toClient(Entity value) {
return value.getId();
}
public Entity toValue(String clientValue) {
Entity entity = new Entity();
entity.setId(clientValue);
entity.setLabel("label" + clientValue);
return entity;
}
};
ValueEncoderFactory<Entity> valueEncoderFactory = new ValueEncoderFactory<Entity>() {
public ValueEncoder<Entity> create(Class<Entity> type) {
return encoder;
}
};
configuration.add(Entity.class, valueEncoderFactory);
}
Aggregations