1 17 package org.alfresco.jcr.exporter; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.List ; 24 25 import javax.jcr.RepositoryException; 26 import javax.jcr.Value; 27 import javax.jcr.ValueFormatException; 28 29 import org.alfresco.jcr.dictionary.JCRNamespace; 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.repository.ContentData; 37 import org.alfresco.service.cmr.repository.NodeRef; 38 import org.alfresco.service.cmr.repository.NodeService; 39 import org.alfresco.service.cmr.repository.Path; 40 import org.alfresco.service.cmr.security.AccessPermission; 41 import org.alfresco.service.cmr.view.Exporter; 42 import org.alfresco.service.cmr.view.ExporterContext; 43 import org.alfresco.service.cmr.view.ExporterException; 44 import org.alfresco.service.namespace.QName; 45 import org.alfresco.util.Base64; 46 import org.alfresco.util.ISO9075; 47 import org.xml.sax.ContentHandler ; 48 import org.xml.sax.SAXException ; 49 import org.xml.sax.helpers.AttributesImpl ; 50 51 52 57 public class JCRDocumentXMLExporter implements Exporter 58 { 59 60 private SessionImpl session; 61 private ContentHandler contentHandler; 62 private List <QName> currentProperties = new ArrayList <QName>(); 63 private List <Object > currentValues = new ArrayList <Object >(); 64 65 66 73 public JCRDocumentXMLExporter(SessionImpl session, ContentHandler contentHandler) 74 { 75 this.session = session; 76 this.contentHandler = contentHandler; 77 } 78 79 80 83 public void start(ExporterContext exportNodeRef) 84 { 85 try 86 { 87 contentHandler.startDocument(); 88 } 89 catch (SAXException e) 90 { 91 throw new ExporterException("Failed to process export start event", e); 92 } 93 } 94 95 98 public void startNamespace(String prefix, String uri) 99 { 100 try 101 { 102 contentHandler.startPrefixMapping(prefix, uri); 103 } 104 catch (SAXException e) 105 { 106 throw new ExporterException("Failed to process start namespace event - prefix " + prefix + " uri " + uri, e); 107 } 108 } 109 110 113 public void endNamespace(String prefix) 114 { 115 try 116 { 117 contentHandler.endPrefixMapping(prefix); 118 } 119 catch (SAXException e) 120 { 121 throw new ExporterException("Failed to process end namespace event - prefix " + prefix, e); 122 } 123 } 124 125 128 public void startNode(NodeRef nodeRef) 129 { 130 } 131 132 135 public void endNode(NodeRef nodeRef) 136 { 137 try 138 { 139 QName nodeName = getNodeName(nodeRef); 140 contentHandler.endElement(nodeName.getNamespaceURI(), nodeName.getLocalName(), toPrefixString(nodeName)); 141 } 142 catch (SAXException e) 143 { 144 throw new ExporterException("Failed to process end node event - node ref " + nodeRef.toString(), e); 145 } 146 } 147 148 151 public void startAspects(NodeRef nodeRef) 152 { 153 } 154 155 158 public void endAspects(NodeRef nodeRef) 159 { 160 } 161 162 165 public void startAspect(NodeRef nodeRef, QName aspect) 166 { 167 } 168 169 172 public void endAspect(NodeRef nodeRef, QName aspect) 173 { 174 } 175 176 179 public void startACL(NodeRef nodeRef) 180 { 181 } 182 183 186 public void permission(NodeRef nodeRef, AccessPermission permission) 187 { 188 } 189 190 193 public void endACL(NodeRef nodeRef) 194 { 195 } 196 197 200 public void startProperties(NodeRef nodeRef) 201 { 202 currentProperties.clear(); 203 currentValues.clear(); 204 } 205 206 209 public void endProperties(NodeRef nodeRef) 210 { 211 try 212 { 213 AttributesImpl attrs = new AttributesImpl (); 215 216 NodeImpl nodeImpl = new NodeImpl(session, nodeRef); 218 PropertyImpl primaryType = new JCRPrimaryTypeProperty(nodeImpl); 219 attrs.addAttribute(JCRPrimaryTypeProperty.PROPERTY_NAME.getNamespaceURI(), JCRPrimaryTypeProperty.PROPERTY_NAME.getLocalName(), 220 toPrefixString(JCRPrimaryTypeProperty.PROPERTY_NAME), null, getValue(primaryType.getValue().getString())); 221 222 PropertyImpl mixinTypes = new JCRMixinTypesProperty(nodeImpl); 224 Collection <String > mixins = new ArrayList <String >(); 225 for (Value value : mixinTypes.getValues()) 226 { 227 mixins.add(value.getString()); 228 } 229 attrs.addAttribute(JCRMixinTypesProperty.PROPERTY_NAME.getNamespaceURI(), JCRMixinTypesProperty.PROPERTY_NAME.getLocalName(), 230 toPrefixString(JCRMixinTypesProperty.PROPERTY_NAME), null, getCollectionValue(mixins)); 231 232 attrs.addAttribute(JCRUUIDProperty.PROPERTY_NAME.getNamespaceURI(), JCRUUIDProperty.PROPERTY_NAME.getLocalName(), 234 toPrefixString(JCRUUIDProperty.PROPERTY_NAME), null, getValue(nodeRef.getId())); 235 236 for (int i = 0; i < currentProperties.size(); i++) 238 { 239 Object value = currentValues.get(i); 240 String strValue = (value instanceof Collection ) ? getCollectionValue((Collection )value) : getValue(value); 241 QName propName = currentProperties.get(i); 242 propName = encodeQName(propName); 243 attrs.addAttribute(propName.getNamespaceURI(), propName.getLocalName(), toPrefixString(propName), null, strValue); 244 } 245 246 QName nodeName = getNodeName(nodeRef); 248 contentHandler.startElement(nodeName.getNamespaceURI(), nodeName.getLocalName(), toPrefixString(nodeName), attrs); 249 } 250 catch (ValueFormatException e) 251 { 252 throw new ExporterException("Failed to process properties event - nodeRef " + nodeRef); 253 } 254 catch (RepositoryException e) 255 { 256 throw new ExporterException("Failed to process properties event - nodeRef " + nodeRef); 257 } 258 catch (SAXException e) 259 { 260 throw new ExporterException("Failed to process properties event - nodeRef " + nodeRef); 261 } 262 } 263 264 267 public void startProperty(NodeRef nodeRef, QName property) 268 { 269 } 270 271 274 public void endProperty(NodeRef nodeRef, QName property) 275 { 276 } 277 278 281 public void value(NodeRef nodeRef, QName property, Object value) 282 { 283 currentProperties.add(property); 284 currentValues.add(value); 285 } 286 287 290 public void value(NodeRef nodeRef, QName property, Collection values) 291 { 292 currentProperties.add(property); 293 currentValues.add(values); 294 } 295 296 299 public void content(NodeRef nodeRef, QName property, InputStream content, ContentData contentData) 300 { 301 try 302 { 303 StringBuffer strValue = new StringBuffer (9 * 1024); 304 if (content != null) 305 { 306 InputStream base64content = new Base64.InputStream(content, Base64.ENCODE | Base64.DONT_BREAK_LINES); 308 byte[] buffer = new byte[9 * 1024]; 309 int read; 310 while ((read = base64content.read(buffer, 0, buffer.length)) > 0) 311 { 312 String characters = new String (buffer, 0, read); 313 strValue.append(characters); 314 } 315 } 316 currentProperties.add(property); 317 currentValues.add(strValue.toString()); 318 } 319 catch (IOException e) 320 { 321 throw new ExporterException("Failed to process content event - nodeRef " + nodeRef + "; property " + toPrefixString(property)); 322 } 323 } 324 325 328 public void startAssoc(NodeRef nodeRef, QName assoc) 329 { 330 } 331 332 335 public void endAssoc(NodeRef nodeRef, QName assoc) 336 { 337 } 338 339 342 public void startAssocs(NodeRef nodeRef) 343 { 344 } 345 346 349 public void endAssocs(NodeRef nodeRef) 350 { 351 } 352 353 356 public void startReference(NodeRef nodeRef, QName childName) 357 { 358 } 359 360 363 public void endReference(NodeRef nodeRef) 364 { 365 } 366 367 370 public void warning(String warning) 371 { 372 } 373 374 377 public void end() 378 { 379 try 380 { 381 contentHandler.endDocument(); 382 } 383 catch (SAXException e) 384 { 385 throw new ExporterException("Failed to process end export event", e); 386 } 387 } 388 389 394 private String toPrefixString(QName qname) 395 { 396 return qname.toPrefixString(session.getNamespaceResolver()); 397 } 398 399 405 private QName getNodeName(NodeRef nodeRef) 406 { 407 QName childQName = null; 409 NodeService nodeService = session.getRepositoryImpl().getServiceRegistry().getNodeService(); 410 NodeRef rootNode = nodeService.getRootNode(nodeRef.getStoreRef()); 411 if (rootNode.equals(nodeRef)) 412 { 413 childQName = QName.createQName(JCRNamespace.JCR_URI, "root"); 414 } 415 else 416 { 417 Path path = nodeService.getPath(nodeRef); 418 String childName = path.last().getElementString(); 419 childQName = QName.createQName(childName); 420 childQName = encodeQName(childQName); 421 } 422 423 return childQName; 424 } 425 426 432 private String getValue(Object value) 433 throws RepositoryException 434 { 435 String strValue = session.getTypeConverter().convert(String .class, value); 436 return encodeBlanks(strValue); 437 } 438 439 440 446 private String getCollectionValue(Collection values) 447 { 448 Collection <String > strValues = session.getTypeConverter().getConverter().convert(String .class, values); 449 StringBuffer buffer = new StringBuffer (); 450 int i = 0; 451 for (String strValue : strValues) 452 { 453 buffer.append(encodeBlanks(strValue)); 454 i++; 455 if (i < strValues.size()) 456 { 457 buffer.append(" "); 458 } 459 } 460 return buffer.toString(); 461 } 462 463 469 private QName encodeQName(QName name) 470 { 471 return QName.createQName(name.getNamespaceURI(), ISO9075.encode(name.getLocalName())); 472 } 473 474 480 private String encodeBlanks(String value) 481 { 482 return value.replaceAll(" ", "_x0020_"); 483 } 484 485 } 486 | Popular Tags |