KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > core > client > TemplateHandler


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.core.client;
12
13 import java.io.*;
14
15 import org.eclipse.core.resources.IContainer;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.team.internal.ccvs.core.*;
19 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
20 import org.eclipse.team.internal.ccvs.core.util.SyncFileWriter;
21
22 /**
23  * @author Administrator
24  *
25  * To change this generated comment edit the template variable "typecomment":
26  * Window>Preferences>Java>Templates.
27  * To enable and disable the creation of type comments go to
28  * Window>Preferences>Java>Code Generation.
29  */

30 public class TemplateHandler extends ResponseHandler {
31
32     /**
33      * @see org.eclipse.team.internal.ccvs.core.client.ResponseHandler#getResponseID()
34      */

35     public String JavaDoc getResponseID() {
36         return "Template"; //$NON-NLS-1$
37
}
38
39     /**
40      * @see org.eclipse.team.internal.ccvs.core.client.ResponseHandler#handle(org.eclipse.team.internal.ccvs.core.client.Session, java.lang.String, org.eclipse.core.runtime.IProgressMonitor)
41      */

42     public void handle(Session session, String JavaDoc localDir, IProgressMonitor monitor) throws CVSException {
43         session.readLine(); /* read the remote dir which is not needed */
44         // Only read the template file if the container exists.
45
// This is OK as we only use the template from the project folder which must exist
46
ICVSFolder localFolder = session.getLocalRoot().getFolder(localDir);
47         IContainer container = (IContainer)localFolder.getIResource();
48         ICVSStorage templateFile = null;
49         if (container != null && container.exists()) {
50             try {
51                 templateFile = CVSWorkspaceRoot.getCVSFileFor(SyncFileWriter.getTemplateFile(container));
52             } catch (CVSException e) {
53                 // Log the inability to create the template file
54
CVSProviderPlugin.log(new CVSStatus(IStatus.ERROR, CVSStatus.ERROR, "Could not write template file in " + container.getFullPath() + ": " + e.getMessage(), e, session.getLocalRoot())); //$NON-NLS-1$ //$NON-NLS-2$
55
}
56         }
57         if (container == null || templateFile == null) {
58             // Create a dummy storage handle to recieve the contents from the server
59
templateFile = new ICVSStorage() {
60                 public String JavaDoc getName() {
61                     return "Template"; //$NON-NLS-1$
62
}
63                 public void setContents(
64                     InputStream stream,
65                     int responseType,
66                     boolean keepLocalHistory,
67                     IProgressMonitor monitor)
68                     throws CVSException {
69
70                     try {
71                         // Transfer the contents
72
OutputStream out = new ByteArrayOutputStream();
73                         try {
74                             byte[] buffer = new byte[1024];
75                             int read;
76                             while ((read = stream.read(buffer)) >= 0) {
77                                 Policy.checkCanceled(monitor);
78                                 out.write(buffer, 0, read);
79                             }
80                         } finally {
81                             out.close();
82                         }
83                     } catch (IOException e) {
84                         throw CVSException.wrapException(e);
85                     } finally {
86                         try {
87                             stream.close();
88                         } catch (IOException e1) {
89                             // Ignore close errors
90
}
91                     }
92                 }
93                 public long getSize() {
94                     return 0;
95                 }
96                 public InputStream getContents() throws CVSException {
97                     return new ByteArrayInputStream(new byte[0]);
98                 }
99             };
100         }
101         try {
102             session.receiveFile(templateFile, false, UpdatedHandler.HANDLE_UPDATED, monitor);
103         } catch (CVSException e) {
104             if (!(templateFile instanceof ICVSFile && handleInvalidResourceName(session, (ICVSFile)templateFile, e))) {
105                 throw e;
106             }
107         }
108     }
109
110 }
111
Popular Tags