KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > jcr > importer > PropertyContext


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.jcr.importer;
18
19 import java.io.File JavaDoc;
20 import java.io.FileWriter JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.alfresco.repo.content.MimetypeMap;
28 import org.alfresco.repo.importer.view.ElementContext;
29 import org.alfresco.repo.importer.view.NodeContext;
30 import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
31 import org.alfresco.service.cmr.repository.ContentData;
32 import org.alfresco.service.cmr.view.ImporterException;
33 import org.alfresco.service.namespace.QName;
34 import org.alfresco.util.ISO9075;
35 import org.alfresco.util.TempFileProvider;
36
37
38 /**
39  * Maintains state about currently imported Property
40  *
41  * @author David Caruana
42  *
43  */

44 public class PropertyContext extends ElementContext
45 {
46     private NodeContext parentContext;
47     private QName propertyName;
48     private QName propertyType;
49     
50     private List JavaDoc<StringBuffer JavaDoc> values = new ArrayList JavaDoc<StringBuffer JavaDoc>();
51     private Map JavaDoc<QName, FileWriter JavaDoc> contentWriters = new HashMap JavaDoc<QName, FileWriter JavaDoc>();
52     
53     
54     /**
55      * Construct
56      *
57      * @param elementName
58      * @param parentContext
59      * @param propertyName
60      * @param propertyType
61      */

62     public PropertyContext(QName elementName, NodeContext parentContext, QName propertyName, QName propertyType)
63     {
64         super(elementName, parentContext.getDictionaryService(), parentContext.getImporter());
65         this.parentContext = parentContext;
66         this.propertyName = propertyName;
67         this.propertyType = propertyType;
68     }
69
70     /**
71      * Get node containing property
72      *
73      * @return node
74      */

75     public NodeContext getNode()
76     {
77         return parentContext;
78     }
79     
80     /**
81      * Get property name
82      *
83      * @return property name
84      */

85     public QName getName()
86     {
87         return propertyName;
88     }
89
90     /**
91      * Get property type
92      *
93      * @return property type
94      */

95     public QName getType()
96     {
97         return propertyType;
98     }
99     
100     /**
101      * Is property multi-valued?
102      *
103      * @return true => multi-valued; false => single value
104      */

105     public boolean isMultiValue()
106     {
107         return values.size() > 1;
108     }
109     
110     /**
111      * Is null property value
112      *
113      * @return true => value has not been provided
114      */

115     public boolean isNull()
116     {
117         return values.size() == 0;
118     }
119     
120     /**
121      * Get property values
122      *
123      * @return values
124      */

125     public List JavaDoc<StringBuffer JavaDoc> getValues()
126     {
127         return values;
128     }
129     
130     /**
131      * Start a new property value
132      */

133     public void startValue()
134     {
135         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(128);
136         if (propertyType.equals(DataTypeDefinition.CONTENT))
137         {
138             // create temporary file to hold content
139
File JavaDoc tempFile = TempFileProvider.createTempFile("import", ".tmp");
140             try
141             {
142                 FileWriter JavaDoc tempWriter = new FileWriter JavaDoc(tempFile);
143                 contentWriters.put(propertyName, tempWriter);
144                 ContentData contentData = new ContentData(tempFile.getAbsolutePath(), MimetypeMap.MIMETYPE_BINARY, 0, tempWriter.getEncoding());
145                 buffer.append(contentData.toString());
146             }
147             catch(IOException JavaDoc e)
148             {
149                 throw new ImporterException("Failed to create temporary content holder for property " + propertyName, e);
150             }
151         }
152         values.add(buffer);
153     }
154
155     /**
156      * End a property value
157      */

158     public void endValue()
159     {
160         if (propertyType.equals(DataTypeDefinition.CONTENT))
161         {
162             // close content writer
163
FileWriter JavaDoc tempWriter = contentWriters.get(propertyName);
164             try
165             {
166                 tempWriter.close();
167                 contentWriters.remove(propertyName);
168             }
169             catch(IOException JavaDoc e)
170             {
171                 throw new ImporterException("Failed to create temporary content holder for property " + propertyName, e);
172             }
173         }
174         else
175         {
176             // decode value
177
StringBuffer JavaDoc buffer = values.get(values.size() -1);
178             values.set(values.size() -1, new StringBuffer JavaDoc(ISO9075.decode(buffer.toString())));
179         }
180     }
181     
182     /**
183      * Append property value characters
184      *
185      * @param ch
186      * @param start
187      * @param length
188      */

189     public void appendCharacters(char[] ch, int start, int length)
190     {
191         if (propertyType.equals(DataTypeDefinition.CONTENT))
192         {
193             FileWriter JavaDoc tempWriter = contentWriters.get(propertyName);
194             try
195             {
196                 tempWriter.write(ch, start, length);
197             }
198             catch(IOException JavaDoc e)
199             {
200                 throw new ImporterException("Failed to write temporary content for property " + propertyName, e);
201             }
202         }
203         else
204         {
205             StringBuffer JavaDoc buffer = values.get(values.size() -1);
206             buffer.append(ch, start, length);
207         }
208     }
209     
210 }
211
Popular Tags