Skip to content

Commit

Permalink
v0.7.0: command line interface with REPL and argument conversion
Browse files Browse the repository at this point in the history
Added
-----
- Command line interface `RomanRepl` for interactive conversions along
  with project information and roman syntax help.
- Conversion of `java -jar Numerus-*.jar` argument

Changed
-------
- Ready for internationalization: hardcoded strings are moved to a
  `ResourceBundle` file

* develop:
  Bump version to v0.7.0
  Add v0.7.0 to changelog
  Add missing Main class in POM and in Manifest
  Add interpretation of command line arguments
  Move all Strings to ResourceBundle
  Add RomanMain as project main class
  Add RomanRepl for interactive conversions
  • Loading branch information
TheMatjaz committed Sep 7, 2015
2 parents db64199 + 863c299 commit c96caba
Show file tree
Hide file tree
Showing 8 changed files with 365 additions and 10 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@ _All notable changes to the [Numerus project](http://matjaz.it/numerus/)
will be documented in this file._

***************
v0.7.0
======
Codename: **Septem**
Build day: 189

Added
-----
- Command line interface `RomanRepl` for interactive conversions along with
project information and roman syntax help.
- Conversion of `java -jar Numerus-*.jar` argument

Changed
-------
- Ready for internationalization: hardcoded strings are moved to a
`ResourceBundle` file


v0.6.0
======
Codename: **Sex**
Expand Down
17 changes: 17 additions & 0 deletions nbactions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<actions>
<action>
<actionName>run</actionName>
<packagings>
<packaging>jar</packaging>
</packagings>
<goals>
<goal>process-classes</goal>
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
</goals>
<properties>
<exec.args>-classpath %classpath it.matjaz.numerus.RomanMain</exec.args>
<exec.executable>java</exec.executable>
</properties>
</action>
</actions>
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>it.matjaz</groupId>
<artifactId>Numerus</artifactId>
<version>0.6.0</version>
<version>0.7.0</version>
<packaging>jar</packaging>

<!-- Project information and description -->
Expand Down Expand Up @@ -63,7 +63,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!--<mainClass>it.matjaz.Numerus.cli.Main</mainClass>-->
<mainClass>it.matjaz.numerus.RomanMain</mainClass>
<netbeans.hint.license>NumerusLicenseHeader</netbeans.hint.license>
</properties>

Expand Down
14 changes: 12 additions & 2 deletions src/main/java/it/matjaz/numerus/RomanConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
*/
package it.matjaz.numerus;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.util.Pair;
Expand All @@ -28,6 +31,11 @@
*/
public class RomanConverter {

/**
* Default ResourceBundle containing english strings.
*/
private static final ResourceBundle romanBundle = ResourceBundle.getBundle("RomanBundle", Locale.US);

/**
* Array of references for translating roman characters into numeric values
* and vice-versa.
Expand Down Expand Up @@ -122,7 +130,8 @@ public int romanNumeralToInteger(RomanNumeral roman) {
*/
private String integerToRomanString(int arabic) throws IllegalArabicValueException {
if (arabic < 0 || arabic > 3999) {
throw new IllegalArabicValueException("Arabic numeral should be an integer in [0, 3999].");
String message = romanBundle.getString("ArabicOutOfRange");
throw new IllegalArabicValueException(message);
}
if (arabic == 0) {
return RomanNumeral.NULLA;
Expand Down Expand Up @@ -159,7 +168,8 @@ public RomanNumeral integerToRomanNumeral(int arabic) throws IllegalArabicValueE
return new RomanNumeral(integerToRomanString(arabic));
} catch (IllegalNumeralSyntaxException ex) {
Logger.getLogger(RomanConverter.class.getName()).log(Level.SEVERE, null, ex);
throw new RuntimeException("RomanConverter could not convert " + arabic + " to a syntactically correct RomanNumeral: " + ex.getMessage());
String message = MessageFormat.format(romanBundle.getString("ConverterInternalErrorWhenConvertingToRomanNumeral"), arabic, ex.getMessage());
throw new RuntimeException(message);
}
}
}
67 changes: 67 additions & 0 deletions src/main/java/it/matjaz/numerus/RomanMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2015, Matjaž <dev@matjaz.it> matjaz.it
*
* This Source Code Form is part of the project Numerus, a roman numerals
* library for Java. The library and its source code may be found on:
* https://github.com/MatjazDev/Numerus and http://matjaz.it/numerus/
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/
*/
package it.matjaz.numerus;

import java.util.Locale;
import java.util.ResourceBundle;

/**
* Numerus main class.
*
* Performs no other action than creating and starting a RomanRepl.
*
* Full path: {@code src/main/java/it/matjaz/numerus/RomanMain.java} or
* {@code it.matjaz.numerus.RomanMain}
*
* @author Matjaž <a href="mailto:dev@matjaz.it">dev@matjaz.it</a>
* <a href="http://matjaz.it">matjaz.it</a>
*/
public class RomanMain {

/**
* Default ResourceBundle containing english strings.
*/
private static final ResourceBundle romanBundle = ResourceBundle.getBundle("RomanBundle", Locale.US);

/**
* Starts the Numerus project.
*
* Starts a REPL to interact with the conversion tools. Alternatively, if
* any command line arguments are passed, tries to interprete theme without
* starting an interactive Numerus shell.
*
* @param args the numbers to convert or REPL commands to execute
*/
public static void main(String[] args) {
if (args.length > 0) {
interpreteCommandLineArguments(args);
} else {
RomanRepl repl = new RomanRepl();
repl.start();
}
}

/**
* Creates a REPL from which calls the input interpreter for each passed
* argument without creating an interactive shell waiting for input.
*
* @param args the numbers to convert or REPL commands to execute
*/
private static void interpreteCommandLineArguments(String[] args) {
RomanRepl repl = new RomanRepl();
for (String command : args) {
System.out.println(romanBundle.getString("Prompt") + command);
repl.interpreteSingleCommand(command);
}
}

}
26 changes: 20 additions & 6 deletions src/main/java/it/matjaz/numerus/RomanNumeral.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
package it.matjaz.numerus;

import java.io.Serializable;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.Objects;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -97,6 +100,11 @@ public class RomanNumeral implements Serializable, Cloneable, CharSequence {
*/
public static final String NULLA = "NULLA";

/**
* Default ResourceBundle containing english strings.
*/
private static final ResourceBundle romanBundle = ResourceBundle.getBundle("RomanBundle", Locale.US);

/**
* Serializable class version number.
* <p>
Expand Down Expand Up @@ -256,26 +264,32 @@ private static String findAllRegexMatchingSubstrings(String textToParse, String
*/
private void throwExceptionIfIllegalRomanSyntax(String symbols) throws IllegalNumeralSyntaxException {
if (symbols.isEmpty()) {
throw new IllegalNumeralSyntaxException("Empty roman numeral.");
String message = romanBundle.getString("NonRomanChars");
throw new IllegalNumeralSyntaxException(romanBundle.getString("EmptyRomanNumeral"));
}
if (symbols.length() >= 20) {
throw new IllegalNumeralSyntaxException("Impossibly long roman numeral.");
String message = romanBundle.getString("NonRomanChars");
throw new IllegalNumeralSyntaxException(romanBundle.getString("TooLongRomanNumeral"));
}
if (!symbols.matches(CORRECT_ROMAN_SYNTAX_REGEX)) {
String illegalChars;
illegalChars = findAllRegexMatchingSubstrings(symbols, NON_ROMAN_CHARS_REGEX);
if (!illegalChars.isEmpty()) {
throw new IllegalNumeralSyntaxException("Non roman characters: " + illegalChars);
String message = MessageFormat.format(romanBundle.getString("NonRomanChars"), illegalChars);
throw new IllegalNumeralSyntaxException(message);
}
illegalChars = findAllRegexMatchingSubstrings(symbols, FOUR_CONSECUTIVE_TEN_LIKE_CHARS_REGEX);
if (!illegalChars.isEmpty()) {
throw new IllegalNumeralSyntaxException("Four consecutive: " + illegalChars);
String message = MessageFormat.format(romanBundle.getString("FourConsecutiveChars"), illegalChars);
throw new IllegalNumeralSyntaxException(message);
}
illegalChars = findAllRegexMatchingSubstrings(symbols, TWO_SAME_FIVE_LIKE_CHARS_REGEX);
if (!illegalChars.isEmpty()) {
throw new IllegalNumeralSyntaxException("Two D, L or V same characters: " + illegalChars);
String message = MessageFormat.format(romanBundle.getString("TwoDLVChars"), illegalChars);
throw new IllegalNumeralSyntaxException(message);
}
throw new IllegalNumeralSyntaxException("Generic roman numeral syntax error.");
String message = romanBundle.getString("GenericRomanSyntaxError");
throw new IllegalNumeralSyntaxException(romanBundle.getString("GenericRomanSyntaxError"));
}
}

Expand Down
Loading

0 comments on commit c96caba

Please sign in to comment.