สำหรับโจทย์ในวันนี้ืคือการเขียนโปรแกรมต่อจากของเมื่อวานเกี่ยวกับการตวจสอบ permission ของ file ว่าเราสามารถ access เข้าใช้ได้หรือไม่โดย หากตรงกับกฏของการ denied ให้ทำการ denied เลย แต่หากไม่ตรงก็ให้ทำการตวจสอบว่าสามารถเข้าใช้ได้หรือไม่ ครับโดยมี source code สำหรับข้อนี้เป็นโจทย์มาให้คือ
1 2 3 4 5 6 7 8 9 10 | public static void main(String[] args) { PathManager pathManager = new PathManager(); pathManager.addDeniedRule("\\w.*\\.exe$"); pathManager.addAcceptedRule("^\\d"); if(pathMananager.isAllow("c:/temp/12.exe")) System.out.println("accepted"); else System.out.println("denied"); } |
สำหรับกฏต่างๆ user สามารถเขียน method เพื่ีอ add rule ไปได้เรื่อยๆ ครับ สำหรับการเขียนของผมในวันนี้ใช้เรื่องของ Hashset, regular expression เบื้องต้น และใช้ method String.matches(String reg) เพื่อตรวจสอบว่าตรงกับ regular expression ที่เราต้องการหรือไม่ สำหรับโค้ดที่ได้ประมาณนี้ครับ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | package softwareskill; import java.util.HashSet; /** * * @author mLri */ public class PathManager { //field HashSet deniedRule; HashSet acceptedRule; public static void main(String[] args) { PathManager pathManager = new PathManager(); pathManager.addDeniedRule("\\w*\\.exe$"); pathManager.addDeniedRule("\\w*\\.bat$"); pathManager.addDeniedRule("\\w*\\.msi$"); pathManager.addAcceptedRule("^\\d.*"); pathManager.addAcceptedRule("\\w*(\\.jpg||\\.pdf||\\.png)$"); //pathManager.addAcceptedRule("\\w*\\.png$"); //pathManager.addAcceptedRule("\\w*\\.pdf$"); if (pathManager.isAllow("c:/tem/2aaa.jpg")) { System.out.println("accepted"); } else { System.out.println("denied"); } } public PathManager() { this.deniedRule = new HashSet(); this.acceptedRule = new HashSet(); } public boolean addDeniedRule(String exp) { return this.deniedRule.add(exp); } public boolean addAcceptedRule(String exp) { return this.acceptedRule.add(exp); } public boolean isAllow(String file) { String[] tmp = file.split("/"); if (tmp != null) { file = tmp[tmp.length-1]; } if (this.checkDeniedRule(file)) { return this.checkAcceptRule(file); } else { return false; } } private boolean checkAcceptRule(String file) { if (acceptedRule.isEmpty()) { return true; } else { for (String rule : acceptedRule) { //System.out.println("file is " + file); //System.out.println("rule is " + rule); if (file.matches(rule)) { //System.out.println("rule is accepted"); return true; } } return false; } } private boolean checkDeniedRule(String file) { if (deniedRule.isEmpty()) { return true; } else { for (String rule : deniedRule) { //System.out.println("file is " + file); //System.out.println("rule is " + rule); if (file.matches(rule)) { //System.out.println("rule is denied"); return false; } } return true; } } } |
HashSet คือ class ประมาณเดียวกับ Array สามารถเช็คการซำ้กันของสมาชิกได้หากซ้ำกันจะไม่ Add เพิ่มลงไปครับ
Regular Expression คือสมการที่ใช้ในการตรวจสอบรูปแบบ format ของ string ว่าตรงตามที่เราต้องการหรือไม่โดยจะสามารถนำไปใช้ในการตรวจสอบ email การอัพโหลดไฟล์ ชนิดของไฟล์ประมาณนั้นครับ
http://www.amk.ca/python/howto/regex/
http://www.aprelium.com/abyssws/articles/regex-basics.html
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.
