KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > exporter > ViewXMLExporter


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.exporter;
18
19 import java.io.InputStream JavaDoc;
20 import java.util.Collection JavaDoc;
21
22 import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
23 import org.alfresco.service.cmr.dictionary.DictionaryService;
24 import org.alfresco.service.cmr.dictionary.PropertyDefinition;
25 import org.alfresco.service.cmr.repository.ChildAssociationRef;
26 import org.alfresco.service.cmr.repository.ContentData;
27 import org.alfresco.service.cmr.repository.NodeRef;
28 import org.alfresco.service.cmr.repository.NodeService;
29 import org.alfresco.service.cmr.repository.Path;
30 import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
31 import org.alfresco.service.cmr.security.AccessPermission;
32 import org.alfresco.service.cmr.security.PermissionService;
33 import org.alfresco.service.cmr.view.Exporter;
34 import org.alfresco.service.cmr.view.ExporterContext;
35 import org.alfresco.service.cmr.view.ExporterException;
36 import org.alfresco.service.namespace.NamespaceService;
37 import org.alfresco.service.namespace.QName;
38 import org.xml.sax.ContentHandler JavaDoc;
39 import org.xml.sax.SAXException JavaDoc;
40 import org.xml.sax.helpers.AttributesImpl JavaDoc;
41
42
43 /**
44  * Exporter that exports Repository information to XML (Alfresco Repository View Schema)
45  *
46  * @author David Caruana
47  */

48 /*package*/ class ViewXMLExporter
49     implements Exporter
50 {
51     // Repository View Schema Definitions
52
private static final String JavaDoc VIEW_LOCALNAME = "view";
53     private static final String JavaDoc VALUES_LOCALNAME = "values";
54     private static final String JavaDoc VALUE_LOCALNAME = "value";
55     private static final String JavaDoc CHILDNAME_LOCALNAME = "childName";
56     private static final String JavaDoc ASPECTS_LOCALNAME = "aspects";
57     private static final String JavaDoc PROPERTIES_LOCALNAME = "properties";
58     private static final String JavaDoc ASSOCIATIONS_LOCALNAME = "associations";
59     private static final String JavaDoc DATATYPE_LOCALNAME = "datatype";
60     private static final String JavaDoc ISNULL_LOCALNAME = "isNull";
61     private static final String JavaDoc METADATA_LOCALNAME = "metadata";
62     private static final String JavaDoc EXPORTEDBY_LOCALNAME = "exportBy";
63     private static final String JavaDoc EXPORTEDDATE_LOCALNAME = "exportDate";
64     private static final String JavaDoc EXPORTERVERSION_LOCALNAME = "exporterVersion";
65     private static final String JavaDoc EXPORTOF_LOCALNAME = "exportOf";
66     private static final String JavaDoc ACL_LOCALNAME = "acl";
67     private static final String JavaDoc ACE_LOCALNAME = "ace";
68     private static final String JavaDoc ACCESS_LOCALNAME = "access";
69     private static final String JavaDoc AUTHORITY_LOCALNAME = "authority";
70     private static final String JavaDoc PERMISSION_LOCALNAME = "permission";
71     private static final String JavaDoc INHERITPERMISSIONS_LOCALNAME = "inherit";
72     private static final String JavaDoc REFERENCE_LOCALNAME = "reference";
73     private static final String JavaDoc PATHREF_LOCALNAME = "pathref";
74     private static QName VIEW_QNAME;
75     private static QName VALUES_QNAME;
76     private static QName VALUE_QNAME;
77     private static QName PROPERTIES_QNAME;
78     private static QName ASPECTS_QNAME;
79     private static QName ASSOCIATIONS_QNAME;
80     private static QName CHILDNAME_QNAME;
81     private static QName DATATYPE_QNAME;
82     private static QName ISNULL_QNAME;
83     private static QName METADATA_QNAME;
84     private static QName EXPORTEDBY_QNAME;
85     private static QName EXPORTEDDATE_QNAME;
86     private static QName EXPORTERVERSION_QNAME;
87     private static QName EXPORTOF_QNAME;
88     private static QName ACL_QNAME;
89     private static QName ACE_QNAME;
90     private static QName ACCESS_QNAME;
91     private static QName AUTHORITY_QNAME;
92     private static QName PERMISSION_QNAME;
93     private static QName INHERITPERMISSIONS_QNAME;
94     private static QName REFERENCE_QNAME;
95     private static QName PATHREF_QNAME;
96     private static final AttributesImpl JavaDoc EMPTY_ATTRIBUTES = new AttributesImpl JavaDoc();
97     
98     // Service dependencies
99
private NamespaceService namespaceService;
100     private NodeService nodeService;
101     private DictionaryService dictionaryService;
102     private PermissionService permissionService;
103     
104     // View context
105
private ContentHandler JavaDoc contentHandler;
106     private ExporterContext context;
107     
108
109     /**
110      * Construct
111      *
112      * @param namespaceService namespace service
113      * @param nodeService node service
114      * @param contentHandler content handler
115      */

116     ViewXMLExporter(NamespaceService namespaceService, NodeService nodeService,
117             DictionaryService dictionaryService, PermissionService permissionService, ContentHandler JavaDoc contentHandler)
118     {
119         this.namespaceService = namespaceService;
120         this.nodeService = nodeService;
121         this.dictionaryService = dictionaryService;
122         this.permissionService = permissionService;
123         this.contentHandler = contentHandler;
124         
125         VIEW_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, VIEW_LOCALNAME, namespaceService);
126         VALUE_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, VALUE_LOCALNAME, namespaceService);
127         VALUES_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, VALUES_LOCALNAME, namespaceService);
128         CHILDNAME_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, CHILDNAME_LOCALNAME, namespaceService);
129         ASPECTS_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, ASPECTS_LOCALNAME, namespaceService);
130         PROPERTIES_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, PROPERTIES_LOCALNAME, namespaceService);
131         ASSOCIATIONS_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, ASSOCIATIONS_LOCALNAME, namespaceService);
132         DATATYPE_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, DATATYPE_LOCALNAME, namespaceService);
133         ISNULL_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, ISNULL_LOCALNAME, namespaceService);
134         METADATA_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, METADATA_LOCALNAME, namespaceService);
135         EXPORTEDBY_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, EXPORTEDBY_LOCALNAME, namespaceService);
136         EXPORTEDDATE_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, EXPORTEDDATE_LOCALNAME, namespaceService);
137         EXPORTERVERSION_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, EXPORTERVERSION_LOCALNAME, namespaceService);
138         EXPORTOF_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, EXPORTOF_LOCALNAME, namespaceService);
139         ACL_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, ACL_LOCALNAME, namespaceService);
140         ACE_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, ACE_LOCALNAME, namespaceService);
141         ACCESS_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, ACCESS_LOCALNAME, namespaceService);
142         AUTHORITY_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, AUTHORITY_LOCALNAME, namespaceService);
143         PERMISSION_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, PERMISSION_LOCALNAME, namespaceService);
144         INHERITPERMISSIONS_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, INHERITPERMISSIONS_LOCALNAME, namespaceService);
145         REFERENCE_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, REFERENCE_LOCALNAME, namespaceService);
146         PATHREF_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, PATHREF_LOCALNAME, namespaceService);
147     }
148     
149     
150     /* (non-Javadoc)
151      * @see org.alfresco.service.cmr.view.Exporter#start()
152      */

