From 365080f5570f9d2d218ee5afeb365a88a490bd45 Mon Sep 17 00:00:00 2001 From: zapata Date: Tue, 15 Oct 2002 23:07:33 +0000 Subject: [PATCH] support for the new CopyDir producer node --- source/mir/producer/DirCopyingProducerNode.java | 71 ++++++++++++++++++++++ source/mir/producer/ScriptCallingProducerNode.java | 2 - .../reader/DefaultProducerNodeBuilders.java | 67 ++++++++++++++++---- source/mir/util/FileCopier.java | 61 +++++++++++++++++++ .../localizer/basic/MirBasicProducerLocalizer.java | 6 +- 5 files changed, 191 insertions(+), 16 deletions(-) create mode 100755 source/mir/producer/DirCopyingProducerNode.java create mode 100755 source/mir/util/FileCopier.java diff --git a/source/mir/producer/DirCopyingProducerNode.java b/source/mir/producer/DirCopyingProducerNode.java new file mode 100755 index 00000000..c943c989 --- /dev/null +++ b/source/mir/producer/DirCopyingProducerNode.java @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2001, 2002 The Mir-coders group + * + * This file is part of Mir. + * + * Mir 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 2 of the License, or + * (at your option) any later version. + * + * Mir 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 Mir; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * In addition, as a special exception, The Mir-coders gives permission to link + * the code of this program with the com.oreilly.servlet library, any library + * licensed under the Apache Software License, The Sun (tm) Java Advanced + * Imaging library (JAI), The Sun JIMI library (or with modified versions of + * the above that use the same license as the above), and distribute linked + * combinations including the two. You must obey the GNU General Public + * License in all respects for all of the code used other than the above + * mentioned libraries. If you modify this file, you may extend this exception + * to your version of the file, but you are not obligated to do so. If you do + * not wish to do so, delete this exception statement from your version. + */ + +package mir.producer; + +import java.io.*; +import java.util.*; +import mir.util.*; + +public class DirCopyingProducerNode implements ProducerNode { + private String sourceExpression; + private String destinationExpression; + private String sourceBasePath; + private String destinationBasePath; + + public DirCopyingProducerNode(String aSourceBasePath, String aDestinationBasePath, String aSource, String aDestination) { + sourceBasePath = aSourceBasePath; + destinationBasePath = aDestinationBasePath; + sourceExpression = aSource; + destinationExpression = aDestination; + } + + public void produce(Map aValueMap, String aVerb, PrintWriter aLogger) throws ProducerFailure { + String source = ""; + String destination = ""; + + try { + source = ParameterExpander.expandExpression( aValueMap, sourceExpression ); + destination = ParameterExpander.expandExpression( aValueMap, destinationExpression ); + aLogger.println("Copying " + source + " into " + destination); + FileCopier.copyDirectory( + new File(sourceBasePath, source), + new File(destinationBasePath, destination)); + } + catch (Throwable e) { + throw new ProducerFailure("Copying " + source + " into " + destination + " failed: " + e.getMessage(), e); + } + } + + public Set buildVerbSet() { + return new HashSet(); + } +} diff --git a/source/mir/producer/ScriptCallingProducerNode.java b/source/mir/producer/ScriptCallingProducerNode.java index 18ae9b49..6c7b873c 100755 --- a/source/mir/producer/ScriptCallingProducerNode.java +++ b/source/mir/producer/ScriptCallingProducerNode.java @@ -35,8 +35,6 @@ import java.io.*; import java.util.*; import mir.util.*; -// ML: needs to be tested! - public class ScriptCallingProducerNode implements ProducerNode { String scriptExpression; diff --git a/source/mir/producer/reader/DefaultProducerNodeBuilders.java b/source/mir/producer/reader/DefaultProducerNodeBuilders.java index ff726cb3..86ce89a9 100755 --- a/source/mir/producer/reader/DefaultProducerNodeBuilders.java +++ b/source/mir/producer/reader/DefaultProducerNodeBuilders.java @@ -41,20 +41,20 @@ public class DefaultProducerNodeBuilders { public static void registerBuilders(ProducerNodeBuilderLibrary aBuilderLibrary, EntityAdapterModel aModel, Generator.GeneratorLibrary aGeneratorLibrary, - WriterEngine aWriterEngine) { + WriterEngine aWriterEngine, String aSourceBasePath, String aDestinationBasePath) { aBuilderLibrary.registerBuilder("Set", EvaluatedAssignmentProducerNodeBuilder.class); aBuilderLibrary.registerBuilder("Define", ExpandedAssignmentProducerNodeBuilder.class); aBuilderLibrary.registerBuilder("Log", LoggingProducerNodeBuilder.class); aBuilderLibrary.registerBuilder("Execute", ScriptCallingProducerNodeBuilder.class); aBuilderLibrary.registerBuilder("Resource", ResourceBundleProducerNodeBuilder.class); + aBuilderLibrary.registerFactory("CopyDir", new DirCopyProducerNodeBuilder.factory( aSourceBasePath, aDestinationBasePath)); aBuilderLibrary.registerBuilder("DeleteFile", FileDeletingProducerNodeBuilder.class); aBuilderLibrary.registerBuilder("SetFileDate", FileDateSettingProducerNodeBuilder.class); aBuilderLibrary.registerBuilder("If", ConditionalProducerNodeBuilder.class); aBuilderLibrary.registerBuilder("While", LoopProducerNodeBuilder.class); - aBuilderLibrary.registerFactory("Enumerate", new EnumeratingProducerNodeBuilder.factory(aModel)); aBuilderLibrary.registerFactory("List", new ListProducerNodeBuilder.factory(aModel)); aBuilderLibrary.registerFactory("Batch", new BatchingProducerNodeBuilder.factory(aModel)); @@ -101,12 +101,13 @@ public class DefaultProducerNodeBuilders { //////////////////////////////////////////////////////////////////////////////// + private final static String ASSIGNMENT_KEY_ATTRIBUTE = KEY_ATTRIBUTE; + private final static String ASSIGNMENT_VALUE_ATTRIBUTE = "value"; + private final static String[] ASSIGNMENT_REQUIRED_ATTRIBUTES = { ASSIGNMENT_KEY_ATTRIBUTE, ASSIGNMENT_VALUE_ATTRIBUTE }; + private final static String[] ASSIGNMENT_OPTIONAL_ATTRIBUTES = {}; + private final static String[] ASSIGNMENT_SUBNODES = {}; + public static class ExpandedAssignmentProducerNodeBuilder extends AbstractProducerNodeBuilder { - private final static String ASSIGNMENT_KEY_ATTRIBUTE = KEY_ATTRIBUTE; - private final static String ASSIGNMENT_VALUE_ATTRIBUTE = "value"; - private final static String[] ASSIGNMENT_REQUIRED_ATTRIBUTES = { ASSIGNMENT_KEY_ATTRIBUTE, ASSIGNMENT_VALUE_ATTRIBUTE }; - private final static String[] ASSIGNMENT_OPTIONAL_ATTRIBUTES = {}; - private final static String[] ASSIGNMENT_SUBNODES = {}; private String key; private String value; @@ -130,11 +131,6 @@ public class DefaultProducerNodeBuilders { //////////////////////////////////////////////////////////////////////////////// public static class EvaluatedAssignmentProducerNodeBuilder extends AbstractProducerNodeBuilder { - private final static String ASSIGNMENT_KEY_ATTRIBUTE = KEY_ATTRIBUTE; - private final static String ASSIGNMENT_VALUE_ATTRIBUTE = "value"; - private final static String[] ASSIGNMENT_REQUIRED_ATTRIBUTES = { ASSIGNMENT_KEY_ATTRIBUTE, ASSIGNMENT_VALUE_ATTRIBUTE }; - private final static String[] ASSIGNMENT_OPTIONAL_ATTRIBUTES = {}; - private final static String[] ASSIGNMENT_SUBNODES = {}; private String key; private String value; @@ -433,6 +429,53 @@ public class DefaultProducerNodeBuilders { //////////////////////////////////////////////////////////////////////////////// + private final static String DIRCOPY_SOURCE_ATTRIBUTE = "source"; + private final static String DIRCOPY_DESTINATION_ATTRIBUTE = "destination"; + private final static String[] DIRCOPY_REQUIRED_ATTRIBUTES = { DIRCOPY_SOURCE_ATTRIBUTE, DIRCOPY_DESTINATION_ATTRIBUTE }; + private final static String[] DIRCOPY_OPTIONAL_ATTRIBUTES = {}; + private final static String[] DIRCOPY_SUBNODES = {}; + + public static class DirCopyProducerNodeBuilder extends AbstractProducerNodeBuilder { + private String source; + private String destination; + private String sourceBasePath; + private String destinationBasePath; + + public DirCopyProducerNodeBuilder(String aSourceBasePath, String aDestinationBasePath) { + super(DIRCOPY_SUBNODES); + + sourceBasePath = aSourceBasePath; + destinationBasePath = aDestinationBasePath; + } + + public void setAttributes(Map anAttributes) throws ProducerConfigExc { + ReaderTool.checkAttributes(anAttributes, DIRCOPY_REQUIRED_ATTRIBUTES, DIRCOPY_OPTIONAL_ATTRIBUTES); + + source = (String) anAttributes.get(DIRCOPY_SOURCE_ATTRIBUTE); + destination = (String) anAttributes.get(DIRCOPY_DESTINATION_ATTRIBUTE); + }; + + public ProducerNode constructNode() { + return new DirCopyingProducerNode(sourceBasePath, destinationBasePath, source, destination); + }; + + public static class factory implements ProducerNodeBuilderFactory { + private String sourceBasePath; + private String destinationBasePath; + + public factory(String aSourceBasePath, String aDestinationBasePath) { + sourceBasePath = aSourceBasePath; + destinationBasePath = aDestinationBasePath; + } + + public ProducerNodeBuilder makeBuilder() { + return new DirCopyProducerNodeBuilder(sourceBasePath, destinationBasePath); + } + } + } + +//////////////////////////////////////////////////////////////////////////////// + public static class GeneratingProducerNodeBuilder extends AbstractProducerNodeBuilder { private final static String GENERATION_GENERATOR_ATTRIBUTE = "generator"; private final static String GENERATION_DESTINATION_ATTRIBUTE = "destination"; diff --git a/source/mir/util/FileCopier.java b/source/mir/util/FileCopier.java new file mode 100755 index 00000000..e4442deb --- /dev/null +++ b/source/mir/util/FileCopier.java @@ -0,0 +1,61 @@ +package mir.util; + +import java.io.*; +import java.util.*; + +public class FileCopier { + protected static final int FILE_COPY_BUFFER_SIZE = 65536; + + protected FileCopier() { + } + + public static void copyFile(File aSourceFile, File aDestinationFile) throws IOException { + FileInputStream inputStream; + FileOutputStream outputStream; + int nrBytesRead; + byte[] buffer = new byte[FILE_COPY_BUFFER_SIZE]; + + inputStream = new FileInputStream(aSourceFile); + try { + outputStream = new FileOutputStream(aDestinationFile); + try { + do { + nrBytesRead = inputStream.read(buffer); + if (nrBytesRead>0) + outputStream.write(buffer, 0, nrBytesRead); + } + while (nrBytesRead>=0); + } + finally { + outputStream.close(); + } + } + finally { + inputStream.close(); + } + } + + public static void copyDirectory(File aSourceDirectory, File aDestinationDirectory) throws IOException { + int i; + File sourceFile; + File destinationFile; + File[] files = aSourceDirectory.listFiles(); + + if (!aDestinationDirectory.exists()) + aDestinationDirectory.mkdirs(); + + for (i=0; i