001: /*
002: * ====================================================================
003: * Copyright (c) 2004-2008 TMate Software Ltd. All rights reserved.
004: *
005: * This software is licensed as described in the file COPYING, which
006: * you should have received as part of this distribution. The terms
007: * are also available at http://svnkit.com/license.html
008: * If newer versions of this license are posted there, you may use a
009: * newer version instead, at your option.
010: * ====================================================================
011: */
012: package org.tmatesoft.svn.core.internal.wc.admin;
013:
014: import org.tmatesoft.svn.core.SVNErrorCode;
015: import org.tmatesoft.svn.core.SVNErrorMessage;
016: import org.tmatesoft.svn.core.SVNException;
017: import org.tmatesoft.svn.core.internal.util.SVNEncodingUtil;
018: import org.tmatesoft.svn.core.internal.wc.SVNErrorManager;
019: import org.tmatesoft.svn.core.internal.wc.SVNFileUtil;
020:
021: import java.io.BufferedReader;
022: import java.io.File;
023: import java.io.IOException;
024: import java.io.InputStreamReader;
025: import java.io.OutputStreamWriter;
026: import java.io.Writer;
027: import java.util.ArrayList;
028: import java.util.Collection;
029: import java.util.HashMap;
030: import java.util.Iterator;
031: import java.util.Map;
032:
033: /**
034: * @version 1.1.1
035: * @author TMate Software Ltd.
036: */
037: public class SVNLogImpl extends SVNLog {
038:
039: private File myFile;
040: private File myTmpFile;
041:
042: public SVNLogImpl(File logFile, File tmpFile, SVNAdminArea adminArea) {
043: super (adminArea);
044: myFile = logFile;
045: myTmpFile = tmpFile;
046: }
047:
048: public void save() throws SVNException {
049: if (myTmpFile == null || myCache == null) {
050: return;
051: }
052:
053: Writer os = null;
054: try {
055: os = new OutputStreamWriter(SVNFileUtil
056: .openFileForWriting(myTmpFile), "UTF-8");
057: for (Iterator commands = myCache.iterator(); commands
058: .hasNext();) {
059: Map command = (Map) commands.next();
060: String name = (String) command.remove("");
061: os.write("<");
062: os.write(name);
063: for (Iterator attrs = command.keySet().iterator(); attrs
064: .hasNext();) {
065: String attr = (String) attrs.next();
066: String value = (String) command.get(attr);
067: if (value == null) {
068: value = "";
069: }
070: value = SVNEncodingUtil.xmlEncodeAttr(value);
071: os.write("\n ");
072: os.write(attr);
073: os.write("=\"");
074: os.write(value);
075: os.write("\"");
076: }
077: os.write("/>\n");
078: }
079: } catch (IOException e) {
080: SVNErrorMessage err = SVNErrorMessage.create(
081: SVNErrorCode.IO_ERROR,
082: "Cannot write log file ''{0}'': {1}", new Object[] {
083: myFile, e.getLocalizedMessage() });
084: SVNErrorManager.error(err, e);
085: } finally {
086: SVNFileUtil.closeFile(os);
087: myCache = null;
088: }
089: SVNFileUtil.rename(myTmpFile, myFile);
090: SVNFileUtil.setReadonly(myFile, true);
091: }
092:
093: public Collection readCommands() throws SVNException {
094: if (!myFile.exists()) {
095: return null;
096: }
097: BufferedReader reader = null;
098: Collection commands = new ArrayList();
099: try {
100: reader = new BufferedReader(new InputStreamReader(
101: SVNFileUtil.openFileForReading(myFile), "UTF-8"));
102: String line;
103: Map attrs = new HashMap();
104: String name = null;
105: while ((line = reader.readLine()) != null) {
106: line = line.trim();
107: if (line.startsWith("<")) {
108: name = line.substring(1);
109: continue;
110: }
111: int index = line.indexOf('=');
112: if (index > 0) {
113: String attrName = line.substring(0, index).trim();
114: String value = line.substring(index + 1).trim();
115: if (value.endsWith("/>")) {
116: value = value.substring(0, value.length()
117: - "/>".length());
118: }
119: if (value.startsWith("\"")) {
120: value = value.substring(1);
121: }
122: if (value.endsWith("\"")) {
123: value = value.substring(0, value.length() - 1);
124: }
125: value = SVNEncodingUtil.xmlDecode(value);
126: if ("".equals(value)
127: && !SVNLog.NAME_ATTR.equals(attrName)) {
128: value = null;
129: }
130: attrs.put(attrName, value);
131: }
132: if (line.endsWith("/>") && name != null) {
133: // run command
134: attrs.put("", name);
135: commands.add(attrs);
136: attrs = new HashMap();
137: name = null;
138: }
139: }
140: } catch (IOException e) {
141: SVNErrorMessage err = SVNErrorMessage.create(
142: SVNErrorCode.IO_ERROR,
143: "Cannot read log file ''{0}'': {1}", new Object[] {
144: myFile, e.getLocalizedMessage() });
145: SVNErrorManager.error(err, e);
146: } finally {
147: SVNFileUtil.closeFile(reader);
148: }
149:
150: return commands;
151: }
152:
153: public String toString() {
154: return "Log: " + myFile;
155: }
156:
157: public void delete() throws SVNException {
158: SVNFileUtil.deleteFile(myFile);
159: SVNFileUtil.deleteFile(myTmpFile);
160: }
161:
162: public boolean exists() {
163: return myFile.exists();
164: }
165:
166: }
|