|
22 | 22 | import uniandes.tsdl.mutapk.model.location.MutationLocation; |
23 | 23 |
|
24 | 24 | public class InvalidColorDetector extends TextBasedDetector { |
25 | | - |
| 25 | + |
26 | 26 | public InvalidColorDetector(){ |
27 | 27 | this.type = MutationType.INVALID_COLOR; |
28 | 28 | } |
29 | | - |
| 29 | + |
30 | 30 | @Override |
31 | 31 | public List<MutationLocation> analyzeApp(String rootPath) throws Exception { |
32 | 32 | List<MutationLocation> locations = new ArrayList<MutationLocation>(); |
33 | 33 | List<Entry<String,String>> colorHex = new ArrayList<Entry<String,String>>(); |
34 | | - |
| 34 | + |
35 | 35 |
|
36 | 36 | String path = rootPath + File.separator+"res"+File.separator+"values"+File.separator + Helper.COLORS; |
37 | 37 |
|
38 | 38 | File fXmlFile = new File(path); |
39 | | - DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); |
40 | | - DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); |
41 | | - Document doc = dBuilder.parse(fXmlFile); |
42 | | - doc.getDocumentElement().normalize(); |
43 | | - |
44 | | - // Get all activities |
45 | | - NodeList nodeList = doc.getElementsByTagName("color"); |
46 | | - for (int i = 0; i < nodeList.getLength(); i++) { |
47 | | - Node node = nodeList.item(i); |
48 | | - String colorName = node.getAttributes().getNamedItem("name").getNodeValue(); |
49 | | - colorHex.add(new SimpleEntry<String,String>(colorName,node.getTextContent())); |
50 | | - } |
51 | | - |
52 | | - // Read Source Code |
53 | | - BufferedReader reader = new BufferedReader(new FileReader( |
54 | | - new File(path))); |
55 | | - String line = null; |
56 | | - int startLine = 0; |
57 | | - int startCol = 0; |
58 | | - int endCol = 0; |
59 | | - int lineNum = 0; |
60 | | - String originalColor = ""; |
61 | | - String colorName = ""; |
62 | | - boolean isColorTag = false; |
63 | | - int currentLine = 0; |
64 | | - while ((line = reader.readLine()) != null) { |
| 39 | + if(fXmlFile.exists()) { |
| 40 | + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); |
| 41 | + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); |
| 42 | + Document doc = dBuilder.parse(fXmlFile); |
| 43 | + doc.getDocumentElement().normalize(); |
65 | 44 |
|
66 | | - if (line.contains("<color")) { |
67 | | - startLine = currentLine; |
68 | | - isColorTag = true; |
| 45 | + // Get all activities |
| 46 | + NodeList nodeList = doc.getElementsByTagName("color"); |
| 47 | + for (int i = 0; i < nodeList.getLength(); i++) { |
| 48 | + Node node = nodeList.item(i); |
| 49 | + String colorName = node.getAttributes().getNamedItem("name").getNodeValue(); |
| 50 | + colorHex.add(new SimpleEntry<String,String>(colorName,node.getTextContent())); |
69 | 51 | } |
70 | | - if (isColorTag) { |
71 | | - for(Entry<String,String> color : colorHex){ |
72 | | - if(line.contains(color.getKey())){ |
73 | | - startCol = line.indexOf(color.getValue())+1; |
74 | | - endCol = startCol+color.getValue().length()-1; |
75 | | - lineNum = currentLine; |
76 | | - originalColor = color.getValue(); |
77 | | - colorName = color.getKey(); |
78 | | - } |
| 52 | + for(Entry<String,String> color : colorHex){ |
| 53 | + System.out.println(color.getKey()); |
| 54 | + System.out.println(color.getValue()); |
| 55 | + } |
| 56 | + |
| 57 | + // Read Source Code |
| 58 | + BufferedReader reader = new BufferedReader(new FileReader( |
| 59 | + new File(path))); |
| 60 | + String line = null; |
| 61 | + int startLine = 0; |
| 62 | + int startCol = 0; |
| 63 | + int endCol = 0; |
| 64 | + int lineNum = 0; |
| 65 | + String originalColor = ""; |
| 66 | + String colorName = ""; |
| 67 | + boolean isColorTag = false; |
| 68 | + int currentLine = 0; |
| 69 | + while ((line = reader.readLine()) != null) { |
| 70 | + |
| 71 | + if (line.contains("<color")) { |
| 72 | + startLine = currentLine; |
| 73 | + isColorTag = true; |
79 | 74 | } |
80 | | - if (line.contains(">")) { |
81 | | - locations.add(InvalidColorMutationLocation.buildLocation(path, startLine, currentLine, startCol, endCol, lineNum, -1, this.getType(), colorName, originalColor)); |
82 | | - isColorTag = false; |
| 75 | + if (isColorTag) { |
| 76 | + for(Entry<String,String> color : colorHex){ |
| 77 | + if(line.contains("\""+color.getKey()+"\"")){ |
| 78 | + startCol = line.indexOf(color.getValue())+1; |
| 79 | + endCol = startCol+color.getValue().length()-1; |
| 80 | + lineNum = currentLine; |
| 81 | + originalColor = color.getValue(); |
| 82 | + colorName = color.getKey(); |
| 83 | + } |
| 84 | + } |
| 85 | + if (line.contains(">")) { |
| 86 | + locations.add(InvalidColorMutationLocation.buildLocation(path, startLine, currentLine, startCol, endCol, lineNum, -1, this.getType(), colorName, originalColor)); |
| 87 | + isColorTag = false; |
| 88 | + } |
83 | 89 | } |
84 | | - } |
85 | 90 |
|
86 | | - currentLine++; |
| 91 | + currentLine++; |
| 92 | + } |
| 93 | + reader.close(); |
87 | 94 | } |
88 | | - reader.close(); |
89 | | - |
90 | 95 | return locations; |
91 | 96 | } |
92 | 97 |
|
|
0 commit comments