org.raml.model.Raml2 Maven / Gradle / Ivy
The newest version!
package org.raml.model;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import org.raml.model.parameter.Header;
import org.raml.model.parameter.QueryParameter;
import org.raml.model.parameter.UriParameter;
/**
* Extension to RAML model which keeps information about traits and resource types
*
* @author pavel
* @version $Id: $Id
*/
public class Raml2 extends Raml {
private Map resourceTypesModel = new LinkedHashMap();
private Map traitsModel = new LinkedHashMap();
private Map schemaMap=new LinkedHashMap();
/**
* Getter for the field schemaMap
.
*
* @return a {@link java.util.Map} object.
*/
public Map getSchemaMap() {
return schemaMap;
}
/**
* getSchemaContent.
*
* @param schemaName a {@link java.lang.String} object.
* @return a {@link java.lang.String} object.
*/
public String getSchemaContent(String schemaName){
for (Mapq:schemas){
if (q.containsKey(schemaName)){
return q.get(schemaName);
}
}
return null;
}
private HashMap createSchemaMap(final String schemaName, final String content) {
final HashMap newSchemaLine = new HashMap();
newSchemaLine.put(schemaName, content);
return newSchemaLine;
}
private boolean schemaDeclared(final String schemaName) {
return findMapWithSchemaDeclaration(schemaName) != null;
}
private Map findMapWithSchemaDeclaration(String schemaName) {
for (final Map line : schemas){
if (line.keySet().contains(schemaName)){
return line;
}
}
return null;
}
/**
* addGlobalSchema.
*
* @param schemaName Schema name will be used as filename without extension and relative path.
* @param content a {@link java.lang.String} object.
* @param json If this parameter is false then it suppose that one should add an XML Schema.
* @param addSchemaSuffix a boolean.
*/
public void addGlobalSchema(
final String schemaName,
final String content,
final boolean json,
final boolean addSchemaSuffix)
{
final HashMap newSchemaLine = createSchemaMap(schemaName, content);
final String path = "schemas/" + schemaName + (addSchemaSuffix ? "-schema" : "") + "." + (json ? "json" : "xsd");
schemaMap.put(schemaName, path);
if (!schemaDeclared(schemaName)) {
schemas.add(newSchemaLine);
}
}
/**
* addOrReplaceSchemaContent.
*
* @param schemaName a {@link java.lang.String} object.
* @param content a {@link java.lang.String} object.
*/
public void addOrReplaceSchemaContent(
final String schemaName,
final String content)
{
final Map foundMap = findMapWithSchemaDeclaration(schemaName);
if (foundMap != null) {
foundMap.put(schemaName, content);
} else {
final Map map = createSchemaMap(schemaName, content);
schemas.add(map);
schemaMap.put(schemaName, "schemas/"+schemaName+".json");
}
}
/**
* Getter for the field resourceTypesModel
.
*
* @return a {@link java.util.Map} object.
*/
public Map getResourceTypesModel() {
return resourceTypesModel;
}
/**
* Setter for the field resourceTypesModel
.
*
* @param resourceTypesModel a {@link java.util.Map} object.
*/
public void setResourceTypesModel(Map resourceTypesModel) {
this.resourceTypesModel = resourceTypesModel;
}
/**
* Getter for the field traitsModel
.
*
* @return a {@link java.util.Map} object.
*/
public Map getTraitsModel() {
return traitsModel;
}
/**
* Setter for the field traitsModel
.
*
* @param traitsModel a {@link java.util.Map} object.
*/
public void setTraitsModel(Map traitsModel) {
this.traitsModel = traitsModel;
}
/**
* visit.
*
* @param v a {@link org.raml.model.IRamlFileVisitor} object.
*/
public void visit(IRamlFileVisitor v) {
visitResources(this.getResources(), v);
Map resourceTypesModel2 = getResourceTypesModel();
for (ResourceType z:resourceTypesModel2.values()){
v.visitResourceType(z);
visitResource(v, z);
}
for (TraitModel m:getTraitsModel().values()){
visitAction(v, m);
}
}
/**
* addGlobalSchema.
*
* @param name a {@link java.lang.String} object.
* @param content a {@link java.lang.String} object.
* @param json a boolean.
*/
public void addGlobalSchema(String name,String content,boolean json){
HashMap e = new HashMap();
String path=name;
if(json){
path+="-schema.json";
}
else{
path+="-schema.xsd";
}
e.put(name, content);
getSchemaMap().put(name, "/schemas/"+path);
for (Maps:schemas){
if (s.keySet().contains(name)){
return;
}
}
schemas.add(e);
}
private void visitResources(Map resourceTypesModel2,
IRamlFileVisitor v) {
for (Resource q : resourceTypesModel2.values()) {
visitResource(v, q);
}
}
private void visitResource(IRamlFileVisitor v, Resource q) {
boolean startVisit = v.startVisit(q);
if (startVisit) {
Map uriParameters = q.getUriParameters();
for (String u:uriParameters.keySet()){
v.visit(u,uriParameters.get(u));
}
Map actions = q.getActions();
for (Action a:actions.values()){
visitAction(v, a);
}
visitResources(q.getResources(), v);
}
v.endVisit(q);
}
private void visitAction(IRamlFileVisitor v, Action a) {
boolean startVisit2 = v.startVisit(a);
if (startVisit2){
Map headers = a.getHeaders();
for (String h:headers.keySet()){
v.visit(h,headers.get(h));
}
Map qp = a.getQueryParameters();
for (String h:qp.keySet()){
v.visit(h,qp.get(h));
}
v.startVisitBody();
Map body = a.getBody();
for (MimeType m:body.values()){
v.visit(m);
}
v.endVisitBody();
Map responses = a.getResponses();
for (String c:responses.keySet()){
Response response = responses.get(c);
v.startVisit(c,response);
for (MimeType m:response.getBody().values()){
v.visit(m);
}
Map headers2 = response.getHeaders();
for (String h:headers2.keySet()){
v.visit(h,headers2.get(h));
}
v.endVisit(responses.get(c));
}
}
v.endVisit(a);
}
}