KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > client > PropertiesClient


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.subversion.client;
21
22 import java.io.*;
23 import java.util.*;
24 import org.netbeans.modules.subversion.Subversion;
25 import org.netbeans.modules.subversion.client.parser.ParserSvnInfo;
26 import org.netbeans.modules.subversion.config.KVFile;
27 import org.tigris.subversion.svnclientadapter.ISVNInfo;
28 import org.tigris.subversion.svnclientadapter.SVNClientException;
29
30 /**
31  * Implements properties access that is not supported
32  * by svnClientAdapter library. It access <tt>.svn</tt>
33  * metadata directly:
34  *
35  * <pre>
36  * trunk/
37  * .svn/
38  * dir-props (KV file format)
39  * dir-props-base (KV file format)
40  * props/
41  * filename.svn-base (KV file format)
42  * filename_newprop.svn-base (KV file format)
43  * props-base/
44  * filename.svn-base (KV file format)
45  * filename
46  * filename_newprop
47  * </pre>
48  *
49  * <b>The implemetation should be moved into svnClientAdpater
50  * library!</b>
51  *
52  * @author Petr Kuzel
53  */

54 public final class PropertiesClient {
55
56     private final File file;
57
58     /** Creates a new instance of PropertiesClient */
59     public PropertiesClient(File file) {
60         assert file != null;
61         this.file = file;
62     }
63
64     /**
65      * Loads BASE properties for given file.
66      * @return property map&lt;String, byte[]> never null
67      */

68     public Map<String JavaDoc, byte[]> getBaseProperties() throws IOException {
69         File store;
70         try {
71             store = getPropertyFile(true);
72         } catch (SVNClientException ex) {
73             throw new IOException(ex.getMessage());
74         }
75         if (store != null && store.isFile()) {
76             KVFile kv = new KVFile(store);
77             return normalize(kv.getMap());
78         } else {
79             return new HashMap<String JavaDoc, byte[]>();
80         }
81     }
82
83     /**
84      * Loads (locally modified) properties for given file.
85      * @return property map&lt;String, byte[]> never null
86      */

87     public Map<String JavaDoc, byte[]> getProperties() throws IOException {
88         File store;
89         try {
90             store = getPropertyFile(false);
91         } catch (SVNClientException ex) {
92             throw new IOException(ex.getMessage());
93         }
94         if (store != null && store.isFile()) {
95             KVFile kv = new KVFile(store);
96             return normalize(kv.getMap());
97         } else {
98             return new HashMap<String JavaDoc, byte[]>();
99         }
100     }
101
102     private File getPropertyFile(boolean base) throws SVNClientException {
103         // XXX realy not sure if this is the best way ...
104
SvnClient client = Subversion.getInstance().getClient(false);
105         ISVNInfo info = null;
106         try {
107             info = client.getInfoFromWorkingCopy(file);
108         } catch (SVNClientException ex) {
109             throw ex;
110         }
111         if(info instanceof ParserSvnInfo) {
112             if(base) {
113                 return ((ParserSvnInfo) info).getBasePropertyFile();
114             } else {
115                 return ((ParserSvnInfo) info).getPropertyFile();
116             }
117         } else {
118             throw new SVNClientException("Unexpected value:" + info + " should be from type " + ParserSvnInfo.class);
119         }
120     }
121     
122     private Map<String JavaDoc, byte[]> normalize(Map map) {
123         Map<String JavaDoc, byte[]> ret = new HashMap<String JavaDoc, byte[]>(map.size());
124         Iterator<Map.Entry> it = map.entrySet().iterator();
125         while (it.hasNext()) {
126             Map.Entry next = it.next();
127             // getKey().toString() == the normalization
128
ret.put(next.getKey().toString(), (byte[]) next.getValue());
129         }
130         return ret;
131     }
132
133     /** Not implemented. */
134     public Map getProperties(int revision) throws IOException {
135         throw new UnsupportedOperationException JavaDoc();
136     }
137 }
138
Popular Tags