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.lib.cvsclient.command.add;
043:
044: import java.io.*;
045:
046: import org.netbeans.lib.cvsclient.command.*;
047: import org.netbeans.lib.cvsclient.event.*;
048:
049: /**
050: * Handles the building of add information object and the firing of
051: * events when complete objects are built.
052: *
053: * @author Milos Kleint
054: */
055: public class AddBuilder implements Builder {
056: private static final String UNKNOWN = ": nothing known about"; //NOI18N
057: private static final String ADDED = " added to the repository"; //NOI18N
058: private static final String WARNING = ": warning: "; //NOI18N
059: private static final String ALREADY_ENTERED = " has already been entered"; //NOI18N
060: private static final String SCHEDULING = ": scheduling file `"; //NOI18N
061: private static final String USE_COMMIT = ": use 'cvs commit' "; //NOI18N
062: private static final String DIRECTORY = "Directory "; //NOI18N
063: private static final String READDING = ": re-adding file "; //NOI18N
064: private static final String RESURRECTED = ", resurrected"; //NOI18N
065: private static final String RESUR_VERSION = ", version "; //NOI18N
066:
067: private static final boolean DEBUG = false;
068:
069: /**
070: * The status object that is currently being built.
071: */
072: private AddInformation addInformation;
073:
074: /**
075: * The event manager to use.
076: */
077: private EventManager eventManager;
078:
079: /**
080: * The directory in which the file being processed lives.
081: * This is relative to the local directory
082: */
083: private String fileDirectory;
084:
085: private AddCommand addCommand;
086:
087: private boolean readingTags;
088:
089: public AddBuilder(EventManager eventManager, AddCommand addCommand) {
090: this .eventManager = eventManager;
091: this .addCommand = addCommand;
092: }
093:
094: public void outputDone() {
095: if (addInformation != null) {
096: FileInfoEvent event = new FileInfoEvent(this ,
097: addInformation);
098: eventManager.fireCVSEvent(event);
099: addInformation = null;
100: }
101: }
102:
103: public void parseLine(String line, boolean isErrorMessage) {
104: if (line.endsWith(ADDED)) {
105: String directory = line.substring(DIRECTORY.length(), line
106: .indexOf(ADDED));
107: addDirectory(directory);
108: } else if (line.indexOf(SCHEDULING) >= 0) {
109: String filename = line.substring(
110: line.indexOf(SCHEDULING) + SCHEDULING.length(),
111: line.indexOf('\'')).trim();
112: addFile(filename);
113: } else if (line.indexOf(READDING) >= 0) {
114: String filename = line.substring(
115: line.indexOf(READDING) + READDING.length(),
116: line.indexOf('(')).trim();
117: addFile(filename);
118: } else if (line.endsWith(RESURRECTED)) {
119: String filename = line.substring(0, line.length()
120: - RESURRECTED.length());
121: resurrectFile(filename);
122: }
123: // ignore the rest..
124: }
125:
126: private File createFile(String fileName) {
127: File locFile = addCommand.getFileEndingWith(fileName);
128: if (locFile == null) {
129: // in case the exact match was not achieved using the getFileEndingWith method
130: // let's try to find the best match possible.
131: // iterate from the back of the filename string and try to match the endings
132: // of getFiles(). the best match is picked then.
133: // Works ok for files and directories in add, should not probably be used
134: // elsewhere where it's possible to have recursive commands and where resulting files
135: // are not listed in getFiles()
136: String name = fileName.replace('\\', '/');
137: File[] files = addCommand.getFiles();
138: int maxLevel = name.length();
139: File bestMatch = null;
140: String[] paths = new String[files.length];
141: for (int index = 0; index < files.length; index++) {
142: paths[index] = files[index].getAbsolutePath().replace(
143: '\\', '/');
144: }
145: int start = name.lastIndexOf('/');
146: String part = null;
147: if (start < 0) {
148: part = name;
149: } else {
150: part = name.substring(start + 1);
151: }
152: while (start >= 0 || part != null) {
153: boolean wasMatch = false;
154: for (int index = 0; index < paths.length; index++) {
155: if (paths[index].endsWith(part)) {
156: bestMatch = files[index];
157: wasMatch = true;
158: }
159: }
160: start = name.lastIndexOf('/', start - 1);
161: if (start < 0 || !wasMatch) {
162: break;
163: }
164: part = name.substring(start + 1);
165: }
166: return bestMatch;
167: }
168: return locFile;
169: }
170:
171: private void addDirectory(String name) {
172: addInformation = new AddInformation();
173: addInformation.setType(AddInformation.FILE_ADDED);
174: String dirName = name.replace('\\', '/');
175: /* int index = dirName.lastIndexOf('/');
176: if (index > 0) {
177: dirName = dirName.substring(index + 1, dirName.length());
178: }
179: */
180: addInformation.setFile(createFile(dirName));
181: outputDone();
182: }
183:
184: private void addFile(String name) {
185: addInformation = new AddInformation();
186: addInformation.setFile(createFile(name));
187: addInformation.setType(AddInformation.FILE_ADDED);
188: outputDone();
189: }
190:
191: private void resurrectFile(String line) {
192: int versionIndex = line.lastIndexOf(RESUR_VERSION);
193: String version = line.substring(
194: versionIndex + RESUR_VERSION.length()).trim();
195: String cutLine = line.substring(0, versionIndex).trim();
196: int fileIndex = cutLine.lastIndexOf(' ');
197: String name = cutLine.substring(fileIndex).trim();
198:
199: if (DEBUG) {
200: System.out.println("line1=" + line); //NOI18N
201: System.out.println("versionIndex=" + versionIndex); //NOI18N
202: System.out.println("version=" + version); //NOI18N
203: System.out.println("fileindex=" + fileIndex); //NOI18N
204: System.out.println("filename=" + name); //NOI18N
205: }
206:
207: addInformation = new AddInformation();
208: addInformation.setType(AddInformation.FILE_RESURRECTED);
209: addInformation.setFile(createFile(name));
210: outputDone();
211: }
212:
213: public void parseEnhancedMessage(String key, Object value) {
214: }
215: }
|