1. /* $Id: LoaderFromStream.java,v 1.5 2004/05/10 06:34:01 skitching Exp $
  2. *
  3. * Copyright 2004 The Apache Software Foundation.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.commons.digester.plugins.strategies;
  18. import java.io.InputStream;
  19. import java.io.ByteArrayInputStream;
  20. import java.io.ByteArrayOutputStream;
  21. import java.io.IOException;
  22. import org.xml.sax.InputSource;
  23. import org.apache.commons.digester.Digester;
  24. import org.apache.commons.digester.plugins.RuleLoader;
  25. import org.apache.commons.digester.plugins.PluginException;
  26. import org.apache.commons.digester.xmlrules.FromXmlRuleSet;
  27. import org.apache.commons.logging.Log;
  28. /**
  29. * A rule-finding algorithm which loads an xmlplugins-format file.
  30. * <p>
  31. * Note that the "include" feature of xmlrules is not supported.
  32. *
  33. * @since 1.6
  34. */
  35. public class LoaderFromStream extends RuleLoader {
  36. private byte[] input;
  37. private FromXmlRuleSet ruleSet;
  38. /** See {@link #load}. */
  39. public LoaderFromStream(InputStream s) throws Exception {
  40. load(s);
  41. }
  42. /**
  43. * The contents of the input stream are loaded into memory, and
  44. * cached for later use.
  45. * <p>
  46. * The caller is responsible for closing the input stream after this
  47. * method has returned.
  48. */
  49. private void load(InputStream s) throws IOException {
  50. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  51. byte[] buf = new byte[256];
  52. for(;;) {
  53. int i = s.read(buf);
  54. if (i == -1)
  55. break;
  56. baos.write(buf, 0, i);
  57. }
  58. input = baos.toByteArray();
  59. }
  60. /**
  61. * Add the rules previously loaded from the input stream into the
  62. * specified digester.
  63. */
  64. public void addRules(Digester d, String path) throws PluginException {
  65. Log log = d.getLogger();
  66. boolean debug = log.isDebugEnabled();
  67. if (debug) {
  68. log.debug(
  69. "LoaderFromStream: loading rules for plugin at path ["
  70. + path + "]");
  71. }
  72. // Note that this input-source doesn't have any idea of its
  73. // system id, so it has no way of resolving relative URLs
  74. // such as the "include" feature of xmlrules. This is ok,
  75. // because that doesn't work well with our approach of
  76. // caching the input data in memory anyway.
  77. InputSource source = new InputSource(new ByteArrayInputStream(input));
  78. FromXmlRuleSet ruleSet = new FromXmlRuleSet(source);
  79. ruleSet.addRuleInstances(d, path);
  80. }
  81. }