Home Manual Reference Source Test

src/lib/NodeAddress.js

import { replaceExtension } from 'gulp-util';

const ExtensionFromVariableType = 0;

const ExtensionForDataType = {
  ByteString: ExtensionFromVariableType,
  XmlElement: 'xml',
  Boolean: 'bool',
};

const ExtensionForVariableType = {
  'VariableTypes.ATVISE.HtmlHelp': 'html',
};

/**
 * A helper class used to translate database addresses to file paths.
 */
export default class NodeAddress {

  // Get extension
  // If the dataType has his own extension, use it
  // Else if the variableType has his own extension, use it
  // Otherwise use the extension from the nodeId
  /**
   * Returns the extension to use for a database node.
   * @param {Object} dataType The node dataType
   * @param {Object} typeDefinition The node typeDefinition
   * @return {String} The extension to use for this type of node.
   */
  static extensionFor(dataType, typeDefinition) {
    const extType = ExtensionForDataType[dataType];
    let extension = extType === undefined ?
      dataType.toLowerCase() : extType;

    if (extension === ExtensionFromVariableType) {
      const ext = ExtensionForVariableType[typeDefinition];
      if (ext) {
        extension = ext;
      } else {
        extension = false; // Use extension from node path
      }
    }

    return extension;
  }

  /**
   * Returns a file path for a node
   * @param {Object} referenceDescription The node's reference description from atvise-server.
   * @param {Object} readResults The node's read results from atvise-server.
   * @returns {String} The node's file path
   */
  static toFilePath(referenceDescription, readResults) {
    const typeDef = referenceDescription.typeDefinition.value;
    const nodePath = referenceDescription.nodeId.value;
    const dataType = readResults.value.dataType.key;
    const isArray = readResults.value.arrayType.value === 1;

    let extension = NodeAddress.extensionFor(dataType, typeDef);
    if (isArray) {
      if (extension) {
        extension += '.array';
      } else {
        extension = 'array';
      }
    }

    // Create file path
    // node paths are joined by a '.' char
    // The only exception are Resource paths, where everything after the 'RESOURCES' component
    // is joined by '/'
    const parts = nodePath.split('RESOURCES');

    parts[0] = parts[0].replace(/\./g, '/');

    const result = parts.join('RESOURCES');
    if (extension !== false) {
      return `${result}.${extension}`;
    }

    return result;
  }

  /**
   * Returns a node path for an {@link AtviseFile}.
   * @param {AtviseFile} file The file to get the path for.
   * @return {String} The file's node path
   */
  static getNodePath(file) {
    const typeDef = file.rc.typeDefinition;
    const dataType = file.rc.dataType;
    const extension = NodeAddress.extensionFor(dataType, typeDef);

    let path = file.relative;
    // Remove .array extension
    if (file.rc.array) {
      path = replaceExtension(path, '');
    }

    if (extension !== false) {
      path = replaceExtension(path, '');
    }

    const parts = path.split('RESOURCES');
    parts[0] = parts[0].replace(/(\/|\\)/g, '.');
    if (parts.length > 1) {
      parts[1] = parts[1].replace(/\\/g, '/');
    }

    return parts.join('RESOURCES');
  }

}