153     public void start(ExporterContext context)
154     {
155         try
156         {
157             this.context = context;
158             contentHandler.startDocument();
159             contentHandler.startPrefixMapping(NamespaceService.REPOSITORY_VIEW_PREFIX, NamespaceService.REPOSITORY_VIEW_1_0_URI);
160             contentHandler.startElement(NamespaceService.REPOSITORY_VIEW_PREFIX, VIEW_LOCALNAME, VIEW_QNAME.toPrefixString(), EMPTY_ATTRIBUTES);
161
162             //
163
// output metadata
164
//
165
contentHandler.startElement(NamespaceService.REPOSITORY_VIEW_PREFIX, METADATA_LOCALNAME, METADATA_QNAME.toPrefixString(), EMPTY_ATTRIBUTES);
166
167             // exported by
168
contentHandler.startElement(NamespaceService.REPOSITORY_VIEW_PREFIX, EXPORTEDBY_LOCALNAME, EXPORTEDBY_QNAME.toPrefixString(), EMPTY_ATTRIBUTES);
169             contentHandler.characters(context.getExportedBy().toCharArray(), 0, context.getExportedBy().length());
170             contentHandler.endElement(NamespaceService.REPOSITORY_VIEW_PREFIX, EXPORTEDBY_LOCALNAME, EXPORTEDBY_QNAME.toPrefixString());
171
172             // exported date
173
contentHandler.startElement(NamespaceService.REPOSITORY_VIEW_PREFIX, EXPORTEDDATE_LOCALNAME, EXPORTEDDATE_QNAME.toPrefixString(), EMPTY_ATTRIBUTES);
174             String JavaDoc date = DefaultTypeConverter.INSTANCE.convert(String JavaDoc.class, context.getExportedDate());
175             contentHandler.characters(date.toCharArray(), 0, date.length());
176             contentHandler.endElement(NamespaceService.REPOSITORY_VIEW_PREFIX, EXPORTEDDATE_LOCALNAME, EXPORTEDDATE_QNAME.toPrefixString());
177             
178             // exporter version
179
contentHandler.startElement(NamespaceService.REPOSITORY_VIEW_PREFIX, EXPORTERVERSION_LOCALNAME, EXPORTERVERSION_QNAME.toPrefixString(), EMPTY_ATTRIBUTES);
180             contentHandler.characters(context.getExporterVersion().toCharArray(), 0, context.getExporterVersion().length());
181             contentHandler.endElement(NamespaceService.REPOSITORY_VIEW_PREFIX, EXPORTERVERSION_LOCALNAME, EXPORTERVERSION_QNAME.toPrefixString());
182
183             // export of
184
contentHandler.startElement(NamespaceService.REPOSITORY_VIEW_PREFIX, EXPORTOF_LOCALNAME, EXPORTOF_QNAME.toPrefixString(), EMPTY_ATTRIBUTES);
185             String JavaDoc path = nodeService.getPath(context.getExportOf()).toPrefixString(namespaceService);
186             contentHandler.characters(path.toCharArray(), 0, path.length());
187             contentHandler.endElement(NamespaceService.REPOSITORY_VIEW_PREFIX, EXPORTOF_LOCALNAME, EXPORTOF_QNAME.toPrefixString());
188             
189             contentHandler.endElement(NamespaceService.REPOSITORY_VIEW_PREFIX, METADATA_LOCALNAME, METADATA_QNAME.toPrefixString());
190         }
191         catch (SAXException JavaDoc e)
192         {
193             throw new ExporterException("Failed to process export start event", e);
194         }
195     }
196
197     /* (non-Javadoc)
198      * @see org.alfresco.service.cmr.view.Exporter#startNamespace(java.lang.String, java.lang.String)
199      */

