Java tutorial
/* * * * * Copyright 2016 Guillermo Bauz (birelian) - birelianATgmailDOTcom * * * * * * This file is part of liquibase-xml-to-yaml * * * * liquibase-xml-to-yaml is free software: you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation, either version 3 of the License, or * * (at your option) any later version. * * * * liquibase-xml-to-yaml is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * * along with liquibase-xml-to-yaml. If not, see <http://www.gnu.org/licenses/>. * * * */ package net.birelian.xmltoyaml; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import liquibase.changelog.ChangeLogParameters; import liquibase.changelog.DatabaseChangeLog; import liquibase.parser.ChangeLogParserFactory; import liquibase.resource.ClassLoaderResourceAccessor; import liquibase.resource.ResourceAccessor; import net.birelian.xmltoyaml.exception.ApplicationException; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.io.Charsets; import org.apache.commons.io.IOUtils; /** * Main class */ public class Main { private static final String SOURCE_DIR = "source/"; private static final String OUTPUT_DIR = "generated/"; private static final String SOURCE_EXTENSION = "xml"; private static final String TARGET_EXTENSION = "yaml"; private final ResourceAccessor resourceAccessor = new ClassLoaderResourceAccessor(); private final ChangeLogParameters changeLogParameters = new ChangeLogParameters(); /** * Main method */ public static void main(String[] args) throws ApplicationException { Main application = new Main(); application.execute(); } /** * Execute application * * @throws ApplicationException If any */ private void execute() throws ApplicationException { // Check that output directory exists. Create if it doesn't. checkOutputDirectory(); // First try to read files from the SOURCE dir in the classpath List<String> files = getFileListFromClasspath(); // If files is empty try to get them from SOURCE dir in the file system if (CollectionUtils.isEmpty(files)) { files = getFilesFromFileSystem(); } // Convert files for (String file : files) { processFile(file); } } /** * Process a file * * @param fileName File name * * @throws ApplicationException If any */ private void processFile(String fileName) throws ApplicationException { String changeLogFile = SOURCE_DIR + fileName; try { // Get and parse the change log file DatabaseChangeLog changeLog; changeLog = ChangeLogParserFactory.getInstance().getParser(SOURCE_EXTENSION, resourceAccessor) .parse(changeLogFile, changeLogParameters, resourceAccessor); // Create file and stream File file = createFile(fileName); FileOutputStream outputStream = new FileOutputStream(file); // Write stream liquibase.serializer.ChangeLogSerializerFactory.getInstance().getSerializer("." + TARGET_EXTENSION) .write(changeLog.getChangeSets(), outputStream); // Close the stream outputStream.flush(); outputStream.close(); System.out.println("File " + file.getName() + " successfully created"); } catch (Exception e) { throw new ApplicationException("Unable to parse file " + fileName, e); } } /** * Create a file * * @param fileName file name * * @return The file * * @throws ApplicationException If any */ private File createFile(String fileName) throws ApplicationException { File file = new File(OUTPUT_DIR + fileName + "." + TARGET_EXTENSION); // Create file if it doesn't exist if (!file.exists()) { try { boolean created = file.createNewFile(); if (!created) { throw new ApplicationException("Unable to create file " + file.getName()); } } catch (IOException e) { throw new ApplicationException("Unable to create file " + fileName, e); } } return file; } /** * Get change log files from the classpath * * @return Files from the file system * * @throws ApplicationException If any */ private List<String> getFileListFromClasspath() throws ApplicationException { if (Main.class.getClassLoader().getResourceAsStream(SOURCE_DIR) != null) { try { return IOUtils.readLines(Main.class.getClassLoader().getResourceAsStream(SOURCE_DIR), Charsets.UTF_8); } catch (IOException e) { throw new ApplicationException("Unable to get files from the classpath", e); } } return null; } /** * Get change log files from the file system * * @return Files from the file system * * @throws ApplicationException If any */ private List<String> getFilesFromFileSystem() throws ApplicationException { validateSourceDirectory(); List<String> files = new ArrayList<>(); try { Files.walk(Paths.get(SOURCE_DIR)).forEach(filePath -> { if (Files.isRegularFile(filePath)) { files.add(filePath.getFileName().toString()); } }); return files; } catch (IOException e) { throw new ApplicationException("Unable to traverse source directory"); } } /** * Validate that the source directory exists. * * @throws ApplicationException If any */ private void validateSourceDirectory() throws ApplicationException { File targetDirectory = new File(SOURCE_DIR); if (!targetDirectory.exists()) { throw new ApplicationException("Source directory does not exist"); } } /** * Check that the output directory exists. Create it if necessary. * * @throws ApplicationException If any */ private void checkOutputDirectory() throws ApplicationException { File targetDirectory = new File(OUTPUT_DIR); boolean created; if (!targetDirectory.exists()) { System.out.println("Creating directory: " + OUTPUT_DIR); created = targetDirectory.mkdir(); if (!created) { throw new ApplicationException("Unable to create output directory"); } } } }