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.nuxeo.ecm.core.api.Blob; 030import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 031import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolder; 032import org.nuxeo.ecm.core.api.impl.blob.StringBlob; 033import org.nuxeo.ecm.core.convert.api.ConversionException; 034import org.nuxeo.ecm.core.convert.extension.Converter; 035import org.nuxeo.ecm.core.convert.extension.ConverterDescriptor; 036import org.xml.sax.SAXException; 037 038public class XML2TextConverter implements Converter { 039 040 @Override 041 public BlobHolder convert(BlobHolder holder, Map<String, Serializable> parameters) throws ConversionException { 042 return new SimpleBlobHolder(new StringBlob(convert(holder.getBlob(), parameters))); 043 } 044 045 String convert(Blob blob, Map<String, Serializable> parameters) { 046 if (blob.getLength() == 0L) { 047 return ""; 048 } 049 try (InputStream stream = blob.getStream()) { 050 Xml2TextHandler xml2text = new Xml2TextHandler(); 051 return xml2text.parse(stream); 052 } catch (IOException | SAXException | ParserConfigurationException e) { 053 throw new ConversionException("Error during XML2Text conversion", e); 054 } 055 } 056 057 @Override 058 public void init(ConverterDescriptor descriptor) { 059 } 060 061}