1 17 package org.alfresco.repo.exporter; 18 19 import java.io.InputStream ; 20 import java.text.SimpleDateFormat ; 21 import java.util.Collection ; 22 import java.util.Date ; 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 ; 32 import org.xml.sax.SAXException ; 33 import org.xml.sax.helpers.AttributesImpl ; 34 35 36 41 class XMLExporter 42 implements Exporter 43 { 44 private final static String VIEW_LOCALNAME = "view"; 45 private final static String 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 contentHandler; 52 private final static AttributesImpl EMPTY_ATTRIBUTES = new AttributesImpl (); 53 54 55 62 XMLExporter(NamespaceService namespaceService, NodeService nodeService, ContentHandler 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 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 e) 85 { 86 throw new ExporterException("Failed to process export start event", e); 87 } 88 } 89 90 93 public void startNamespace(String prefix, String uri) 94 { 95 try 96 { 97 contentHandler.startPrefixMapping(prefix, uri); 98 } 99 catch (SAXException e) 100 { 101 throw new ExporterException("Failed to process start namespace event - prefix " + prefix + " uri " + uri, e); 102 } 103 } 104 105 108 public void endNamespace(String prefix) 109 { 110 try 111 { 112 contentHandler.endPrefixMapping(prefix); 113 } 114 catch (SAXException e) 115 { 116 throw new ExporterException("Failed to process end namespace event - prefix " + prefix, e); 117 } 118 } 119 120 123 public void startNode(NodeRef nodeRef) 124 { 125 try 126 { 127 Path path = nodeService.getPath(nodeRef); 128 String childName = path.last().getElementString(); 129 QName childQName = QName.createQName(childName); 130 AttributesImpl attrs = new AttributesImpl (); 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 e) 137 { 138 throw new ExporterException("Failed to process start node event - node ref " + nodeRef.toString(), e); 139 } 140 } 141 142 143 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 e) 154 { 155 throw new ExporterException("Failed to process end node event - node ref " + nodeRef.toString(), e); 156 } 157 } 158 159 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 e) 169 { 170 throw new ExporterException("Failed to process start aspect event - node ref " + nodeRef.toString() + "; aspect " + toPrefixString(aspect), e); 171 } 172 } 173 174 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 e) 184 { 185 throw new ExporterException("Failed to process end aspect event - node ref " + nodeRef.toString() + "; aspect " + toPrefixString(aspect), e); 186 } 187 } 188 189 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 e) 199 { 200 throw new ExporterException("Failed to process start property event - nodeRef " + nodeRef + "; property " + toPrefixString(property), e); 201 } 202 } 203 204 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 e) 214 { 215 throw new ExporterException("Failed to process end property event - nodeRef " + nodeRef + "; property " + toPrefixString(property), e); 216 } 217 } 218 219 222 public void value(NodeRef nodeRef, QName property, Object value) 223 { 224 if (value != null) 225 { 226 try 227 { 228 String strValue; 229 if (value instanceof Date ) 230 { 231 SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss"); 232 strValue = format.format((Date )value); 233 } 234 else 235 { 236 strValue = value.toString(); 237 } 238 contentHandler.characters(strValue.toCharArray(), 0, strValue.length()); 239 } 240 catch (SAXException e) 241 { 242 throw new ExporterException("Failed to process value - property " + toPrefixString(property), e); 243 } 244 } 245 } 246 247 250 public void content(NodeRef nodeRef, QName property, InputStream content) 251 { 252 } 254 255 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 e) 265 { 266 throw new ExporterException("Failed to process start assoc event - nodeRef " + nodeRef + "; association " + toPrefixString(assoc), e); 267 } 268 } 269 270 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 e) 280 { 281 throw new ExporterException("Failed to process end assoc event - nodeRef " + nodeRef + "; association " + toPrefixString(assoc), e); 282 } 283 } 284 285 288 public void warning(String warning) 289 { 290 } 291 292 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 e) 304 { 305 throw new ExporterException("Failed to process end export event", e); 306 } 307 } 308 309 314 private String toPrefixString(QName qname) 315 { 316 Collection <String > prefixes = namespaceService.getPrefixes(qname.getNamespaceURI()); 317 if (prefixes.isEmpty() == false) 318 { 319 String 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 |