001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.subversion.client;
043:
044: import java.io.*;
045: import java.util.*;
046: import org.netbeans.modules.subversion.Subversion;
047: import org.netbeans.modules.subversion.client.parser.ParserSvnInfo;
048: import org.netbeans.modules.subversion.config.KVFile;
049: import org.tigris.subversion.svnclientadapter.ISVNInfo;
050: import org.tigris.subversion.svnclientadapter.SVNClientException;
051:
052: /**
053: * Implements properties access that is not supported
054: * by svnClientAdapter library. It access <tt>.svn</tt>
055: * metadata directly:
056: *
057: * <pre>
058: * trunk/
059: * .svn/
060: * dir-props (KV file format)
061: * dir-props-base (KV file format)
062: * props/
063: * filename.svn-base (KV file format)
064: * filename_newprop.svn-base (KV file format)
065: * props-base/
066: * filename.svn-base (KV file format)
067: * filename
068: * filename_newprop
069: * </pre>
070: *
071: * <b>The implemetation should be moved into svnClientAdpater
072: * library!</b>
073: *
074: * @author Petr Kuzel
075: */
076: public final class PropertiesClient {
077:
078: private final File file;
079:
080: /** Creates a new instance of PropertiesClient */
081: public PropertiesClient(File file) {
082: assert file != null;
083: this .file = file;
084: }
085:
086: /**
087: * Loads BASE properties for given file.
088: * @return property map<String, byte[]> never null
089: */
090: public Map<String, byte[]> getBaseProperties() throws IOException {
091: File store;
092: try {
093: store = getPropertyFile(true);
094: } catch (SVNClientException ex) {
095: throw new IOException(ex.getMessage());
096: }
097: if (store != null && store.isFile()) {
098: KVFile kv = new KVFile(store);
099: return kv.getNormalizedMap();
100: } else {
101: return new HashMap<String, byte[]>();
102: }
103: }
104:
105: /**
106: * Loads (locally modified) properties for given file.
107: * @return property map<String, byte[]> never null
108: */
109: public Map<String, byte[]> getProperties() throws IOException {
110: File store;
111: try {
112: store = getPropertyFile(false);
113: } catch (SVNClientException ex) {
114: throw new IOException(ex.getMessage());
115: }
116: if (store != null && store.isFile()) {
117: KVFile kv = new KVFile(store);
118: return kv.getNormalizedMap();
119: } else {
120: return new HashMap<String, byte[]>();
121: }
122: }
123:
124: private File getPropertyFile(boolean base)
125: throws SVNClientException {
126: // XXX realy not sure if this is the best way ...
127: SvnClient client = Subversion.getInstance().getClient(false);
128: ISVNInfo info = null;
129: try {
130: info = client.getInfoFromWorkingCopy(file);
131: } catch (SVNClientException ex) {
132: throw ex;
133: }
134: if (info instanceof ParserSvnInfo) {
135: if (base) {
136: return ((ParserSvnInfo) info).getBasePropertyFile();
137: } else {
138: return ((ParserSvnInfo) info).getPropertyFile();
139: }
140: } else {
141: throw new SVNClientException("Unexpected value:" + info
142: + " should be from type " + ParserSvnInfo.class);
143: }
144: }
145:
146: /** Not implemented. */
147: public Map getProperties(int revision) throws IOException {
148: throw new UnsupportedOperationException();
149: }
150: }
|