Code example for CharArrayWriter

Methods: toCharArray

0
	protected static Log log = LogFactory.getLog(XMLParser.class);
 
	@Override 
	public void internalParse(InputStream input) {
		try { 
			CharArrayWriter writer = new CharArrayWriter();
			ExtractorHandler handler = new ExtractorHandler(writer);
 
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser parser = factory.newSAXParser();
			XMLReader reader = parser.getXMLReader();
			reader.setContentHandler(handler);
			reader.setErrorHandler(handler);
 
			// It is unspecified whether the XML parser closes the stream when 
			// done parsing. To ensure that the stream gets closed just once, 
			// we prevent the parser from closing it by catching the close() 
			// call and explicitly close the stream in a finally block. 
			InputSource source = new InputSource(new FilterInputStream(input) {
				public void close() { 
				} 
			}); 
			if (getEncoding() != null) { 
				try { 
					Charset.forName(getEncoding());
					source.setEncoding(getEncoding());
				} catch (Exception e) {
					log.warn("Unsupported encoding '" + getEncoding() + "', using default ("
							+ System.getProperty("file.encoding") + ") instead.");
				} 
			} 
			reader.parse(source);
 
			content.append(StringUtil.writeToString(new CharArrayReader(writer.toCharArray())));
		} catch (Exception e) {
			log.warn("Failed to extract XML text content", e);
		} 
	} 
}