ImplementationSource.java 4.9 KB

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