KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.SimpleDateFormat JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Date JavaDoc;
23
24 import org.alfresco.service.cmr.repository.NodeRef;
25 import org.alfresco.service.cmr.repository.NodeService;
26 import org.alfresco.service.cmr.repository.Path;
27 import org.alfresco.service.cmr.view.Exporter;
28 import org.alfresco.service.cmr.view.ExporterException;
29 import org.alfresco.service.namespace.NamespaceService;
30 import org.alfresco.service.namespace.QName;
31 import org.xml.sax.ContentHandler JavaDoc;
32 import org.xml.sax.SAXException JavaDoc;
33 import org.xml.sax.helpers.AttributesImpl JavaDoc;
34
35
36 /**
37  * Exporter that exports Repository information to XML (Alfresco Repository View Schema)
38  *
39  * @author David Caruana
40  */

41 /*package*/ class XMLExporter
42     implements Exporter
43 {
44     private final static String JavaDoc VIEW_LOCALNAME = "view";
45     private final static String JavaDoc CHILDNAME_LOCALNAME = "childName";
46     private static QName VIEW_QNAME;
47     private static QName CHILDNAME_QNAME;
48     
49     private NamespaceService namespaceService;
50     private NodeService nodeService;
51     private ContentHandler JavaDoc contentHandler;
52     private final static AttributesImpl JavaDoc EMPTY_ATTRIBUTES = new AttributesImpl JavaDoc();
53     
54
55     /**
56      * Construct
57      *
58      * @param namespaceService namespace service
59      * @param nodeService node service
60      * @param contentHandler content handler
61      */

62     /*package*/ XMLExporter(NamespaceService namespaceService, NodeService nodeService, ContentHandler JavaDoc contentHandler)
63     {
64         this.namespaceService = namespaceService;
65         this.nodeService = nodeService;
66         this.contentHandler = contentHandler;
67         
68         VIEW_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, VIEW_LOCALNAME, namespaceService);
69         CHILDNAME_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, CHILDNAME_LOCALNAME, namespaceService);
70     }
71     
72     
73     /* (non-Javadoc)
74      * @see org.alfresco.service.cmr.view.Exporter#start()
75      */

76     public void start()
77     {
78         try
79         {
80             contentHandler.startDocument();
81             contentHandler.startPrefixMapping(NamespaceService.REPOSITORY_VIEW_PREFIX, NamespaceService.REPOSITORY_VIEW_1_0_URI);
82             contentHandler.startElement(NamespaceService.REPOSITORY_VIEW_PREFIX, VIEW_LOCALNAME, VIEW_QNAME.toPrefixString(), EMPTY_ATTRIBUTES);
83         }
84         catch (SAXException JavaDoc e)
85         {
86             throw new ExporterException("Failed to process export start event", e);
87         }
88     }
89
90     /* (non-Javadoc)
91      * @see org.alfresco.service.cmr.view.Exporter#startNamespace(java.lang.String, java.lang.String)
92      */

93     public void startNamespace(String JavaDoc prefix, String JavaDoc uri)
94     {
95         try
96         {
97             contentHandler.startPrefixMapping(prefix, uri);
98         }
99         catch (SAXException JavaDoc e)
100         {
101             throw new ExporterException("Failed to process start namespace event - prefix " + prefix + " uri " + uri, e);
102         }
103     }
104
105     /* (non-Javadoc)
106      * @see org.alfresco.service.cmr.view.Exporter#endNamespace(java.lang.String)
107      */

108     public void endNamespace(String JavaDoc prefix)
109     {
110         try
111         {
112             contentHandler.endPrefixMapping(prefix);
113         }
114         catch (SAXException JavaDoc e)
115         {
116             throw new ExporterException("Failed to process end namespace event - prefix " + prefix, e);
117         }
118     }
119
120     /* (non-Javadoc)
121      * @see org.alfresco.service.cmr.view.Exporter#startNode(org.alfresco.service.cmr.repository.NodeRef)
122      */