200     public void startNamespace(String JavaDoc prefix, String JavaDoc uri)
201     {
202         try
203         {
204             contentHandler.startPrefixMapping(prefix, uri);
205         }
206         catch (SAXException JavaDoc e)
207         {
208             throw new ExporterException("Failed to process start namespace event - prefix " + prefix + " uri " + uri, e);
209         }
210     }
211
212     /* (non-Javadoc)
213      * @see org.alfresco.service.cmr.view.Exporter#endNamespace(java.lang.String)
214      */

215     public void endNamespace(String JavaDoc prefix)
216     {
217         try
218         {
219             contentHandler.endPrefixMapping(prefix);
220         }
221         catch (SAXException JavaDoc e)
222         {
223             throw new ExporterException("Failed to process end namespace event - prefix " + prefix, e);
224         }
225     }
226
227     /* (non-Javadoc)
228      * @see org.alfresco.service.cmr.view.Exporter#startNode(org.alfresco.service.cmr.repository.NodeRef)
229      */

230     public void startNode(NodeRef nodeRef)
231     {
232         try
233         {
234             AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
235
236             Path path = nodeService.getPath(nodeRef);
237             if (path.size() > 1)
238             {
239                 // a child name does not exist for root
240
Path.ChildAssocElement pathElement = (Path.ChildAssocElement)path.last();
241                 QName childQName = pathElement.getRef().getQName();
242                 attrs.addAttribute(NamespaceService.REPOSITORY_VIEW_1_0_URI, CHILDNAME_LOCALNAME, CHILDNAME_QNAME.toPrefixString(), null, toPrefixString(childQName));
243             }
244             
245             QName type = nodeService.getType(nodeRef);
246             contentHandler.startElement(type.getNamespaceURI(), type.getLocalName(), toPrefixString(type), attrs);
247         }
248         catch (SAXException JavaDoc e)
249         {
250             throw new ExporterException("Failed to process start node event - node ref " + nodeRef.toString(), e);
251         }
252     }
253
254     
255     /* (non-Javadoc)
256      * @see org.alfresco.service.cmr.view.Exporter#endNode(org.alfresco.service.cmr.repository.NodeRef)
257      */

258     public void endNode(NodeRef nodeRef)
259     {
260         try
261         {
262             QName type = nodeService.getType(nodeRef);
263             contentHandler.endElement(type.getNamespaceURI(), type.getLocalName(), toPrefixString(type));
264         }
265         catch (SAXException JavaDoc e)
266         {
267             throw new ExporterException("Failed to process end node event - node ref " + nodeRef.toString(), e);
268         }
269     }
270
271     /* (non-Javadoc)
272      * @see org.alfresco.service.cmr.view.Exporter#startAspects(org.alfresco.service.cmr.repository.NodeRef)
273      */

