01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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.cocoon.xml;
18:
19: import org.apache.excalibur.xml.sax.XMLizable;
20: import org.xml.sax.ContentHandler;
21: import org.xml.sax.SAXException;
22: import org.xml.sax.InputSource;
23:
24: import javax.xml.parsers.SAXParserFactory;
25: import javax.xml.parsers.SAXParser;
26: import javax.xml.parsers.ParserConfigurationException;
27: import java.io.IOException;
28: import java.io.StringReader;
29:
30: /**
31: * XMLizable a String
32: *
33: * @since 2.1.7
34: * @author Bruno Dumon
35: */
36: public class StringXMLizable implements XMLizable {
37: private String data;
38:
39: public StringXMLizable(String data) {
40: this .data = data;
41: }
42:
43: public void toSAX(ContentHandler contentHandler)
44: throws SAXException {
45: SAXParserFactory parserFactory = SAXParserFactory.newInstance();
46: parserFactory.setNamespaceAware(true);
47: SAXParser parser = null;
48: try {
49: parser = parserFactory.newSAXParser();
50: } catch (ParserConfigurationException e) {
51: throw new SAXException("Error creating SAX parser.", e);
52: }
53: parser.getXMLReader().setContentHandler(contentHandler);
54: InputSource is = new InputSource(new StringReader(data));
55: try {
56: parser.getXMLReader().parse(is);
57: } catch (IOException e) {
58: throw new SAXException(e);
59: }
60: }
61: }
|