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:
013: package org.tmatesoft.svn.cli.command;
014:
015: import java.io.File;
016: import java.io.IOException;
017: import java.io.InputStream;
018: import java.io.PrintStream;
019: import java.text.DateFormat;
020: import java.text.SimpleDateFormat;
021: import java.util.Date;
022: import java.util.TimeZone;
023:
024: import org.tmatesoft.svn.cli.SVNArgument;
025: import org.tmatesoft.svn.cli.SVNCommand;
026: import org.tmatesoft.svn.core.ISVNDirEntryHandler;
027: import org.tmatesoft.svn.core.SVNDirEntry;
028: import org.tmatesoft.svn.core.SVNException;
029: import org.tmatesoft.svn.core.SVNNodeKind;
030: import org.tmatesoft.svn.core.SVNURL;
031: import org.tmatesoft.svn.core.wc.SVNLogClient;
032: import org.tmatesoft.svn.core.wc.SVNRevision;
033: import org.tmatesoft.svn.core.wc.xml.SVNXMLDirEntryHandler;
034: import org.tmatesoft.svn.core.wc.xml.SVNXMLSerializer;
035:
036: /**
037: * @version 1.1.1
038: * @author TMate Software Ltd.
039: */
040: public class SVNLsCommand extends SVNCommand implements
041: ISVNDirEntryHandler {
042:
043: private PrintStream myPrintStream;
044: private boolean myIsVerbose;
045:
046: private static final DateFormat LONG_DATE_FORMAT = new SimpleDateFormat(
047: "MM' 'dd' 'yyyy");
048: private static final DateFormat SHORT_DATE_FORMAT = new SimpleDateFormat(
049: "MM' 'dd' 'HH:mm");
050:
051: static {
052: SHORT_DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT"));
053: LONG_DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT"));
054: }
055:
056: public void run(InputStream in, PrintStream out, PrintStream err)
057: throws SVNException {
058: run(out, err);
059: }
060:
061: public void run(PrintStream out, PrintStream err)
062: throws SVNException {
063: boolean recursive = getCommandLine().hasArgument(
064: SVNArgument.RECURSIVE);
065: myIsVerbose = getCommandLine().hasArgument(SVNArgument.VERBOSE);
066: myPrintStream = out;
067:
068: boolean isXml = getCommandLine().hasArgument(SVNArgument.XML);
069: SVNXMLSerializer serializer = isXml ? new SVNXMLSerializer(
070: myPrintStream) : null;
071: SVNXMLDirEntryHandler handler = isXml ? new SVNXMLDirEntryHandler(
072: serializer)
073: : null;
074:
075: SVNRevision revision = parseRevision(getCommandLine());
076: SVNLogClient logClient = getClientManager().getLogClient();
077: if (!getCommandLine().hasURLs() && !getCommandLine().hasPaths()) {
078: getCommandLine().setPathAt(0, "");
079: }
080: if (handler != null) {
081: if (!getCommandLine().hasArgument(SVNArgument.INCREMENTAL)) {
082: handler.startDocument();
083: }
084: }
085: for (int i = 0; i < getCommandLine().getURLCount(); i++) {
086: String url = getCommandLine().getURL(i);
087: if (handler != null) {
088: handler.startTarget(url);
089: }
090: logClient
091: .doList(SVNURL.parseURIEncoded(url),
092: getCommandLine().getPegRevision(i),
093: revision == null ? SVNRevision.UNDEFINED
094: : revision, myIsVerbose || isXml,
095: recursive, isXml ? handler
096: : (ISVNDirEntryHandler) this );
097: if (handler != null) {
098: handler.endTarget();
099: }
100: }
101: for (int i = 0; i < getCommandLine().getPathCount(); i++) {
102: File path = new File(getCommandLine().getPathAt(i))
103: .getAbsoluteFile();
104: if (handler != null) {
105: handler.startTarget(path.getAbsolutePath().replace(
106: File.separatorChar, '/'));
107: }
108: logClient
109: .doList(path, getCommandLine()
110: .getPathPegRevision(i), revision == null
111: || !revision.isValid() ? SVNRevision.BASE
112: : revision, myIsVerbose || isXml,
113: recursive, isXml ? handler
114: : (ISVNDirEntryHandler) this );
115: if (handler != null) {
116: handler.endTarget();
117: }
118: }
119: if (handler != null) {
120: if (!getCommandLine().hasArgument(SVNArgument.INCREMENTAL)) {
121: handler.endDocument();
122: }
123: try {
124: serializer.flush();
125: } catch (IOException e) {
126: }
127: }
128: }
129:
130: public void handleDirEntry(SVNDirEntry dirEntry) {
131: if (myIsVerbose) {
132: StringBuffer verbose = new StringBuffer();
133: verbose.append(SVNCommand.formatString(dirEntry
134: .getRevision()
135: + "", 7, false));
136: verbose.append(' ');
137: verbose.append(SVNCommand.formatString(
138: dirEntry.getAuthor() == null ? " ? " : dirEntry
139: .getAuthor(), 16, true));
140: verbose.append(' ');
141: verbose.append(dirEntry.getLock() != null ? 'O' : ' ');
142: verbose.append(' ');
143: verbose.append(SVNCommand.formatString(
144: dirEntry.getKind() == SVNNodeKind.DIR ? ""
145: : dirEntry.getSize() + "", 10, false));
146: verbose.append(' ');
147: // time now.
148: Date d = dirEntry.getDate();
149: String timeStr = "";
150: if (d != null) {
151: if (System.currentTimeMillis() - d.getTime() < 365 * 1000 * 86400 / 2) {
152: timeStr = SHORT_DATE_FORMAT.format(d);
153: } else {
154: timeStr = LONG_DATE_FORMAT.format(d);
155: }
156: }
157: verbose.append(SVNCommand.formatString(timeStr, 12, false));
158: verbose.append(' ');
159: myPrintStream.print(verbose.toString());
160: }
161: myPrintStream.print(dirEntry.getRelativePath());
162: if (dirEntry.getKind() == SVNNodeKind.DIR) {
163: myPrintStream.print('/');
164: }
165: myPrintStream.println();
166: }
167:
168: }
|