Skip to content

Commit

Permalink
Merge pull request #12 from EdmonL/issue11
Browse files Browse the repository at this point in the history
keep comments by default, fixes #11
  • Loading branch information
edmonl committed Nov 15, 2014
2 parents a206fec + 2924c39 commit bed27a1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 39 deletions.
50 changes: 24 additions & 26 deletions src/ca/unb/meng/RuleML2TPTP.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,17 @@ private static Options buildOptions() {
.withLongOpt("transformer-factory")
.create('t'));
options.addOption(OptionBuilder
.hasOptionalArg()
.hasArg()
.withArgName("pattern")
.withDescription("keep comments matching given pattern "
+ "or any pattern if pattern is omitted or empty")
.withLongOpt("keep-comments")
.withDescription("use give pattern to match comments")
.withLongOpt("comment-pattern")
.create('c'));
options.addOption(OptionBuilder
.hasArg()
.withArgName("flags")
.withDescription("flags following the specification of XPath "
+ "except for flag v (see NOTES below)")
.withLongOpt("matching-flags")
+ "except for flag \"v\" (see NOTES below)")
.withLongOpt("comment-matching-flags")
.create('g'));
options.addOption(OptionBuilder
.hasArg()
Expand All @@ -182,12 +181,13 @@ private static Options buildOptions() {
private static void printUsage(Options options) {
new HelpFormatter().printHelp("java -jar ruleml2tptp.jar",
null, options, String.format("%nNOTES%n"
+ "If '-s' or '-o' is omitted, the standard input or "
+ "output will be used accordingly.%n"
+ "If '-h' is used, "
+ "no XML transformation will be performed.%n"
+ "Flag v means the matching behavior is reverted, "
+ "so comments DO NOT match the given pattern are kept. "
+ "If '-s' or '-o' is omitted, the standard input or output will be used accordingly.%n"
+ "If '-h' is used, no XML transformation will be performed.%n"
+ "By default, all the comments in the source will be kept in the output. "
+ "Use '-c' to switch to keep only those matching the given pattern. "
+ "An empty pattern has no effect. "
+ "Flag \"v\" reverts the behavior by keeping those not matching the pattern, "
+ "or by ignoring all the comments if no or empty pattern is given."
),
true);
System.out.println();
Expand Down Expand Up @@ -246,15 +246,19 @@ public void run(CommandLine cmd) throws FileNotFoundException, IOException,
new BufferedReader(new InputStreamReader(
xsltNormalizer))));
} else {
// parse translator parameters
String commentPattern = cmd.getOptionValue('c');
String matchingFlags = cmd.getOptionValue('g');
if (matchingFlags == null) {
matchingFlags = "";
if (commentPattern == null) {
commentPattern = "";
}
String commentMatchingFlags = cmd.getOptionValue('g');
if (commentMatchingFlags == null) {
commentMatchingFlags = "";
}
boolean keepComments = cmd.hasOption('c')
&& (matchingFlags.indexOf('v') == -1);
matchingFlags = matchingFlags.replaceAll("v", "");
boolean keepComments = (commentMatchingFlags.indexOf('v') == -1);
commentMatchingFlags = commentMatchingFlags.replaceAll("v", "");
boolean useCrlf = cmd.hasOption('r');

Transformer translator = null; // to set params
if (xsltNormalizer == null) {
translator = tFactory.newTransformer(
Expand All @@ -273,14 +277,8 @@ public void run(CommandLine cmd) throws FileNotFoundException, IOException,
new BufferedReader(new InputStreamReader(
xsltNormalizer))));
}
if (commentPattern != null && !commentPattern.isEmpty()) {
translator.setParameter(
"match-comments", commentPattern);
}
if (!matchingFlags.isEmpty()) {
translator.setParameter(
"matching-flags", matchingFlags);
}
translator.setParameter("comment-pattern", commentPattern);
translator.setParameter("comment-matching-flags", commentMatchingFlags);
translator.setParameter("keep-comments", keepComments);
translator.setParameter("use-crlf", useCrlf);
}
Expand Down
46 changes: 33 additions & 13 deletions src/resources/ruleml2tptp.xslt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
xmlns:r="http://ruleml.org/spec">

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

Expand All @@ -20,16 +20,36 @@

<!-- Comments. -->
<xsl:template match="comment()" mode="#all">
<xsl:if test="matches(., $match-comments, $matching-flags) = $keep-comments">
<xsl:variable name="step-1"
select="replace(., '(^[ \t]+)|([ \t]+$)', '', 'm')"/>
<xsl:variable name="final"
select="replace($step-1, '^([^%\r\n].*)$', '% $1', 'm')"/>
<xsl:value-of select="$final"/>
<xsl:if test="not(matches($final, '\n$'))">
<xsl:value-of select="$nl"/>
</xsl:if>
</xsl:if>
<xsl:choose>
<xsl:when test="$comment-pattern = ''">
<xsl:if test="$keep-comments">
<xsl:call-template name="convert-comment">
<xsl:with-param name="comment" select="."/>
</xsl:call-template>
</xsl:if>
</xsl:when>
<xsl:when test="matches(., $comment-pattern, $comment-matching-flags) = $keep-comments">
<xsl:call-template name="convert-comment">
<xsl:with-param name="comment" select="."/>
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:template>

<xsl:template name="convert-comment">
<xsl:param name="comment" as="xs:string" required="yes"/>
<!-- Trim lines. -->
<xsl:variable name="step1"
select="replace($comment, '(^[ \t]+)|([ \t]+$)', '', 'm')"/>
<!-- Trim blank lines. -->
<xsl:variable name="step2"
select="replace($step1, '(^\r*\n)|(\r*\n$)', '')"/>
<!-- Insert '% '. -->
<xsl:variable name="final"
select="replace($step2, '^([^%\r\n].*)$', '% $1', 'm')"/>
<!-- Switch to a new line after the comment. -->
<xsl:value-of select="$final"/>
<xsl:value-of select="$nl"/>
</xsl:template>

<!-- Line break. -->
Expand Down

0 comments on commit bed27a1

Please sign in to comment.