274     public void startAspects(NodeRef nodeRef)
275     {
276         try
277         {
278             contentHandler.startElement(ASPECTS_QNAME.getNamespaceURI(), ASPECTS_LOCALNAME, toPrefixString(ASPECTS_QNAME), EMPTY_ATTRIBUTES);
279         }
280         catch (SAXException JavaDoc e)
281         {
282             throw new ExporterException("Failed to process start aspects", e);
283         }
284     }
285
286     /* (non-Javadoc)
287      * @see org.alfresco.service.cmr.view.Exporter#endAspects(org.alfresco.service.cmr.repository.NodeRef)
288      */

289     public void endAspects(NodeRef nodeRef)
290     {
291         try
292         {
293             contentHandler.endElement(ASPECTS_QNAME.getNamespaceURI(), ASPECTS_LOCALNAME, toPrefixString(ASPECTS_QNAME));
294         }
295         catch (SAXException JavaDoc e)
296         {
297             throw new ExporterException("Failed to process end aspects", e);
298         }
299     }
300
301     /* (non-Javadoc)
302      * @see org.alfresco.service.cmr.view.Exporter#startAspect(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName)
303      */

304     public void startAspect(NodeRef nodeRef, QName aspect)
305     {
306         try
307         {
308             contentHandler.startElement(aspect.getNamespaceURI(), aspect.getLocalName(), toPrefixString(aspect), EMPTY_ATTRIBUTES);
309         }
310         catch (SAXException JavaDoc e)
311         {
312             throw new ExporterException("Failed to process start aspect event - node ref " + nodeRef.toString() + "; aspect " + toPrefixString(aspect), e);
313         }
314     }
315
316     /* (non-Javadoc)
317      * @see org.alfresco.service.cmr.view.Exporter#endAspect(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName)
318      */

319     public void endAspect(NodeRef nodeRef, QName aspect)
320     {
321         try
322         {
323             contentHandler.endElement(aspect.getNamespaceURI(), aspect.getLocalName(), toPrefixString(aspect));
324         }
325         catch (SAXException JavaDoc e)
326         {
327             throw new ExporterException("Failed to process end aspect event - node ref " + nodeRef.toString() + "; aspect " + toPrefixString(aspect), e);
328         }
329     }
330
331     /* (non-Javadoc)
332      * @see org.alfresco.service.cmr.view.Exporter#startACL(org.alfresco.service.cmr.repository.NodeRef)
333      */

334     public void startACL(NodeRef nodeRef)
335     {
336         try
337         {
338             AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
339             boolean inherit = permissionService.getInheritParentPermissions(nodeRef);
340             if (!inherit)
341             {
342                 attrs.addAttribute(NamespaceService.REPOSITORY_VIEW_1_0_URI, INHERITPERMISSIONS_LOCALNAME, INHERITPERMISSIONS_QNAME.toPrefixString(), null, "false");
343             }
344             contentHandler.startElement(ACL_QNAME.getNamespaceURI(), ACL_QNAME.getLocalName(), toPrefixString(ACL_QNAME), attrs);
345         }
346         catch (SAXException JavaDoc e)
347         {
348             throw new ExporterException("Failed to process start ACL event - node ref " + nodeRef.toString());
349         }
350     }
351
352     /* (non-Javadoc)
353      * @see org.alfresco.service.cmr.view.Exporter#permission(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.cmr.security.AccessPermission)
354      */

