Search in sources :

Example 1 with Track

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;
}
Also used : InputSource(org.xml.sax.InputSource) DefaultHandler(org.xml.sax.helpers.DefaultHandler) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException) Track(org.apache.tapestry5.integration.app1.data.Track)

Example 2 with Track

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);
}
Also used : ExceptionInfo(org.apache.tapestry5.ioc.services.ExceptionInfo)

Example 3 with Track

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;
}
Also used : Track(org.apache.tapestry5.integration.app1.data.Track)

Example 4 with Track

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;
        }
    };
}
Also used : URL(java.net.URL) Track(org.apache.tapestry5.integration.app1.data.Track)

Example 5 with Track

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);
}
Also used : Entity(org.apache.tapestry5.integration.app1.data.Entity) ValueEncoder(org.apache.tapestry5.ValueEncoder) ToDoItem(org.apache.tapestry5.integration.app1.data.ToDoItem) ValueEncoderFactory(org.apache.tapestry5.services.ValueEncoderFactory) GenericValueEncoderFactory(org.apache.tapestry5.internal.services.GenericValueEncoderFactory) Track(org.apache.tapestry5.integration.app1.data.Track)

Aggregations

Track (org.apache.tapestry5.integration.app1.data.Track)5 URL (java.net.URL)1 MarkupWriter (org.apache.tapestry5.MarkupWriter)1 ValueEncoder (org.apache.tapestry5.ValueEncoder)1 Location (org.apache.tapestry5.commons.Location)1 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)1 UnknownValueException (org.apache.tapestry5.commons.util.UnknownValueException)1 Entity (org.apache.tapestry5.integration.app1.data.Entity)1 ToDoItem (org.apache.tapestry5.integration.app1.data.ToDoItem)1 AbstractEventContext (org.apache.tapestry5.internal.AbstractEventContext)1 ComponentEventImpl (org.apache.tapestry5.internal.services.ComponentEventImpl)1 GenericValueEncoderFactory (org.apache.tapestry5.internal.services.GenericValueEncoderFactory)1 MarkupWriterImpl (org.apache.tapestry5.internal.services.MarkupWriterImpl)1 NotificationEventCallback (org.apache.tapestry5.internal.util.NotificationEventCallback)1 ExceptionInfo (org.apache.tapestry5.ioc.services.ExceptionInfo)1 ValueEncoderFactory (org.apache.tapestry5.services.ValueEncoderFactory)1 Logger (org.slf4j.Logger)1 Test (org.testng.annotations.Test)1 InputSource (org.xml.sax.InputSource)1 SAXException (org.xml.sax.SAXException)1