midrange.com code scratchpad
Name:
ConvertDToFreeAction
Scriptlanguage:
Java
Tabwidth:
4
Date:
07/03/2014 05:31:34 pm
IP:
Logged
Description:
Convert D-specifications to fully free RPG. Also handles DS (put the cursor on the DS), PI, PR, P...B and P...E specifications. Put the cursor on the line with the definition (ie not on the first line of a continued name, but on the last line, the one that defines the return type and has the keywords)
Code:
  1. package com.kc2hiz.lpexextensions; 
  2.  
  3. import com.ibm.lpex.core.LpexAction;
  4.  
  5. import com.ibm.lpex.core.LpexView;
  6. import com.ibm.lpex.core.LpexLog;
  7. import java.util.ArrayList;
  8. import java.util.regex.Pattern;
  9. import java.util.regex.Matcher;
  10.  
  11. /**
  12.  * Convert D-specification to fully free format
  13.  * <p>Intended to convert one spec at a time to allow for easier review of the conversion.
  14.  * <p>for use as an Lpex User Action.
  15.  * @author buck
  16.  * @version 01.00.00
  17.  *
  18.  */
  19. public class ConvertDToFreeAction implements LpexAction {
  20.     
  21.     /**
  22.      * Check to see if we should be allowed to perform the D to free conversion
  23.      * @param view LpexView to operate on
  24.      * @return true if action is available for this view
  25.      * @see com.ibm.lpex.core.LpexAction#available(com.ibm.lpex.core.LpexView)
  26.      */
  27.     @Override
  28.     public boolean available(LpexView view) {
  29.           return view.currentElement() > 0 &&
  30.                      !view.queryOn("readonly"); /* &&
  31.                      view.queryOn("block.anythingSelected"); */
  32.     }
  33.  
  34.     /**
  35.      * Converts D-specifications to fully free
  36.      * @param view LpexView to operate on
  37.      * @see com.ibm.lpex.core.LpexAction#doAction(com.ibm.lpex.core.LpexView)
  38.      */
  39.     @Override
  40.     public void doAction(LpexView view) {
  41.         
  42.         // work with the line the cursor is on
  43.         int thisLine = view.currentElement();
  44.         String sourceStmt = view.elementText(thisLine);
  45.  
  46.         // leave if no text
  47.         if (sourceStmt.length() == 0) {
  48.             view.doCommand("set messageText Empty text");            
  49.             return;
  50.         }
  51.  
  52.         // need to at least see 6 columns or we don't possibly have a d-spec
  53.         if (sourceStmt.length() <= 5) {
  54.             view.doCommand("set messageText Line too short");
  55.             return;
  56.         } 
  57.         
  58.         // Instantiate a DSpec object.  The constructor will break out the columns.
  59.         DSpec dspec = new DSpec(view, sourceStmt, thisLine);
  60.  
  61.         // leave if we're not looking at a D- or P-specification
  62.         if (!dspec.spec.equals("d") && !dspec.spec.equals("p")) {
  63.             view.doCommand("set messageText Not a D- or P-spec");            
  64.             return;
  65.         }
  66.  
  67.         // Handle the various definition types we know about
  68.         if (dspec.defType.trim().equals("b") ||
  69.             dspec.defType.trim().equals("c") ||
  70.             dspec.defType.trim().equals("e") ||
  71.             dspec.defType.equals("ds") ||
  72.             dspec.defType.equals("pi") ||
  73.             dspec.defType.equals("pr") ||
  74.             dspec.defType.trim().equals("s")) {
  75.                 convertSubfieldsToFree(view, dspec);
  76.                 // position the cursor to the top of the area we converted from
  77.                 view.doDefaultCommand("locate element " + thisLine);
  78.                 view.doDefaultCommand("set position 1");
  79.         } else {
  80.                 view.doCommand("set messageText Unusable D-spec. In the middle of a structure? " + dspec.defType);
  81.         }
  82.         
  83.     }
  84.  
  85.     /**
  86.      * Convert one or more field definitions from fixed to free
  87.      * If a standalone or constant, converts just the one line
  88.      * If a DS, PI or PR, converts the entire structure
  89.      * @param view LpexView - the current view we're working on
  90.      * @param dspec String - the first line of the structure (DS, PR, PI)
  91.      */
  92. private void convertSubfieldsToFree(LpexView view, DSpec dspec) {
  93.         int e = 0;
  94.         int lastSubfieldNumber = view.currentElement();    
  95.         int specLineNumber = view.currentElement();    
  96.         String dsTemp = "";
  97.         ArrayList<String> dsLines = new ArrayList<String>();
  98.         String dsDclTemp = "";
  99.                 
  100.         // the declare uses the definition type
  101.         // unless b, which is a P-spec and s/b 'proc'
  102.         // unless e, which gets nothing for the declaration
  103.         if (!dspec.defType.trim().equals("e")) {
  104.             if (!dspec.defType.trim().equals("b")) {
  105.                 dsDclTemp = "        dcl-" + dspec.defType.trim() + " "    + dspec.name;
  106.             } else {
  107.                 dsDclTemp = "        dcl-proc " + dspec.name;
  108.             }
  109.                 
  110.             // if there is a datatype, append it
  111.             String subfieldKeywords = getSubfieldKeywords(dspec.fromPos, dspec.len, dspec.dataType, dspec.decimals); 
  112.             if (subfieldKeywords.length() != 0) {
  113.                 dsDclTemp = dsDclTemp.concat(" " + subfieldKeywords);
  114.             }
  115.  
  116.             // if there are keywords, append them
  117.             if (dspec.keywords.length() != 0) {
  118.                 dsDclTemp = dsDclTemp.concat(" " + dspec.keywords);
  119.             }
  120.  
  121.             // append the semicolon
  122.             dsDclTemp = dsDclTemp.concat(";");
  123.  
  124.             // right hand comments (if any) come after the semicolon
  125.             if (dspec.rhComment.length() != 0) {
  126.                 dsDclTemp = dsDclTemp.concat(" // " + dspec.rhComment);
  127.             }
  128.         
  129.             // now that we have a fully formed line, add it to the array of lines
  130.             dsLines.add(dsDclTemp);
  131.         }
  132.  
  133.         
  134.         // loop forward through the next set of source lines
  135.         // until the end of the structure is found
  136.         // note that for standalone and constant lines, the very next spec terminates the 'structure'
  137.         for (e = specLineNumber + 1; e <= view.elements(); e++) {
  138.             
  139.             String dsSubfield = view.elementText(e);
  140.             
  141.             // if blank line, assume end of struc
  142.             int textLengthDS = dsSubfield.length();
  143.             if (textLengthDS == 0) {
  144.                 break;
  145.             }
  146.  
  147.             // need to at least see 6 columns or we don't possibly have a d-spec
  148.             if (textLengthDS <= 5) {
  149.                 break;
  150.             } 
  151.  
  152.             // make a new DSpec object which will break out the columns
  153.             DSpec subfield = new DSpec(view, dsSubfield, e);
  154.  
  155.             // comments have no fields to parse, but  
  156.             // carry the comments forward into the converted block
  157.             if (subfield.isComment) {
  158.                 dsTemp = "       // " + subfield.longComment.trim();
  159.             } else {
  160.                 
  161.                 // read the next line if this one is a continuation
  162.                 if (dsSubfield.matches(".*\\.\\.\\.")) {
  163.                     continue;
  164.                 }
  165.  
  166.                 // leave the loop if we've reached the end of the subfields
  167.                 if (subfield.spec.equals("d") && !subfield.defType.equals("  ")) {
  168.                     break;
  169.                 }
  170.  
  171.                 // leave if we're not looking at a D-specification
  172.                 if (!subfield.spec.equals("d")) {
  173.                     break;
  174.                 }
  175.  
  176.                 // now generate the keywords for data type
  177.                 String dataTypeKwdDS = getSubfieldKeywords(subfield.fromPos, subfield.len, subfield.dataType, subfield.decimals);
  178.  
  179.                 dsTemp =  "           " + 
  180.                         subfield.name + " " + dataTypeKwdDS;
  181.                 // keywords are optional; don't leave a trailing space if none needed
  182.                 if (subfield.keywords.length() != 0) {
  183.                     dsTemp = dsTemp.concat(" " + subfield.keywords);
  184.                 }
  185.  
  186.                 // add the terminating semicolon
  187.                 dsTemp = dsTemp.concat(";");
  188.  
  189.                 // if we have a right hand comment, carry it forward
  190.                 if (subfield.rhComment.length() != 0) {
  191.                     dsTemp = dsTemp.concat(" // " + subfield.rhComment.trim());
  192.                 }
  193.                 
  194.                 // save the element number of the last subfield we actually processed
  195.                 lastSubfieldNumber = e;
  196.             }
  197.             dsLines.add(dsTemp);
  198.             
  199.         };
  200.         
  201.         // ...and the end
  202.         // not needed for standalone and constant
  203.         if (!dspec.defType.trim().equals("c") && 
  204.             !dspec.defType.trim().equals("s") &&
  205.             !dspec.defType.trim().equals("b") &&
  206.             !dspec.defType.trim().equals("p")) {
  207.             
  208.             String endDsTemp = "";
  209.             // the declare uses the definition type
  210.             // unless b/e, which are P-specs and s/b 'proc'
  211.             if (!dspec.defType.trim().equals("e")) {
  212.                 endDsTemp = "        end-"  + dspec.defType + ";";
  213.             } else {
  214.                 endDsTemp = "        end-proc;";
  215.             }
  216.             
  217.             dsLines.add(endDsTemp);
  218.         }
  219.  
  220.         // position cursor AFTER the block we just read
  221.         view.doCommand("locate line " + (lastSubfieldNumber));
  222.         
  223.         // loop through the array and write the contents out
  224.         for (String dsLine: dsLines) {
  225.             if (!dsLine.isEmpty()) {
  226.                 view.doDefaultCommand("insert " + dsLine);
  227.             }
  228.         }
  229.     }
  230.  
  231.  
  232. /**
  233.  * build up the character type keyword based on the data type
  234.  * @param fromPos String
  235.  * @param len String
  236.  * @param dataType String
  237.  * @param decimals String
  238.  * @return dataTypeKwd String
  239.  */
  240. private String getSubfieldKeywords(String fromPos, String len, String dataType, String decimals) {
  241.     String dataTypeKwd = "";
  242.     int tempLen = 0;
  243.  
  244.     // no keyword if no 'to positions'
  245.     if (len.length() != 0) {
  246.         // the data type can be blank and the RPG compiler will
  247.         // supply a rational default.  We need to do the same thing.
  248.         if (dataType.length() == 0) {
  249.             if (decimals.length() == 0) {
  250.                 dataType = "a";
  251.             } else {
  252.                 dataType = "p";
  253.             }
  254.         }
  255.  
  256.         // have a length adjustment.  No data type keyword for this.
  257.         // instead, append the adjustment to the LIKE keyword
  258.         // in the getKeywordsFromText method
  259.         if (!len.matches(".*\\+.*")) {
  260.             // old style from/to specs
  261.             // convert length 
  262.             String tempLenChar = len;
  263.             if (fromPos.length() != 0) {
  264.                 tempLen = Integer.parseInt(len) - Integer.parseInt(fromPos) + 1;
  265.                 tempLenChar = String.valueOf(tempLen);
  266.             }
  267.             switch (dataType) {
  268.             case "a":
  269.                 dataTypeKwd = "char(" + tempLenChar + ")";
  270.                 break;
  271.             case "f":
  272.                 dataTypeKwd = "float(" + tempLenChar + ")";
  273.                 break;
  274.             case "i":
  275.                 dataTypeKwd = "int(" + tempLenChar + ")";
  276.                 break;
  277.             case "p":
  278.                 dataTypeKwd = "packed(" + tempLenChar + ": " + decimals + ")";
  279.                 break;
  280.             case "s":
  281.                 dataTypeKwd = "zoned(" + tempLenChar + ": " + decimals + ")";
  282.                 break;
  283.             case "u":
  284.                 dataTypeKwd = "uns(" + tempLenChar + ")";
  285.                 break;
  286.             default:
  287.                 dataTypeKwd = "unk(" + dataType + ") tempLenChar("
  288.                         + tempLenChar + ")";
  289.                 break;
  290.             }
  291.  
  292.             // we may have an old style from-to situation
  293.             // use the POS keyword to tell the compiler where the subfield starts
  294.             if (fromPos.length() != 0) {
  295.                 dataTypeKwd = dataTypeKwd.concat(" pos(" + fromPos + ")");
  296.             }
  297.         }
  298.     }
  299.     return dataTypeKwd;
  300. }
  301.  
  302. }
  303.  
  304. /**
  305.  * This stores the various column based fields for a d-spec
  306.  * This object tokenises the line with the definitions.  It will also move backward 
  307.  * in the view's text lines to accumulate names continued from earlier lines.
  308.  * @param view LpexView 
  309.  * @param dSpec String a single raw d-spec (field / subfield)
  310.  * @author buck
  311.  *
  312.  */
  313. class DSpec {
  314.     public String spec = "";
  315.     public String name = "";
  316.     public String extType = "";
  317.     public String dsType = "";
  318.     public String defType = "";
  319.     public String fromPos = "";
  320.     public String len = "";
  321.     public String dataType = "";
  322.     public String decimals = "";
  323.     public String keywords = "";
  324.     public String rhComment = "";
  325.     public String dataTypeKwd = "";
  326.     public boolean isComment = false;
  327.     public String longComment = "";
  328.     
  329.     // constructor
  330.     public DSpec(LpexView view, String sourceStmt, int thisLine) {
  331.         
  332.         // I prefer lower case, so everything except the name will be monocased
  333.         // RDi trims each line, so they don't all equal 100 bytes...
  334.         // must test for out of bounds before substringing!
  335.         
  336.         /*
  337.          *  1 -  5 sequence number / text
  338.          *  6 -  6 dSpec 
  339.          *  7 - 21 name
  340.          * 22 - 22 external type (' ', 'e')
  341.          * 23 - 23 DS type (' ', 's', 'u')
  342.          * 24 - 25 def type (' ', 'c', 'ds', 'pi', 'pr')
  343.          * 26 - 32 from
  344.          * 33 - 39 len / to
  345.          * 40 - 40 type (a, p, i, z, *, etc)
  346.          * 41 - 42 decimal
  347.          * 44 - 80 keywords
  348.          * 81 -100 comment
  349.          * 
  350.          * Alternatively, there can be a comment of the form:
  351.          *  6 -  6 dSpec 
  352.          *  7 -  7 *
  353.          *  8 -100 comment
  354.          *  
  355.          * Alternatively, there can be a comment of the form:  
  356.          * 6 -  7 blanks
  357.          * 8 -100 // comment.  Note that there can be any number of blanks 
  358.          *                     preceding the //.
  359.          *                     
  360.          * Alternatively, there can be a continuation line of the form:
  361.          * 6 -  6 d
  362.          * 7 - 80 someName...                     
  363.          * 
  364.          */
  365.             
  366.         spec = getSpecFromText(sourceStmt);
  367.         
  368.         isComment = isComment(sourceStmt);
  369.         
  370.         // early exit if a comment
  371.         if (isComment) {
  372.             longComment = getComment(sourceStmt);
  373.             return;
  374.         }
  375.  
  376.         extType = getExtTypeFromText(sourceStmt);
  377.         dsType = getDsTypeFromText(sourceStmt);
  378.         defType = getDefTypeFromText(sourceStmt);
  379.         fromPos = getFromPosFromText(sourceStmt);
  380.         len = getLenFromText(sourceStmt);
  381.         dataType = getDataTypeFromText(sourceStmt);
  382.         decimals = getDecimalsFromText(sourceStmt);
  383.         keywords = getKeywordsFromText(sourceStmt, len);
  384.         rhComment = getRhCommentFromText(sourceStmt);
  385.         // do this last to load the rest of the spec columns before it
  386.         name = getNameFromText(view, sourceStmt, thisLine);
  387.  
  388.     }
  389.  
  390.     // ==========================================================
  391.     // individual methods to extract the columns
  392.     /**
  393.      * Extract RPG specification type (D, P, C, F) from a line of source code
  394.      * @param sourceStmt String raw text
  395.      * @return spec String lower case spec (c, d, f, p, etc.)
  396.      */
  397.     private String getSpecFromText(String sourceStmt) {
  398.         String spec = "";
  399.         
  400.         if (sourceStmt.length() > 5) {
  401.             spec = sourceStmt.substring(5, 6).toLowerCase();
  402.         }
  403.         return spec;
  404.     }
  405.  
  406.     /**
  407.      * Extract name from raw d-spec
  408.      * @param view LpexView
  409.      * @param sourceStmt raw d-spec
  410.      * @param thisLine int line number passed in
  411.      * @return name String
  412.      */
  413.     private String getNameFromText(LpexView view, String sourceStmt, int thisLine ) {
  414.         // name is variable length, ending anywhere from 7 to 21
  415.         // so we could potentially have a name like i which would
  416.         // only be in column 7.
  417.         String name = "";
  418.         StringBuilder sbname = new StringBuilder();
  419.  
  420.         // line too short to hold a name
  421.         if (sourceStmt.length() < 6) {
  422.             return "";
  423.         }
  424.         
  425.         int i = 21;
  426.         if (sourceStmt.length() <= i) {
  427.             i = sourceStmt.length();
  428.         }    
  429.         sbname = sbname.append(sourceStmt.substring(6, i).trim());
  430.         
  431.         
  432.         // names can be continued on another line
  433.         // we started on the specification line;
  434.         // back up until we run out of continuation lines
  435.         // go backward from the line with the specs
  436.         for (int j = thisLine - 1; j != 0; j--) {
  437.             String sourceStmtPrior = view.elementText(j);
  438.             String specPrior = "";
  439.             String namePrior = "";
  440.             String defTypePrior = "";
  441.             String defTypePrior1 = "";
  442.             boolean isCommentPrior = false;
  443.             
  444.             specPrior = getSpecFromText(sourceStmtPrior);
  445.             isCommentPrior = isComment(sourceStmtPrior);
  446.  
  447.             // read another line if this is a comment
  448.             if (isCommentPrior) {
  449.                 continue;
  450.             }
  451.  
  452.             // line too short to hold a name
  453.             if (sourceStmtPrior.length() <= 6) {
  454.                 continue;
  455.             }
  456.             
  457.             i = 21;
  458.             if (sourceStmtPrior.length() <= i) {
  459.                 i = sourceStmtPrior.length();
  460.             }    
  461.             namePrior = sourceStmtPrior.substring(6, i).trim();
  462.                 
  463.             defTypePrior1 = defTypePrior;
  464.             defTypePrior = getDefTypeFromText(sourceStmtPrior);
  465.             
  466.             // is the name continued?
  467.             if (sourceStmtPrior.matches(".*\\.\\.\\.")) {
  468.                 sbname.insert(0, sourceStmtPrior.substring(6, sourceStmtPrior.length() - 3).trim());
  469.                 continue;
  470.             } else {
  471.                 // if d or p spec AND
  472.                 // name is blank AND
  473.                 // defType (s, pi etc) doesn't change THEN
  474.                 // it's legal and read another
  475.                 if ((specPrior.equals("d") || specPrior.equals("p")) &&
  476.                     (namePrior.length() == 0) &&
  477.                     (defTypePrior.equals(defTypePrior1))) {
  478.                     continue;
  479.                 }
  480.                 break;
  481.             }
  482.         }
  483.  
  484.         
  485.         // convert the StringBuilder name to the permanent String
  486.         name = sbname.toString();
  487.         return name;
  488.     }
  489.  
  490.     /**
  491.      * Get external data type from raw d-spec
  492.      * @param sourceStmt String raw d-spec
  493.      * @return extType String
  494.      */
  495.     private String getExtTypeFromText(String sourceStmt) {
  496.         String extType = "";
  497.         
  498.         if (sourceStmt.length() > 21) {
  499.             extType = sourceStmt.substring(21, 22).toLowerCase();
  500.         }
  501.         
  502.         return extType;
  503.     }
  504.  
  505.     /**
  506.      * Extract data structure type from d-spec
  507.      * @param sourceStmt String raw d-spec
  508.      * @return dsType String data structure type
  509.      */
  510.     private String  getDsTypeFromText(String sourceStmt) {
  511.         String dsType = "";
  512.         
  513.         if (sourceStmt.length() > 22) {
  514.             dsType = sourceStmt.substring(22, 23).toLowerCase();
  515.         }
  516.         return dsType;
  517.     }
  518.  
  519.     /**
  520.      * Extract definition type from raw d-spec
  521.      * @param sourceStmt dSpec String raw d-spec
  522.      * @return defType String definition type
  523.      */
  524.     private String getDefTypeFromText(String sourceStmt) {
  525.         String defType = "";
  526.         
  527.         if (sourceStmt.length() > 23) {
  528.             int i = 25;
  529.             if (sourceStmt.length() <= i) {
  530.                 i = sourceStmt.length();
  531.             }
  532.             defType = sourceStmt.substring(23, i).toLowerCase();
  533.         }
  534.         return defType;
  535.     }
  536.  
  537.     /**
  538.      * Extract From position from raw d-spec
  539.      * @param sourceStmt dSpec String raw d-spec
  540.      * @return fromPos String From position
  541.      */
  542.     private String getFromPosFromText(String sourceStmt) {
  543.         String fromPos = "";
  544.         
  545.         if (sourceStmt.length() > 31) {
  546.             fromPos = sourceStmt.substring(25, 32).toLowerCase().trim();
  547.         }
  548.         return fromPos;
  549.     }
  550.  
  551.     /**
  552.      * Extract Length / To position from raw d-spec
  553.      * @param sourceStmt dSpec String raw d-spec
  554.      * @return len String Length / To position 
  555.      */
  556.     private String getLenFromText(String sourceStmt) {
  557.         String len = "";
  558.         
  559.         if (sourceStmt.length() > 38) {
  560.             len = sourceStmt.substring(32, 39).toLowerCase().trim();
  561.         }
  562.         return len;
  563.     }
  564.  
  565.     /**
  566.      * Extract data type from raw d-spec
  567.      * @param sourceStmt dSpec String raw d-spec
  568.      * @return dataType String data type
  569.      */
  570.     private String getDataTypeFromText(String sourceStmt) {
  571.         String dataType = "";
  572.         
  573.         if (sourceStmt.length() > 39) {
  574.             dataType = sourceStmt.substring(39, 40).toLowerCase().trim();
  575.         }
  576.         return dataType;
  577.     }
  578.  
  579.     /**
  580.      * Extract decimals from raw d-spec
  581.      * @param sourceStmt dSpec String raw d-spec
  582.      * @return decimals String decimals
  583.      */
  584.     private String getDecimalsFromText(String sourceStmt) {
  585.         String decimals = "";
  586.         
  587.         if (sourceStmt.length() > 41) {
  588.             decimals = sourceStmt.substring(40, 42).toLowerCase().trim();
  589.         }
  590.         return decimals;
  591.     }
  592.  
  593.     /**
  594.      * Extract keywords from raw d-spec
  595.      * @param sourceStmt dSpec String raw d-spec
  596.      * @param len String to / length
  597.      * @return keywords String keywords
  598.      */
  599.     private String getKeywordsFromText(String sourceStmt, String len) {
  600.         String keywords = "";
  601.         String keywordsAdj = "";
  602.         
  603.         log("start getKeywordsFromTextString " + sourceStmt);
  604.         
  605.         if (sourceStmt.length() > 43) {
  606.             int i = 80;
  607.             if (sourceStmt.length() <= i) {
  608.                 i = sourceStmt.length();
  609.             }
  610.             keywords = sourceStmt.substring(43, i).toLowerCase().trim();
  611.             
  612.             // do we have a length adjustment?
  613.             if (keywords.length() > 0) {                        // have keywords
  614.                 log("keywords.length()=" + keywords.length());    
  615.                 if (len.matches(".*\\+.*")) {                    // have plus
  616.                     log("len.matches +");
  617.                     if (keywords.matches(".*like(.*).*")) {        // have LIKE()
  618.                         log("keywords.matches like(");
  619.                         
  620.                         // yes, length adjustment
  621.                         // delete the spaces
  622.                         String lenAdj = len.replace(" ", "").trim();
  623.                         log("lenAdj=" + lenAdj);
  624.  
  625.                         // split the LIKE() into two strings by use of regex groups
  626.                         // ...LIKE(LIKEVAR
  627.                         // ) INZ(12)...
  628.                         Pattern likeSplit = Pattern.compile("(.*like\\([^\\)]*)(\\).*)");
  629.                         Matcher m = likeSplit.matcher(keywords);
  630.                         if (m.matches()) {
  631.                             log("group 1=" + m.group(1));
  632.                             log("group 1=" + m.group(2));
  633.                         
  634.                             // re-assemble with the length adjustment inserted
  635.                             keywordsAdj = m.group(1) + ": " + lenAdj + m.group(2);
  636.                             log("keywordsAdj=" + keywordsAdj);
  637.                             
  638.                             keywords = keywordsAdj;
  639.                         } else {
  640.                             log("no match");
  641.                         }
  642.                     }
  643.                 }
  644.                 
  645.             }
  646.         }
  647.         return keywords;
  648.     }
  649.  
  650.     /**
  651.      * Extract right hand comment from raw d-spec
  652.      * @param sourceStmt String raw d-spec
  653.      * @return rhComment String right-hand comment
  654.      */
  655.     private String getRhCommentFromText(String sourceStmt) {
  656.         String rhComment = "";
  657.         
  658.         if (sourceStmt.length() > 81) {
  659.             int i = 100;
  660.             if (sourceStmt.length() <= i) {
  661.                 i = sourceStmt.length();
  662.             }
  663.             rhComment = sourceStmt.substring(80, i).trim();
  664.         }
  665.         return rhComment;
  666.     }
  667.  
  668.  
  669.     // ==========================================================
  670.     // utility methods
  671.     /**
  672.      * is this entire spec a comment line?
  673.      * @param sourceStmt String - raw D-specification
  674.      * @param d DSpec - the parsed d-spec object
  675.      * @return true if entire line is a comment
  676.      */
  677.     private boolean isComment(String sourceStmt) {
  678.         boolean isComment = false;
  679.  
  680.         if (sourceStmt.length() >= 8) {
  681.  
  682.             // comments can be either a * in column 7 or
  683.             // a pair of slashes preceded by white space
  684.             // the first is easy:
  685.             if (sourceStmt.substring(6, 7).equals("*")) {
  686.                 isComment = true;
  687.             }
  688.  
  689.             // the second is a bit harder: 
  690.             if (sourceStmt.matches(".{5} *//.*")) {
  691.                 isComment = true;
  692.             }
  693.         }
  694.         return isComment;
  695.     }
  696.  
  697.     /**
  698.      * extract comment from a comment line
  699.      * @param sourceStmt String - raw D-specification
  700.      * @return comment String
  701.      */
  702.     private String getComment(String sourceStmt) {
  703.         String comment = "";
  704.  
  705.         if (isComment(sourceStmt)) {
  706.  
  707.             // comments can be either a * in column 7 or
  708.             // a pair of slashes preceded by white space
  709.             // the first is easy:
  710.             if (sourceStmt.substring(6, 7).equals("*")) {
  711.                 comment = sourceStmt.substring(8, sourceStmt.length());
  712.             }
  713.  
  714.             // the second is a bit harder: 
  715.             if (sourceStmt.matches(".{5} *//.*")) {
  716.                 String[] parts = sourceStmt.split(".{5} *//");
  717.                 comment = parts[1];
  718.             }
  719.         }
  720.         return comment;
  721.     }
  722.  
  723.     /**
  724.      * Log debugging text to the error log
  725.      * @param message String 
  726.      */
  727.     private void log(String message) {
  728.         final boolean DEBUG = false;
  729.         
  730.         if (DEBUG) {
  731.             LpexLog.log(message);
  732.         }
  733.     }    
  734. }
  735.  
  736. ===============================
  737.  
  738. package com.kc2hiz.lpexextensions;
  739.  
  740. import org.eclipse.ui.plugin.AbstractUIPlugin;
  741. import org.osgi.framework.BundleContext;
  742.  
  743. /**
  744.  * The activator class controls the plug-in life cycle
  745.  * Needs to implement com.ibm.lpex.alef.LpexPreload and have a preload() method
  746.  */
  747. public class Activator extends AbstractUIPlugin 
  748.     implements com.ibm.lpex.alef.LpexPreload {
  749.  
  750.     // The plug-in ID
  751.     public static final String PLUGIN_ID = "com.kc2hiz.lpexextensions"; //$NON-NLS-1$
  752.     // The shared instance
  753.     private static Activator plugin;
  754.     
  755.     /**
  756.      * The constructor
  757.      */
  758.     public Activator() {
  759.         plugin = this;
  760.     }
  761.  
  762.     /**
  763.      * Start the plug-in
  764.      * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
  765.      */
  766.     public void start(BundleContext context) throws Exception {
  767.         super.start(context);
  768.         // plugin = this;
  769.     }
  770.  
  771.     /**
  772.      * Stop the plug-in
  773.      * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
  774.      */
  775.     public void stop(BundleContext context) throws Exception {
  776.         plugin = null;
  777.         super.stop(context);
  778.     }
  779.  
  780.     /**
  781.      * Returns the shared instance
  782.      *
  783.      * @return the shared instance
  784.      */
  785.     public static Activator getDefault() {
  786.         return plugin;
  787.     }
  788.     
  789.     /**
  790.      * Activity to perform for LpexPreload
  791.      * @see com.ibm.lpex.alef.LpexPreload#preload()
  792.      */
  793.     public void preload() {
  794.     }
  795. }
  796.  
  797.  
  798. ==========================================
  799.  
  800. MANIFEST.MF
  801.  
  802. Manifest-Version: 1.0
  803. Bundle-ManifestVersion: 2
  804. Bundle-Name: Lpexextensions
  805. Bundle-SymbolicName: com.kc2hiz.lpexextensions;singleton:=true
  806. Bundle-Version: 1.0.0.qualifier
  807. Bundle-Activator: com.kc2hiz.lpexextensions.Activator
  808. Bundle-Vendor: KC2HIZ
  809. Require-Bundle: org.eclipse.ui,
  810.  org.eclipse.core.runtime,
  811.  com.ibm.lpex;bundle-version="4.2.3"
  812. Bundle-RequiredExecutionEnvironment: JavaSE-1.7
  813. Bundle-ActivationPolicy: lazy
  814. Export-Package: com.kc2hiz.lpexextensions
  815.  
  816.  
  817. =============================================
  818.  
  819. plugin.xml
  820.  
  821. <?xml version="1.0" encoding="UTF-8"?>
  822. <?eclipse version="3.4"?>
  823. <plugin>
  824.    <extension
  825.          point="com.ibm.lpex.preload">
  826.       <preload
  827.             class="com.kc2hiz.lpexextensions.Activator">
  828.       </preload>
  829.    </extension>
  830.  
  831. </plugin>
  832.  
© 2004-2019 by midrange.com generated in 0.154s valid xhtml & css