/* (non-Javadoc)
   * @see org.alfresco.service.cmr.view.Exporter#value(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName, java.io.Serializable)
   */
  public void value(NodeRef nodeRef, QName property, Object value, int index) {
    try {
      // determine data type of value
      QName valueDataType = null;
      PropertyDefinition propDef = dictionaryService.getProperty(property);
      DataTypeDefinition dataTypeDef = (propDef == null) ? null : propDef.getDataType();
      if (dataTypeDef == null || dataTypeDef.getName().equals(DataTypeDefinition.ANY)) {
        dataTypeDef = (value == null) ? null : dictionaryService.getDataType(value.getClass());
        if (dataTypeDef != null) {
          valueDataType = dataTypeDef.getName();
        }
      }

      // convert node references to paths
      if (value instanceof NodeRef && referenceType.equals(ReferenceType.PATHREF)) {
        NodeRef valueNodeRef = (NodeRef) value;
        if (nodeRef.getStoreRef().equals(valueNodeRef.getStoreRef())) {
          Path nodeRefPath = createPath(context.getExportOf(), nodeRef, valueNodeRef);
          value = (nodeRefPath == null) ? null : nodeRefPath.toPrefixString(namespaceService);
        }
      }

      // output value wrapper if value is null or property data type is ANY or value is part of
      // collection
      if (value == null || valueDataType != null || index != -1) {
        AttributesImpl attrs = new AttributesImpl();
        if (value == null) {
          attrs.addAttribute(
              NamespaceService.REPOSITORY_VIEW_PREFIX,
              ISNULL_LOCALNAME,
              ISNULL_QNAME.toPrefixString(),
              null,
              "true");
        }
        if (valueDataType != null) {
          attrs.addAttribute(
              NamespaceService.REPOSITORY_VIEW_PREFIX,
              DATATYPE_LOCALNAME,
              DATATYPE_QNAME.toPrefixString(),
              null,
              toPrefixString(valueDataType));
        }
        contentHandler.startElement(
            NamespaceService.REPOSITORY_VIEW_PREFIX,
            VALUE_LOCALNAME,
            toPrefixString(VALUE_QNAME),
            attrs);
      }

      // output value
      String strValue = (String) DefaultTypeConverter.INSTANCE.convert(String.class, value);
      if (strValue != null) {
        for (int i = 0; i < strValue.length(); i++) {
          char[] temp = new char[] {strValue.charAt(i)};
          contentHandler.characters(temp, 0, 1);
        }
      }

      // output value wrapper if property data type is any
      if (value == null || valueDataType != null || index != -1) {
        contentHandler.endElement(
            NamespaceService.REPOSITORY_VIEW_PREFIX, VALUE_LOCALNAME, toPrefixString(VALUE_QNAME));
      }
    } catch (SAXException e) {
      throw new ExporterException(
          "Failed to process value event - nodeRef "
              + nodeRef
              + "; property "
              + toPrefixString(property)
              + "; value "
              + value,
          e);
    }
  }
  /* (non-Javadoc)
   * @see org.alfresco.service.cmr.view.Exporter#start()
   */
  public void start(ExporterContext context) {
    try {
      this.context = context;
      contentHandler.startDocument();
      contentHandler.startPrefixMapping(
          NamespaceService.REPOSITORY_VIEW_PREFIX, NamespaceService.REPOSITORY_VIEW_1_0_URI);
      contentHandler.startElement(
          NamespaceService.REPOSITORY_VIEW_PREFIX,
          VIEW_LOCALNAME,
          VIEW_QNAME.toPrefixString(),
          EMPTY_ATTRIBUTES);

      //
      // output metadata
      //
      contentHandler.startElement(
          NamespaceService.REPOSITORY_VIEW_PREFIX,
          METADATA_LOCALNAME,
          METADATA_QNAME.toPrefixString(),
          EMPTY_ATTRIBUTES);

      // exported by
      contentHandler.startElement(
          NamespaceService.REPOSITORY_VIEW_PREFIX,
          EXPORTEDBY_LOCALNAME,
          EXPORTEDBY_QNAME.toPrefixString(),
          EMPTY_ATTRIBUTES);
      contentHandler.characters(
          context.getExportedBy().toCharArray(), 0, context.getExportedBy().length());
      contentHandler.endElement(
          NamespaceService.REPOSITORY_VIEW_PREFIX,
          EXPORTEDBY_LOCALNAME,
          EXPORTEDBY_QNAME.toPrefixString());

      // exported date
      contentHandler.startElement(
          NamespaceService.REPOSITORY_VIEW_PREFIX,
          EXPORTEDDATE_LOCALNAME,
          EXPORTEDDATE_QNAME.toPrefixString(),
          EMPTY_ATTRIBUTES);
      String date = DefaultTypeConverter.INSTANCE.convert(String.class, context.getExportedDate());
      contentHandler.characters(date.toCharArray(), 0, date.length());
      contentHandler.endElement(
          NamespaceService.REPOSITORY_VIEW_PREFIX,
          EXPORTEDDATE_LOCALNAME,
          EXPORTEDDATE_QNAME.toPrefixString());

      // exporter version
      contentHandler.startElement(
          NamespaceService.REPOSITORY_VIEW_PREFIX,
          EXPORTERVERSION_LOCALNAME,
          EXPORTERVERSION_QNAME.toPrefixString(),
          EMPTY_ATTRIBUTES);
      contentHandler.characters(
          context.getExporterVersion().toCharArray(), 0, context.getExporterVersion().length());
      contentHandler.endElement(
          NamespaceService.REPOSITORY_VIEW_PREFIX,
          EXPORTERVERSION_LOCALNAME,
          EXPORTERVERSION_QNAME.toPrefixString());

      // export of
      contentHandler.startElement(
          NamespaceService.REPOSITORY_VIEW_PREFIX,
          EXPORTOF_LOCALNAME,
          EXPORTOF_QNAME.toPrefixString(),
          EMPTY_ATTRIBUTES);
      String path = nodeService.getPath(context.getExportOf()).toPrefixString(namespaceService);
      contentHandler.characters(path.toCharArray(), 0, path.length());
      contentHandler.endElement(
          NamespaceService.REPOSITORY_VIEW_PREFIX,
          EXPORTOF_LOCALNAME,
          EXPORTOF_QNAME.toPrefixString());

      contentHandler.endElement(
          NamespaceService.REPOSITORY_VIEW_PREFIX,
          METADATA_LOCALNAME,
          METADATA_QNAME.toPrefixString());
    } catch (SAXException e) {
      throw new ExporterException("Failed to process export start event", e);
    }
  }