1 package eu.simuline.m2latex.core;
2
3 import java.io.File;
4 import java.util.EnumMap;
5 import java.util.Map;
6 import java.util.Optional;
7
8 import com.florianingerl.util.regex.MatchResult;
9
10 /**
11 * Container which comprises, besides the latex main file
12 * also several files creation of which shall be done once for ever.
13 */
14 class LatexMainDesc implements Comparable<LatexMainDesc> {
15 final File texFile;
16 final File pdfFile;
17 final File dviFile;
18 final File xdvFile;
19
20 final File logFile;
21 final File auxFile;
22
23 final File idxFile;
24 final File indFile;
25 final File ilgFile;
26
27 final File glsFile;
28 final File glgFile;
29
30 final File xxxFile;
31
32 final File parentDir;
33
34 final Map<Auxiliary, FileId> aux2fileId;
35
36
37 private final MatchResult matchRes;
38
39 // private static boolean matches(Matcher matcher,
40 // LatexMainParameterNames name) {
41 // return matcher.group(name.toString()) != null;
42 // }
43
44 // private static void printGroups(Matcher matcher) {
45 // List<String> names = Arrays.asList(LatexMainParameterNames.values())
46 // .stream().map(val -> val.toString()).collect(Collectors.toList());;
47 // for (String name : names) {
48 // System.out.println(name + " |" + matcher.group(name) + "|");
49 // }
50 // }
51
52 // static Optional<LatexMainDesc> getLatexMain(File texFile, Matcher matcher) {
53 // printGroups(matcher);
54
55 // return (matches(matcher, LatexMainParameterNames.programMagic)
56 // || matches(matcher, LatexMainParameterNames.targetsMagic)
57 // || matches(matcher, LatexMainParameterNames.docClassMagic)
58 // || matches(matcher, LatexMainParameterNames.docClass))
59 // ? Optional.of(new LatexMainDesc(texFile, matcher))
60 // : Optional.empty();
61 // }
62
63 // // TBD: eliminate
64 // // For tests only.
65 // LatexMainDesc(File texFile) {
66 // this(texFile, null);
67 // }
68
69 LatexMainDesc(File texFile, MatchResult matchRes) {
70 this.matchRes = matchRes;
71 this.texFile = texFile;
72 this.xxxFile =
73 TexFileUtils.replaceSuffix(texFile, LatexProcessor.SUFFIX_VOID);
74 this.pdfFile = withSuffix(LatexProcessor.SUFFIX_PDF);
75 this.dviFile = withSuffix(LatexProcessor.SUFFIX_DVI);
76 this.xdvFile = withSuffix(LatexProcessor.SUFFIX_XDV);
77 this.logFile = withSuffix(LatexProcessor.SUFFIX_LOG);
78 this.auxFile = withSuffix(LatexProcessor.SUFFIX_AUX);
79
80 this.idxFile = withSuffix(LatexProcessor.SUFFIX_IDX);
81 this.indFile = withSuffix(LatexProcessor.SUFFIX_IND);
82 this.ilgFile = withSuffix(LatexProcessor.SUFFIX_ILG);
83
84 this.glsFile = withSuffix(LatexProcessor.SUFFIX_GLS);
85 this.glgFile = withSuffix(LatexProcessor.SUFFIX_GLG);
86 this.parentDir = this.texFile.getParentFile();
87 this.aux2fileId = new EnumMap<>(Auxiliary.class);
88 }
89
90 /**
91 * Returns the content of the group of the name specified,
92 * if matched, else <code>null</code>.
93 *
94 * @param groupName
95 * a representation of the name of a group in a regular expression.
96 * @return
97 * the text matched in the group as an optional,
98 * where empty optional indicates
99 * that the pattern has the group but nothing matches,
100 * e.g. because of an alternative like <code>(?<name>x)?</code>.
101 * @throws IllegalArgumentException
102 * If the matching pattern has no capturing group with the given name.
103 */
104 Optional<String> groupMatch(LatexMainParameterNames groupName) {
105 // formally this may throw a IllegalStateException, but this is excluded.
106 return Optional.ofNullable(this.matchRes.group(groupName.toString()));
107 }
108
109 boolean groupMatches(LatexMainParameterNames groupName) {
110 return this.matchRes.group(groupName.toString()) != null;
111 }
112
113 // Currently, document class is always defined.
114 String getDocClass() {
115 String res = this.matchRes.group(LatexMainParameterNames.docClass.toString());
116 assert res != null;
117 return res;
118 }
119
120 // Optional<String> getDocClass(LogWrapper log) {
121 // String docClassMagic = groupMatch(LatexMainParameterNames.docClassMagic);
122 // String docClass = groupMatch(LatexMainParameterNames.docClass);
123 // if (docClassMagic != null) {
124 // if (docClass != null) {
125 // log.warn("XXXX: Doc class in magic comment '" + docClassMagic
126 // + "'' overwrites '" + docClass + "' in documentclass/style. ");
127 // }
128 // return Optional.of(docClassMagic);
129 // }
130 // return Optional.ofNullable(docClass);
131 // }
132
133 File withSuffix(String suffix) {
134 return TexFileUtils.appendSuffix(this.xxxFile, suffix);
135 }
136
137 public int compareTo(LatexMainDesc other) {
138 return this.texFile.compareTo(other.texFile);
139 }
140
141 public String toString() {
142 return "<LatexMainDesc texFile='" + this.texFile.getName() + "'>" +
143 this.matchRes + "</LatexMainDesc>";
144 }
145 } // class LatexMainDesc