return entity.getTypeName();
}
});
group.registerModelAdaptor(EntityDescription.class,
new ObjectModelAdaptor() {
@Override
public Object getProperty(Interpreter interp, ST self, Object o,
Object property, String propertyName)
throws STNoSuchPropertyException {
EntityDescription entity = (EntityDescription) o;
if ("docString".equals(propertyName)) {
return jsDocString(entity.getDocString());
}
else if ("canonicalName".equals(propertyName)) {
return getPackageName(entity) + "." + upcase(entity.getTypeName());
}
else if ("supertype".equals(propertyName)) {
EntityDescription supertype = entity.getSupertype();
if (supertype == null) {
supertype = baseHasUuid;
}
return supertype;
}
else if ("properties".equals(propertyName)) {
Map<String, Property> propertyMap = new HashMap<String, Property>();
for (Property p : entity.getProperties()) {
propertyMap.put(p.getName(), p);
}
List<Property> sortedProperties = new ArrayList<Property>();
sortedProperties.addAll(propertyMap.values());
Collections.sort(sortedProperties, new Comparator<Property>() {
@Override
public int compare(Property p1, Property p2) {
return p1.getName().compareTo(p2.getName());
}
});
return sortedProperties;
}
else if ("collectionProperties".equals(propertyName)) {
return getSortedCollectionProperties(entity);
}
else if ("uniqueTypeCollectionListProperties".equals(propertyName)) {
List<Property> props = getSortedCollectionProperties(entity);
Iterator<Property> iter = props.iterator();
Set<String> seen = new HashSet<String>();
while (iter.hasNext()) {
Property prop = iter.next();
String name = collectionNameForProperty(prop);
if (!seen.contains(name)) {
seen.add(name);
}
else {
iter.remove();
}
}
return props;
}
else if ("validations".equals(propertyName)) {
Map<String, List<String>> map = new HashMap<String, List<String>>();
for (Property p : entity.getProperties()) {
List<String> validations = new ArrayList<String>();
List<Annotation> docAnnotations = p.getDocAnnotations();
if (docAnnotations != null) {
for (Annotation a : docAnnotations) {
String name = a.annotationType().getName();
String validation = validationMap.get(name);
if (validation != null) {
validation = "new " + validation + "(";
validation += getValidationParameters(a);
validation += ")";
validations.add(validation);
}
}
}
if (!validations.isEmpty()) {
map.put(p.getName(), validations);
}
}
return map;
}
else if ("validationRequires".equals(propertyName)) {
Set<String> requires = new HashSet<String>();
for (Property p : entity.getProperties()) {
List<Annotation> docAnnotations = p.getDocAnnotations();
if (docAnnotations != null) {
for (Annotation a : docAnnotations) {
String name = a.annotationType().getName();
String require = validationMap.get(name);
if (require != null) {
requires.add(require);
}
}
}
}
return requires;
}
return super.getProperty(interp, self, o, property, propertyName);
}
});
group.registerModelAdaptor(Property.class, new ObjectModelAdaptor() {
@Override
public Object getProperty(Interpreter interp, ST self, Object o,
Object property, String propertyName)
throws STNoSuchPropertyException {
Property p = (Property) o;
if ("docString".equals(propertyName)) {
String docString = jsDocString(p.getDocString());
List<String> enumValues = p.getType().getEnumValues();
if (enumValues == null && p.getType().getListElement() != null &&
p.getType().getListElement().getEnumValues() != null) {
enumValues = p.getType().getListElement().getEnumValues();
}
if (enumValues != null) {
docString = docString == null ? "" : docString;
docString += "\n\nPossible values: ";
for (int i = 0; i < enumValues.size(); i++) {
docString += enumValues.get(i);
if (i < enumValues.size() - 1) docString += ", ";
}
}
return docString;
}
else if ("jsType".equals(propertyName)) {
return jsTypeForType(p.getType());
}
else if ("listElementEnum".equals(propertyName)) {
if (p.getType() != null && p.getType().getListElement() != null &&
p.getType().getListElement().getEnumValues() != null) {
String enumVals = "";
int idx = 0;
for (String val : p.getType().getListElement().getEnumValues()) {
if (idx != 0) {
enumVals += ", ";
}
enumVals += val;
idx++;
}
return enumVals;
}
}
else if ("listElementKind".equals(propertyName)) {
if (p.getType().getListElement() != null &&
p.getType().getListElement().getName() != null) {
return (packageName + "." + upcase(p.getType().getListElement().toString()));
}
}
else if ("collectionName".equals(propertyName)) {
return collectionNameForProperty(p);
}
else if ("canonicalListElementKind".equals(propertyName)) {
if (p.getType().getListElement() != null) {
String collectionModelType = jsTypeForType(p.getType().getListElement());
Property implied = p.getImpliedProperty();
if (implied != null) {
return "function(attrs, options) {\n" +
" return new " + collectionModelType + "(\n" +
" _(attrs).extend({ " + implied.getName()
+ " : self }), options);\n" +
" }";
}
else {
return collectionModelType;
}
}
}
else if ("defaultValue".equals(propertyName)) {
String defaultVal = "undefined";
if (p.isEmbedded()) {
defaultVal = "new " + jsTypeForType(p.getType()) + "()";
}
if (p.getType().getJsonKind().equals(JsonKind.LIST) &&
jsTypeForType(p.getType().getListElement()).equals("String")) {
defaultVal = "[]";
jsTypeForType(p.getType());
}
else if (p.getType().getJsonKind().equals(JsonKind.LIST) &&
p.getType().getListElement().getName() != null) {
defaultVal = "new " + collectionNameForProperty(p) + "()";
}
else if (p.getType().getJsonKind().equals(JsonKind.MAP)) {
return "{}";
}
return defaultVal;
}
return super.getProperty(interp, self, o, property, propertyName);
}
});
group.registerModelAdaptor(String.class, new ObjectModelAdaptor() {
@Override
public Object getProperty(Interpreter interp, ST self, Object o,
Object property, String propertyName)
throws STNoSuchPropertyException {
final String string = (String) o;
if ("chunks".equals(propertyName)) {
/*
* Split a string into individual chunks that can be reflowed. This implementation is
* pretty simplistic, but helps make the generated documentation at least somewhat more
* readable.
*/
return new Iterator<CharSequence>() {
int index;
int length = string.length();
CharSequence next;
{
advance();
}
@Override
public boolean hasNext() {
return next != null;
}
@Override
public CharSequence next() {
CharSequence toReturn = next;
advance();
return toReturn;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
private void advance() {
int start = advance(false);
int end = advance(true);
if (start == end) {
next = null;
} else {
next = string.subSequence(start, end);
}
}
/**
* Advance to next non-whitespace character.
*/
private int advance(boolean whitespace) {
while (index < length
&& (whitespace ^ Character.isWhitespace(string.charAt(index)))) {
index++;
}
return index;
}
};
}
return super.getProperty(interp, self, o, property, propertyName);
}
});
group.registerModelAdaptor(ApiDescription.class,
new ObjectModelAdaptor() {
@Override
public Object getProperty(Interpreter interp, ST self, Object o,
Object property, String propertyName)
throws STNoSuchPropertyException {
ApiDescription apiDescription = (ApiDescription) o;
if ("endpoints".equals(propertyName)) {
Set<EndpointDescription> uniqueEndpoints =
new HashSet<EndpointDescription>(apiDescription.getEndpoints());
List<EndpointDescription> sortedEndpoints = new ArrayList<EndpointDescription>(
uniqueEndpoints);
Collections.sort(sortedEndpoints, new Comparator<EndpointDescription>() {
@Override
public int compare(EndpointDescription e1, EndpointDescription e2) {
return e1.getPath().compareTo(e2.getPath());
}
});
Iterator<EndpointDescription> iter = sortedEndpoints.iterator();
while (iter.hasNext()) {
EndpointDescription ed = iter.next();
if (ed.getPath() != null && ed.getPath().contains("{path:.*}")) {
iter.remove();
}
}
return sortedEndpoints;
}
else if ("flatpackEndpoints".equals(propertyName)) {
List<EndpointDescription> sortedEndpoints = new ArrayList<EndpointDescription>(
apiDescription.getEndpoints());
Collections.sort(sortedEndpoints, new Comparator<EndpointDescription>() {
@Override
public int compare(EndpointDescription e1, EndpointDescription e2) {
return e1.getPath().compareTo(e2.getPath());
}
});
Iterator<EndpointDescription> iter = sortedEndpoints.iterator();
while (iter.hasNext()) {
if (!hasCustomRequestBuilderClass(iter.next())) {
iter.remove();
}
}
return sortedEndpoints;
}
else if ("requireNames".equals(propertyName)) {
return entityRequires;
}
return super.getProperty(interp, self, o, property, propertyName);
}
});
group.registerModelAdaptor(EndpointDescription.class,
new ObjectModelAdaptor() {
@Override
public Object getProperty(Interpreter interp, ST self, Object o,
Object property, String propertyName)
throws STNoSuchPropertyException {
EndpointDescription end = (EndpointDescription) o;
if ("docString".equals(propertyName)) {
return jsDocString(end.getDocString());
}
if ("methodName".equals(propertyName)) {
String path = end.getPath();
String[] parts = path.split(Pattern.quote("/"));
StringBuilder sb = new StringBuilder();
sb.append(end.getMethod().toLowerCase());
for (int i = 3, j = parts.length; i < j; i++) {
String part = parts[i];
if (part.length() == 0) continue;
if (!part.startsWith("{") && !part.endsWith("}")) {
if (part.contains(".")) {
String[] dotPart = part.split(Pattern.quote("."));
for (String dot : dotPart) {
sb.append(upcase(dot));
}
}
else {
sb.append(upcase(part));
}
}
else {
sb.append(upcase(part.substring(1, part.length() - 1)));
}
}
return sb.toString();
}
else if ("methodParameterList".equals(propertyName)) {
String path = end.getPath();
String[] parts = path.split(Pattern.quote("/"));
StringBuilder sb = new StringBuilder();
int paramCount = 0;
for (int i = 3, j = parts.length; i < j; i++) {
String part = parts[i];
if (part.length() == 0) continue;
if (part.startsWith("{") && part.endsWith("}")) {
String name = part.substring(1, part.length() - 1);
sb.append(name);
paramCount++;
if (end.getPathParameters() != null
&& paramCount < end.getPathParameters().size()) {
sb.append(", ");
}
}
}
if (end.getEntity() != null) {
if (paramCount > 0) {
sb.append(", ");
}
sb.append(getNameForType(end.getEntity()));
}
return sb.toString();
}
else if ("entityName".equals(propertyName)) {
return getNameForType(end.getEntity());
}
else if ("requestBuilderClassName".equals(propertyName)) {
if (hasCustomRequestBuilderClass(end)) {
return getBuilderReturnType(end);
}
else {
return "com.getperka.flatpack.client.JsonRequest";
}
}
else if ("requestBuilderBlockName".equals(propertyName)) {
return getBuilderReturnType(end) + "Block";
}
else if ("pathDecoded".equals(propertyName)) {
// URL-decode the path in the endpoint description
try {
String decoded = URLDecoder.decode(end.getPath(), "UTF8");
return decoded;
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
return super.getProperty(interp, self, o, property, propertyName);
}
});
group.registerModelAdaptor(ParameterDescription.class,
new ObjectModelAdaptor() {
@Override
public Object getProperty(Interpreter interp, ST self, Object o,
Object property, String propertyName)
throws STNoSuchPropertyException {
ParameterDescription param = (ParameterDescription) o;