This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Options options = new Options(); | |
// create the simple option without using the OptionBuilder | |
options.addOption("o", "opt", false, "An option, no args"); | |
// simple help option using the builder | |
options.addOption(OptionBuilder.withLongOpt("help") | |
.create("h")); | |
// an option with a value separator, defaults to = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final GnuParser parser = new GnuParser(); | |
final Options options = configureOptions(); | |
// parse with the parser | |
CommandLine cmdLine = null; | |
try { | |
cmdLine = parser.parse(options, args); | |
} catch(ParseException e) { | |
e.printStackTrace(); | |
return; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// check to see if they need help | |
// notice we only use the short-version, but it checks both! | |
if(cmdLine.hasOption("h")) { | |
new HelpFormatter().printHelp("sop4j-cli", options); | |
return; | |
} | |
// check to see if they passed in an opt | |
// recommended to use the long version as it's more readable | |
if(cmdLine.hasOption("opt")) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create the velocity engine | |
final VelocityEngine velocity = new VelocityEngine(); | |
// set the logger to our Log4J root logger | |
velocity.setProperty("runtime.log.logsystem.log4j.logger", "root"); | |
// use the resource loader as it's easy with Maven's path setup | |
velocity.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath"); | |
velocity.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create the context to set all the variables | |
final VelocityContext context = new VelocityContext(); | |
// setup the context | |
context.put("user", new UserBean("bill")); | |
context.put("site", "www.sop4j.com"); | |
context.put("articles", Arrays.asList(new ArticleBean("Welcome"), | |
new ArticleBean("Parsing the Command Line"), | |
new ArticleBean("Document Templating or Mail Merge"))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// get the template | |
final Template template = velocity.getTemplate("my_template.vm"); | |
final StringWriter sw = new StringWriter(); | |
// merge the template and context | |
template.merge(context, sw); | |
// print out the resulting document | |
System.out.println(sw.toString()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## this is a single-line comment in the template | |
Dear ${user.Name}- ## notice we use the formal directive here because we want the - right after the variable | |
This is an e-mail because you signed up for $site and we're really happy that you did. | |
#* | |
This is how you check to see if a value is null | |
This is also a multi-line comment | |
*# | |
#if( $message ) | |
This special message won't be shown because our \$message is null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Dear Bill- | |
This is an e-mail because you signed up for www.sop4j.com and we're really happy that you did. | |
Here are 3 cool articles you can read: | |
- Welcome | |
- Parsing the Command Line | |
- Document Templating or Mail Merge |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Subscribe | |
public void getIntegerEvent(Integer event) throws InterruptedException { | |
TimeUnit.MILLISECONDS.sleep(sleep); | |
System.out.println(System.currentTimeMillis() + ":\t" + name + " got " + event); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final DescriptiveStatistics descriptiveStats = new DescriptiveStatistics(); // stores values | |
final SummaryStatistics summaryStats = new SummaryStatistics(); // doesn't store values | |
final Frequency frequency = new Frequency(); | |
// add numbers into our stats | |
for(int i=0; i < NUM_VALUES; ++i) { | |
values[i] = rng.nextInt(MAX_VALUE); | |
descriptiveStats.addValue(values[i]); | |
summaryStats.addValue(values[i]); |
OlderNewer