it.tidalwave.semantic.document.DocumentWriter Maven / Gradle / Ivy
/***********************************************************************************************************************
*
* blueBill Core - open source birding
* Copyright (C) 2009-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
*
***********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
***********************************************************************************************************************
*
* WWW: http://bluebill.tidalwave.it
* SCM: https://kenai.com/hg/bluebill~core-src
*
**********************************************************************************************************************/
package it.tidalwave.semantic.document;
import javax.annotation.Nonnull;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import it.tidalwave.util.NotFoundException;
import lombok.Cleanup;
import lombok.RequiredArgsConstructor;
import static it.tidalwave.semantic.document.Document.*;
/***********************************************************************************************************************
*
* A {@link DocumentVisitor} which writes a {@link Document} and its attachments to a folder.
*
* @author Fabrizio Giudici
* @version $Id$
*
**********************************************************************************************************************/
@RequiredArgsConstructor
public class DocumentWriter extends DocumentVisitorSupport
{
private static final Logger log = LoggerFactory.getLogger(DocumentWriter.class);
@Nonnull
private final String documentBaseName;
@Nonnull
private final File folder;
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
// TODO: increment the ProgressListener
@Override
public void visitBody (final @Nonnull Document document)
throws IOException
{
write(new File(folder, documentBaseName + ".txt"), document);
}
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
@Override
public void visitAttachment (final @Nonnull Document attachment)
throws IOException
{
write(new File(folder, documentBaseName + getExtension(attachment)), attachment);
}
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
private void write (final @Nonnull File file, final @Nonnull Document document)
throws IOException
{
log.debug("write({}, {})", file.getAbsolutePath(), document);
try
{
@Cleanup final FileWriter w = new FileWriter(file);
w.write(document.get(BODY));
}
catch (NotFoundException e)
{
// ok, no body
}
}
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
@Nonnull
private static String getExtension (final @Nonnull Document document)
{
try
{
final String ext = EXTENSION_MAP_BY_MIME_TYPE.get(document.get(MIME_TYPE));
return (ext == null) ? "" : "." + ext;
}
catch (NotFoundException e)
{
return ""; // no mime type
}
}
}