355     public void permission(NodeRef nodeRef, AccessPermission permission)
356     {
357         try
358         {
359             // output access control entry
360
AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
361             attrs.addAttribute(NamespaceService.REPOSITORY_VIEW_1_0_URI, ACCESS_LOCALNAME, ACCESS_QNAME.toPrefixString(), null, permission.getAccessStatus().toString());
362             contentHandler.startElement(ACE_QNAME.getNamespaceURI(), ACE_QNAME.getLocalName(), toPrefixString(ACE_QNAME), attrs);
363
364             // output authority
365
contentHandler.startElement(AUTHORITY_QNAME.getNamespaceURI(), AUTHORITY_QNAME.getLocalName(), toPrefixString(AUTHORITY_QNAME), EMPTY_ATTRIBUTES);
366             String JavaDoc authority = permission.getAuthority();
367             contentHandler.characters(authority.toCharArray(), 0, authority.length());
368             contentHandler.endElement(AUTHORITY_QNAME.getNamespaceURI(), AUTHORITY_QNAME.getLocalName(), toPrefixString(AUTHORITY_QNAME));
369
370             // output permission
371
contentHandler.startElement(PERMISSION_QNAME.getNamespaceURI(), PERMISSION_QNAME.getLocalName(), toPrefixString(PERMISSION_QNAME), EMPTY_ATTRIBUTES);
372             String JavaDoc strPermission = permission.getPermission();
373             contentHandler.characters(strPermission.toCharArray(), 0, strPermission.length());
374             contentHandler.endElement(PERMISSION_QNAME.getNamespaceURI(), PERMISSION_QNAME.getLocalName(), toPrefixString(PERMISSION_QNAME));
375             
376             // end access control entry
377
contentHandler.endElement(ACE_QNAME.getNamespaceURI(), ACE_QNAME.getLocalName(), toPrefixString(ACE_QNAME));
378         }
379         catch (SAXException JavaDoc e)
380         {
381             throw new ExporterException("Failed to process permission event - node ref " + nodeRef.toString() + "; permission " + permission);
382         }
383     }
384
385     /* (non-Javadoc)
386      * @see org.alfresco.service.cmr.view.Exporter#endACL(org.alfresco.service.cmr.repository.NodeRef)
387      */

388     public void endACL(NodeRef nodeRef)
389     {
390         try
391         {
392             contentHandler.endElement(ACL_QNAME.getNamespaceURI(), ACL_QNAME.getLocalName(), toPrefixString(ACL_QNAME));
393         }
394         catch (SAXException JavaDoc e)
395         {
396             throw new ExporterException("Failed to process end ACL event - node ref " + nodeRef.toString());
397         }
398     }
399     
400     /* (non-Javadoc)
401      * @see org.alfresco.service.cmr.view.Exporter#startProperties(org.alfresco.service.cmr.repository.NodeRef)
402      */

403     public void startProperties(NodeRef nodeRef)
404     {
405         try
406         {
407             contentHandler.startElement(PROPERTIES_QNAME.getNamespaceURI(), PROPERTIES_LOCALNAME, toPrefixString(PROPERTIES_QNAME), EMPTY_ATTRIBUTES);
408         }
409         catch (SAXException JavaDoc e)
410         {
411             throw new ExporterException("Failed to process start properties", e);
412         }
413     }
414
415     /* (non-Javadoc)
416      * @see org.alfresco.service.cmr.view.Exporter#endProperties(org.alfresco.service.cmr.repository.NodeRef)
417      */

418     public void endProperties(NodeRef nodeRef)
419     {
420         try
421         {
422             contentHandler.endElement(PROPERTIES_QNAME.getNamespaceURI(), PROPERTIES_LOCALNAME, toPrefixString(PROPERTIES_QNAME));
423         }
424         catch (SAXException JavaDoc e)
425         {
426             throw new ExporterException("Failed to process start properties", e);
427         }
428     }
429
430     /* (non-Javadoc)
431      * @see org.alfresco.service.cmr.view.Exporter#startProperty(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName)
432      */

433     public void startProperty(NodeRef nodeRef, QName property)
434     {
435         try
436         {
437             contentHandler.startElement(property.getNamespaceURI(), property.getLocalName(), toPrefixString(property), EMPTY_ATTRIBUTES);
438         }
439         catch (SAXException JavaDoc e)
440         {
441             throw new ExporterException("Failed to process start property event - nodeRef " + nodeRef + "; property " + toPrefixString(property), e);
442         }
443     }
444
445     /* (non-Javadoc)
446      * @see org.alfresco.service.cmr.view.Exporter#endProperty(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName)
447      */

448     public void endProperty(NodeRef nodeRef, QName property)
449     {
450         try
451         {
452             contentHandler.endElement(property.getNamespaceURI(), property.getLocalName(), toPrefixString(property));
453         }
454         catch (SAXException JavaDoc e)
455         {
456             throw new ExporterException("Failed to process end property event - nodeRef " + nodeRef + "; property " + toPrefixString(property), e);
457         }
458     }
459
460     /* (non-Javadoc)
461      * @see org.alfresco.service.cmr.view.Exporter#value(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName, java.io.Serializable)
462      */

