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 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 ; 49 import org.xml.sax.SAXException ; 50 import org.xml.sax.helpers.AttributesImpl ; 51 52 53 58 public class JCRSystemXMLExporter implements Exporter 59 { 60 public final static String NODE_LOCALNAME = "node"; 61 public final static String NAME_LOCALNAME = "name"; 62 public final static String PROPERTY_LOCALNAME = "property"; 63 public final static String TYPE_LOCALNAME = "type"; 64 public final static String 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 contentHandler; 74 75 private final static AttributesImpl EMPTY_ATTRIBUTES = new AttributesImpl (); 76 77 78 85 public JCRSystemXMLExporter(SessionImpl session, ContentHandler contentHandler) 86 { 87 this.session = session; 88 this.contentHandler = contentHandler; 89 } 90 91 92 95 public void start(ExporterContext exportNodeRef) 96 { 97 try 98 { 99 contentHandler.startDocument(); 100 } 101 catch (SAXException e) 102 { 103 throw new ExporterException("Failed to process export start event", e); 104 } 105 } 106 107 110 public void startNamespace(String prefix, String uri) 111 { 112 try 113 { 114 contentHandler.startPrefixMapping(prefix, uri); 115 } 116 catch (SAXException e) 117 { 118 throw new ExporterException("Failed to process start namespace event - prefix " + prefix + " uri " + uri, e); 119 } 120 } 121 122 125 public void endNamespace(String prefix) 126 { 127 try 128 { 129 contentHandler.endPrefixMapping(prefix); 130 } 131 catch (SAXException e) 132 { 133 throw new ExporterException("Failed to process end namespace event - prefix " + prefix, e); 134 } 135 } 136 137 140 public void startNode(NodeRef nodeRef) 141 { 142 try 143 { 144 String 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 AttributesImpl attrs = new AttributesImpl (); 161 attrs.addAttribute(NAME_QNAME.getNamespaceURI(), NAME_LOCALNAME, toPrefixString(NAME_QNAME), null, toPrefixString(childQName)); 162 163 contentHandler.startElement(NODE_QNAME.getNamespaceURI(), NODE_LOCALNAME, toPrefixString(NODE_QNAME), attrs); 165 166 NodeImpl nodeImpl = new NodeImpl(session, nodeRef); 170 171 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 PropertyImpl mixinTypes = new JCRMixinTypesProperty(nodeImpl); 179 startProperty(nodeRef, JCRMixinTypesProperty.PROPERTY_NAME); 180 Collection <String > mixins = new ArrayList <String >(); 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 startProperty(nodeRef, JCRUUIDProperty.PROPERTY_NAME); 190 value(nodeRef, JCRUUIDProperty.PROPERTY_NAME, nodeRef.getId()); 191 endProperty(nodeRef, JCRUUIDProperty.PROPERTY_NAME); 192 } 193 catch (SAXException 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 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 e) 213 { 214 throw new ExporterException("Failed to process end node event - node ref " + nodeRef.toString(), e); 215 } 216 } 217 218 221 public void startAspects(NodeRef nodeRef) 222 { 223 } 224 225 228 public void endAspects(NodeRef nodeRef) 229 { 230 } 231 232 235 public void startAspect(NodeRef nodeRef, QName aspect) 236 { 237 } 238 239 242 public void endAspect(NodeRef nodeRef, QName aspect) 243 { 244 } 245 246 249 public void startACL(NodeRef nodeRef) 250 { 251 } 252 253 256 public void permission(NodeRef nodeRef, AccessPermission permission) 257 { 258 } 259 260 263 public void endACL(NodeRef nodeRef) 264 { 265 } 266 267 270 public void startProperties(NodeRef nodeRef) 271 { 272 } 273 274 277 public void endProperties(NodeRef nodeRef) 278 { 279 } 280 281 284 public void startProperty(NodeRef nodeRef, QName property) 285 { 286 try 287 { 288 DictionaryService dictionaryService = session.getRepositoryImpl().getServiceRegistry().getDictionaryService(); 290 PropertyDefinition propDef = dictionaryService.getProperty(property); 291 PropertyDefinitionImpl propDefImpl = new PropertyDefinitionImpl(session.getTypeManager(), propDef); 292 String datatype = PropertyType.nameFromValue(propDefImpl.getRequiredType()); 293 AttributesImpl attrs = new AttributesImpl (); 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 contentHandler.startElement(PROPERTY_QNAME.getNamespaceURI(), PROPERTY_LOCALNAME, toPrefixString(PROPERTY_QNAME), attrs); 299 } 300 catch (SAXException e) 301 { 302 throw new ExporterException("Failed to process start property event - nodeRef " + nodeRef + "; property " + toPrefixString(property), e); 303 } 304 } 305 306 309 public void endProperty(NodeRef nodeRef, QName property) 310 { 311 try 312 { 313 contentHandler.endElement(PROPERTY_QNAME.getNamespaceURI(), PROPERTY_LOCALNAME, toPrefixString(PROPERTY_QNAME)); 315 } 316 catch (SAXException e) 317 { 318 throw new ExporterException("Failed to process end property event - nodeRef " + nodeRef + "; property " + toPrefixString(property), e); 319 } 320 } 321 322 325 public void value(NodeRef nodeRef, QName property, Object value) 326 { 327 try 328 { 329 contentHandler.startElement(VALUE_QNAME.getNamespaceURI(), VALUE_LOCALNAME, toPrefixString(VALUE_QNAME), EMPTY_ATTRIBUTES); 331 String strValue = session.getTypeConverter().convert(String .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 e) 340 { 341 throw new ExporterException("Failed to process value event - nodeRef " + nodeRef + "; property " + toPrefixString(property) + "; value " + value, e); 342 } 343 } 344 345 348 public void value(NodeRef nodeRef, QName property, Collection values) 349 { 350 try 351 { 352 Collection <String > strValues = session.getTypeConverter().getConverter().convert(String .class, values); 354 355 for (String strValue : strValues) 356 { 357 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 e) 364 { 365 throw new ExporterException("Failed to process value event - nodeRef " + nodeRef + "; property " + toPrefixString(property) + "; value " + values, e); 366 } 367 } 368 369 372 public void content(NodeRef nodeRef, QName property, InputStream 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 InputStream 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 characters = new String (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 e) 394 { 395 throw new ExporterException("Failed to process content event - nodeRef " + nodeRef + "; property " + toPrefixString(property)); 396 } 397 catch (IOException e) 398 { 399 throw new ExporterException("Failed to process content event - nodeRef " + nodeRef + "; property " + toPrefixString(property)); 400 } 401 } 402 403 406 public void startAssoc(NodeRef nodeRef, QName assoc) 407 { 408 } 409 410 413 public void endAssoc(NodeRef nodeRef, QName assoc) 414 { 415 } 416 417 420 public void startAssocs(NodeRef nodeRef) 421 { 422 } 423 424 427 public void endAssocs(NodeRef nodeRef) 428 { 429 } 430 431 434 public void startReference(NodeRef nodeRef, QName childName) 435 { 436 } 437 438 441 public void endReference(NodeRef nodeRef) 442 { 443 } 444 445 448 public void warning(String warning) 449 { 450 } 451 452 455 public void end() 456 { 457 try 458 { 459 contentHandler.endDocument(); 460 } 461 catch (SAXException e) 462 { 463 throw new ExporterException("Failed to process end export event", e); 464 } 465 } 466 467 472 private String toPrefixString(QName qname) 473 { 474 return qname.toPrefixString(session.getNamespaceResolver()); 475 } 476 477 } 478 | Popular Tags |