Skip to content

Commit bed27a1

Browse files
committed
Merge pull request #12 from EdmonL/issue11
keep comments by default, fixes #11
2 parents a206fec + 2924c39 commit bed27a1

File tree

2 files changed

+57
-39
lines changed

2 files changed

+57
-39
lines changed

src/ca/unb/meng/RuleML2TPTP.java

+24-26
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,17 @@ private static Options buildOptions() {
153153
.withLongOpt("transformer-factory")
154154
.create('t'));
155155
options.addOption(OptionBuilder
156-
.hasOptionalArg()
156+
.hasArg()
157157
.withArgName("pattern")
158-
.withDescription("keep comments matching given pattern "
159-
+ "or any pattern if pattern is omitted or empty")
160-
.withLongOpt("keep-comments")
158+
.withDescription("use give pattern to match comments")
159+
.withLongOpt("comment-pattern")
161160
.create('c'));
162161
options.addOption(OptionBuilder
163162
.hasArg()
164163
.withArgName("flags")
165164
.withDescription("flags following the specification of XPath "
166-
+ "except for flag v (see NOTES below)")
167-
.withLongOpt("matching-flags")
165+
+ "except for flag \"v\" (see NOTES below)")
166+
.withLongOpt("comment-matching-flags")
168167
.create('g'));
169168
options.addOption(OptionBuilder
170169
.hasArg()
@@ -182,12 +181,13 @@ private static Options buildOptions() {
182181
private static void printUsage(Options options) {
183182
new HelpFormatter().printHelp("java -jar ruleml2tptp.jar",
184183
null, options, String.format("%nNOTES%n"
185-
+ "If '-s' or '-o' is omitted, the standard input or "
186-
+ "output will be used accordingly.%n"
187-
+ "If '-h' is used, "
188-
+ "no XML transformation will be performed.%n"
189-
+ "Flag v means the matching behavior is reverted, "
190-
+ "so comments DO NOT match the given pattern are kept. "
184+
+ "If '-s' or '-o' is omitted, the standard input or output will be used accordingly.%n"
185+
+ "If '-h' is used, no XML transformation will be performed.%n"
186+
+ "By default, all the comments in the source will be kept in the output. "
187+
+ "Use '-c' to switch to keep only those matching the given pattern. "
188+
+ "An empty pattern has no effect. "
189+
+ "Flag \"v\" reverts the behavior by keeping those not matching the pattern, "
190+
+ "or by ignoring all the comments if no or empty pattern is given."
191191
),
192192
true);
193193
System.out.println();
@@ -246,15 +246,19 @@ public void run(CommandLine cmd) throws FileNotFoundException, IOException,
246246
new BufferedReader(new InputStreamReader(
247247
xsltNormalizer))));
248248
} else {
249+
// parse translator parameters
249250
String commentPattern = cmd.getOptionValue('c');
250-
String matchingFlags = cmd.getOptionValue('g');
251-
if (matchingFlags == null) {
252-
matchingFlags = "";
251+
if (commentPattern == null) {
252+
commentPattern = "";
253+
}
254+
String commentMatchingFlags = cmd.getOptionValue('g');
255+
if (commentMatchingFlags == null) {
256+
commentMatchingFlags = "";
253257
}
254-
boolean keepComments = cmd.hasOption('c')
255-
&& (matchingFlags.indexOf('v') == -1);
256-
matchingFlags = matchingFlags.replaceAll("v", "");
258+
boolean keepComments = (commentMatchingFlags.indexOf('v') == -1);
259+
commentMatchingFlags = commentMatchingFlags.replaceAll("v", "");
257260
boolean useCrlf = cmd.hasOption('r');
261+
258262
Transformer translator = null; // to set params
259263
if (xsltNormalizer == null) {
260264
translator = tFactory.newTransformer(
@@ -273,14 +277,8 @@ public void run(CommandLine cmd) throws FileNotFoundException, IOException,
273277
new BufferedReader(new InputStreamReader(
274278
xsltNormalizer))));
275279
}
276-
if (commentPattern != null && !commentPattern.isEmpty()) {
277-
translator.setParameter(
278-
"match-comments", commentPattern);
279-
}
280-
if (!matchingFlags.isEmpty()) {
281-
translator.setParameter(
282-
"matching-flags", matchingFlags);
283-
}
280+
translator.setParameter("comment-pattern", commentPattern);
281+
translator.setParameter("comment-matching-flags", commentMatchingFlags);
284282
translator.setParameter("keep-comments", keepComments);
285283
translator.setParameter("use-crlf", useCrlf);
286284
}

