1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.config.source.yaml;
35
36 import info.magnolia.config.registry.DefinitionMetadataBuilder;
37 import info.magnolia.resourceloader.Resource;
38
39 import java.util.regex.Matcher;
40 import java.util.regex.Pattern;
41
42 import org.apache.commons.io.FilenameUtils;
43 import org.apache.commons.lang3.StringUtils;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47
48
49
50
51
52
53 public class RegexBasedPathToMetadataInferrer implements PathToMetadataInferrer {
54
55 private static final Logger log = LoggerFactory.getLogger(RegexBasedPathToMetadataInferrer.class);
56
57 public static final String GROUP_MODULE = "module";
58 public static final String GROUP_NAME = "name";
59 public static final String GROUP_RELATIVE_PATH = "relPath";
60
61 private final Pattern pathPattern;
62
63 public RegexBasedPathToMetadataInferrer(Pattern pathPattern) {
64 this.pathPattern = pathPattern;
65 }
66
67 @Override
68 public DefinitionMetadataBuilder populateFrom(DefinitionMetadataBuilder metadataBuilder, Resource resource) {
69 final String relativePath = resource.getPath();
70 final Matcher matcher = matcherFor(relativePath);
71 final String fallbackDefinitionName = fallbackDefinitionName(matcher, relativePath);
72 final String moduleName = moduleName(matcher, relativePath);
73 final String relativeLocation = relativeLocation(matcher, relativePath);
74 return metadataBuilder
75 .name(fallbackDefinitionName)
76 .module(moduleName)
77 .relativeLocation(relativeLocation);
78 }
79
80 protected String fallbackDefinitionName(Matcher matcher, String path) {
81 if (matcher.groupCount() < 1) {
82
83 final String nameWithoutExtension = FilenameUtils.getBaseName(path);
84 log.debug("Pattern {} does not have a capturing group to infer definition name from path {}; falling back to filename without extension: {}", pathPattern, path, nameWithoutExtension);
85 return nameWithoutExtension;
86 }
87
88 try {
89
90
91
92
93 return matcher.group(GROUP_NAME);
94 } catch (IllegalArgumentException e) {
95 if (matcher.groupCount() > 1) {
96
97 return matcher.group(matcher.groupCount());
98 } else {
99 return matcher.group(1);
100 }
101 }
102 }
103
104 protected String moduleName(Matcher matcher, String path) {
105 if (matcher.groupCount() < 2) {
106 log.debug("Pattern {} does not have a capturing group to infer module name from path {}; falling back to default: {}", pathPattern, path, "yaml");
107 return "yaml";
108 }
109
110 try {
111
112
113
114
115 return matcher.group(GROUP_MODULE);
116 } catch (IllegalArgumentException e) {
117
118 return matcher.group(1);
119 }
120 }
121
122 protected String relativeLocation(Matcher matcher, String path) {
123
124 try {
125
126
127 String group = matcher.group(GROUP_RELATIVE_PATH);
128 return StringUtils.isNotBlank(group) ? group + matcher.group(GROUP_NAME) : matcher.group(GROUP_NAME);
129 } catch (IllegalArgumentException e) {
130 if (matcher.groupCount() > 1) {
131
132 String subPath = path.substring(matcher.end(1), matcher.end(matcher.groupCount()));
133 return StringUtils.strip(subPath, "/");
134 }
135 return FilenameUtils.removeExtension(StringUtils.removeStart(path, "/"));
136 }
137 }
138
139 protected Matcher matcherFor(String relativePath) {
140 final Matcher matcher = pathPattern.matcher(relativePath);
141 if (!matcher.matches()) {
142
143 throw new IllegalStateException(String.format("%s doesn't match pattern %s, rejecting.", relativePath, pathPattern));
144 }
145 return matcher;
146 }
147
148 }