KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileInputStream JavaDoc;
21 import java.io.FileNotFoundException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.Serializable JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Stack JavaDoc;
26
27 import javax.jcr.InvalidSerializedDataException;
28
29 import org.alfresco.jcr.dictionary.JCRNamespace;
30 import org.alfresco.jcr.item.property.JCRMixinTypesProperty;
31 import org.alfresco.jcr.item.property.JCRPrimaryTypeProperty;
32 import org.alfresco.jcr.item.property.JCRUUIDProperty;
33 import org.alfresco.jcr.session.SessionImpl;
34 import org.alfresco.repo.importer.ImportContentHandler;
35 import org.alfresco.repo.importer.Importer;
36 import org.alfresco.repo.importer.view.ElementContext;
37 import org.alfresco.repo.importer.view.NodeContext;
38 import org.alfresco.repo.importer.view.ParentContext;
39 import org.alfresco.service.cmr.dictionary.AspectDefinition;
40 import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
41 import org.alfresco.service.cmr.dictionary.DictionaryService;
42 import org.alfresco.service.cmr.dictionary.InvalidTypeException;
43 import org.alfresco.service.cmr.dictionary.PropertyDefinition;
44 import org.alfresco.service.cmr.dictionary.TypeDefinition;
45 import org.alfresco.service.cmr.repository.NodeRef;
46 import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
47 import org.alfresco.service.cmr.view.ImporterException;
48 import org.alfresco.service.namespace.NamespacePrefixResolver;
49 import org.alfresco.service.namespace.QName;
50 import org.alfresco.util.Base64;
51 import org.alfresco.util.ISO9075;
52 import org.xml.sax.Attributes JavaDoc;
53 import org.xml.sax.Locator JavaDoc;
54 import org.xml.sax.SAXException JavaDoc;
55 import org.xml.sax.SAXParseException JavaDoc;
56
57
58 /**
59  * Alfresco implementation of a Doc View Import Content Handler
60  *
61  * @author David Caruana
62  */

