KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > jcr > exporter > JCRSystemXMLExporter


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.exporter;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23
24 import javax.jcr.PropertyType;
25 import javax.jcr.RepositoryException;
26 import javax.jcr.Value;
27
28 import org.alfresco.jcr.dictionary.JCRNamespace;
29 import org.alfresco.jcr.dictionary.PropertyDefinitionImpl;
30 import org.alfresco.jcr.item.NodeImpl;
31 import org.alfresco.jcr.item.PropertyImpl;
32 import org.alfresco.jcr.item.property.JCRMixinTypesProperty;
33 import org.alfresco.jcr.item.property.JCRPrimaryTypeProperty;
34 import org.alfresco.jcr.item.property.JCRUUIDProperty;
35 import org.alfresco.jcr.session.SessionImpl;
36 import org.alfresco.service.cmr.dictionary.DictionaryService;
37 import org.alfresco.service.cmr.dictionary.PropertyDefinition;
38 import org.alfresco.service.cmr.repository.ContentData;
39 import org.alfresco.service.cmr.repository.NodeRef;
40 import org.alfresco.service.cmr.repository.NodeService;
41 import org.alfresco.service.cmr.repository.Path;
42 import org.alfresco.service.cmr.security.AccessPermission;
43 import org.alfresco.service.cmr.view.Exporter;
44 import org.alfresco.service.cmr.view.ExporterContext;
45 import org.alfresco.service.cmr.view.ExporterException;
46 import org.alfresco.service.namespace.QName;
47 import org.alfresco.util.Base64;
48 import org.xml.sax.ContentHandler JavaDoc;
49 import org.xml.sax.SAXException JavaDoc;
50 import org.xml.sax.helpers.AttributesImpl JavaDoc;
51
52
53 /**
54  * Alfresco Implementation of JCR System XML Exporter
55  *
56  * @author David Caruana
57  */

58 public class JCRSystemXMLExporter implements Exporter
59 {
60     public final static String JavaDoc NODE_LOCALNAME = "node";
61     public final static String JavaDoc NAME_LOCALNAME = "name";
62     public final static String JavaDoc PROPERTY_LOCALNAME = "property";
63     public final static String JavaDoc TYPE_LOCALNAME = "type";
64     public final static String JavaDoc VALUE_LOCALNAME = "value";
65     
66     public final static QName NODE_QNAME = QName.createQName(JCRNamespace.SV_URI, NODE_LOCALNAME);
67     public final static QName NAME_QNAME = QName.createQName(JCRNamespace.SV_URI, NAME_LOCALNAME);
68     public final static QName PROPERTY_QNAME = QName.createQName(JCRNamespace.SV_URI, PROPERTY_LOCALNAME);
69     public final static QName TYPE_QNAME = QName.createQName(JCRNamespace.SV_URI, TYPE_LOCALNAME);
70     public final static QName VALUE_QNAME = QName.createQName(JCRNamespace.SV_URI, VALUE_LOCALNAME);
71     
72     private SessionImpl session;
73     private ContentHandler JavaDoc contentHandler;
74     
75     private final static AttributesImpl JavaDoc EMPTY_ATTRIBUTES = new AttributesImpl JavaDoc();
76     
77
78     /**
79      * Construct
80      *
81      * @param namespaceService namespace service
82      * @param nodeService node service
83      * @param contentHandler content handler
84      */

85     public JCRSystemXMLExporter(SessionImpl session, ContentHandler JavaDoc contentHandler)
86     {
87         this.session = session;
88         this.contentHandler = contentHandler;
89     }
90     
91     
92     /* (non-Javadoc)
93      * @see org.alfresco.service.cmr.view.Exporter#start()
94      */

95     public void start(ExporterContext exportNodeRef)
96     {
97         try
98         {
99             contentHandler.startDocument();
100         }
101         catch (SAXException JavaDoc e)
102         {
103             throw new ExporterException("Failed to process export start event", e);
104         }
105     }
106
107     /* (non-Javadoc)
108      * @see org.alfresco.service.cmr.view.Exporter#startNamespace(java.lang.String, java.lang.String)
109      */

110     public void startNamespace(String JavaDoc prefix, String JavaDoc uri)
111     {
112         try
113         {
114             contentHandler.startPrefixMapping(prefix, uri);
115         }
116         catch (SAXException JavaDoc e)
117         {
118             throw new ExporterException("Failed to process start namespace event - prefix " + prefix + " uri " + uri, e);
119         }
120     }
121
122     /* (non-Javadoc)
123      * @see org.alfresco.service.cmr.view.Exporter#endNamespace(java.lang.String)
124      */

125     public void endNamespace(String JavaDoc prefix)
126     {
127         try
128         {
129             contentHandler.endPrefixMapping(prefix);
130         }
131         catch (SAXException JavaDoc e)
132         {
133             throw new ExporterException("Failed to process end namespace event - prefix " + prefix, e);
134         }
135     }
136
137     /* (non-Javadoc)
138      * @see org.alfresco.service.cmr.view.Exporter#startNode(org.alfresco.service.cmr.repository.NodeRef)
139      */

140     public void startNode(NodeRef nodeRef)
141     {
142         try
143         {
144             // establish name of node
145
String JavaDoc childName;
146             NodeService nodeService = session.getRepositoryImpl().getServiceRegistry().getNodeService();
147             NodeRef rootNode = nodeService.getRootNode(nodeRef.getStoreRef());
148             if (rootNode.equals(nodeRef))
149             {
150                 childName = "jcr:root";
151             }
152             else
153             {
154                 Path path = nodeService.getPath(nodeRef);
155                 childName = path.last().getElementString();
156             }
157             QName childQName = QName.createQName(childName);
158
159             // create jcr node attributes
160
AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
161             attrs.addAttribute(NAME_QNAME.getNamespaceURI(), NAME_LOCALNAME, toPrefixString(NAME_QNAME), null, toPrefixString(childQName));
162             
163             // emit node element
164
contentHandler.startElement(NODE_QNAME.getNamespaceURI(), NODE_LOCALNAME, toPrefixString(NODE_QNAME), attrs);
165
166             //
167
// emit jcr specifics
168
//
169
NodeImpl nodeImpl = new NodeImpl(session, nodeRef);
170             
171             // primary type
172
PropertyImpl primaryType = new JCRPrimaryTypeProperty(nodeImpl);
173             startProperty(nodeRef, JCRPrimaryTypeProperty.PROPERTY_NAME);
174             value(nodeRef, JCRPrimaryTypeProperty.PROPERTY_NAME, primaryType.getValue().getString());
175             endProperty(nodeRef, JCRPrimaryTypeProperty.PROPERTY_NAME);
176             
177             // mixin type
178
PropertyImpl mixinTypes = new JCRMixinTypesProperty(nodeImpl);
179             startProperty(nodeRef, JCRMixinTypesProperty.PROPERTY_NAME);
180             Collection JavaDoc<String JavaDoc> mixins = new ArrayList JavaDoc<String JavaDoc>();
181             for (Value value : mixinTypes.getValues())
182             {
183                 mixins.add(value.getString());
184             }
185             value(nodeRef, JCRMixinTypesProperty.PROPERTY_NAME, mixins);
186             endProperty(nodeRef, JCRMixinTypesProperty.PROPERTY_NAME);
187             
188             // uuid (for mix:referencable)
189
startProperty(nodeRef, JCRUUIDProperty.PROPERTY_NAME);
190             value(nodeRef, JCRUUIDProperty.PROPERTY_NAME, nodeRef.getId());
191             endProperty(nodeRef, JCRUUIDProperty.PROPERTY_NAME);
192         }
193         catch (SAXException JavaDoc e)
194         {
195             throw new ExporterException("Failed to process start node event - node ref " + nodeRef.toString(), e);
196         }
197         catch (RepositoryException e)
198         {
199             throw new ExporterException("Failed to process start node event - node ref " + nodeRef.toString(), e);
200         }
201     }
202     
203     /* (non-Javadoc)
204      * @see org.alfresco.service.cmr.view.Exporter#endNode(org.alfresco.service.cmr.repository.NodeRef)
205      */

206     public void endNode(NodeRef nodeRef)
207     {
208         try
209         {
210             contentHandler.endElement(NODE_QNAME.getNamespaceURI(), NODE_LOCALNAME, toPrefixString(NODE_QNAME));
211         }
212         catch (SAXException JavaDoc e)
213         {
214             throw new ExporterException("Failed to process end node event - node ref " + nodeRef.toString(), e);
215         }
216     }
217
218     /* (non-Javadoc)
219      * @see org.alfresco.service.cmr.view.Exporter#startAspects(org.alfresco.service.cmr.repository.NodeRef)
220      */

221     public void startAspects(NodeRef nodeRef)
222     {
223     }
224
225     /* (non-Javadoc)
226      * @see org.alfresco.service.cmr.view.Exporter#endAspects(org.alfresco.service.cmr.repository.NodeRef)
227      */

228     public void endAspects(NodeRef nodeRef)
229     {
230     }
231     
232     /* (non-Javadoc)
233      * @see org.alfresco.service.cmr.view.Exporter#startAspect(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName)
234      */

235     public void startAspect(NodeRef nodeRef, QName aspect)
236     {
237     }
238
239     /* (non-Javadoc)
240      * @see org.alfresco.service.cmr.view.Exporter#endAspect(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName)
241      */

242     public void endAspect(NodeRef nodeRef, QName aspect)
243     {
244     }
245
246     /* (non-Javadoc)
247      * @see org.alfresco.service.cmr.view.Exporter#startACL(org.alfresco.service.cmr.repository.NodeRef)
248      */

249     public void startACL(NodeRef nodeRef)
250     {
251     }
252
253     /* (non-Javadoc)
254      * @see org.alfresco.service.cmr.view.Exporter#permission(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.cmr.security.AccessPermission)
255      */

256     public void permission(NodeRef nodeRef, AccessPermission permission)
257     {
258     }
259
260     /* (non-Javadoc)
261      * @see org.alfresco.service.cmr.view.Exporter#endACL(org.alfresco.service.cmr.repository.NodeRef)
262      */

263     public void endACL(NodeRef nodeRef)
264     {
265     }
266     
267     /* (non-Javadoc)
268      * @see org.alfresco.service.cmr.view.Exporter#startProperties(org.alfresco.service.cmr.repository.NodeRef)
269      */

270     public void startProperties(NodeRef nodeRef)
271     {
272     }
273
274     /* (non-Javadoc)
275      * @see org.alfresco.service.cmr.view.Exporter#endProperties(org.alfresco.service.cmr.repository.NodeRef)
276      */

277     public void endProperties(NodeRef nodeRef)
278     {
279     }
280
281     /* (non-Javadoc)
282      * @see org.alfresco.service.cmr.view.Exporter#startProperty(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName)
283      */

284     public void startProperty(NodeRef nodeRef, QName property)
285     {
286         try
287         {
288             // create jcr node attributes
289
DictionaryService dictionaryService = session.getRepositoryImpl().getServiceRegistry().getDictionaryService();
290             PropertyDefinition propDef = dictionaryService.getProperty(property);
291             PropertyDefinitionImpl propDefImpl = new PropertyDefinitionImpl(session.getTypeManager(), propDef);
292             String JavaDoc datatype = PropertyType.nameFromValue(propDefImpl.getRequiredType());
293             AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
294             attrs.addAttribute(NAME_QNAME.getNamespaceURI(), NAME_LOCALNAME, toPrefixString(NAME_QNAME), null, toPrefixString(property));
295             attrs.addAttribute(TYPE_QNAME.getNamespaceURI(), TYPE_LOCALNAME, toPrefixString(TYPE_QNAME), null, datatype);
296             
297             // emit property element
298
contentHandler.startElement(PROPERTY_QNAME.getNamespaceURI(), PROPERTY_LOCALNAME, toPrefixString(PROPERTY_QNAME), attrs);
299         }
300         catch (SAXException JavaDoc e)
301         {
302             throw new ExporterException("Failed to process start property event - nodeRef " + nodeRef + "; property " + toPrefixString(property), e);
303         }
304     }
305
306     /* (non-Javadoc)
307      * @see org.alfresco.service.cmr.view.Exporter#endProperty(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName)
308      */

309     public void endProperty(NodeRef nodeRef, QName property)
310     {
311         try
312         {
313             // emit property element
314
contentHandler.endElement(PROPERTY_QNAME.getNamespaceURI(), PROPERTY_LOCALNAME, toPrefixString(PROPERTY_QNAME));
315         }
316         catch (SAXException JavaDoc e)
317         {
318             throw new ExporterException("Failed to process end property event - nodeRef " + nodeRef + "; property " + toPrefixString(property), e);
319         }
320     }
321
322     /* (non-Javadoc)
323      * @see org.alfresco.service.cmr.view.Exporter#value(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName, java.io.Serializable)
324      */

325     public void value(NodeRef nodeRef, QName property, Object JavaDoc value)
326     {
327         try
328         {
329             // emit value element
330
contentHandler.startElement(VALUE_QNAME.getNamespaceURI(), VALUE_LOCALNAME, toPrefixString(VALUE_QNAME), EMPTY_ATTRIBUTES);
331             String JavaDoc strValue = session.getTypeConverter().convert(String JavaDoc.class, value);
332             contentHandler.characters(strValue.toCharArray(), 0, strValue.length());
333             contentHandler.endElement(VALUE_QNAME.getNamespaceURI(), VALUE_LOCALNAME, toPrefixString(VALUE_QNAME));
334         }
335         catch (RepositoryException e)
336         {
337             throw new ExporterException("Failed to process value event - nodeRef " + nodeRef + "; property " + toPrefixString(property) + "; value " + value, e);
338         }
339         catch (SAXException JavaDoc e)
340         {
341             throw new ExporterException("Failed to process value event - nodeRef " + nodeRef + "; property " + toPrefixString(property) + "; value " + value, e);
342         }
343     }
344
345     /* (non-Javadoc)
346      * @see org.alfresco.service.cmr.view.Exporter#value(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName, java.util.Collection)
347      */

348     public void value(NodeRef nodeRef, QName property, Collection JavaDoc values)
349     {
350         try
351         {
352             // convert using JCR type converter
353
Collection JavaDoc<String JavaDoc> strValues = session.getTypeConverter().getConverter().convert(String JavaDoc.class, values);
354
355             for (String JavaDoc strValue : strValues)
356             {
357                 // emit value element
358
contentHandler.startElement(VALUE_QNAME.getNamespaceURI(), VALUE_LOCALNAME, toPrefixString(VALUE_QNAME), EMPTY_ATTRIBUTES);
359                 contentHandler.characters(strValue.toCharArray(), 0, strValue.length());
360                 contentHandler.endElement(VALUE_QNAME.getNamespaceURI(), VALUE_LOCALNAME, toPrefixString(VALUE_QNAME));
361             }
362         }
363         catch (SAXException JavaDoc e)
364         {
365             throw new ExporterException("Failed to process value event - nodeRef " + nodeRef + "; property " + toPrefixString(property) + "; value " + values, e);
366         }
367     }
368     
369     /* (non-Javadoc)
370      * @see org.alfresco.service.cmr.view.Exporter#content(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName, java.io.InputStream)
371      */

372     public void content(NodeRef nodeRef, QName property, InputStream JavaDoc content, ContentData contentData)
373     {
374         try
375         {
376             contentHandler.startElement(VALUE_QNAME.getNamespaceURI(), VALUE_LOCALNAME, toPrefixString(VALUE_QNAME), EMPTY_ATTRIBUTES);
377             
378             if (content != null)
379             {
380                 // emit base64 encoded content
381
InputStream JavaDoc base64content = new Base64.InputStream(content, Base64.ENCODE | Base64.DONT_BREAK_LINES);
382                 byte[] buffer = new byte[9 * 1024];
383                 int read;
384                 while ((read = base64content.read(buffer, 0, buffer.length)) > 0)
385                 {
386                     String JavaDoc characters = new String JavaDoc(buffer, 0, read);
387                     contentHandler.characters(characters.toCharArray(), 0, characters.length());
388                 }
389             }
390             
391             contentHandler.endElement(VALUE_QNAME.getNamespaceURI(), VALUE_LOCALNAME, toPrefixString(VALUE_QNAME));
392         }
393         catch (SAXException JavaDoc e)
394         {
395             throw new ExporterException("Failed to process content event - nodeRef " + nodeRef + "; property " + toPrefixString(property));
396         }
397         catch (IOException JavaDoc e)
398         {
399             throw new ExporterException("Failed to process content event - nodeRef " + nodeRef + "; property " + toPrefixString(property));
400         }
401     }
402
403     /* (non-Javadoc)
404      * @see org.alfresco.service.cmr.view.Exporter#startAssoc(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName)
405      */

406     public void startAssoc(NodeRef nodeRef, QName assoc)
407     {
408     }
409
410     /* (non-Javadoc)
411      * @see org.alfresco.service.cmr.view.Exporter#endAssoc(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName)
412      */

413     public void endAssoc(NodeRef nodeRef, QName assoc)
414     {
415     }
416
417     /* (non-Javadoc)
418      * @see org.alfresco.service.cmr.view.Exporter#startAssocs(org.alfresco.service.cmr.repository.NodeRef)
419      */

420     public void startAssocs(NodeRef nodeRef)
421     {
422     }
423
424     /* (non-Javadoc)
425      * @see org.alfresco.service.cmr.view.Exporter#endAssocs(org.alfresco.service.cmr.repository.NodeRef)
426      */

427     public void endAssocs(NodeRef nodeRef)
428     {
429     }
430
431     /* (non-Javadoc)
432      * @see org.alfresco.service.cmr.view.Exporter#startReference(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName)
433      */

434     public void startReference(NodeRef nodeRef, QName childName)
435     {
436     }
437
438     /* (non-Javadoc)
439      * @see org.alfresco.service.cmr.view.Exporter#endReference(org.alfresco.service.cmr.repository.NodeRef)
440      */

441     public void endReference(NodeRef nodeRef)
442     {
443     }
444     
445     /* (non-Javadoc)
446      * @see org.alfresco.service.cmr.view.Exporter#warning(java.lang.String)
447      */

448     public void warning(String JavaDoc warning)
449     {
450     }
451     
452     /* (non-Javadoc)
453      * @see org.alfresco.service.cmr.view.Exporter#end()
454      */

455     public void end()
456     {
457         try
458         {
459             contentHandler.endDocument();
460         }
461         catch (SAXException JavaDoc e)
462         {
463             throw new ExporterException("Failed to process end export event", e);
464         }
465     }
466
467     /**
468      * Get the prefix for the specified URI
469      * @param uri the URI
470      * @return the prefix (or null, if one is not registered)
471      */

472     private String JavaDoc toPrefixString(QName qname)
473     {
474         return qname.toPrefixString(session.getNamespaceResolver());
475     }
476
477 }
478
Popular Tags