KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > digester > plugins > strategies > LoaderFromStream


1 /* $Id: LoaderFromStream.java 155412 2005-02-26 12:58:36Z dirkv $
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  
18 package org.apache.commons.digester.plugins.strategies;
19
20 import java.io.InputStream JavaDoc;
21 import java.io.ByteArrayInputStream JavaDoc;
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import org.xml.sax.InputSource JavaDoc;
25
26 import org.apache.commons.digester.Digester;
27 import org.apache.commons.digester.plugins.RuleLoader;
28 import org.apache.commons.digester.plugins.PluginException;
29 import org.apache.commons.digester.xmlrules.FromXmlRuleSet;
30 import org.apache.commons.logging.Log;
31
32 /**
33  * A rule-finding algorithm which loads an xmlplugins-format file.
34  * <p>
35  * Note that the "include" feature of xmlrules is not supported.
36  *
37  * @since 1.6
38  */

39
40 public class LoaderFromStream extends RuleLoader {
41
42     private byte[] input;
43     private FromXmlRuleSet ruleSet;
44     
45     /** See {@link #load}. */
46     public LoaderFromStream(InputStream JavaDoc s) throws Exception JavaDoc {
47         load(s);
48     }
49
50     /**
51      * The contents of the input stream are loaded into memory, and
52      * cached for later use.
53      * <p>
54      * The caller is responsible for closing the input stream after this
55      * method has returned.
56      */

57     private void load(InputStream JavaDoc s) throws IOException JavaDoc {
58         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
59         byte[] buf = new byte[256];
60         for(;;) {
61             int i = s.read(buf);
62             if (i == -1)
63                 break;
64             baos.write(buf, 0, i);
65         }
66         input = baos.toByteArray();
67     }
68     
69     /**
70      * Add the rules previously loaded from the input stream into the
71      * specified digester.
72      */

73     public void addRules(Digester d, String JavaDoc path) throws PluginException {
74         Log log = d.getLogger();
75         boolean debug = log.isDebugEnabled();
76         if (debug) {
77             log.debug(
78                 "LoaderFromStream: loading rules for plugin at path ["
79                 + path + "]");
80         }
81
82         // Note that this input-source doesn't have any idea of its
83
// system id, so it has no way of resolving relative URLs
84
// such as the "include" feature of xmlrules. This is ok,
85
// because that doesn't work well with our approach of
86
// caching the input data in memory anyway.
87

88         InputSource JavaDoc source = new InputSource JavaDoc(new ByteArrayInputStream JavaDoc(input));
89         FromXmlRuleSet ruleSet = new FromXmlRuleSet(source);
90         ruleSet.addRuleInstances(d, path);
91     }
92 }
93
94
Popular Tags