/**
   * Add a new policy to the device repository in use. This will add the policy to the definitions
   * document and to the master device file.
   *
   * @param policyName the name of the new policy. Cannot be null.
   * @param composition the PolicyTypeComposition of the new policy
   * @param type the PolicyType of the new policy
   * @throws IllegalArgumentException if the named policy already exists or if any of the arguments
   *     are null.
   */
  private void addNewPolicyToRepository(
      String policyName, PolicyTypeComposition composition, PolicyType type) {
    if (policyName == null) {
      throw new IllegalArgumentException("Cannot be null: " + policyName);
    }
    if (composition == null) {
      throw new IllegalArgumentException("Cannot be null: " + composition);
    }
    if (type == null) {
      throw new IllegalArgumentException("Cannot be null: " + type);
    }
    String masterDeviceName = context.getDeviceRepositoryAccessorManager().retrieveRootDeviceName();
    boolean policyExists =
        context.getDeviceRepositoryAccessorManager().retrievePolicy(masterDeviceName, policyName)
            != null;

    if (policyExists) {
      throw new IllegalArgumentException(
          "Policy " + policyName + " already exists. Aborting new policy.");
    }

    // Add the policy to the definitions document
    Element category = categoriesComposite.getSelectedCategoryElement();
    Element policy =
        context
            .getODOMFactory()
            .element(DeviceRepositorySchemaConstants.POLICY_ELEMENT_NAME, category.getNamespace());
    policy.setAttribute(DeviceRepositorySchemaConstants.POLICY_NAME_ATTRIBUTE, policyName);
    category.addContent(policy);
    composition.addTypeElement(policy, type, context.getODOMFactory());

    // Add the policy to the master device
    Element masterDevice =
        context.getDeviceRepositoryAccessorManager().retrieveDeviceElement(masterDeviceName);

    StringBuffer xPathBuffer = new StringBuffer();
    xPathBuffer
        .append("//")
        . //$NON-NLS-1$
        append(MCSNamespace.DEVICE.getPrefix())
        .append(':')
        .append(DeviceRepositorySchemaConstants.POLICIES_ELEMENT_NAME);
    XPath policiesXPath = new XPath(xPathBuffer.toString(), new Namespace[] {MCSNamespace.DEVICE});

    try {
      Element policies = policiesXPath.selectSingleElement(masterDevice);
      composition.addDefaultPolicyValue(
          policies,
          policyName,
          type,
          context.getODOMFactory(),
          context.getDeviceRepositoryAccessorManager());
    } catch (XPathException e) {
      EclipseCommonPlugin.handleError(ABPlugin.getDefault(), e);
    }
  }
 /**
  * Get the name of the selected category. If a policy is selected then this method will return the
  * name of the category that the policy belongs to.
  *
  * @return the selected category name or null if no category or policy is selected.
  */
 public String getSelectedCategoryName() {
   Element category = categoriesComposite.getSelectedCategoryElement();
   return (category == null)
       ? null
       : category.getAttributeValue(DeviceRepositorySchemaConstants.CATEGORY_NAME_ATTRIBUTE);
 }