Advertisement
Guest User

romaparse

a guest
Jul 2nd, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.68 KB | None | 0 0
  1. import javax.xml.parsers.DocumentBuilder;
  2. import javax.xml.parsers.DocumentBuilderFactory;
  3. import javax.xml.parsers.ParserConfigurationException;
  4. import javax.xml.xpath.XPath;
  5. import javax.xml.xpath.XPathConstants;
  6. import javax.xml.xpath.XPathExpressionException;
  7. import javax.xml.xpath.XPathFactory;
  8.  
  9. import java.io.BufferedReader;
  10. import java.io.File;
  11. import java.io.FileInputStream;
  12. import java.io.FileReader;
  13. import java.io.FileWriter;
  14. import java.io.IOException;
  15. import java.util.HashMap;
  16.  
  17. import org.w3c.dom.Document;
  18. import org.w3c.dom.NodeList;
  19. import org.xml.sax.SAXException;
  20.  
  21. public class ParseAspect {
  22.  
  23.     public static void main(String[] args) {
  24.         HashMap<Integer, String> myFuncMap = new HashMap<Integer, String>();
  25.         StringBuilder funcresultnew = new StringBuilder();
  26.         File[] listOfFiles = new File(args[0]).listFiles();
  27.         int funcCounter;
  28.  
  29.         try (BufferedReader br = new BufferedReader(new FileReader(args[1]));
  30.                 FileWriter fw = new FileWriter(new File(args[2]));) {
  31.  
  32.             String line = br.readLine();
  33.             funcCounter = 0;
  34.             while (line != null) {
  35.                 myFuncMap.put(funcCounter, line);
  36.                 line = br.readLine();
  37.                 funcCounter++;
  38.             }
  39.  
  40.             for (int i = 0; i < listOfFiles.length; i++) {
  41.                 System.out.println(listOfFiles[i].getPath());
  42.                 funcresultnew.append(parseScript(listOfFiles[i].getPath(), myFuncMap));
  43.             }
  44.  
  45.             fw.write(funcresultnew.toString());
  46.  
  47.             for (int i = 0; i < myFuncMap.size(); i++) {
  48.                 if (!funcresultnew.toString().toLowerCase()
  49.                         .contains(myFuncMap.get(i).toLowerCase())) {
  50.                     fw.write(System.lineSeparator());
  51.                     fw.write(myFuncMap.get(i).toLowerCase());
  52.                 }
  53.             }
  54.  
  55.         } catch (IOException e) {
  56.             System.out.println(e.getMessage());
  57.         }
  58.     }
  59.  
  60.     public static String parseScript(String filename,
  61.             HashMap<Integer, String> myFuncMapfrom) {
  62.  
  63.         int funcCounter = 0;
  64.         DocumentBuilderFactory builderFactory = DocumentBuilderFactory
  65.                 .newInstance();
  66.         DocumentBuilder builder;
  67.         StringBuilder funcresult = new StringBuilder();
  68.         StringBuilder funcstrings = new StringBuilder();
  69.         XPath xPath = XPathFactory.newInstance().newXPath();
  70.         HashMap<Integer, String> myFuncMapto = new HashMap<Integer, String>(
  71.                 myFuncMapfrom);
  72.         Document xmlDocument = null;
  73.         try (FileInputStream file = new FileInputStream(new File(filename))) {
  74.             builder = builderFactory.newDocumentBuilder();
  75.             xmlDocument = builder.parse(file);
  76.             String expression = "/m3project/documents/document/nodes/node"
  77.                     + "[@type='12']/property[name='SQLStatement']/actualvalue/objectinstance/member"
  78.                     + "/value[text()]";
  79.             NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(
  80.                     xmlDocument, XPathConstants.NODESET);
  81.             for (int x = 0; x < nodeList.getLength(); x++) {
  82.                 for (int i = 0; i < myFuncMapto.size(); i++) {
  83.                     if (nodeList.item(x).getFirstChild().getNodeValue()
  84.                             .toLowerCase()
  85.                             .contains(myFuncMapto.get(i).toLowerCase())) {
  86.                         funcCounter++;
  87.                         funcstrings.append(nodeList.item(x).getFirstChild()
  88.                                 .getNodeValue());
  89.                         funcstrings.append(System.lineSeparator());
  90.                     }
  91.                 }
  92.             }
  93.         } catch (SAXException | ParserConfigurationException
  94.                 | XPathExpressionException | IOException ex) {
  95.             System.out.println(ex.getMessage());
  96.         }
  97.  
  98.         if (funcCounter > 0) {
  99.             funcresult.append(System.lineSeparator());
  100.             funcresult.append("*****************FileName***************");
  101.             funcresult.append(System.lineSeparator());
  102.             funcresult.append(filename);
  103.             funcresult.append(System.lineSeparator());
  104.             funcresult.append(funcstrings.toString());
  105.             funcresult.append(System.lineSeparator());
  106.             funcresult.append("Count: " + funcCounter);
  107.         }
  108.         return funcresult.toString();
  109.  
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement