KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > importer > FileImportPackageHandler


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.importer;
18
19 import java.io.BufferedReader JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.io.Reader JavaDoc;
26 import java.io.UnsupportedEncodingException JavaDoc;
27
28 import org.alfresco.service.cmr.view.ImportPackageHandler;
29 import org.alfresco.service.cmr.view.ImporterException;
30
31
32 /**
33  * Handler for importing Repository content streams from file system
34  *
35  * @author David Caruana
36  */

37 public class FileImportPackageHandler
38     implements ImportPackageHandler
39 {
40     protected File JavaDoc sourceDir;
41     protected File JavaDoc dataFile;
42     protected String JavaDoc dataFileEncoding;
43
44     /**
45      * Construct
46      *
47      * @param sourceDir
48      * @param dataFile
49      * @param dataFileEncoding
50      */

51     public FileImportPackageHandler(File JavaDoc sourceDir, File JavaDoc dataFile, String JavaDoc dataFileEncoding)
52     {
53         this.sourceDir = sourceDir;
54         this.dataFile = new File JavaDoc(sourceDir, dataFile.getPath());
55         this.dataFileEncoding = dataFileEncoding;
56     }
57
58     /* (non-Javadoc)
59      * @see org.alfresco.service.cmr.view.ImportPackageHandler#startImport()
60      */

61     public void startImport()
62     {
63         log("Importing from package " + dataFile.getAbsolutePath());
64     }
65     
66     /* (non-Javadoc)
67      * @see org.alfresco.service.cmr.view.ImportPackageHandler#getDataStream()
68      */

69     public Reader JavaDoc getDataStream()
70     {
71         try
72         {
73             InputStream JavaDoc inputStream = new FileInputStream JavaDoc(dataFile);
74             Reader JavaDoc inputReader = (dataFileEncoding == null) ? new InputStreamReader JavaDoc(inputStream) : new InputStreamReader JavaDoc(inputStream, dataFileEncoding);
75             return new BufferedReader JavaDoc(inputReader);
76         }
77         catch(UnsupportedEncodingException JavaDoc e)
78         {
79             throw new ImporterException("Encoding " + dataFileEncoding + " is not supported");
80         }
81         catch(IOException JavaDoc e)
82         {
83             throw new ImporterException("Failed to read package " + dataFile.getAbsolutePath() + " due to " + e.getMessage());
84         }
85     }
86     
87     /* (non-Javadoc)
88      * @see org.alfresco.service.cmr.view.ImportStreamHandler#importStream(java.lang.String)
89      */

90     public InputStream JavaDoc importStream(String JavaDoc content)
91     {
92         File JavaDoc fileURL = new File JavaDoc(content);
93         if (fileURL.isAbsolute() == false)
94         {
95             fileURL = new File JavaDoc(sourceDir, content);
96         }
97         
98         try
99         {
100             return new FileInputStream JavaDoc(fileURL);
101         }
102         catch(IOException JavaDoc e)
103         {
104             throw new ImporterException("Failed to read content url " + content + " from file " + fileURL.getAbsolutePath());
105         }
106     }
107
108     /* (non-Javadoc)
109      * @see org.alfresco.service.cmr.view.ImportPackageHandler#endImport()
110      */

111     public void endImport()
112     {
113     }
114     
115     /**
116      * Log Import Message
117      *
118      * @param message message to log
119      */

120     protected void log(String JavaDoc message)
121     {
122     }
123     
124 }
125
126
Popular Tags