001/* 002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 */ 020package org.nuxeo.ecm.core.convert.plugins.text.extractors; 021 022import java.io.IOException; 023import java.io.InputStream; 024import java.io.Serializable; 025import java.util.Map; 026 027import javax.xml.parsers.ParserConfigurationException; 028 029import org.apache.commons.logging.Log; 030import org.apache.commons.logging.LogFactory; 031import org.nuxeo.ecm.core.api.Blobs; 032import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 033import org.nuxeo.ecm.core.convert.api.ConversionException; 034import org.nuxeo.ecm.core.convert.cache.SimpleCachableBlobHolder; 035import org.nuxeo.ecm.core.convert.extension.Converter; 036import org.nuxeo.ecm.core.convert.extension.ConverterDescriptor; 037import org.xml.sax.SAXException; 038 039public class XML2TextConverter implements Converter { 040 041 private static final Log log = LogFactory.getLog(XML2TextConverter.class); 042 043 @Override 044 public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters) throws ConversionException { 045 046 InputStream stream = null; 047 try { 048 stream = blobHolder.getBlob().getStream(); 049 Xml2TextHandler xml2text = new Xml2TextHandler(); 050 String text = xml2text.parse(stream); 051 052 return new SimpleCachableBlobHolder(Blobs.createBlob(text)); 053 } catch (IOException | SAXException | ParserConfigurationException e) { 054 throw new ConversionException("Error during XML2Text conversion", e); 055 } finally { 056 if (stream != null) { 057 try { 058 stream.close(); 059 } catch (IOException e) { 060 log.error("Error while closing Blob stream", e); 061 } 062 } 063 } 064 } 065 066 @Override 067 public void init(ConverterDescriptor descriptor) { 068 } 069 070}