Source for gnu.xml.dom.ImplementationSource

   1: /* ImplementationSource.java --
   2:    Copyright (C) 2004 Free Software Foundation, Inc..
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: package gnu.xml.dom;
  38: 
  39: import java.util.ArrayList;
  40: import java.util.Arrays;
  41: import java.util.Iterator;
  42: import java.util.List;
  43: 
  44: import org.w3c.dom.DOMImplementation;
  45: import org.w3c.dom.DOMImplementationList;
  46: import org.w3c.dom.DOMImplementationSource;
  47: 
  48: /**
  49:  * Implementation source for GNU JAXP.
  50:  *
  51:  * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  52:  */
  53: public class ImplementationSource
  54:   implements DOMImplementationSource
  55: {
  56: 
  57:   private static final String DIGITS = "1234567890";
  58: 
  59:   /*
  60:    * GNU DOM implementations.
  61:    */
  62:   private static final DOMImplementation[] implementations;
  63: 
  64:   static
  65:   {
  66:     List acc = new ArrayList();
  67:     acc.add(new gnu.xml.dom.DomImpl());
  68:     try
  69:       {
  70:         Class t = Class.forName("gnu.xml.libxmlj.dom.GnomeDocumentBuilder");
  71:         acc.add(t.newInstance());
  72:       }
  73:     catch (Exception e)
  74:       {
  75:         // libxmlj not available
  76:       }
  77:     catch (UnsatisfiedLinkError e)
  78:       {
  79:         // libxmlj not available
  80:       }
  81:     implementations = new DOMImplementation[acc.size()];
  82:     acc.toArray(implementations);
  83:   }
  84: 
  85:   public DOMImplementation getDOMImplementation(String features)
  86:   {
  87:     List available = getImplementations(features);
  88:     if (available.isEmpty())
  89:       {
  90:         return null;
  91:       }
  92:     return (DOMImplementation) available.get(0);
  93:   }
  94: 
  95:   public DOMImplementationList getDOMImplementationList(String features)
  96:   {
  97:     List available = getImplementations(features);
  98:     return new ImplementationList(available);
  99:   }
 100: 
 101:   /**
 102:    * Returns a list of the implementations that support the specified
 103:    * features.
 104:    */
 105:   private final List getImplementations(String features)
 106:   {
 107:     List available = new ArrayList(Arrays.asList(implementations));
 108:     for (Iterator i = parseFeatures(features).iterator(); i.hasNext(); )
 109:       {
 110:         String feature = (String) i.next();
 111:         String version = null;
 112:         int si = feature.indexOf(' ');
 113:         if (si != -1)
 114:           {
 115:             version = feature.substring(si + 1);
 116:             feature = feature.substring(0, si);
 117:           }
 118:         for (Iterator j = available.iterator(); j.hasNext(); )
 119:           {
 120:             DOMImplementation impl = (DOMImplementation) j.next();
 121:             if (!impl.hasFeature(feature, version))
 122:               {
 123:                 j.remove();
 124:               }
 125:           }
 126:       }
 127:     return available;
 128:   }
 129: 
 130:   /**
 131:    * Parses the feature list into feature tokens.
 132:    */
 133:   final List parseFeatures(String features)
 134:   {
 135:     List list = new ArrayList();
 136:     int pos = 0, start = 0;
 137:     int len = features.length();
 138:     for (; pos < len; pos++)
 139:       {
 140:         char c = features.charAt(pos);
 141:         if (c == ' ')
 142:           {
 143:             if (pos + 1 < len &&
 144:                 DIGITS.indexOf(features.charAt(pos + 1)) == -1)
 145:               {
 146:                 list.add(getFeature(features, start, pos));
 147:                 start = pos + 1;
 148:               }
 149:           }
 150:       }
 151:     if (pos > start)
 152:       {
 153:         list.add(getFeature(features, start, len));
 154:       }
 155:     return list;
 156:   }
 157: 
 158:   final String getFeature(String features, int start, int end)
 159:   {
 160:     if (features.length() > 0 && features.charAt(start) == '+')
 161:       {
 162:         return features.substring(start + 1, end);
 163:       }
 164:     return features.substring(start, end);
 165:   }
 166: 
 167: }