<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ไอเอินบล๊อก &#187; pathManager</title>
	<atom:link href="http://www.iearn.in.th/tag/pathmanager/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.iearn.in.th</link>
	<description>Just Play and Just Knowledge</description>
	<lastBuildDate>Fri, 05 Aug 2011 15:10:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>File permission with regular expression</title>
		<link>http://www.iearn.in.th/2008/04/25/file-permission-with-regular-expression/</link>
		<comments>http://www.iearn.in.th/2008/04/25/file-permission-with-regular-expression/#comments</comments>
		<pubDate>Fri, 25 Apr 2008 11:37:30 +0000</pubDate>
		<dc:creator>nolifelover</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[File]]></category>
		<category><![CDATA[Hashset]]></category>
		<category><![CDATA[mLri]]></category>
		<category><![CDATA[pathManager]]></category>
		<category><![CDATA[permission]]></category>
		<category><![CDATA[regular expression]]></category>
		<category><![CDATA[softwareskill]]></category>
		<category><![CDATA[source code]]></category>

		<guid isPermaLink="false">http://www.jaideawhosting.com/nolifelover/?p=126</guid>
		<description><![CDATA[สำหรับโจทย์ในวันนี้ืคือการเขียนโปรแกรมต่อจากของเมื่อวานเกี่ยวกับการตวจสอบ permission ของ file ว่าเราสามารถ access เข้าใช้ได้หรือไม่โดย หากตรงกับกฏของการ denied ให้ทำการ denied เลย แต่หากไม่ตรงก็ให้ทำการตวจสอบว่าสามารถเข้าใช้ได้หรือไม่ ครับโดยมี source code สำหรับข้อนี้เป็นโจทย์มาให้คือ 1 2 3 4 5 6 7 8 9 10  public static void main&#40;String&#91;&#93; args&#41;  &#123;   PathManager pathManager = new PathManager&#40;&#41;;   pathManager.addDeniedRule&#40;&#34;\\w.*\\.exe$&#34;&#41;;   pathManager.addAcceptedRule&#40;&#34;^\\d&#34;&#41;;   if&#40;pathMananager.isAllow&#40;&#34;c:/temp/12.exe&#34;&#41;&#41;   System.out.println&#40;&#34;accepted&#34;&#41;;   else   System.out.println&#40;&#34;denied&#34;&#41;;  &#125; สำหรับกฏต่างๆ user สามารถเขียน [...]]]></description>
			<content:encoded><![CDATA[<p>สำหรับโจทย์ในวันนี้ืคือการเขียนโปรแกรมต่อจากของเมื่อวานเกี่ยวกับการตวจสอบ permission ของ file ว่าเราสามารถ access เข้าใช้ได้หรือไม่โดย หากตรงกับกฏของการ denied ให้ทำการ denied เลย แต่หากไม่ตรงก็ให้ทำการตวจสอบว่าสามารถเข้าใช้ได้หรือไม่ ครับโดยมี source code สำหรับข้อนี้เป็นโจทย์มาให้คือ</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"> <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>
 <span style="color: #009900;">&#123;</span>
 	PathManager pathManager <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PathManager<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 	pathManager.<span style="color: #006633;">addDeniedRule</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>w.*<span style="color: #000099; font-weight: bold;">\\</span>.exe$&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 	pathManager.<span style="color: #006633;">addAcceptedRule</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;^<span style="color: #000099; font-weight: bold;">\\</span>d&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>pathMananager.<span style="color: #006633;">isAllow</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;c:/temp/12.exe&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
 		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;accepted&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 	<span style="color: #000000; font-weight: bold;">else</span>
 		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;denied&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><span id="more-126"></span></p>
<p>สำหรับกฏต่างๆ user สามารถเขียน method เพื่ีอ add rule ไปได้เรื่อยๆ ครับ สำหรับการเขียนของผมในวันนี้ใช้เรื่องของ Hashset, regular expression เบื้องต้น และใช้ method String.matches(String reg) เพื่อตรวจสอบว่าตรงกับ regular expression ที่เราต้องการหรือไม่ สำหรับโค้ดที่ได้ประมาณนี้ครับ</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">softwareskill</span><span style="color: #339933;">;</span>  
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.HashSet</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 *
 * @author mLri
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PathManager <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//field</span>
    <span style="color: #003399;">HashSet</span> deniedRule<span style="color: #339933;">;</span>
    <span style="color: #003399;">HashSet</span> acceptedRule<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        PathManager pathManager <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PathManager<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        pathManager.<span style="color: #006633;">addDeniedRule</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>w*<span style="color: #000099; font-weight: bold;">\\</span>.exe$&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        pathManager.<span style="color: #006633;">addDeniedRule</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>w*<span style="color: #000099; font-weight: bold;">\\</span>.bat$&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        pathManager.<span style="color: #006633;">addDeniedRule</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>w*<span style="color: #000099; font-weight: bold;">\\</span>.msi$&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        pathManager.<span style="color: #006633;">addAcceptedRule</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;^<span style="color: #000099; font-weight: bold;">\\</span>d.*&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        pathManager.<span style="color: #006633;">addAcceptedRule</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>w*(<span style="color: #000099; font-weight: bold;">\\</span>.jpg||<span style="color: #000099; font-weight: bold;">\\</span>.pdf||<span style="color: #000099; font-weight: bold;">\\</span>.png)$&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">//pathManager.addAcceptedRule(&quot;\\w*\\.png$&quot;);</span>
        <span style="color: #666666; font-style: italic;">//pathManager.addAcceptedRule(&quot;\\w*\\.pdf$&quot;);</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>pathManager.<span style="color: #006633;">isAllow</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;c:/tem/2aaa.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;accepted&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;denied&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> PathManager<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">deniedRule</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">HashSet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">acceptedRule</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">HashSet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> addDeniedRule<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> exp<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">deniedRule</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>exp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> addAcceptedRule<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> exp<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">acceptedRule</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>exp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> isAllow<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> file<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> tmp <span style="color: #339933;">=</span> file.<span style="color: #006633;">split</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>tmp <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            file <span style="color: #339933;">=</span> tmp<span style="color: #009900;">&#91;</span>tmp.<span style="color: #006633;">length</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">checkDeniedRule</span><span style="color: #009900;">&#40;</span>file<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">checkAcceptRule</span><span style="color: #009900;">&#40;</span>file<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> checkAcceptRule<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> file<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>acceptedRule.<span style="color: #006633;">isEmpty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> rule <span style="color: #339933;">:</span> acceptedRule<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">//System.out.println(&quot;file is &quot; + file);</span>
                <span style="color: #666666; font-style: italic;">//System.out.println(&quot;rule is &quot; + rule);</span>
                <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>file.<span style="color: #006633;">matches</span><span style="color: #009900;">&#40;</span>rule<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #666666; font-style: italic;">//System.out.println(&quot;rule is accepted&quot;);</span>
                    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> checkDeniedRule<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> file<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>deniedRule.<span style="color: #006633;">isEmpty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> rule <span style="color: #339933;">:</span> deniedRule<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">//System.out.println(&quot;file is &quot; + file);</span>
                <span style="color: #666666; font-style: italic;">//System.out.println(&quot;rule is &quot; + rule);</span>
                <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>file.<span style="color: #006633;">matches</span><span style="color: #009900;">&#40;</span>rule<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #666666; font-style: italic;">//System.out.println(&quot;rule is denied&quot;);</span>
                    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>HashSet คือ class ประมาณเดียวกับ Array สามารถเช็คการซำ้กันของสมาชิกได้หากซ้ำกันจะไม่ Add เพิ่มลงไปครับ<br />
Regular Expression คือสมการที่ใช้ในการตรวจสอบรูปแบบ format ของ string ว่าตรงตามที่เราต้องการหรือไม่โดยจะสามารถนำไปใช้ในการตรวจสอบ email การอัพโหลดไฟล์ ชนิดของไฟล์ประมาณนั้นครับ<br />
<a href="http://www.amk.ca/python/howto/regex/">http://www.amk.ca/python/howto/regex/</a><br />
<a href="http://www.aprelium.com/abyssws/articles/regex-basics.html">http://www.aprelium.com/abyssws/articles/regex-basics.html</a></p>

<p class="sayac_bilgi">54 views</p>
]]></content:encoded>
			<wfw:commentRss>http://www.iearn.in.th/2008/04/25/file-permission-with-regular-expression/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