123     public void startNode(NodeRef nodeRef)
124     {
125         try
126         {
127             Path path = nodeService.getPath(nodeRef);
128             String JavaDoc childName = path.last().getElementString();
129             QName childQName = QName.createQName(childName);
130             AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
131             attrs.addAttribute(NamespaceService.REPOSITORY_VIEW_1_0_URI, CHILDNAME_LOCALNAME, CHILDNAME_QNAME.toPrefixString(), null, toPrefixString(childQName));
132             
133             QName type = nodeService.getType(nodeRef);
134             contentHandler.startElement(type.getNamespaceURI(), type.getLocalName(), toPrefixString(type), attrs);
135         }
136         catch (SAXException JavaDoc e)
137         {
138             throw new ExporterException("Failed to process start node event - node ref " + nodeRef.toString(), e);
139         }
140     }
141
142     
143     /* (non-Javadoc)
144      * @see org.alfresco.service.cmr.view.Exporter#endNode(org.alfresco.service.cmr.repository.NodeRef)
145      */

146     public void endNode(NodeRef nodeRef)
147     {
148         try
149         {
150             QName type = nodeService.getType(nodeRef);
151             contentHandler.endElement(type.getNamespaceURI(), type.getLocalName(), toPrefixString(type));
152         }
153         catch (SAXException JavaDoc e)
154         {
155             throw new ExporterException("Failed to process end node event - node ref " + nodeRef.toString(), e);
156         }
157     }
158
159     /* (non-Javadoc)
160      * @see org.alfresco.service.cmr.view.Exporter#startAspect(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName)
161      */

162     public void startAspect(NodeRef nodeRef, QName aspect)
163     {
164         try
165         {
166             contentHandler.startElement(aspect.getNamespaceURI(), aspect.getLocalName(), toPrefixString(aspect), EMPTY_ATTRIBUTES);
167         }
168         catch (SAXException JavaDoc e)
169         {
170             throw new ExporterException("Failed to process start aspect event - node ref " + nodeRef.toString() + "; aspect " + toPrefixString(aspect), e);
171         }
172     }
173
174     /* (non-Javadoc)
175      * @see org.alfresco.service.cmr.view.Exporter#endAspect(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName)
176      */

177     public void endAspect(NodeRef nodeRef, QName aspect)
178     {
179         try
180         {
181             contentHandler.endElement(aspect.getNamespaceURI(), aspect.getLocalName(), toPrefixString(aspect));
182         }
183         catch (SAXException JavaDoc e)
184         {
185             throw new ExporterException("Failed to process end aspect event - node ref " + nodeRef.toString() + "; aspect " + toPrefixString(aspect), e);
186         }
187     }
188
189     /* (non-Javadoc)
190      * @see org.alfresco.service.cmr.view.Exporter#startProperty(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName)
191      */

192     public void startProperty(NodeRef nodeRef, QName property)
193     {
194         try
195         {
196             contentHandler.startElement(property.getNamespaceURI(), property.getLocalName(), toPrefixString(property), EMPTY_ATTRIBUTES);
197         }
198         catch (SAXException JavaDoc e)
199         {
200             throw new ExporterException("Failed to process start property event - nodeRef " + nodeRef + "; property " + toPrefixString(property), e);
201         }
202     }
203
204     /* (non-Javadoc)
205      * @see org.alfresco.service.cmr.view.Exporter#endProperty(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName)
206      */

207     public void endProperty(NodeRef nodeRef, QName property)
208     {
209         try
210         {
211             contentHandler.endElement(property.getNamespaceURI(), property.getLocalName(), toPrefixString(property));
212         }
213         catch (SAXException JavaDoc e)
214         {
215             throw new ExporterException("Failed to process end property event - nodeRef " + nodeRef + "; property " + toPrefixString(property), e);
216         }
217     }
218
219     /* (non-Javadoc)
220      * @see org.alfresco.service.cmr.view.Exporter#value(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName, java.io.Serializable)
221      */

