in reply to How to match and transform a multiline pattern?
Update: Corrected and simplified.
Based upon the single example you've provided this will need modifications, but it might give you a starting point. The basic assumption it makes are that functions in your java code always end with a single '}' in the first column of an otherwise blank line. It uses that to read the file one function at a time and performs the modifications.
As coded, the regex is pretty sensetive to variations in the formating of the source code. This can be loosened as appropriate, but increases the risk of false substitutions. It attempts to retain the original indentation, but it may not work in all situations.
#! perl -slw use strict; $/ = "\n}\n"; while( <DATA> ) { s[ ( nodeName.equals \( \s* " ( [^"]+ ) " ( .+? ) #" new \s+ ( Integer | Double ) \( .+? \)\)\); ) ][msg.isSetField(${2}.FIELD${3}(msg.get$4(${2}.FIELD));]sgx; print; } __DATA__ public Product getProduct(Node node) { // --- bunch of logic here --- if (nodeName.equals("ItemNum")) { product.setItemNumber( new Integer(StringUtils.parseInt(nodeValue, 0))); } if (nodeName.equals("AvgPrice")) { product.setAveragePrice( new Double(StringUtils.parseDouble(nodeValue, 0.0))); } } public Product getProduct(Node node) { // --- bunch of logic here --- if (nodeName.equals("ItemNum")) { product.setItemNumber( new Integer(StringUtils.parseInt(nodeValue, 0))); } if (nodeName.equals("AvgPrice")) { product.setAveragePrice( new Double(StringUtils.parseDouble(nodeValue, 0.0))); } } public Product getProduct(Node node) { // --- bunch of logic here --- if (nodeName.equals("ItemNum")) { product.setItemNumber( new Integer(StringUtils.parseInt(nodeValue, 0))); } if (nodeName.equals("AvgPrice")) { product.setAveragePrice( new Double(StringUtils.parseDouble(nodeValue, 0.0))); } }
Outputs:
C:\test>junk1 public Product getProduct(Node node) { // --- bunch of logic here --- if ( msg.isSetField(ItemNum.FIELD)) { product.setItemNumber( (msg.getInteger(ItemNum.FIELD)); } if ( msg.isSetField(AvgPrice.FIELD)) { product.setAveragePrice( (msg.getDouble(AvgPrice.FIELD)); } } public Product getProduct(Node node) { // --- bunch of logic here --- if ( msg.isSetField(ItemNum.FIELD)) { product.setItemNumber( (msg.getInteger(ItemNum.FIELD)); } if ( msg.isSetField(AvgPrice.FIELD)) { product.setAveragePrice( (msg.getDouble(AvgPrice.FIELD)); } } public Product getProduct(Node node) { // --- bunch of logic here --- if ( msg.isSetField(ItemNum.FIELD)) { product.setItemNumber( (msg.getInteger(ItemNum.FIELD)); } if ( msg.isSetField(AvgPrice.FIELD)) { product.setAveragePrice( (msg.getDouble(AvgPrice.FIELD)); } }
Which is just 3 repetitions of the one example.
I'm not sure I would bother to code a filter for a single 2000 line sourcefile. I could almost certainly do that much more quickly in my editor with a macro or two--or even manually--but its fun to try :)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to match and transform a multiline pattern?
by nbhatia (Novice) on Apr 20, 2007 at 19:39 UTC | |
by BrowserUk (Patriarch) on Apr 20, 2007 at 19:46 UTC | |
by GrandFather (Saint) on Apr 20, 2007 at 23:12 UTC | |
by nbhatia (Novice) on Apr 23, 2007 at 12:38 UTC |