463     public void value(NodeRef nodeRef, QName property, Object JavaDoc value)
464     {
465         try
466         {
467             // determine data type of value
468
QName valueDataType = null;
469             PropertyDefinition propDef = dictionaryService.getProperty(property);
470             DataTypeDefinition dataTypeDef = (propDef == null) ? null : propDef.getDataType();
471             if (dataTypeDef == null || dataTypeDef.getName().equals(DataTypeDefinition.ANY))
472             {
473                 dataTypeDef = (value == null) ? null : dictionaryService.getDataType(value.getClass());
474                 if (dataTypeDef != null)
475                 {
476                     valueDataType = dataTypeDef.getName();
477                 }
478             }
479
480             // convert node references to paths
481
if (value instanceof NodeRef)
482             {
483                 Path nodeRefPath = createRelativePath(context.getExportOf(), nodeRef, (NodeRef)value);
484                 value = (nodeRefPath == null) ? null : nodeRefPath.toPrefixString(namespaceService);
485             }
486             
487             // output value wrapper if value is null or property data type is ANY
488
if (value == null || valueDataType != null)
489             {
490                 AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
491                 if (value == null)
492                 {
493                     attrs.addAttribute(NamespaceService.REPOSITORY_VIEW_PREFIX, ISNULL_LOCALNAME, ISNULL_QNAME.toPrefixString(), null, "true");
494                 }
495                 if (valueDataType != null)
496                 {
497                     attrs.addAttribute(NamespaceService.REPOSITORY_VIEW_PREFIX, DATATYPE_LOCALNAME, DATATYPE_QNAME.toPrefixString(), null, toPrefixString(valueDataType));
498                 }
499                 contentHandler.startElement(NamespaceService.REPOSITORY_VIEW_PREFIX, VALUE_LOCALNAME, toPrefixString(VALUE_QNAME), attrs);
500             }
501             
502             // output value
503
String JavaDoc strValue = (String JavaDoc)DefaultTypeConverter.INSTANCE.convert(String JavaDoc.class, value);
504             if (strValue != null)
505             {
506                 contentHandler.characters(strValue.toCharArray(), 0, strValue.length());
507             }
508
509             // output value wrapper if property data type is any
510
if (value == null || valueDataType != null)
511             {
512                 contentHandler.endElement(NamespaceService.REPOSITORY_VIEW_PREFIX, VALUE_LOCALNAME, toPrefixString(VALUE_QNAME));
513             }
514         }
515         catch (SAXException JavaDoc e)
516         {
517             throw new ExporterException("Failed to process value event - nodeRef " + nodeRef + "; property " + toPrefixString(property) + "; value " + value, e);
518         }
519     }
520
521     /* (non-Javadoc)
522      * @see org.alfresco.service.cmr.view.Exporter#value(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName, java.util.Collection)
523      */

524     public void value(NodeRef nodeRef, QName property, Collection JavaDoc values)
525     {
526         try
527         {
528             PropertyDefinition propDef = dictionaryService.getProperty(property);
529             DataTypeDefinition dataTypeDef = (propDef == null) ? null : propDef.getDataType();
530             
531             // start collection
532
contentHandler.startElement(NamespaceService.REPOSITORY_VIEW_PREFIX, VALUES_LOCALNAME, toPrefixString(VALUES_QNAME), EMPTY_ATTRIBUTES);
533             
534             for (Object JavaDoc value : values)
535             {
536                 // determine data type of value
537
QName valueDataType = null;
538                 if (dataTypeDef == null || dataTypeDef.getName().equals(DataTypeDefinition.ANY))
539                 {
540                     dataTypeDef = (value == null) ? null : dictionaryService.getDataType(value.getClass());
541                     if (dataTypeDef != null)
542                     {
543                         valueDataType = dataTypeDef.getName();
544                     }
545                 }
546
547                 // output value wrapper with datatype
548
AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
549                 if (value == null)
550                 {
551                     attrs.addAttribute(NamespaceService.REPOSITORY_VIEW_PREFIX, ISNULL_LOCALNAME, ISNULL_QNAME.toPrefixString(), null, "true");
552                 }
553                 if (valueDataType != null)
554                 {
555                     attrs.addAttribute(NamespaceService.REPOSITORY_VIEW_PREFIX, DATATYPE_LOCALNAME, DATATYPE_QNAME.toPrefixString(), null, toPrefixString(valueDataType));
556                 }
557                 contentHandler.startElement(NamespaceService.REPOSITORY_VIEW_PREFIX, VALUE_LOCALNAME, toPrefixString(VALUE_QNAME), attrs);
558
559                 // convert node references to paths
560
if (value instanceof NodeRef)
561                 {
562                     value = createRelativePath(context.getExportOf(), nodeRef, (NodeRef)value).toPrefixString(namespaceService);
563                 }
564                 
565                 // output value
566
String JavaDoc strValue = (String JavaDoc)DefaultTypeConverter.INSTANCE.convert(String JavaDoc.class, value);
567                 if (strValue != null)
568                 {
569                     contentHandler.characters(strValue.toCharArray(), 0, strValue.length());
570                 }
571
572                 // output value wrapper if property data type is any
573
contentHandler.endElement(NamespaceService.REPOSITORY_VIEW_PREFIX, VALUE_LOCALNAME, toPrefixString(VALUE_QNAME));
574             }
575
576             // end collection
577
contentHandler.endElement(NamespaceService.REPOSITORY_VIEW_PREFIX, VALUES_LOCALNAME, toPrefixString(VALUES_QNAME));
578         }
579         catch (SAXException JavaDoc e)
580         {
581             throw new ExporterException("Failed to process multi-value event - nodeRef " + nodeRef + "; property " + toPrefixString(property), e);
582         }
583     }
584     
585     /* (non-Javadoc)
586      * @see org.alfresco.service.cmr.view.Exporter#content(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName, java.io.InputStream)
587      */

588     public void content(NodeRef nodeRef, QName property, InputStream JavaDoc content, ContentData contentData)
589     {
590         // TODO: Base64 encode content and send out via Content Handler
591
}
592     
593     /* (non-Javadoc)
594      * @see org.alfresco.service.cmr.view.Exporter#startAssoc(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName)
595      */

596     public void startAssoc(NodeRef nodeRef, QName assoc)
597     {
598         try
599         {
600             contentHandler.startElement(assoc.getNamespaceURI(), assoc.getLocalName(), toPrefixString(assoc), EMPTY_ATTRIBUTES);
601         }
602         catch (SAXException JavaDoc e)
603         {
604             throw new ExporterException("Failed to process start assoc event - nodeRef " + nodeRef + "; association " + toPrefixString(assoc), e);
605         }
606     }
607
608     /* (non-Javadoc)
609      * @see org.alfresco.service.cmr.view.Exporter#endAssoc(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName)
610      */

611     public void endAssoc(NodeRef nodeRef, QName assoc)
612     {
613         try
614         {
615             contentHandler.endElement(assoc.getNamespaceURI(), assoc.getLocalName(), toPrefixString(assoc));
616         }
617         catch (SAXException JavaDoc e)
618         {
619             throw new ExporterException("Failed to process end assoc event - nodeRef " + nodeRef + "; association " + toPrefixString(assoc), e);
620         }
621     }
622
623     /* (non-Javadoc)
624      * @see org.alfresco.service.cmr.view.Exporter#startAssocs(org.alfresco.service.cmr.repository.NodeRef)
625      */

626     public void startAssocs(NodeRef nodeRef)
627     {
628         try
629         {
630             contentHandler.startElement(ASSOCIATIONS_QNAME.getNamespaceURI(), ASSOCIATIONS_LOCALNAME, toPrefixString(ASSOCIATIONS_QNAME), EMPTY_ATTRIBUTES);
631         }
632         catch (SAXException JavaDoc e)
633         {
634             throw new ExporterException("Failed to process start associations", e);
635         }
636     }
637
638     /* (non-Javadoc)
639      * @see org.alfresco.service.cmr.view.Exporter#endAssocs(org.alfresco.service.cmr.repository.NodeRef)
640      */

641     public void endAssocs(NodeRef nodeRef)
642     {
643         try
644         {
645             contentHandler.endElement(ASSOCIATIONS_QNAME.getNamespaceURI(), ASSOCIATIONS_LOCALNAME, toPrefixString(ASSOCIATIONS_QNAME));
646         }
647         catch (SAXException JavaDoc e)
648         {
649             throw new ExporterException("Failed to process end associations", e);
650         }
651     }
652
653     /* (non-Javadoc)
654      * @see org.alfresco.service.cmr.view.Exporter#startReference(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName)
655      */

656     public void startReference(NodeRef nodeRef, QName childName)
657     {
658         try
659         {
660             Path path = createRelativePath(context.getExportParent(), context.getExportParent(), nodeRef);
661             AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
662             attrs.addAttribute(NamespaceService.REPOSITORY_VIEW_1_0_URI, PATHREF_LOCALNAME, PATHREF_QNAME.toPrefixString(), null, path.toPrefixString(namespaceService));
663             if (childName != null)
664             {
665                 attrs.addAttribute(NamespaceService.REPOSITORY_VIEW_1_0_URI, CHILDNAME_LOCALNAME, CHILDNAME_QNAME.toPrefixString(), null, childName.toPrefixString(namespaceService));
666             }
667             contentHandler.startElement(REFERENCE_QNAME.getNamespaceURI(), REFERENCE_LOCALNAME, toPrefixString(REFERENCE_QNAME), attrs);
668         }
669         catch (SAXException JavaDoc e)
670         {
671             throw new ExporterException("Failed to process start reference", e);
672         }
673     }
674
675     /*
676      * (non-Javadoc)
677      * @see org.alfresco.service.cmr.view.Exporter#endReference(org.alfresco.service.cmr.repository.NodeRef)
678      */

679     public void endReference(NodeRef nodeRef)
680     {
681         try
682         {
683             contentHandler.endElement(REFERENCE_QNAME.getNamespaceURI(), REFERENCE_LOCALNAME, toPrefixString(REFERENCE_QNAME));
684         }
685         catch (SAXException JavaDoc e)
686         {
687             throw new ExporterException("Failed to process end reference", e);
688         }
689     }
690     
691     /* (non-Javadoc)
692      * @see org.alfresco.service.cmr.view.Exporter#warning(java.lang.String)
693      */

694     public void warning(String JavaDoc warning)
695     {
696     }
697     
698     /* (non-Javadoc)
699      * @see org.alfresco.service.cmr.view.Exporter#end()
700      */

701     public void end()
702     {
703         try
704         {
705             contentHandler.endElement(NamespaceService.REPOSITORY_VIEW_PREFIX, VIEW_LOCALNAME, VIEW_QNAME.toPrefixString());
706             contentHandler.endPrefixMapping(NamespaceService.REPOSITORY_VIEW_PREFIX);
707             contentHandler.endDocument();
708         }
709         catch (SAXException JavaDoc e)
710         {
711             throw new ExporterException("Failed to process end export event", e);
712         }
713     }
714
715     /**
716      * Get the prefix for the specified URI
717      * @param uri the URI
718      * @return the prefix (or null, if one is not registered)
719      */

720     private String JavaDoc toPrefixString(QName qname)
721     {
722         return qname.toPrefixString(namespaceService);
723     }
724
725     /**
726      * Return relative path between from and to references within export root
727      *
728      * @param fromRef from reference
729      * @param toRef to reference
730      * @return path
731      */

732     private Path createRelativePath(NodeRef rootRef, NodeRef fromRef, NodeRef toRef)
733     {
734         // Check that item exists first
735
if (!nodeService.exists(toRef))
736         {
737             // return null path
738
return null;
739         }
740         
741         Path rootPath = nodeService.getPath(rootRef);
742         Path fromPath = nodeService.getPath(fromRef);
743         Path toPath = nodeService.getPath(toRef);
744         Path relativePath = null;
745
746         try
747         {
748             // Determine if 'to path' is a category
749
// TODO: This needs to be resolved in a more appropriate manner - special support is
750
// required for categories.
751
for (int i = 0; i < toPath.size(); i++)
752             {
753                 Path.Element pathElement = toPath.get(i);
754                 if (pathElement.getPrefixedString(namespaceService).equals("cm:categoryRoot"))
755                 {
756                     Path.ChildAssocElement childPath = (Path.ChildAssocElement)pathElement;
757                     relativePath = new Path();
758                     relativePath.append(new Path.ChildAssocElement(new ChildAssociationRef(null, null, null, childPath.getRef().getParentRef())));
759                     relativePath.append(toPath.subPath(i + 1, toPath.size() -1));
760                     break;
761                 }
762             }
763
764             if (relativePath == null)
765             {
766                 // Determine if from node is relative to export tree
767
int i = 0;
768                 while (i < rootPath.size() && i < fromPath.size() && rootPath.get(i).equals(fromPath.get(i)))
769                 {
770                     i++;
771                 }
772                 if (i == rootPath.size())
773                 {
774                     // Determine if to node is relative to export tree
775
i = 0;
776                     while (i < rootPath.size() && i < toPath.size() && rootPath.get(i).equals(toPath.get(i)))
777                     {
778                         i++;
779                     }
780                     if (i == rootPath.size())
781                     {
782                         // build relative path between from and to
783
relativePath = new Path();
784                         for (int p = 0; p < fromPath.size() - i; p++)
785                         {
786                             relativePath.append(new Path.ParentElement());
787                         }
788                         if (i < toPath.size())
789                         {
790                             relativePath.append(toPath.subPath(i, toPath.size() -1));
791                         }
792                     }
793                 }
794             }
795             
796             if (relativePath == null)
797             {
798                 // default to absolute path
799
relativePath = toPath;
800             }
801         }
802         catch(Throwable JavaDoc e)
803         {
804             String JavaDoc msg = "Failed to determine relative path: root path=" + rootPath + "; from path=" + fromPath + "; to path=" + toPath;
805             throw new ExporterException(msg, e);
806         }
807         
808         return relativePath;
809     }
810
811 }
812
Popular Tags