63 public class JCRDocXMLHandler implements ImportContentHandler
64 {
65     private Importer importer;
66     private SessionImpl session;
67     private DictionaryService dictionaryService;
68     private NamespacePrefixResolver importResolver;
69     private Stack JavaDoc<ElementContext> contextStack = new Stack JavaDoc<ElementContext>();
70     
71
72     /**
73      * Construct
74      *
75      * @param session JCR Session
76      * @param importResolver Namespace Resolver for the Import
77      */

78     public JCRDocXMLHandler(SessionImpl session, NamespacePrefixResolver importResolver)
79     {
80         this.session = session;
81         this.importResolver = importResolver;
82         this.dictionaryService = session.getRepositoryImpl().getServiceRegistry().getDictionaryService();
83     }
84
85     /*
86      * (non-Javadoc)
87      * @see org.alfresco.repo.importer.ImportContentHandler#setImporter(org.alfresco.repo.importer.Importer)
88      */

89     public void setImporter(Importer importer)
90     {
91         this.importer = importer;
92     }
93
94     /*
95      * (non-Javadoc)
96      * @see org.alfresco.repo.importer.ImportContentHandler#importStream(java.lang.String)
97      */

98     public InputStream JavaDoc importStream(String JavaDoc content)
99     {
100         File JavaDoc contentFile = new File JavaDoc(content);
101         try
102         {
103             FileInputStream JavaDoc contentStream = new FileInputStream JavaDoc(contentFile);
104             return new Base64.InputStream(contentStream, Base64.DECODE | Base64.DONT_BREAK_LINES);
105         }
106         catch (FileNotFoundException JavaDoc e)
107         {
108             throw new ImporterException("Failed to retrieve import input stream on temporary content file " + content);
109         }
110     }
111
112     /*
113      * (non-Javadoc)
114      * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
115      */

116     public void setDocumentLocator(Locator JavaDoc locator)
117     {
118         // NOOP
119
}
120
121     /*
122      * (non-Javadoc)
123      * @see org.xml.sax.ContentHandler#startDocument()
124      */

125     public void startDocument() throws SAXException JavaDoc
126     {
127         // NOOP
128
}
129
130     /*
131      * (non-Javadoc)
132      * @see org.xml.sax.ContentHandler#endDocument()
133      */

134     public void endDocument() throws SAXException JavaDoc
135     {
136         // NOOP
137
}
138
139     /*
140      * (non-Javadoc)
141      * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String, java.lang.String)
142      */

143     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException JavaDoc
144     {
145         // NOOP
146
}
147
148     /*
149      * (non-Javadoc)
150      * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
151      */

152     public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc
153     {
154         // NOOP
155
}
156
157     /*
158      * (non-Javadoc)
159      * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
160      */

161     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc
162     {
163         try
164         {
165             // construct qname for element
166
QName elementName = decodeQName(QName.createQName(qName, importResolver));
167
168             // setup parent context
169
ParentContext parentContext = null;
170             if (contextStack.empty())
171             {
172                 // create root parent context
173
parentContext = new ParentContext(elementName, dictionaryService, importer);
174             }
175             else
176             {
177                 // create parent context
178
NodeContext parentNode = (NodeContext)contextStack.peek();
179                 parentContext = new ParentContext(elementName, parentNode);
180             }
181
182             // create node context
183
NodeContext node = new NodeContext(elementName, parentContext, null);
184             node.setChildName(elementName.toPrefixString(importResolver));
185             contextStack.push(node);
186             
187             // process node properties
188
for (int i = 0; i < atts.getLength(); i++)
189             {
190                 QName propName = decodeQName(QName.createQName(atts.getURI(i), atts.getLocalName(i)));
191                 String JavaDoc value = atts.getValue(i);
192                 
193                 //
194
// process "well-known" properties
195
//
196

197                 if (propName.equals(JCRPrimaryTypeProperty.PROPERTY_NAME))
198                 {
199                     // primary type
200
QName primaryTypeQName = QName.createQName(value, importResolver);
201                     TypeDefinition typeDef = dictionaryService.getType(primaryTypeQName);
202                     if (typeDef == null)
203                     {
204                         throw new InvalidTypeException(primaryTypeQName);
205                     }
206                     node.setTypeDefinition(typeDef);
207                 }
208                 else if (propName.equals(JCRMixinTypesProperty.PROPERTY_NAME))
209                 {
210                     // aspects
211
String JavaDoc[] aspects = value.split(" ");
212                     for (String JavaDoc aspect : aspects)
213                     {
214                         // ignore JCR specific aspects
215
QName aspectQName = QName.createQName(aspect, importResolver);
216                         if (!(JCRNamespace.JCR_URI.equals(aspectQName.getNamespaceURI()) ||
217                               JCRNamespace.MIX_URI.equals(aspectQName.getNamespaceURI())))
218                         {
219                             AspectDefinition aspectDef = dictionaryService.getAspect(aspectQName);
220                             if (aspectDef == null)
221                             {
222                                 throw new InvalidTypeException(aspectQName);
223                             }
224                             node.addAspect(aspectDef);
225                         }
226                     }
227                 }
228                 else if (JCRUUIDProperty.PROPERTY_NAME.equals(propName))
229                 {
230                     node.setUUID(value);
231                 }
232
233                 //
234
// Note: ignore JCR specific properties
235
//
236

237                 else if (JCRNamespace.JCR_URI.equals(propName.getNamespaceURI()))
238                 {
239                 }
240                 
241                 //
242
// process all other properties
243
//
244

245                 else
246                 {
247                     // determine type of property
248
PropertyDefinition propDef = dictionaryService.getProperty(propName);
249                     if (propDef == null)
250                     {
251                         throw new ImporterException("Property " + propName + " is not known to the repository data dictionary");
252                     }
253                     DataTypeDefinition dataTypeDef = propDef.getDataType();
254
255                     // extract values from node xml attribute
256
String JavaDoc[] propValues = null;
257                     PropertyContext propertyContext = new PropertyContext(elementName, node, propName, dataTypeDef.getName());
258                     if (dataTypeDef.getName().equals(DataTypeDefinition.CONTENT))
259                     {
260                         // Note: we only support single valued content properties
261
propValues = new String JavaDoc[] { value };
262                     }
263                     else
264                     {
265                         // attempt to split multi-value properties
266
propValues = value.split(" ");
267                     }
268                     
269                     // extract values appropriately
270
for (String JavaDoc propValue : propValues)
271                     {
272                         propertyContext.startValue();
273                         propertyContext.appendCharacters(propValue.toCharArray(), 0, propValue.length());
274                         propertyContext.endValue();
275                     }
276                     
277                     // add each value to the node
278
if (propertyContext.isMultiValue())
279                     {
280                         node.addPropertyCollection(propName);
281                     }
282                     List JavaDoc<StringBuffer JavaDoc> nodeValues = propertyContext.getValues();
283                     for (StringBuffer JavaDoc nodeValue : nodeValues)
284                     {
285                         // first, cast value to appropriate type (using JCR converters)
286
Serializable JavaDoc objVal = (Serializable JavaDoc)session.getTypeConverter().convert(dataTypeDef, nodeValue.toString());
287                         String JavaDoc strValue = DefaultTypeConverter.INSTANCE.convert(String JavaDoc.class, objVal);
288                         node.addProperty(propName, strValue);
289                     }
290                 }
291             }
292
293             // import node
294
NodeRef nodeRef = node.getImporter().importNode(node);
295             node.setNodeRef(nodeRef);
296         }
297         catch(Exception JavaDoc e)
298         {
299             throw new SAXException JavaDoc("Failed to process element " + qName, e);
300         }
301     }
302     
303     /*
304      * (non-Javadoc)
305      * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
306      */

307     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc
308     {
309         try
310         {
311             // ensure context matches parse
312
ElementContext context = (ElementContext)contextStack.pop();
313             QName elementName = QName.createQName(qName, importResolver);
314             if (!context.getElementName().equals(elementName))
315             {
316                 throw new InvalidSerializedDataException("Expected element " + context.getElementName() + " but was " + elementName);
317             }
318
319             // signal end of node
320
NodeContext nodeContext = (NodeContext)context;
321             nodeContext.getImporter().childrenImported(nodeContext.getNodeRef());
322         }
323         catch(Exception JavaDoc e)
324         {
325             throw new SAXException JavaDoc("Failed to process element " + qName, e);
326         }
327     }
328
329     /*
330      * (non-Javadoc)
331      * @see org.xml.sax.ContentHandler#characters(char[], int, int)
332      */

333     public void characters(char[] ch, int start, int length) throws SAXException JavaDoc
334     {
335     }
336
337     /*
338      * (non-Javadoc)
339      * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
340      */

341     public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException JavaDoc
342     {
343     }
344
345     /*
346      * (non-Javadoc)
347      * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String, java.lang.String)
348      */

349     public void processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException JavaDoc
350     {
351     }
352
353     /*
354      * (non-Javadoc)
355      * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
356      */

357     public void skippedEntity(String JavaDoc name) throws SAXException JavaDoc
358     {
359     }
360
361     /*
362      * (non-Javadoc)
363      * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
364      */

365     public void warning(SAXParseException JavaDoc exception) throws SAXException JavaDoc
366     {
367     }
368
369     /*
370      * (non-Javadoc)
371      * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
372      */

373     public void error(SAXParseException JavaDoc exception) throws SAXException JavaDoc
374     {
375     }
376
377     /*
378      * (non-Javadoc)
379      * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
380      */

381     public void fatalError(SAXParseException JavaDoc exception) throws SAXException JavaDoc
382     {
383     }
384
385     /**
386      * Decode QName
387      *
388      * @param name name to decode
389      * @return the decoded name
390      */

391     private QName decodeQName(QName name)
392     {
393         return QName.createQName(name.getNamespaceURI(), ISO9075.decode(name.getLocalName()));
394     }
395
396 }
397
Popular Tags