222     public void value(NodeRef nodeRef, QName property, Object JavaDoc value)
223     {
224         if (value != null)
225         {
226             try
227             {
228                 String JavaDoc strValue;
229                 if (value instanceof Date JavaDoc)
230                 {
231                     SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc("yyyy-MM-dd'T'HH:mm:ss");
232                     strValue = format.format((Date JavaDoc)value);
233                 }
234                 else
235                 {
236                     strValue = value.toString();
237                 }
238                 contentHandler.characters(strValue.toCharArray(), 0, strValue.length());
239             }
240             catch (SAXException JavaDoc e)
241             {
242                 throw new ExporterException("Failed to process value - property " + toPrefixString(property), e);
243             }
244         }
245     }
246
247     /* (non-Javadoc)
248      * @see org.alfresco.service.cmr.view.Exporter#content(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName, java.io.InputStream)
249      */

250     public void content(NodeRef nodeRef, QName property, InputStream JavaDoc content)
251     {
252         // TODO: Base64 encode content and send out via Content Handler
253
}
254
255     /* (non-Javadoc)
256      * @see org.alfresco.service.cmr.view.Exporter#startAssoc(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName)
257      */

258     public void startAssoc(NodeRef nodeRef, QName assoc)
259     {
260         try
261         {
262             contentHandler.startElement(assoc.getNamespaceURI(), assoc.getLocalName(), toPrefixString(assoc), EMPTY_ATTRIBUTES);
263         }
264         catch (SAXException JavaDoc e)
265         {
266             throw new ExporterException("Failed to process start assoc event - nodeRef " + nodeRef + "; association " + toPrefixString(assoc), e);
267         }
268     }
269
270     /* (non-Javadoc)
271      * @see org.alfresco.service.cmr.view.Exporter#endAssoc(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName)
272      */

273     public void endAssoc(NodeRef nodeRef, QName assoc)
274     {
275         try
276         {
277             contentHandler.endElement(assoc.getNamespaceURI(), assoc.getLocalName(), toPrefixString(assoc));
278         }
279         catch (SAXException JavaDoc e)
280         {
281             throw new ExporterException("Failed to process end assoc event - nodeRef " + nodeRef + "; association " + toPrefixString(assoc), e);
282         }
283     }
284
285     /* (non-Javadoc)
286      * @see org.alfresco.service.cmr.view.Exporter#warning(java.lang.String)
287      */

288     public void warning(String JavaDoc warning)
289     {
290     }
291     
292     /* (non-Javadoc)
293      * @see org.alfresco.service.cmr.view.Exporter#end()
294      */

295     public void end()
296     {
297         try
298         {
299             contentHandler.endElement(NamespaceService.REPOSITORY_VIEW_PREFIX, VIEW_LOCALNAME, VIEW_QNAME.toPrefixString());
300             contentHandler.endPrefixMapping(NamespaceService.REPOSITORY_VIEW_PREFIX);
301             contentHandler.endDocument();
302         }
303         catch (SAXException JavaDoc e)
304         {
305             throw new ExporterException("Failed to process end export event", e);
306         }
307     }
308
309     /**
310      * Get the prefix for the specified URI
311      * @param uri the URI
312      * @return the prefix (or null, if one is not registered)
313      */

314     private String JavaDoc toPrefixString(QName qname)
315     {
316         Collection JavaDoc<String JavaDoc> prefixes = namespaceService.getPrefixes(qname.getNamespaceURI());
317         if (prefixes.isEmpty() == false)
318         {
319             String JavaDoc prefix = prefixes.iterator().next();
320             QName prefixedQName = QName.createQName(prefix, qname.getLocalName(), namespaceService);
321             return prefixedQName.toPrefixString();
322         }
323         else
324         {
325             return qname.toPrefixString();
326         }
327     }
328     
329 }
330
Popular Tags