Skip to content

Instantly share code, notes, and snippets.

@geoffreywiseman
Created July 1, 2014 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geoffreywiseman/dacb08ad59d82a04ed0b to your computer and use it in GitHub Desktop.
Save geoffreywiseman/dacb08ad59d82a04ed0b to your computer and use it in GitHub Desktop.
Uber-Simple Use of the RAML Parser as a Validator
package org.raml;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
import org.raml.parser.rule.ValidationResult;
import org.raml.parser.visitor.RamlValidationService;
public class Validator {
public static void main(String[] arguments) throws FileNotFoundException {
File file = new File(
"/path/to/myramlfile.raml");
InputStream stream = new FileInputStream(file);
List<ValidationResult> results = RamlValidationService.createDefault()
.validate(stream);
System.out.println("Validation Results:");
for (ValidationResult item : results) {
printResult(item);
}
}
private static void printResult(ValidationResult item) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("\t");
stringBuilder.append(item.getLevel());
stringBuilder.append(" ");
stringBuilder.append(item.getMessage());
stringBuilder.append(" (line ");
stringBuilder.append(item.getLine());
if (item.getStartColumn() != -1) {
stringBuilder.append(", col ");
stringBuilder.append(item.getStartColumn());
if (item.getEndColumn() != item.getStartColumn()) {
stringBuilder.append(" to ");
stringBuilder.append(item.getEndColumn());
}
stringBuilder.append(")");
System.out.println(stringBuilder.toString());
}
}
}
@geoffreywiseman
Copy link
Author

Obviously one of the first things one might like to do with this is feed the path to the RAML file in from the command-line arguments, which is an easy mod, but one I didn't yet make.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment