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 Renderer<E> {
033    
034      /** . */
035      private static final Renderer<?>[] renderables;
036    
037      static {
038        ArrayList<Renderer<?>> tmp = new ArrayList<Renderer<?>>();
039        Iterator<Renderer> i = ServiceLoader.load(Renderer.class).iterator();
040        while (i.hasNext()) {
041          try {
042            Renderer renderable = i.next();
043            tmp.add(renderable);
044          }
045          catch (ServiceConfigurationError e) {
046            // Config error
047          }
048        }
049        renderables = tmp.toArray(new Renderer<?>[tmp.size()]);
050      }
051    
052      public static Renderer<Object> ANY = new Renderer<Object>() {
053        @Override
054        public Class<Object> getType() {
055          return Object.class;
056        }
057    
058        @Override
059        public LineRenderer renderer(Iterator<Object> stream) {
060          StringBuilder sb = new StringBuilder();
061          while (stream.hasNext()) {
062            Object next = stream.next();
063            if (next instanceof CharSequence) {
064              sb.append((CharSequence)next);
065            } else {
066              sb.append(next);
067            }
068          }
069          return new LabelElement(sb.toString()).renderer();
070        }
071      };
072    
073      public static <I> Renderer<? super I> getRenderable(Class<I> itemType) {
074        for (Renderer<?> formatter : renderables) {
075          try {
076            if (formatter.getType().isAssignableFrom(itemType)) {
077              return (Renderer<I>)formatter;
078            }
079          }
080          catch (Exception e) {
081          }
082          catch (NoClassDefFoundError e) {
083          }
084        }
085        return null;
086      }
087    
088      public abstract Class<E> getType();
089    
090      public abstract LineRenderer renderer(Iterator<E> stream);
091    
092    }