/** * 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); } }
/** * Select's the new named policy element in the tree. * * @param newPolicyName the name of the new policy to select. */ private void selectNewPolicy(final String newPolicyName) { // Create the XPath for selecting the new policy element just created // from the definitions document. final StringBuffer newPolicyXPathBuffer = new StringBuffer(); newPolicyXPathBuffer .append("//") .append(MCSNamespace.DEVICE_DEFINITIONS.getPrefix()) .append(':') .append(DeviceRepositorySchemaConstants.POLICY_ELEMENT_NAME) .append("[@") .append(DeviceRepositorySchemaConstants.POLICY_NAME_ATTRIBUTE) .append("=\"") .append(newPolicyName) .append("\"]"); final XPath newPolicyXPath = new XPath( newPolicyXPathBuffer.toString(), new Namespace[] {MCSNamespace.DEVICE_DEFINITIONS}); Element newPolicyElement = null; try { // Get the definitions root for the XPath search. final Element definitionsRoot = context .getDeviceRepositoryAccessorManager() .getDeviceDefinitionsDocument() .getRootElement(); // Retrieve the new policy element. newPolicyElement = newPolicyXPath.selectSingleElement(definitionsRoot); } catch (XPathException e) { EclipseCommonPlugin.handleError(ABPlugin.getDefault(), e); } TreeViewer treeViewer = categoriesComposite.getTreeViewer(); // Expand the tree to the new policy element's level and select it. treeViewer.expandToLevel(newPolicyElement, 1); treeViewer.setSelection(new StructuredSelection(newPolicyElement), true); }