|
1 | 1 | package com.github._1c_syntax.mdclasses.metadata.utils; |
2 | 2 |
|
| 3 | +import com.github._1c_syntax.mdclasses.mdo.MDObjectBase; |
3 | 4 | import com.github._1c_syntax.mdclasses.metadata.Configuration; |
4 | 5 | import com.github._1c_syntax.mdclasses.metadata.SupportConfiguration; |
5 | 6 | import com.github._1c_syntax.mdclasses.metadata.additional.ConfigurationSource; |
6 | | -import com.github._1c_syntax.mdclasses.metadata.additional.ModuleType; |
7 | 7 | import com.github._1c_syntax.mdclasses.metadata.additional.SupportVariant; |
8 | 8 | import lombok.extern.slf4j.Slf4j; |
9 | | -import org.apache.commons.io.FileUtils; |
10 | | -import org.apache.commons.io.FilenameUtils; |
11 | 9 |
|
12 | 10 | import java.io.File; |
13 | | -import java.io.IOException; |
14 | 11 | import java.net.URI; |
15 | | -import java.nio.charset.StandardCharsets; |
16 | | -import java.nio.file.Files; |
17 | 12 | import java.nio.file.Path; |
18 | | -import java.util.Collection; |
| 13 | +import java.nio.file.Paths; |
| 14 | +import java.util.Collections; |
19 | 15 | import java.util.HashMap; |
| 16 | +import java.util.HashSet; |
20 | 17 | import java.util.Map; |
21 | | -import java.util.regex.Matcher; |
22 | | -import java.util.regex.Pattern; |
| 18 | +import java.util.Set; |
23 | 19 |
|
24 | 20 | @Slf4j |
25 | 21 | public class Common { |
26 | 22 |
|
27 | | - public static final String EXTENSION_BSL = "bsl"; |
28 | | - public static final String FILE_SEPARATOR = Pattern.quote(System.getProperty("file.separator")); |
29 | | - |
30 | | - public static final String REGEX_GUID = "uuid=\"(.*)\""; |
31 | | - public static final Pattern PATTERN_REGEX_GUID = Pattern.compile(REGEX_GUID, Pattern.MULTILINE); |
32 | 23 | public static final String EXTENSION_XML = "xml"; |
33 | 24 | public static final String EXTENSION_MDO = "mdo"; |
34 | 25 |
|
35 | 26 | private Common() { |
36 | 27 | // only statics |
37 | 28 | } |
38 | 29 |
|
39 | | - public static ModuleType getModuleTypeByFileName(String[] partsFileName) { |
40 | | - |
41 | | - String fileName = partsFileName[0]; |
42 | | - |
43 | | - ModuleType moduleType; |
44 | | - if (fileName.equalsIgnoreCase("CommandModule")) { |
45 | | - moduleType = ModuleType.CommandModule; |
46 | | - } else if (fileName.equalsIgnoreCase("ObjectModule")) { |
47 | | - moduleType = ModuleType.ObjectModule; |
48 | | - } else if (fileName.equalsIgnoreCase("ManagerModule")) { |
49 | | - moduleType = ModuleType.ManagerModule; |
50 | | - } else if (fileName.equalsIgnoreCase("ManagedApplicationModule")) { |
51 | | - moduleType = ModuleType.ManagedApplicationModule; |
52 | | - } else if (fileName.equalsIgnoreCase("OrdinaryApplicationModule")) { |
53 | | - moduleType = ModuleType.OrdinaryApplicationModule; |
54 | | - } else if (fileName.equalsIgnoreCase("SessionModule")) { |
55 | | - moduleType = ModuleType.SessionModule; |
56 | | - } else if (fileName.equalsIgnoreCase("RecordSetModule")) { |
57 | | - moduleType = ModuleType.RecordSetModule; |
58 | | - } else if (fileName.equalsIgnoreCase("ExternalConnectionModule")) { |
59 | | - moduleType = ModuleType.ExternalConnectionModule; |
60 | | - } else if (fileName.equalsIgnoreCase("ApplicationModule")) { |
61 | | - moduleType = ModuleType.ApplicationModule; |
62 | | - } else if (fileName.equalsIgnoreCase("ValueManagerModule")) { |
63 | | - moduleType = ModuleType.ValueManagerModule; |
64 | | - } else if (fileName.equalsIgnoreCase("Module")) { |
65 | | - if (partsFileName[1].equalsIgnoreCase("Form") |
66 | | - || partsFileName[2].equalsIgnoreCase("Forms") |
67 | | - || partsFileName[2].equalsIgnoreCase("CommonForms")) { |
68 | | - moduleType = ModuleType.FormModule; |
69 | | - } else { |
70 | | - moduleType = ModuleType.CommonModule; |
71 | | - } |
72 | | - } else { |
73 | | - moduleType = null; |
74 | | - LOGGER.error("Module type not find: " + fileName); |
75 | | - } |
76 | | - return moduleType; |
77 | | - } |
78 | | - |
79 | | - public static Map<URI, ModuleType> getModuleTypesByPath(Path rootPath) { |
80 | | - |
81 | | - Map<URI, ModuleType> modulesByType = new HashMap<>(); |
82 | | - String rootPathString = rootPath.toString() + System.getProperty("file.separator"); |
83 | | - Collection<File> files = FileUtils.listFiles(rootPath.toFile(), new String[]{EXTENSION_BSL}, true); |
84 | | - |
85 | | - files.stream().forEach(file -> { |
86 | | - String[] elementsPath = getPartsByPath(file.toPath().toAbsolutePath()); |
87 | | - |
88 | | - // TODO: отрефакторить |
89 | | - String thirdPath = ""; |
90 | | - if (elementsPath.length > 2) { |
91 | | - thirdPath = elementsPath[elementsPath.length - 3]; |
92 | | - } |
93 | | - |
94 | | - String[] partsFileName = new String[]{ |
95 | | - FilenameUtils.getBaseName(elementsPath[elementsPath.length - 1]), |
96 | | - elementsPath[elementsPath.length - 2], |
97 | | - thirdPath |
98 | | - }; |
99 | | - |
100 | | - ModuleType moduleType = getModuleTypeByFileName(partsFileName); |
101 | | - modulesByType.put(getAbsoluteUri(file), moduleType); |
102 | | - }); |
103 | | - |
104 | | - return modulesByType; |
105 | | - |
106 | | - } |
107 | | - |
108 | | - public static Map<URI, Map<SupportConfiguration, SupportVariant>> getModuleSupports(Configuration configuration, Map<URI, ModuleType> modulesByType) { |
109 | | - |
110 | | - final Path rootPath; |
111 | | - boolean isEDT = configuration.getConfigurationSource() == ConfigurationSource.EDT; |
| 30 | + public static Map<URI, Map<SupportConfiguration, SupportVariant>> getModuleSupports(Configuration configuration) { |
112 | 31 | Map<URI, Map<SupportConfiguration, SupportVariant>> modulesBySupport = new HashMap<>(); |
113 | | - |
114 | 32 | File fileParentConfiguration; |
115 | | - if (isEDT) { |
116 | | - rootPath = Path.of(configuration.getRootPath().toString(), "src"); |
117 | | - fileParentConfiguration = new File(rootPath.toString(), "Configuration/ParentConfigurations.bin"); |
| 33 | + if (configuration.getConfigurationSource() == ConfigurationSource.EDT) { |
| 34 | + fileParentConfiguration = Paths.get(configuration.getRootPath().toString(), |
| 35 | + "src", "Configuration/ParentConfigurations.bin") |
| 36 | + .toFile(); |
118 | 37 | } else { |
119 | | - rootPath = configuration.getRootPath(); |
120 | | - fileParentConfiguration = new File(rootPath.toString(), "Ext/ParentConfigurations.bin"); |
| 38 | + fileParentConfiguration = Paths.get(configuration.getRootPath().toString(), |
| 39 | + "Ext/ParentConfigurations.bin") |
| 40 | + .toFile(); |
121 | 41 | } |
122 | 42 |
|
123 | 43 | if (fileParentConfiguration.exists()) { |
124 | 44 | ParseSupportData supportData = new ParseSupportData(fileParentConfiguration.toPath()); |
125 | 45 | final Map<String, Map<SupportConfiguration, SupportVariant>> supportMap = supportData.getSupportMap(); |
126 | 46 |
|
127 | | - String rootPathString = rootPath.toString() + System.getProperty("file.separator"); |
128 | | - Collection<File> files = FileUtils.listFiles(rootPath.toFile(), new String[]{EXTENSION_BSL}, true); |
129 | | - |
130 | | - files.stream().forEach(file -> { |
131 | | - |
132 | | - // FIXME: неправильное поведение, считается от каталога внутри scr |
133 | | - URI uri = getAbsoluteUri(file); |
134 | | - String[] elementsPath = file.toPath().toString().replace(rootPathString, "").split(FILE_SEPARATOR); |
135 | | - |
136 | | - Map<SupportConfiguration, SupportVariant> moduleSupport = null; |
137 | | - ModuleType moduleType = modulesByType.get(uri); |
138 | | - String objectGuid = ""; |
139 | | - |
140 | | - // TODO: доработать поиски гуидов форм |
141 | | - if (isEDT) { |
142 | | - objectGuid = getObjectGuidEDT(rootPath, elementsPath, moduleType); |
143 | | - } else { |
144 | | - objectGuid = getObjectGuidOriginal(rootPath, elementsPath, moduleType); |
145 | | - } |
146 | | - |
147 | | - if (objectGuid.isEmpty()) { |
148 | | - LOGGER.info("Не удалось найти идентфикатор по объекту " + uri.toString()); |
149 | | - } else { |
150 | | - moduleSupport = supportMap.get(objectGuid); |
151 | | - } |
152 | | - |
153 | | - if (moduleSupport == null) { |
154 | | - moduleSupport = new HashMap<>(); |
155 | | - } |
156 | | - |
157 | | - modulesBySupport.put(uri, moduleSupport); |
158 | | - |
159 | | - }); |
| 47 | + configuration.getChildren() |
| 48 | + .forEach((mdoType, stringMDObjectBaseMap) -> |
| 49 | + stringMDObjectBaseMap.forEach((name, mdObject) -> { |
| 50 | + var mdoModuleSupport = getMDObjectSupport(supportMap, mdObject); |
| 51 | + if (!mdoModuleSupport.isEmpty()) { |
| 52 | + modulesBySupport.putAll(mdoModuleSupport); |
| 53 | + } |
| 54 | + }) |
| 55 | + ); |
160 | 56 | } |
161 | 57 | return modulesBySupport; |
162 | 58 | } |
163 | 59 |
|
164 | | - private static String getObjectGuidEDT(Path rootPath, String[] elementsPath, ModuleType moduleType) { |
165 | | - Path path = null; |
166 | | - if (isModuleConfiguration(moduleType)) { |
167 | | - |
168 | | - path = new File(rootPath.toString(), "Configuration/Configuration.mdo").toPath(); |
169 | | - |
170 | | - } else { |
171 | | - String second = ""; |
172 | | - if (elementsPath.length >= 3) { |
173 | | - second = elementsPath[elementsPath.length - 3]; |
174 | | - } |
175 | | - if (second.equalsIgnoreCase("Commands") || (second.equalsIgnoreCase("Forms"))) { |
176 | | - path = getSimplePath(rootPath, elementsPath, 4, EXTENSION_MDO); |
177 | | - } else { |
178 | | - path = getSimplePath(rootPath, elementsPath, 2, EXTENSION_MDO); |
179 | | - } |
180 | | - } |
181 | | - return getGuidFromFile(path); |
182 | | - } |
183 | | - |
184 | | - private static String getObjectGuidOriginal(Path rootPath, String[] elementsPath, ModuleType moduleType) { |
185 | | - String guid = ""; |
186 | | - Path path; |
187 | | - if (isModuleConfiguration(moduleType)) { |
188 | | - path = new File(rootPath.toString(), "Configuration.xml").toPath(); |
189 | | - } else { |
190 | | - String currentElement = elementsPath[elementsPath.length - 2]; |
191 | | - if (currentElement.equalsIgnoreCase("Ext")) { |
192 | | - String second = ""; |
193 | | - if (elementsPath.length >= 4) { |
194 | | - second = elementsPath[elementsPath.length - 4]; |
195 | | - } |
196 | | - if (second.equalsIgnoreCase("Commands")) { |
197 | | - path = getSimplePath(rootPath, elementsPath, 5, EXTENSION_XML); |
198 | | - } else { |
199 | | - path = getSimplePath(rootPath, elementsPath, 3, EXTENSION_XML); |
200 | | - } |
201 | | - } else if (currentElement.equalsIgnoreCase("Form")) { |
202 | | - path = getSimplePath(rootPath, elementsPath, 4, EXTENSION_XML); |
203 | | - } else { |
204 | | - return guid; |
205 | | - } |
| 60 | + private static Map<URI, Map<SupportConfiguration, SupportVariant>> getMDObjectSupport(Map<String, Map<SupportConfiguration, SupportVariant>> supportMap, MDObjectBase mdObject) { |
| 61 | + Map<URI, Map<SupportConfiguration, SupportVariant>> modulesBySupport = new HashMap<>(); |
| 62 | + Set<URI> uris = new HashSet<>(); |
| 63 | + if (mdObject.getMdoURI() != null) { |
| 64 | + uris.add(mdObject.getMdoURI()); |
206 | 65 | } |
207 | | - return getGuidFromFile(path); |
208 | | - } |
209 | | - |
210 | | - public static String getGuidFromFile(Path path) { |
211 | | - File xml = path.toFile(); |
212 | | - String guid = ""; |
213 | | - if (xml.exists()) { |
214 | | - guid = findGuidIntoFileXML(xml); |
| 66 | + if (mdObject.getModulesByType() != null) { |
| 67 | + mdObject.getModulesByType().forEach((uri, moduleType) -> uris.add(uri)); |
215 | 68 | } |
216 | | - return guid; |
217 | | - } |
218 | 69 |
|
219 | | - public static Path getSimplePath(Path rootPath, String[] elementsPath, int startPosition, String extension) { |
220 | | - String path = ""; |
221 | | - String lastElement = ""; |
222 | | - int position = 0; |
223 | | - for (String el : elementsPath) { |
224 | | - if (position == elementsPath.length - startPosition + 1) { |
225 | | - break; |
226 | | - } |
227 | | - path = path + (el + System.getProperty("file.separator")); |
228 | | - lastElement = el; |
229 | | - position++; |
230 | | - } |
231 | | - if (extension.equals(EXTENSION_XML)) { |
232 | | - path = path.substring(0, path.length() - 1) + "." + extension; |
| 70 | + Map<SupportConfiguration, SupportVariant> moduleSupport = Collections.emptyMap(); |
| 71 | + if (mdObject.getUuid() == null) { |
| 72 | + LOGGER.info("Не удалось найти идентфикатор по объекту " + mdObject); |
233 | 73 | } else { |
234 | | - path = path + lastElement + "." + extension; |
235 | | - } |
236 | | - return Path.of(rootPath.toString(), path).toAbsolutePath(); |
237 | | - } |
238 | | - |
239 | | - public static String findGuidIntoFileXML(File file) { |
240 | | - String result = ""; |
241 | | - String content = getContentFile(file); |
242 | | - Matcher matcher = PATTERN_REGEX_GUID.matcher(content); |
243 | | - if (matcher.find()) { |
244 | | - result = matcher.group(1); |
245 | | - } |
246 | | - return result; |
247 | | - } |
248 | | - |
249 | | - public static String getContentFile(File file) { |
250 | | - String content = ""; |
251 | | - try { |
252 | | - content = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8); |
253 | | - } catch (IOException e) { |
254 | | - LOGGER.error("Don't read file", e.getMessage()); |
| 74 | + moduleSupport = supportMap.getOrDefault(mdObject.getUuid(), Collections.emptyMap()); |
255 | 75 | } |
256 | | - return content; |
257 | | - } |
258 | | - |
259 | | - public static boolean isModuleConfiguration(ModuleType moduleType) { |
260 | | - return moduleType == ModuleType.ApplicationModule |
261 | | - || moduleType == ModuleType.ExternalConnectionModule |
262 | | - || moduleType == ModuleType.ManagedApplicationModule |
263 | | - || moduleType == ModuleType.OrdinaryApplicationModule |
264 | | - || moduleType == ModuleType.SessionModule; |
265 | | - } |
266 | | - |
267 | | - private static String[] getPartsByPath(Path path) { |
268 | | - var count = path.getNameCount(); |
269 | | - String[] array = new String[count]; |
270 | | - for (int position = 0; position < path.getNameCount(); position++) { |
271 | | - //array[count - 1 - position] = path.getName(position).toString(); |
272 | | - array[position] = path.getName(position).toString(); |
| 76 | + for (URI uri : uris) { |
| 77 | + modulesBySupport.put(uri, moduleSupport); |
273 | 78 | } |
274 | | - return array; |
| 79 | + return modulesBySupport; |
275 | 80 | } |
276 | 81 |
|
277 | 82 | public static Path getAbsolutePath(File file) { |
|
0 commit comments