org.dmd.dms.doc.web.ExtendedReferenceTypeFormatter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dark-matter-data Show documentation
Show all versions of dark-matter-data Show documentation
The dark-matter project provides mechanism to define concepts associated with Domain Specific Languages (DSLs) and generate code that can be extended with business logic that supports the given DSL purpose.
package org.dmd.dms.doc.web;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.TreeMap;
import org.dmd.dmc.DmcValueException;
import org.dmd.dmc.types.DefinitionName;
import org.dmd.dms.AttributeDefinition;
import org.dmd.dms.ExtendedReferenceTypeDefinition;
import org.dmd.dms.SchemaManager;
import org.dmd.dms.TypeDefinition;
import org.dmd.dms.generated.types.Field;
import org.dmd.dmw.DmwWrapper;
public class ExtendedReferenceTypeFormatter {
static public void dumpDetails(BufferedWriter out, SchemaManager schema, ExtendedReferenceTypeDefinition td) throws IOException {
typeName(out, td);
description(out, td);
referenced(out, td);
fields(out,schema,td);
usage(out, schema, td);
}
static void typeName(BufferedWriter out, ExtendedReferenceTypeDefinition td) throws IOException {
out.write(" " + td.getName() + " \n");
}
static void description(BufferedWriter out, ExtendedReferenceTypeDefinition td) throws IOException {
if (td.getDescription() != null){
out.write(" \n");
out.write(" \n");
out.write(" Description \n");
out.write(" " + td.getDescriptionWithNewlines() + " \n");
out.write(" \n\n");
}
}
static void referenced(BufferedWriter out, ExtendedReferenceTypeDefinition td) throws IOException {
if (td.getExtendedReferenceClass() != null){
String schema = td.getExtendedReferenceClass().getDefinedIn().getName().getNameString();
String type = td.getExtendedReferenceClass().getObjectName().getNameString();
out.write(" \n");
out.write(" \n");
out.write(" Reference to: \n");
out.write(" " + type + " \n");
out.write(" \n\n");
}
}
static void fields(BufferedWriter out, SchemaManager sm, ExtendedReferenceTypeDefinition td) throws IOException {
out.write(" \n");
out.write(" \n");
out.write(" Field Separator: \n");
if (td.getFieldSeparator() == null)
out.write("SPACE");
else
out.write(" " + td.getFieldSeparator() + "");
out.write(" \n");
out.write(" \n\n");
Iterator fields = td.getField();
int fieldNum = 1;
while(fields.hasNext()){
Field field = fields.next();
out.write(" \n");
out.write(" \n");
out.write(" Field " + fieldNum + " \n");
out.write(" " + field.getName() + " \n");
TypeDefinition fieldType = sm.tdef(field.getType().getObjectName().getNameString());
if (fieldType == null){
try {
fieldType = sm.findInternalType(new DefinitionName(field.getType().getObjectName().getNameString()));
} catch (DmcValueException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String type = TypeFormatter.getTypeName(fieldType);
String schema = fieldType.getDefinedIn().getName().getNameString();
out.write(" " + type + " \n");
out.write(" " + field.getDescription() + " \n");
out.write(" \n\n");
fieldNum++;
}
}
static void usage(BufferedWriter out, SchemaManager schema, ExtendedReferenceTypeDefinition td) throws IOException {
// We have to determine the usage from the internally generated type
TypeDefinition internalType = schema.findInternalType(td.getName());
ArrayList referring = internalType.getReferringObjects();
// There's always one reference because the schema refers to its complex types
if ( (referring != null) && (referring.size() > 1) ){
// if (referring != null){
TreeMap attributes = new TreeMap();
for(DmwWrapper wrapper: referring){
if (wrapper instanceof AttributeDefinition){
AttributeDefinition ad = (AttributeDefinition) wrapper;
attributes.put(ad.getName(), ad);
}
}
if (attributes.size() > 0){
out.write(" \n");
out.write(" \n");
out.write(" Used in: \n");
out.write(" \n");
out.write(formatUsage(attributes));
out.write(" \n");
out.write(" \n\n");
}
}
}
static String formatUsage(TreeMap ads){
StringBuffer sb = new StringBuffer();
sb.append("\n");
int count = 0;
for(AttributeDefinition ad: ads.values()){
if ((count % 3) == 0){
if (count > 3){
sb.append("\n");
}
sb.append(" \n");
}
String ref = "" + ad.getDefinedIn().getName() + "";
sb.append(" " + ad.getName().getNameString() + " (" + ref + ") \n" );
count++;
}
sb.append(" \n");
sb.append("
\n");
return(sb.toString());
}
static public String getTypeName(TypeDefinition td){
String type = td.getName().getNameString();
if (td.getIsRefType()){
if (td.getOriginalClass() != null)
type = td.getOriginalClass().getName().getNameString();
}
if (td.getIsEnumType()){
type = td.getEnumName();
}
return(type);
}
}