This repository has been archived by the owner on Jan 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
DefaultDocumentationImporter.java
81 lines (68 loc) · 2.96 KB
/
DefaultDocumentationImporter.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.structurizr.importer.documentation;
import com.structurizr.documentation.Documentable;
import com.structurizr.documentation.Format;
import com.structurizr.documentation.Section;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Arrays;
/**
* This implementation scans a given directory and automatically imports all Markdown or AsciiDoc
* files in that directory.
*
* See https://structurizr.com/help/documentation/headings for details of how section headings and numbering are handled.
*/
public class DefaultDocumentationImporter implements DocumentationImporter {
/**
* Imports Markdown/AsciiDoc files from the specified path, each in its own section.
*
* @param documentable the item that documentation should be associated with
* @param path the path to import documentation from
*/
@Override
public void importDocumentation(Documentable documentable, File path) {
if (documentable == null) {
throw new IllegalArgumentException("A workspace, software system, container, or component must be specified.");
}
if (path == null) {
throw new IllegalArgumentException("A path must be specified.");
} else if (!path.exists()) {
throw new IllegalArgumentException(path.getAbsolutePath() + " does not exist.");
}
try {
if (path.isDirectory()) {
File[] filesInDirectory = path.listFiles();
if (filesInDirectory != null) {
Arrays.sort(filesInDirectory);
for (File file : filesInDirectory) {
if (!file.isDirectory() && !file.getName().startsWith(".")) {
importFile(documentable, file);
}
}
}
} else {
importFile(documentable, path);
}
// now trim the filenames
for (Section section : documentable.getDocumentation().getSections()) {
String filename = section.getFilename();
filename = filename.replace(path.getCanonicalPath(), "");
if (filename.startsWith("/")) {
filename = filename.substring(1);
}
section.setFilename(filename);
}
} catch (Exception e) {
throw new DocumentationImportException(e);
}
}
protected void importFile(Documentable documentable, File file) throws Exception {
if (FormatFinder.isMarkdownOrAsciiDoc(file)) {
Format format = FormatFinder.findFormat(file);
String content = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
Section section = new Section(format, content);
section.setFilename(file.getCanonicalPath());
documentable.getDocumentation().addSection(section);
}
}
}