Java tutorial
/* * Copyright 2012 Sazneo. * * 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. */ package com.sazneo.export.file; import com.sazneo.export.file.util.Encoding; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import java.io.*; import java.text.MessageFormat; /** * @author Jamie Atkinson */ public class FileProcessor { Log logger = LogFactory.getLog(FileProcessor.class); private File exportFile = null; private File outputDir = null; //----------------------------------------------------------------------------------------------- Constructors /** * Hide default constructor */ private FileProcessor() { super(); } /** * Initialise a new <code>FileProcessor</code> object * @param exportFile <code>File</code> referencing the exported XML file from Sazneo * @param outputDir <code>File</code> referencing the output dir to export files to */ public FileProcessor(File exportFile, File outputDir) { logger.debug(MessageFormat.format("Exporting files from: {0}", exportFile.getAbsolutePath())); this.exportFile = exportFile; logger.debug(MessageFormat.format("Setting output dir to: {0}", outputDir.getAbsolutePath())); this.outputDir = outputDir; } //----------------------------------------------------------------------------------------------- Public Methods /** * Process the XML file */ public void process() { try { Reader reader = new FileReader(exportFile); XMLInputFactory xmlFactory = XMLInputFactory.newInstance(); XMLStreamReader xmlStreamReader = xmlFactory.createXMLStreamReader(reader); boolean fileTag = false; String fileName = null; Integer fileSize = null; while (xmlStreamReader.hasNext()) { xmlStreamReader.next(); if (xmlStreamReader.getEventType() == XMLStreamReader.START_ELEMENT) { if ("file".equals(xmlStreamReader.getLocalName())) { fileTag = true; fileName = MessageFormat.format("{0}_{1}", xmlStreamReader.getAttributeValue(null, "id"), xmlStreamReader.getAttributeValue(null, "filename")); fileSize = Integer.parseInt(xmlStreamReader.getAttributeValue(null, "size")); logger.debug(MessageFormat.format("XML contains file with name: {0} and size {1} bytes", fileName, fileSize)); } } if (fileTag) { if (xmlStreamReader.getEventType() == XMLStreamReader.CHARACTERS) { exportFile(xmlStreamReader.getText(), fileName, fileSize); } if (xmlStreamReader.getEventType() == XMLStreamReader.END_ELEMENT) { fileSize = null; fileName = null; fileTag = false; } } } } catch (FileNotFoundException e) { logger.error(MessageFormat.format("Could not find export file:-\n{0}", e)); } catch (XMLStreamException e) { logger.error(MessageFormat.format("Could not parse xml export file:-\n{0}", e)); } } /** * Decodes the file content from the export XML and then saves it to the output directory * @param encodedContent Base64 encoded text * @param fileName The name of the file to create * @param fileSize The size of the file as reported in the export XML */ private void exportFile(String encodedContent, String fileName, Integer fileSize) { try { File file = new File(outputDir.getAbsolutePath(), fileName); byte[] decoded = Encoding.decode(encodedContent).getBytes(); logger.info(MessageFormat.format("Writing file: {0}", fileName)); FileOutputStream fileOutputStream = new FileOutputStream(file); fileOutputStream.write(decoded); assert (decoded.length == file.length() && fileSize == decoded.length); } catch (IOException e) { logger.error(MessageFormat.format("Error writing file: {0}:-\n{1}", fileName, e)); } } }