Skip to content

Commit 0676943

Browse files
committed
refactoring
1 parent eacb816 commit 0676943

1 file changed

Lines changed: 78 additions & 41 deletions

File tree

jcp/src/main/java/com/igormaznitsa/jcp/containers/FileInfoContainer.java

Lines changed: 78 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
import static java.util.Objects.requireNonNull;
2525

26-
2726
import com.igormaznitsa.jcp.context.PreprocessingState;
2827
import com.igormaznitsa.jcp.context.PreprocessorContext;
2928
import com.igormaznitsa.jcp.directives.AbstractDirectiveHandler;
@@ -128,24 +127,81 @@ private static String findTailRemover(final String str, final PreprocessorContex
128127
return result;
129128
}
130129

131-
public void setTargetFolder(final String targetFolder) {
132-
this.targetFolder = requireNonNull(targetFolder, "Target folder must not be null");
130+
/**
131+
* Check that text line starts with two commented dollar chars
132+
* @param line text line to be examined, must not be null
133+
* @param allowedWhitespaces if true then whitespaces allowed after line comment
134+
* @return true if the line starts with two commented dollar chars, false otherwise
135+
* @since 7.0.6
136+
*/
137+
public static boolean isDoubleDollarPrefixed(final String line, final boolean allowedWhitespaces) {
138+
if (allowedWhitespaces) {
139+
return DIRECTIVE_TWO_DOLLARS_PREFIXED.matcher(line).matches();
140+
} else {
141+
return line.startsWith("//$$");
142+
}
133143
}
134144

135-
public void setTargetName(final String targetName) {
136-
this.targetFileName =
137-
requireNonNull(targetFileName, "Target file name must not be null");
145+
/**
146+
* Check that text line starts with single dollar chars
147+
* @param line text line to be examined, must not be null
148+
* @param allowedWhitespaces if true then whitespaces allowed after line comment
149+
* @return true if the line starts with single dollar chars, false otherwise
150+
* @since 7.0.6
151+
*/
152+
public static boolean isSingleDollarPrefixed(final String line, final boolean allowedWhitespaces) {
153+
if (allowedWhitespaces) {
154+
return DIRECTIVE_SINGLE_DOLLAR_PREFIXED.matcher(line).matches();
155+
} else {
156+
return line.startsWith("//$");
157+
}
158+
}
159+
160+
/**
161+
* Allows to check that a text line can be considered as a JCP directive or special line.
162+
* @param line text line to be examined
163+
* @param allowedWhitespaces if true then whitespaces allowed after line comment
164+
* @return true if the line can be considered as JCP one, false otherwise
165+
* @since 7.0.6
166+
*/
167+
public static boolean isJcpCommentLine(final String line, final boolean allowedWhitespaces) {
168+
return isJcpDirectiveLine(line, allowedWhitespaces)
169+
|| isSingleDollarPrefixed(line, allowedWhitespaces);
170+
}
171+
172+
/**
173+
* Check that a text line contains comment directive.
174+
*
175+
* @param line string to be examined
176+
* @param allowWhitespaces flag to allow spaces betwee hash and started comment chars
177+
* @return true if the line contains a directive, false otherwise
178+
* @since 7.0.6
179+
*/
180+
public static boolean isJcpDirectiveLine(final String line, final boolean allowWhitespaces) {
181+
if (allowWhitespaces) {
182+
return DIRECTIVE_HASH_PREFIXED.matcher(line).matches();
183+
} else {
184+
return line.startsWith(AbstractDirectiveHandler.DIRECTIVE_PREFIX);
185+
}
138186
}
139187

188+
public void setTargetFolder(final String folder) {
189+
this.targetFolder = requireNonNull(folder, "Target folder must not be null");
190+
}
191+
192+
public void setTargetFileName(final String name) {
193+
this.targetFileName =
194+
requireNonNull(name, "Target file name must not be null");
195+
}
140196

141197
public String makeTargetFilePathAsString() {
142-
String targetFolder = this.getTargetFolder();
143-
if (!targetFolder.isEmpty() &&
144-
targetFolder.charAt(targetFolder.length() - 1) != File.separatorChar) {
145-
targetFolder = targetFolder + File.separatorChar;
198+
String folder = this.getTargetFolder();
199+
if (!folder.isEmpty() &&
200+
folder.charAt(folder.length() - 1) != File.separatorChar) {
201+
folder = folder + File.separatorChar;
146202
}
147203

148-
return targetFolder + this.getTargetFileName();
204+
return folder + this.getTargetFileName();
149205
}
150206

151207
@Override
@@ -225,28 +281,10 @@ public List<PreprocessingState.ExcludeIfInfo> processGlobalDirectives(
225281
}
226282
}
227283

228-
private boolean isDoubleDollarPrefixed(final String line, final PreprocessorContext context) {
229-
if (context.isAllowWhitespaces()) {
230-
return DIRECTIVE_TWO_DOLLARS_PREFIXED.matcher(line).matches();
231-
} else {
232-
return line.startsWith("//$$");
233-
}
234-
}
235-
236-
private boolean isSingleDollarPrefixed(final String line, final PreprocessorContext context) {
237-
if (context.isAllowWhitespaces()) {
238-
return DIRECTIVE_SINGLE_DOLLAR_PREFIXED.matcher(line).matches();
239-
} else {
240-
return line.startsWith("//$");
241-
}
242-
}
243-
244284
private boolean isHashPrefixed(final String line, final PreprocessorContext context) {
245-
if (context.isAllowWhitespaces()) {
246-
return DIRECTIVE_HASH_PREFIXED.matcher(line).matches();
247-
} else {
248-
final boolean result = line.startsWith(AbstractDirectiveHandler.DIRECTIVE_PREFIX);
249-
285+
final boolean allowedWhitespaces = context.isAllowWhitespaces();
286+
final boolean result = isJcpDirectiveLine(line, allowedWhitespaces);
287+
if (!allowedWhitespaces) {
250288
if (context.getPreprocessingState().isGlobalPhase() && !result && line.startsWith("// ") &&
251289
DIRECTIVE_HASH_PREFIXED.matcher(line).matches()) {
252290
final TextFileDataContainer textContainer =
@@ -258,9 +296,8 @@ private boolean isHashPrefixed(final String line, final PreprocessorContext cont
258296
}
259297
context.logWarning(WARNING_SPACE_BEFORE_HASH + lineInfo);
260298
}
261-
262-
return result;
263299
}
300+
return result;
264301
}
265302

266303

@@ -271,7 +308,7 @@ private String extractHashPrefixedDirective(final String line,
271308
if (matcher.find()) {
272309
return matcher.group(1);
273310
} else {
274-
throw new Error(
311+
throw new IllegalStateException(
275312
"Unexpected situation, directive is not found, contact developer! (" + line + ')');
276313
}
277314
} else {
@@ -288,7 +325,7 @@ private String extractDoubleDollarPrefixedDirective(final String line,
288325
if (matcher.find()) {
289326
tail = matcher.group(1);
290327
} else {
291-
throw new Error(
328+
throw new IllegalStateException(
292329
"Unexpected situation, '//$$' directive is not found, contact developer! (" + line +
293330
')');
294331
}
@@ -311,7 +348,7 @@ private String extractSingleDollarPrefixedDirective(final String line,
311348
if (matcher.find()) {
312349
tail = matcher.group(1);
313350
} else {
314-
throw new Error(
351+
throw new IllegalStateException(
315352
"Unexpected situation, '//$' directive is not found, contact developer! (" + line +
316353
')');
317354
}
@@ -435,15 +472,15 @@ public PreprocessingState preprocessFile(final PreprocessingState state,
435472
continue;
436473
}
437474
default:
438-
throw new Error("Unsupported result");
475+
throw new IllegalStateException("Unsupported result");
439476
}
440477
}
441478

442479
final ResetablePrinter thePrinter = requireNonNull(preprocessingState.getPrinter());
443480
if (preprocessingState.isDirectiveCanBeProcessed() &&
444481
!preprocessingState.getPreprocessingFlags()
445482
.contains(PreprocessingFlag.TEXT_OUTPUT_DISABLED)) {
446-
final boolean startsWithTwoDollars = isDoubleDollarPrefixed(leftTrimmedString, context);
483+
final boolean startsWithTwoDollars = isDoubleDollarPrefixed(leftTrimmedString, context.isAllowWhitespaces());
447484

448485
if (!startsWithTwoDollars) {
449486
stringToBeProcessed = PreprocessorUtils.processMacroses(leftTrimmedString, context);
@@ -458,7 +495,7 @@ public PreprocessingState preprocessFile(final PreprocessingState state,
458495
} else {
459496
thePrinter.print(text);
460497
}
461-
} else if (isSingleDollarPrefixed(stringToBeProcessed, context)) {
498+
} else if (isSingleDollarPrefixed(stringToBeProcessed, context.isAllowWhitespaces())) {
462499
// Output the tail of the string to the output stream without comments
463500
thePrinter.print(stringPrefix);
464501

@@ -528,7 +565,7 @@ public PreprocessingState preprocessFile(final PreprocessingState state,
528565
if (context.isVerbose()) {
529566
context.logForVerbose(String
530567
.format("Content was %s into file '%s'", (wasSaved ? "saved" : "not saved"),
531-
outFile.toString()));
568+
outFile));
532569
}
533570

534571
if (this.sourceFile != null && context.isKeepAttributes() &&

0 commit comments

Comments
 (0)