|
| 1 | +// Copyright 2024 Goldman Sachs |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package org.finos.legend.pure.m3.serialization.compiler.file; |
| 16 | + |
| 17 | +import org.finos.legend.pure.m3.serialization.compiler.ExtensibleSerializer; |
| 18 | + |
| 19 | +import java.nio.file.FileSystems; |
| 20 | +import java.util.Arrays; |
| 21 | + |
| 22 | +public class FilePathProvider extends ExtensibleSerializer<FilePathProviderExtension> |
| 23 | +{ |
| 24 | + private FilePathProvider(Iterable<? extends FilePathProviderExtension> extensions, int defaultVersion) |
| 25 | + { |
| 26 | + super(extensions, defaultVersion); |
| 27 | + } |
| 28 | + |
| 29 | + public String getElementFilePath(String elementPath) |
| 30 | + { |
| 31 | + return getElementFilePath(elementPath, null); |
| 32 | + } |
| 33 | + |
| 34 | + public String getElementFilePath(String elementPath, String fsSeparator) |
| 35 | + { |
| 36 | + return getElementFilePath(elementPath, fsSeparator, getDefaultExtension()); |
| 37 | + } |
| 38 | + |
| 39 | + public String getElementFilePath(String elementPath, int version) |
| 40 | + { |
| 41 | + return getElementFilePath(elementPath, null, version); |
| 42 | + } |
| 43 | + |
| 44 | + public String getElementFilePath(String elementPath, String fsSeparator, int version) |
| 45 | + { |
| 46 | + return getElementFilePath(elementPath, fsSeparator, getExtension(version)); |
| 47 | + } |
| 48 | + |
| 49 | + private String getElementFilePath(String elementPath, String fsSeparator, FilePathProviderExtension extension) |
| 50 | + { |
| 51 | + return extension.getElementFilePath( |
| 52 | + validateNonEmpty(elementPath, "element path"), |
| 53 | + resolveFSSeparator(fsSeparator)); |
| 54 | + } |
| 55 | + |
| 56 | + public String getModuleMetadataFilePath(String moduleName) |
| 57 | + { |
| 58 | + return getModuleMetadataFilePath(moduleName, null); |
| 59 | + } |
| 60 | + |
| 61 | + public String getModuleMetadataFilePath(String moduleName, String fsSeparator) |
| 62 | + { |
| 63 | + return getModuleMetadataFilePath(moduleName, fsSeparator, getDefaultExtension()); |
| 64 | + } |
| 65 | + |
| 66 | + public String getModuleMetadataFilePath(String moduleName, int version) |
| 67 | + { |
| 68 | + return getModuleMetadataFilePath(moduleName, null, version); |
| 69 | + } |
| 70 | + |
| 71 | + public String getModuleMetadataFilePath(String moduleName, String fsSeparator, int version) |
| 72 | + { |
| 73 | + return getModuleMetadataFilePath(moduleName, fsSeparator, getExtension(version)); |
| 74 | + } |
| 75 | + |
| 76 | + private String getModuleMetadataFilePath(String moduleName, String fsSeparator, FilePathProviderExtension extension) |
| 77 | + { |
| 78 | + return extension.getModuleMetadataFilePath( |
| 79 | + validateNonEmpty(moduleName, "module name"), |
| 80 | + resolveFSSeparator(fsSeparator)); |
| 81 | + } |
| 82 | + |
| 83 | + private static String validateNonEmpty(String string, String description) |
| 84 | + { |
| 85 | + if ((string == null) || string.isEmpty()) |
| 86 | + { |
| 87 | + throw new IllegalArgumentException(description + " may not be null or empty"); |
| 88 | + } |
| 89 | + return string; |
| 90 | + } |
| 91 | + |
| 92 | + private static String resolveFSSeparator(String fsSeparator) |
| 93 | + { |
| 94 | + return (fsSeparator == null) ? getDefaultFSSeparator() : fsSeparator; |
| 95 | + } |
| 96 | + |
| 97 | + private static String getDefaultFSSeparator() |
| 98 | + { |
| 99 | + return FileSystems.getDefault().getSeparator(); |
| 100 | + } |
| 101 | + |
| 102 | + public static Builder builder() |
| 103 | + { |
| 104 | + return new Builder(); |
| 105 | + } |
| 106 | + |
| 107 | + public static class Builder extends ExtensibleSerializer.AbstractBuilder<FilePathProviderExtension, FilePathProvider> |
| 108 | + { |
| 109 | + private Builder() |
| 110 | + { |
| 111 | + } |
| 112 | + |
| 113 | + public Builder withExtension(FilePathProviderExtension extension) |
| 114 | + { |
| 115 | + addExtension(extension); |
| 116 | + return this; |
| 117 | + } |
| 118 | + |
| 119 | + public Builder withExtensions(Iterable<? extends FilePathProviderExtension> extensions) |
| 120 | + { |
| 121 | + addExtensions(extensions); |
| 122 | + return this; |
| 123 | + } |
| 124 | + |
| 125 | + public Builder withExtensions(FilePathProviderExtension... extensions) |
| 126 | + { |
| 127 | + return withExtensions(Arrays.asList(extensions)); |
| 128 | + } |
| 129 | + |
| 130 | + public Builder withLoadedExtensions(ClassLoader classLoader) |
| 131 | + { |
| 132 | + loadExtensions(classLoader); |
| 133 | + return this; |
| 134 | + } |
| 135 | + |
| 136 | + public Builder withLoadedExtensions() |
| 137 | + { |
| 138 | + loadExtensions(); |
| 139 | + return this; |
| 140 | + } |
| 141 | + |
| 142 | + public Builder withDefaultVersion(int defaultVersion) |
| 143 | + { |
| 144 | + setDefaultVersion(defaultVersion); |
| 145 | + return this; |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + protected FilePathProvider build(Iterable<FilePathProviderExtension> extensions, int defaultVersion) |
| 150 | + { |
| 151 | + return new FilePathProvider(extensions, defaultVersion); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + protected Class<FilePathProviderExtension> getExtensionClass() |
| 156 | + { |
| 157 | + return FilePathProviderExtension.class; |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments