Sample code of a user micropattern
You can see here the sample code of a Java™ class that defines a simple COBOL micropattern. This micropattern adds a DISPLAY statement in the source code.
Figure 1. Sample code of a Java class
package sample.mp;
import com.ibm.pdp.engine.IBuilderTag;
import com.ibm.pdp.engine.IGenInfoBuilder;
import com.ibm.pdp.engine.IMicroPattern;
import com.ibm.pdp.maf.rpp.kernel.RadicalElement;
import com.ibm.pdp.maf.rpp.pac.dataelement.DataElement;
import com.ibm.pdp.maf.rpp.service.IMAFModelService;
import com.ibm.pdp.maf.rpp.service.MAFModelService;
import com.ibm.pdp.rpp.micropattern.AbstractGlobalMicroPatternHandler;
public class UserMicroPattern extends AbstractGlobalMicroPatternHandler {
private static String NEW_LINE = System.getProperty("line.separator"); //NON-NLS-1
@Override
public String getId() {
return "MPG"; //NON-NLS-1
}
public void process(final IMicroPattern microPattern, final IGenInfoBuilder genInfoBuilder, final RadicalElement radicalElement ) {
IMAFModelService mafService = MAFModelService.getInstance();
String project = microPattern.getAttribute("Project"); //NON-NLS-1
String code = microPattern.getAttribute("Code"); //NON-NLS-1
if ( project == null || code == null ) {
microPattern.getProcessingContext().setStatus(
IMicroPattern.WARNING_RAISED,
microPattern.getLocation().getBeginIndex(),
"Project/Code Parameters not found!!!",
"Please, review this Micro-Pattern declaration");
return;
}
// Retrieve the Data Element to display
DataElement dataElement = mafService.getDataElement( project, null, code);
if ( dataElement == null ) {
microPattern.getProcessingContext().setStatus(
IMicroPattern.WARNING_RAISED,
microPattern.getLocation().getBeginIndex(),
"Data Element not found!!!",
"Please, review this Micro-Pattern declaration");
return;
}
IBuilderTag procDivisionTag = genInfoBuilder.tagFromName("PROCEDURE-DIVISION"); //NON-NLS-1
if ( procDivisionTag != null ) {
IBuilderTag functionTag = genInfoBuilder.tagFromName("F00"); //NON-NLS-1
if ( functionTag == null )
functionTag = genInfoBuilder.addTag(
procDivisionTag.getEndIndex(), procDivisionTag.getEndIndex(), "F00"); //NON-NLS-1
String newCode =
" F00. EXIT."+NEW_LINE+ //NON-NLS-1
" DISPLAY \"" + dataElement.getLabel() + "\"."+NEW_LINE+ //NON-NLS-1 //NON-NLS-2
" F00-FN. EXIT."+NEW_LINE;
functionTag.setText(newCode);
} else {
microPattern.getProcessingContext().setStatus(
IMicroPattern.WARNING_RAISED,
microPattern.getLocation().getBeginIndex(),
"No procedure division found!!!",
"Please, review this Micro-Pattern declaration");
return;
}
registerReference(microPattern, dataElement);
}
}