KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > util > ParamSaxBufferFactory


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.frontend.util;
17
18 import org.apache.cocoon.xml.ParamSaxBuffer;
19 import org.apache.cocoon.xml.IncludeXMLConsumer;
20 import org.xml.sax.XMLReader JavaDoc;
21 import org.xml.sax.InputSource JavaDoc;
22
23 import javax.xml.parsers.SAXParserFactory JavaDoc;
24 import javax.xml.parsers.SAXParser JavaDoc;
25 import java.io.StringReader JavaDoc;
26
27 public class ParamSaxBufferFactory {
28
29     /**
30      * Helper factory for create ParamSaxBuffers from XML blurbs. The resulting
31      * buffer will not contain start and endDocument events. The xmlBlurb parameter
32      * does not need to have a root element, it can simply be a string with some
33      * parameters in it.
34      */

35     public static ParamSaxBuffer create(String JavaDoc xmlBlurb) {
36         String JavaDoc xml = "<dummyRoot>" + xmlBlurb + "</dummyRoot>";
37
38         ParamSaxBuffer buffer = new ParamSaxBuffer();
39         IncludeXMLConsumer includeConsumer = new IncludeXMLConsumer(buffer);
40         includeConsumer.setIgnoreRootElement(true);
41
42         try {
43             SAXParserFactory JavaDoc parserFactory = SAXParserFactory.newInstance();
44             parserFactory.setNamespaceAware(true);
45             SAXParser JavaDoc parser = parserFactory.newSAXParser();
46             XMLReader JavaDoc reader = parser.getXMLReader();
47             reader.setContentHandler(includeConsumer);
48             InputSource JavaDoc is = new InputSource JavaDoc(new StringReader JavaDoc(xml));
49             reader.parse(is);
50         } catch (Exception JavaDoc e) {
51             throw new RuntimeException JavaDoc(e);
52         }
53
54         return buffer;
55     }
56 }
57
Popular Tags