1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.forgerock.maven.plugins.xcite;
18
19 import org.forgerock.maven.plugins.xcite.utils.FileUtils;
20 import org.forgerock.maven.plugins.xcite.utils.StringUtils;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27
28
29
30
31 public class Resolver {
32
33 private boolean escapeXml;
34 private String indent;
35 private boolean outdent;
36 private File outputDirectory;
37
38
39
40
41
42
43
44
45
46
47
48
49
50 Resolver(File outputDirectory, boolean escapeXml, int indent, boolean outdent) {
51 this.outputDirectory = outputDirectory;
52 this.escapeXml = escapeXml;
53 this.indent = new String(new char[indent]).replace('\0', ' ');
54 this.outdent = outdent;
55 }
56
57
58
59
60
61
62
63
64 void resolve(File sourceDirectory, String[] files) throws IOException {
65 for (String relativePath: files) {
66 resolve(sourceDirectory, new File(relativePath));
67 }
68 }
69
70
71
72
73
74
75
76
77 void resolve(File baseDir, File file) throws IOException {
78 File absFile = new File(baseDir, file.getPath());
79
80 if (!absFile.isFile()) {
81 return;
82 }
83
84 StringBuilder stringBuilder = new StringBuilder();
85 String prefix = "";
86 for (String line: FileUtils.getStrings(absFile)) {
87 stringBuilder.append(prefix);
88 prefix = System.getProperty("line.separator");
89 stringBuilder.append(resolve(absFile, line));
90 }
91
92 File out = new File(outputDirectory, file.getPath());
93 org.codehaus.plexus.util.FileUtils.fileWrite(
94 out, stringBuilder.toString());
95 }
96
97
98
99
100
101
102
103
104
105 String resolve(File file, String line) throws IOException {
106
107
108 String[] parts = split(line);
109
110
111 int i = 0;
112 for (String part: parts) {
113 if (part == null) {
114 parts[i] = "";
115 ++i;
116 continue;
117 }
118
119
120
121 Citation citation = null;
122 if (part.contains("%")) {
123 citation = Citation.valueOf(part, "%");
124 }
125 if (citation == null) {
126 citation = Citation.valueOf(part);
127 }
128
129 if (citation == null) {
130 parts[i] = part;
131 } else {
132 String quote = getQuote(file, citation);
133
134
135
136 parts[i] = (quote.equals(citation.toString())) ? part : quote;
137 }
138
139 ++i;
140 }
141
142
143 StringBuilder stringBuilder = new StringBuilder();
144 for (String part: parts) {
145 stringBuilder.append(part);
146 }
147 return stringBuilder.toString();
148 }
149
150
151
152
153
154
155
156 String[] split(String line) {
157 if (line == null) {
158 return null;
159 }
160
161 if (line.isEmpty()) {
162 return new String[1];
163 }
164
165 ArrayList<String> parts = new ArrayList<String>();
166
167
168
169
170
171
172
173
174 Pattern citationCandidate = Pattern.compile("(\\[[^\\]]+\\])");
175 Matcher matcher = citationCandidate.matcher(line);
176 int index = 0;
177 while (matcher.find()) {
178 String before = line.substring(index, matcher.start());
179 if (!before.isEmpty()) {
180 parts.add(before);
181 }
182 parts.add(matcher.group());
183 index = matcher.end();
184 }
185 if (index < line.length()) {
186 parts.add(line.substring(index));
187 }
188
189 String[] results = new String[parts.size()];
190 return parts.toArray(results);
191 }
192
193
194
195
196
197
198
199
200
201 String getQuote(File file, Citation citation) throws IOException {
202
203
204
205 File citedFile = new File(citation.getPath());
206 if (!citedFile.isAbsolute()) {
207 String currentDirectory = file.getParent();
208 citedFile = new File(currentDirectory, citedFile.getPath());
209 }
210
211
212 if (!citedFile.exists() || !citedFile.isFile()) {
213 return citation.toString();
214 }
215
216
217 ArrayList<String> quoteLines = StringUtils.extractQuote(
218 FileUtils.getStrings(citedFile), citation.getStart(), citation.getEnd());
219
220 if (escapeXml) {
221 quoteLines = StringUtils.escapeXml(quoteLines);
222 }
223
224 if (outdent) {
225 quoteLines = StringUtils.outdent(quoteLines);
226 }
227
228 if (!indent.isEmpty()) {
229 quoteLines = StringUtils.indent(quoteLines, indent);
230 }
231
232
233
234 StringBuilder stringBuilder = new StringBuilder();
235 String prefix = "";
236 for (String quoteLine: quoteLines) {
237 stringBuilder.append(prefix);
238 prefix = System.getProperty("line.separator");
239 stringBuilder.append(resolve(citedFile, quoteLine));
240 }
241 return stringBuilder.toString();
242 }
243 }