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