src/resources/ruleml2tptp.xslt

+33-13
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
xmlns:r="http://ruleml.org/spec">
77

88
<!-- The regular expression to match against comments. -->
9-
<xsl:param name="match-comments" select="'.*'" as="xs:string" required="no"/>
9+
<xsl:param name="comment-pattern" select="''" as="xs:string" required="no"/>
1010
<!-- The flags when matching against comments. -->
11-
<xsl:param name="matching-flags" select="''" as="xs:string" required="no"/>
11+
<xsl:param name="comment-matching-flags" select="''" as="xs:string" required="no"/>
1212
<!-- Keep or skip matched comments. -->
13-
<xsl:param name="keep-comments" select="false()" as="xs:boolean" required="no"/>
13+
<xsl:param name="keep-comments" select="true()" as="xs:boolean" required="no"/>
1414
<!-- Use CRLF instead of LF. -->
1515
<xsl:param name="use-crlf" select="false()" as="xs:boolean" required="no"/>
1616

@@ -20,16 +20,36 @@
2020

2121
<!-- Comments. -->
2222
<xsl:template match="comment()" mode="#all">
23-
<xsl:if test="matches(., $match-comments, $matching-flags) = $keep-comments">
24-
<xsl:variable name="step-1"
25-
select="replace(., '(^[ \t]+)|([ \t]+$)', '', 'm')"/>
26-
<xsl:variable name="final"
27-
select="replace($step-1, '^([^%\r\n].*)$', '% $1', 'm')"/>
28-
<xsl:value-of select="$final"/>
29-
<xsl:if test="not(matches($final, '\n$'))">
30-
<xsl:value-of select="$nl"/>
31-
</xsl:if>
32-
</xsl:if>
23+
<xsl:choose>
24+
<xsl:when test="$comment-pattern = ''">
25+
<xsl:if test="$keep-comments">
26+
<xsl:call-template name="convert-comment">
27+
<xsl:with-param name="comment" select="."/>
28+
</xsl:call-template>
29+
</xsl:if>
30+
</xsl:when>
31+
<xsl:when test="matches(., $comment-pattern, $comment-matching-flags) = $keep-comments">
32+
<xsl:call-template name="convert-comment">
33+
<xsl:with-param name="comment" select="."/>
34+
</xsl:call-template>
35+
</xsl:when>
36+
</xsl:choose>
37+
</xsl:template>
38+
39+
<xsl:template name="convert-comment">
40+
<xsl:param name="comment" as="xs:string" required="yes"/>
41+
<!-- Trim lines. -->
42+
<xsl:variable name="step1"
43+
select="replace($comment, '(^[ \t]+)|([ \t]+$)', '', 'm')"/>
44+
<!-- Trim blank lines. -->
45+
<xsl:variable name="step2"
46+
select="replace($step1, '(^\r*\n)|(\r*\n$)', '')"/>
47+
<!-- Insert '% '. -->
48+
<xsl:variable name="final"
49+
select="replace($step2, '^([^%\r\n].*)$', '% $1', 'm')"/>
50+
<!-- Switch to a new line after the comment. -->
51+
<xsl:value-of select="$final"/>
52+
<xsl:value-of select="$nl"/>
3353
</xsl:template>
3454

3555
<!-- Line break. -->

0 commit comments

Comments
 (0)