Java源码示例:org.raml.v2.api.RamlModelResult
示例1
public static void main(String[] args) throws IOException {
// URL url = Main.class.getResource("api.raml");
URL url = Main.class.getResource("fun.raml");
Reader reader = new InputStreamReader(url.openStream());
RamlModelResult ramlModelResult = new RamlModelBuilder().buildApi(reader, url.getFile());
if (ramlModelResult.hasErrors()) {
for (ValidationResult validationResult : ramlModelResult.getValidationResults()) {
System.err.println(validationResult);
}
} else {
Api realApi = ramlModelResult.getApiV10();
Emitter emitter = new Emitter();
emitter.emit(realApi);
}
}
示例2
private static ApiModel wrapApiModel(RamlModelResult ramlModelResult) throws RuntimeException
{
if (ramlModelResult.hasErrors())
{
throw new RuntimeException("Invalid RAML descriptor " + Iterables.toString(transform(ramlModelResult.getValidationResults(), new Function<ValidationResult, String>()
{
@Nullable
@Override
public String apply(ValidationResult input)
{
return input.getMessage();
}
})));
}
if (ramlModelResult.isVersion08())
{
return new ApiModelImpl(ramlModelResult.getApiV08());
}
else
{
return new org.mule.raml.impl.v10.model.ApiModelImpl(ramlModelResult.getApiV10());
}
}
示例3
@Override
public RamlRoot buildRamlRoot(String ramlFileUrl) {
RamlModelResult ramlModelResult = new RamlModelBuilder().buildApi(ramlFileUrl);
if (ramlModelResult.hasErrors()) {
List<String> errors = ramlModelResult.getValidationResults().stream().map(validationResult -> validationResult.getMessage())
.collect(Collectors.toList());
throw new InvalidRamlResourceException(ramlFileUrl, errors);
}
// The Api is created by RamlModelBuilder during runtime via a yagi
// ModelProxyBuilder.
// In org.raml.v2 there is no direct implementation for Api interface
// during compile time.
Api api = ramlModelResult.getApiV10();
return new RJP10V2RamlRoot(api);
}
示例4
private static RamlModelFactory createRamlModelFactoryFor(String ramlURL) {
RamlModelResult ramlModelResult = new RamlModelBuilder().buildApi(ramlURL);
if (ramlModelResult.hasErrors()) {
if (logger.isErrorEnabled()) {
logger.error("Loaded RAML has validation errors: {}",
StringUtils.collectionToCommaDelimitedString(ramlModelResult.getValidationResults()));
}
throw new InvalidRamlException(ramlURL, ramlModelResult.getValidationResults());
}
if (!ramlModelResult.isVersion10()) {
logger.error("Unsupported version detected!");
throw new UnsupportedRamlVersionException(RamlVersion.V10);
}
logger.info("RJP10V2RamlModelFactory Instantiated");
return new RJP10V2RamlModelFactory();
}
示例5
public static Api load(InputStream is, String directory) {
RamlModelResult ramlModelResult =
new RamlModelBuilder().buildApi(
new InputStreamReader(is), directory);
if (ramlModelResult.hasErrors()) {
for (ValidationResult validationResult : ramlModelResult.getValidationResults()) {
System.out.println(validationResult.getMessage());
}
throw new AssertionError();
} else {
return ramlModelResult.getApiV10();
}
}
示例6
public static void main(String[] args) {
RamlModelResult ramlModelResult = new RamlModelBuilder().buildApi(Paths.get("/Users/mduesterhoeft/dev/ng-checkout/build/ramldoc/api-public.raml").toFile());
if (ramlModelResult.hasErrors()) {
for (ValidationResult validationResult : ramlModelResult.getValidationResults())
{
System.out.println(validationResult.getMessage());
}
} else {
System.out.println("validation successful");
Api api = ramlModelResult.getApiV10();
}
}
示例7
public void getTypes(Path sourcePath){
RamlModelResult ramlModelResult = new RamlModelBuilder().buildApi(sourcePath.toFile().getAbsolutePath());
if (!ramlModelResult.hasErrors()){
//valid raml
Api api = ramlModelResult.getApiV10();
if(api != null){
List<TypeDeclaration> types = api.types();
for (int i = 0; i < types.size(); i++) {
TYPE2PATH_MAP.put(types.get(i).name(), types.get(i).type());
}
}
}
}
示例8
public static void main(String[] args) throws Exception {
Api api = document()
.baseUri("http:fun.com/fun")
.title("Hello!")
.version("1.0beta6")
.withTypes(
TypeDeclarationBuilder.typeDeclaration("Foo").ofType(
TypeBuilder.type("object")
.withFacets(FacetBuilder.facet("required").ofType("boolean"))
.withAnnotations(AnnotationBuilder.annotation("Foo")
.withProperties(PropertyValueBuilder.property("time", "2022-02-02"), PropertyValueBuilder.propertyOfArray("count", 1,2)))
),
TypeDeclarationBuilder.typeDeclaration("EnumFoo").ofType(TypeBuilder.type().enumValues("UN", "DEUX")),
TypeDeclarationBuilder.typeDeclaration("EnumNum").ofType(TypeBuilder.type("integer").enumValues(1,2)),
TypeDeclarationBuilder.typeDeclaration("Goo").ofType(TypeBuilder.type("object")),
TypeDeclarationBuilder.typeDeclaration("GooWithExamples").ofType(TypeBuilder.type("object")
.withProperty(TypePropertyBuilder.property("count", "integer"),TypePropertyBuilder.property("realType", "Foo"))
.withExamples(ExamplesBuilder.example("one").withPropertyValue(PropertyValueBuilder.property("count", 1)))
),
TypeDeclarationBuilder.typeDeclaration("GooWithExample").ofType(TypeBuilder.type("object")
.withProperty(
TypePropertyBuilder.property("count", "integer"),
TypePropertyBuilder.property("counts", TypeBuilder.arrayOf(TypeBuilder.type("string"))),
TypePropertyBuilder.property("realType", "Foo"))
.withExample(ExamplesBuilder.singleExample().strict(false).withPropertyValue(PropertyValueBuilder.property("count", 1)))
)
)
.withAnnotationTypes(
AnnotationTypeBuilder.annotationType("Foo").withProperty(property("time", "date-only")).withProperty(property("count", "integer[]"))
).buildModel();
/* .withResources(
resource("/no")
.description("fooo!!!")
.displayName("Mama!!!")
.with(
method("get")
.description("fooofooofooo")
.withQueryParameter(ParameterBuilder.parameter("apaaa").ofType("integer"))
.withAnnotations(AnnotationBuilder.annotation("Foo").withProperties(
PropertyValueBuilder.property("time", "2022-02-02"),
PropertyValueBuilder.propertyOfArray("count", 7)))
.withBodies(
BodyBuilder.body("application/json")
.ofType(TypeBuilder.type("Foo", "Goo")
.withProperty(TypePropertyBuilder.property("foo", "string"))
)
).withResponses(response(200))
)
*/
// ).buildModel();
StringTypeDeclaration stdzero = (StringTypeDeclaration) api.types().get(0);
System.err.println(stdzero.enumValues());
System.err.println(api.types().get(0).name());
System.err.println();
Emitter emitter = new Emitter();
emitter.emit(api);
StringWriter writer = new StringWriter();
emitter.emit(api, writer);
RamlModelResult re_read = new RamlModelBuilder().buildApi(new StringReader(writer.toString()), ".");
if (re_read.hasErrors()) {
for (ValidationResult validationResult : re_read.getValidationResults()) {
System.err.println(validationResult);
}
}
StringTypeDeclaration std = (StringTypeDeclaration) re_read.getApiV10().types().get(0);
System.err.println(std.enumValues());
ObjectTypeDeclaration third = (ObjectTypeDeclaration) re_read.getApiV10().types().get(3);
System.err.println(third.properties().get(0).name());
System.err.println(third.properties().get(0).type());
System.err.println(third.properties().get(1).name());
System.err.println(third.properties().get(1).type());
System.err.println(third.properties().get(1).parentTypes().get(0).type());
}
示例9
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (skip) {
getLog().info("Skipping execution...");
return;
}
if (ramlFile == null) {
throw new MojoExecutionException("ramlFile is not defined");
}
try {
FileUtils.forceMkdir(outputDirectory);
} catch (final IOException ioe) {
throw new MojoExecutionException("Failed to createHandler directory: " + outputDirectory, ioe);
}
try {
project.addCompileSourceRoot(outputDirectory.getPath());
getLog().info("about to read file " + ramlFile + " in directory " + ramlFile.getParent());
RamlModelResult ramlModelResult =
new RamlModelBuilder().buildApi(
new FileReader(ramlFile),
ramlFile.getAbsolutePath());
if (ramlModelResult.hasErrors()) {
for (ValidationResult validationResult : ramlModelResult.getValidationResults()) {
getLog().error("raml error:" + validationResult.getMessage());
}
throw new MojoExecutionException("invalid raml " + ramlFile);
}
final Api api = ramlModelResult.getApiV10();
RamlToPojo ramlToPojo = RamlToPojoBuilder.builder(api)
.inPackage(defaultPackage)
.fetchTypes(fromAnywhere())
.findTypes(everyWhere()).build(basePlugins);
ramlToPojo.buildPojos().createAllTypes(outputDirectory.getAbsolutePath());
} catch (IOException e) {
throw new MojoExecutionException("execution exception", e);
}
}
示例10
public static ApiModel build(ResourceLoader resourceLoader, File ramlFile)
{
RamlModelResult ramlModelResult = new RamlModelBuilder(resourceLoader).buildApi(ramlFile);
return wrapApiModel(ramlModelResult);
}
示例11
public static ApiModel build(Reader reader, String location)
{
RamlModelResult ramlModelResult = new RamlModelBuilder(new DefaultResourceLoader()).buildApi(reader, location);
return wrapApiModel(ramlModelResult);
}
示例12
Api readContent(final File ramlFile) throws MockImportException {
final RamlModelResult ramlModelResult = new RamlModelBuilder().buildApi(ramlFile);
if (ramlModelResult.hasErrors()) {
final String allErrors = ramlModelResult
.getValidationResults().stream().map(vr -> vr.getPath() + " " + vr.getMessage())
.collect(Collectors.joining("\n"));
throw new MockImportException(allErrors);
}
return ramlModelResult.getApiV10();
}