001    /*
002     * Copyright (C) 2012 eXo Platform SAS.
003     *
004     * This is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU Lesser General Public License as
006     * published by the Free Software Foundation; either version 2.1 of
007     * the License, or (at your option) any later version.
008     *
009     * This software is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with this software; if not, write to the Free
016     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018     */
019    
020    package org.crsh.text;
021    
022    import org.crsh.text.ui.LabelElement;
023    
024    import java.util.ArrayList;
025    import java.util.Iterator;
026    import java.util.ServiceConfigurationError;
027    import java.util.ServiceLoader;
028    
029    /**
030     * Provide a renderable.
031     */
032    public abstract class Renderable<E> {
033    
034      /** . */
035      private static final Renderable<?>[] renderables;
036    
037      static {
038        ArrayList<Renderable<?>> tmp = new ArrayList<Renderable<?>>();
039        Iterator<Renderable> i = ServiceLoader.load(Renderable.class).iterator();
040        while (i.hasNext()) {
041          try {
042            Renderable renderable = i.next();
043            tmp.add(renderable);
044          }
045          catch (ServiceConfigurationError e) {
046            // Config error
047          }
048        }
049        renderables = tmp.toArray(new Renderable<?>[tmp.size()]);
050      }
051    
052      public static Renderable<Object> ANY = new Renderable<Object>() {
053        @Override
054        public Class<Object> getType() {
055          return Object.class;
056        }
057    
058        @Override
059        public Renderer renderer(Iterator<Object> stream) {
060          StringBuilder sb = new StringBuilder();
061          while (stream.hasNext()) {
062            Object next = stream.next();
063            if (next instanceof Chunk) {
064              if (next instanceof Text) {
065                sb.append(((Text)next).buffer);
066              }
067            } else {
068              sb.append(next);
069            }
070          }
071          return new LabelElement(sb.toString()).renderer();
072        }
073      };
074    
075      public static <I> Renderable<? super I> getRenderable(Class<I> itemType) {
076        for (Renderable<?> formatter : renderables) {
077          if (formatter.getType().isAssignableFrom(itemType)) {
078            return (Renderable<I>)formatter;
079          }
080        }
081        return null;
082      }
083    
084      public abstract Class<E> getType();
085    
086      public abstract Renderer renderer(Iterator<E> stream);
087    
088    }