1 17 package org.alfresco.repo.exporter; 18 19 import java.io.InputStream ; 20 import java.util.Collection ; 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 ; 39 import org.xml.sax.SAXException ; 40 import org.xml.sax.helpers.AttributesImpl ; 41 42 43 48 class ViewXMLExporter 49 implements Exporter 50 { 51 private static final String VIEW_LOCALNAME = "view"; 53 private static final String VALUES_LOCALNAME = "values"; 54 private static final String VALUE_LOCALNAME = "value"; 55 private static final String CHILDNAME_LOCALNAME = "childName"; 56 private static final String ASPECTS_LOCALNAME = "aspects"; 57 private static final String PROPERTIES_LOCALNAME = "properties"; 58 private static final String ASSOCIATIONS_LOCALNAME = "associations"; 59 private static final String DATATYPE_LOCALNAME = "datatype"; 60 private static final String ISNULL_LOCALNAME = "isNull"; 61 private static final String METADATA_LOCALNAME = "metadata"; 62 private static final String EXPORTEDBY_LOCALNAME = "exportBy"; 63 private static final String EXPORTEDDATE_LOCALNAME = "exportDate"; 64 private static final String EXPORTERVERSION_LOCALNAME = "exporterVersion"; 65 private static final String EXPORTOF_LOCALNAME = "exportOf"; 66 private static final String ACL_LOCALNAME = "acl"; 67 private static final String ACE_LOCALNAME = "ace"; 68 private static final String ACCESS_LOCALNAME = "access"; 69 private static final String AUTHORITY_LOCALNAME = "authority"; 70 private static final String PERMISSION_LOCALNAME = "permission"; 71 private static final String INHERITPERMISSIONS_LOCALNAME = "inherit"; 72 private static final String REFERENCE_LOCALNAME = "reference"; 73 private static final String 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 EMPTY_ATTRIBUTES = new AttributesImpl (); 97 98 private NamespaceService namespaceService; 100 private NodeService nodeService; 101 private DictionaryService dictionaryService; 102 private PermissionService permissionService; 103 104 private ContentHandler contentHandler; 106 private ExporterContext context; 107 108 109 116 ViewXMLExporter(NamespaceService namespaceService, NodeService nodeService, 117 DictionaryService dictionaryService, PermissionService permissionService, ContentHandler 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 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 contentHandler.startElement(NamespaceService.REPOSITORY_VIEW_PREFIX, METADATA_LOCALNAME, METADATA_QNAME.toPrefixString(), EMPTY_ATTRIBUTES); 166 167 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 contentHandler.startElement(NamespaceService.REPOSITORY_VIEW_PREFIX, EXPORTEDDATE_LOCALNAME, EXPORTEDDATE_QNAME.toPrefixString(), EMPTY_ATTRIBUTES); 174 String date = DefaultTypeConverter.INSTANCE.convert(String .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 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 contentHandler.startElement(NamespaceService.REPOSITORY_VIEW_PREFIX, EXPORTOF_LOCALNAME, EXPORTOF_QNAME.toPrefixString(), EMPTY_ATTRIBUTES); 185 String 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 e) 192 { 193 throw new ExporterException("Failed to process export start event", e); 194 } 195 } 196 197 200 public void startNamespace(String prefix, String uri) 201 { 202 try 203 { 204 contentHandler.startPrefixMapping(prefix, uri); 205 } 206 catch (SAXException e) 207 { 208 throw new ExporterException("Failed to process start namespace event - prefix " + prefix + " uri " + uri, e); 209 } 210 } 211 212 215 public void endNamespace(String prefix) 216 { 217 try 218 { 219 contentHandler.endPrefixMapping(prefix); 220 } 221 catch (SAXException e) 222 { 223 throw new ExporterException("Failed to process end namespace event - prefix " + prefix, e); 224 } 225 } 226 227 230 public void startNode(NodeRef nodeRef) 231 { 232 try 233 { 234 AttributesImpl attrs = new AttributesImpl (); 235 236 Path path = nodeService.getPath(nodeRef); 237 if (path.size() > 1) 238 { 239 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 e) 249 { 250 throw new ExporterException("Failed to process start node event - node ref " + nodeRef.toString(), e); 251 } 252 } 253 254 255 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 e) 266 { 267 throw new ExporterException("Failed to process end node event - node ref " + nodeRef.toString(), e); 268 } 269 } 270 271 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 e) 281 { 282 throw new ExporterException("Failed to process start aspects", e); 283 } 284 } 285 286 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 e) 296 { 297 throw new ExporterException("Failed to process end aspects", e); 298 } 299 } 300 301 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 e) 311 { 312 throw new ExporterException("Failed to process start aspect event - node ref " + nodeRef.toString() + "; aspect " + toPrefixString(aspect), e); 313 } 314 } 315 316 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 e) 326 { 327 throw new ExporterException("Failed to process end aspect event - node ref " + nodeRef.toString() + "; aspect " + toPrefixString(aspect), e); 328 } 329 } 330 331 334 public void startACL(NodeRef nodeRef) 335 { 336 try 337 { 338 AttributesImpl attrs = new AttributesImpl (); 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 e) 347 { 348 throw new ExporterException("Failed to process start ACL event - node ref " + nodeRef.toString()); 349 } 350 } 351 352 355 public void permission(NodeRef nodeRef, AccessPermission permission) 356 { 357 try 358 { 359 AttributesImpl attrs = new AttributesImpl (); 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 contentHandler.startElement(AUTHORITY_QNAME.getNamespaceURI(), AUTHORITY_QNAME.getLocalName(), toPrefixString(AUTHORITY_QNAME), EMPTY_ATTRIBUTES); 366 String 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 contentHandler.startElement(PERMISSION_QNAME.getNamespaceURI(), PERMISSION_QNAME.getLocalName(), toPrefixString(PERMISSION_QNAME), EMPTY_ATTRIBUTES); 372 String 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 contentHandler.endElement(ACE_QNAME.getNamespaceURI(), ACE_QNAME.getLocalName(), toPrefixString(ACE_QNAME)); 378 } 379 catch (SAXException e) 380 { 381 throw new ExporterException("Failed to process permission event - node ref " + nodeRef.toString() + "; permission " + permission); 382 } 383 } 384 385 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 e) 395 { 396 throw new ExporterException("Failed to process end ACL event - node ref " + nodeRef.toString()); 397 } 398 } 399 400 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 e) 410 { 411 throw new ExporterException("Failed to process start properties", e); 412 } 413 } 414 415 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 e) 425 { 426 throw new ExporterException("Failed to process start properties", e); 427 } 428 } 429 430 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 e) 440 { 441 throw new ExporterException("Failed to process start property event - nodeRef " + nodeRef + "; property " + toPrefixString(property), e); 442 } 443 } 444 445 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 e) 455 { 456 throw new ExporterException("Failed to process end property event - nodeRef " + nodeRef + "; property " + toPrefixString(property), e); 457 } 458 } 459 460 463 public void value(NodeRef nodeRef, QName property, Object value) 464 { 465 try 466 { 467 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 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 if (value == null || valueDataType != null) 489 { 490 AttributesImpl attrs = new AttributesImpl (); 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 String strValue = (String )DefaultTypeConverter.INSTANCE.convert(String .class, value); 504 if (strValue != null) 505 { 506 contentHandler.characters(strValue.toCharArray(), 0, strValue.length()); 507 } 508 509 if (value == null || valueDataType != null) 511 { 512 contentHandler.endElement(NamespaceService.REPOSITORY_VIEW_PREFIX, VALUE_LOCALNAME, toPrefixString(VALUE_QNAME)); 513 } 514 } 515 catch (SAXException e) 516 { 517 throw new ExporterException("Failed to process value event - nodeRef " + nodeRef + "; property " + toPrefixString(property) + "; value " + value, e); 518 } 519 } 520 521 524 public void value(NodeRef nodeRef, QName property, Collection values) 525 { 526 try 527 { 528 PropertyDefinition propDef = dictionaryService.getProperty(property); 529 DataTypeDefinition dataTypeDef = (propDef == null) ? null : propDef.getDataType(); 530 531 contentHandler.startElement(NamespaceService.REPOSITORY_VIEW_PREFIX, VALUES_LOCALNAME, toPrefixString(VALUES_QNAME), EMPTY_ATTRIBUTES); 533 534 for (Object value : values) 535 { 536 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 AttributesImpl attrs = new AttributesImpl (); 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 if (value instanceof NodeRef) 561 { 562 value = createRelativePath(context.getExportOf(), nodeRef, (NodeRef)value).toPrefixString(namespaceService); 563 } 564 565 String strValue = (String )DefaultTypeConverter.INSTANCE.convert(String .class, value); 567 if (strValue != null) 568 { 569 contentHandler.characters(strValue.toCharArray(), 0, strValue.length()); 570 } 571 572 contentHandler.endElement(NamespaceService.REPOSITORY_VIEW_PREFIX, VALUE_LOCALNAME, toPrefixString(VALUE_QNAME)); 574 } 575 576 contentHandler.endElement(NamespaceService.REPOSITORY_VIEW_PREFIX, VALUES_LOCALNAME, toPrefixString(VALUES_QNAME)); 578 } 579 catch (SAXException e) 580 { 581 throw new ExporterException("Failed to process multi-value event - nodeRef " + nodeRef + "; property " + toPrefixString(property), e); 582 } 583 } 584 585 588 public void content(NodeRef nodeRef, QName property, InputStream content, ContentData contentData) 589 { 590 } 592 593 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 e) 603 { 604 throw new ExporterException("Failed to process start assoc event - nodeRef " + nodeRef + "; association " + toPrefixString(assoc), e); 605 } 606 } 607 608 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 e) 618 { 619 throw new ExporterException("Failed to process end assoc event - nodeRef " + nodeRef + "; association " + toPrefixString(assoc), e); 620 } 621 } 622 623 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 e) 633 { 634 throw new ExporterException("Failed to process start associations", e); 635 } 636 } 637 638 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 e) 648 { 649 throw new ExporterException("Failed to process end associations", e); 650 } 651 } 652 653 656 public void startReference(NodeRef nodeRef, QName childName) 657 { 658 try 659 { 660 Path path = createRelativePath(context.getExportParent(), context.getExportParent(), nodeRef); 661 AttributesImpl attrs = new AttributesImpl (); 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 e) 670 { 671 throw new ExporterException("Failed to process start reference", e); 672 } 673 } 674 675 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 e) 686 { 687 throw new ExporterException("Failed to process end reference", e); 688 } 689 } 690 691 694 public void warning(String warning) 695 { 696 } 697 698 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 e) 710 { 711 throw new ExporterException("Failed to process end export event", e); 712 } 713 } 714 715 720 private String toPrefixString(QName qname) 721 { 722 return qname.toPrefixString(namespaceService); 723 } 724 725 732 private Path createRelativePath(NodeRef rootRef, NodeRef fromRef, NodeRef toRef) 733 { 734 if (!nodeService.exists(toRef)) 736 { 737 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 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 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 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 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 relativePath = toPath; 800 } 801 } 802 catch(Throwable e) 803 { 804 String 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 |