001/*
002 * (C) Copyright 2009 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 *     Thierry Delprat
018 */
019package org.nuxeo.ecm.platform.importer.random;
020
021import java.io.IOException;
022import java.io.Serializable;
023import java.util.Map;
024
025import org.nuxeo.ecm.core.api.Blob;
026import org.nuxeo.ecm.core.api.Blobs;
027import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
028import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolder;
029import org.nuxeo.ecm.core.convert.api.ConversionException;
030import org.nuxeo.ecm.core.convert.extension.Converter;
031import org.nuxeo.ecm.core.convert.extension.ConverterDescriptor;
032
033public class PartialTextExtractor implements Converter {
034
035    public static final double TEXT_RATIO = 0.01;
036
037    @Override
038    public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters) throws ConversionException {
039        try {
040            Blob blob = blobHolder.getBlob();
041
042            String data = blob.getString();
043            int endIdx = new Double(data.length() * TEXT_RATIO).intValue();
044            String txtData = data.substring(0, endIdx);
045            return new SimpleBlobHolder(Blobs.createBlob(txtData));
046
047        } catch (IOException e) {
048            throw new ConversionException("error extracting partial text content", e);
049        }
050    }
051
052    @Override
053    public void init(ConverterDescriptor descriptor) {
054    }
055
056}