|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright 2013 Real Logic Ltd. | 
|  | 3 | + * | 
|  | 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | + * you may not use this file except in compliance with the License. | 
|  | 6 | + * You may obtain a copy of the License at | 
|  | 7 | + * | 
|  | 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | + * | 
|  | 10 | + * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | + * See the License for the specific language governing permissions and | 
|  | 14 | + * limitations under the License. | 
|  | 15 | + */ | 
|  | 16 | +package uk.co.real_logic.sbe.generation.python; | 
|  | 17 | + | 
|  | 18 | +import uk.co.real_logic.sbe.generation.OutputManager; | 
|  | 19 | +import uk.co.real_logic.sbe.util.Verify; | 
|  | 20 | + | 
|  | 21 | +import java.io.*; | 
|  | 22 | + | 
|  | 23 | +/** | 
|  | 24 | + * {@link OutputManager} for managing the creation of C++98 source files as the target of code generation. | 
|  | 25 | + * The character encoding for the {@link java.io.Writer} is UTF-8. | 
|  | 26 | + */ | 
|  | 27 | +public class ModuleOutputManager implements OutputManager | 
|  | 28 | +{ | 
|  | 29 | + private final File outputDir; | 
|  | 30 | + | 
|  | 31 | + /** | 
|  | 32 | + * Create a new {@link uk.co.real_logic.sbe.generation.OutputManager} for generating C++98 source files into a given package. | 
|  | 33 | + * | 
|  | 34 | + * @param baseDirectoryName for the generated source code. | 
|  | 35 | + * @param namespaceName for the generated source code relative to the baseDirectoryName. | 
|  | 36 | + * @throws IOException if an error occurs during output | 
|  | 37 | + */ | 
|  | 38 | + public ModuleOutputManager(final String baseDirectoryName, final String namespaceName) | 
|  | 39 | + throws IOException | 
|  | 40 | + { | 
|  | 41 | + Verify.notNull(baseDirectoryName, "baseDirectoryName"); | 
|  | 42 | + Verify.notNull(namespaceName, "applicableNamespace"); | 
|  | 43 | + | 
|  | 44 | + final String dirName = | 
|  | 45 | + (baseDirectoryName.endsWith("" + File.separatorChar) ? baseDirectoryName : baseDirectoryName + File.separatorChar) + | 
|  | 46 | + namespaceName.replace('.', '_'); | 
|  | 47 | + | 
|  | 48 | + outputDir = new File(dirName); | 
|  | 49 | + if (!outputDir.exists()) | 
|  | 50 | + { | 
|  | 51 | + if (!outputDir.mkdirs()) | 
|  | 52 | + { | 
|  | 53 | + throw new IllegalStateException("Unable to create directory: " + dirName); | 
|  | 54 | + } | 
|  | 55 | + } | 
|  | 56 | + } | 
|  | 57 | + | 
|  | 58 | + /** | 
|  | 59 | + * Create a new output which will be a C++98 source file in the given package. | 
|  | 60 | + * | 
|  | 61 | + * The {@link java.io.Writer} should be closed once the caller has finished with it. The Writer is | 
|  | 62 | + * buffer for efficient IO operations. | 
|  | 63 | + * | 
|  | 64 | + * @param name the name of the C++ class. | 
|  | 65 | + * @return a {@link java.io.Writer} to which the source code should be written. | 
|  | 66 | + */ | 
|  | 67 | + public Writer createOutput(final String name) throws IOException | 
|  | 68 | + { | 
|  | 69 | + final File targetFile = new File(outputDir, name + ".py"); | 
|  | 70 | + return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFile), "UTF-8")); | 
|  | 71 | + } | 
|  | 72 | +} | 
0 commit comments