Example usage for org.w3c.dom Element setAttribute

List of usage examples for org.w3c.dom Element setAttribute

Introduction

In this page you can find the example usage for org.w3c.dom Element setAttribute.

Prototype

public void setAttribute(String name, String value) throws DOMException;

Source Link

Document

Adds a new attribute.

Usage

From source file:com.twinsoft.convertigo.engine.util.ProjectUtils.java

private static <E extends DatabaseObject> void addElement(Collection<E> collection, Document document,
        Element root, ExportOption... exportOptions) throws EngineException {
    for (E dbo : collection) {
        Element tag = dbo.toXml(document, exportOptions);
        tag.setAttribute("qname", dbo.getQName());
        root.appendChild(tag);//w  w w.jav  a2  s  .  c o  m
        constructDom(document, tag, dbo, exportOptions);
    }
}

From source file:com.connexta.arbitro.utils.PolicyUtils.java

/**
 * This creates XML representation of obligation Element  using List of ObligationElementDTO object
 *
 * @param obligationElementDTOs List of ObligationElementDTO
 * @param doc Document/*w  w  w.  j  a va 2 s .c om*/
 * @return DOM element
 * @throws PolicyBuilderException throws
 */
public static Element createObligationsElement(List<ObligationElementDTO> obligationElementDTOs, Document doc)
        throws PolicyBuilderException {

    Element obligationExpressions = null;

    if (obligationElementDTOs != null && obligationElementDTOs.size() > 0) {

        for (ObligationElementDTO dto : obligationElementDTOs) {
            String id = dto.getId();
            String effect = dto.getEffect();

            if (id != null && id.trim().length() > 0 && effect != null) {
                if (obligationExpressions == null) {
                    obligationExpressions = doc.createElement(PolicyConstants.OBLIGATION_EXPRESSIONS);
                }
                Element obligationExpression = doc.createElement(PolicyConstants.OBLIGATION_EXPRESSION);
                obligationExpression.setAttribute(PolicyConstants.OBLIGATION_ID, id);
                obligationExpression.setAttribute(PolicyConstants.OBLIGATION_EFFECT, effect);
                List<AttributeAssignmentElementDTO> elementDTOs = dto.getAssignmentElementDTOs();
                if (elementDTOs != null) {
                    for (AttributeAssignmentElementDTO elementDTO : elementDTOs) {
                        Element element = createAttributeAssignmentElement(elementDTO, doc);
                        if (element != null) {
                            obligationExpression.appendChild(element);
                        }
                    }
                }
                obligationExpressions.appendChild(obligationExpression);
            }
        }
    }

    return obligationExpressions;
}

From source file:com.twinsoft.convertigo.engine.util.ProjectUtils.java

public static void getFullProjectDOM(Document document, String projectName, ExportOption... exportOptions)
        throws TransformerFactoryConfigurationError, EngineException, TransformerException {
    Element root = document.getDocumentElement();

    Project project = Engine.theApp.databaseObjectsManager.getOriginalProjectByName(projectName);
    Element projectTag = project.toXml(document, exportOptions);
    projectTag.setAttribute("qname", project.getQName());
    root.appendChild(projectTag);/*from ww  w .j a v  a  2  s .  c  om*/

    constructDom(document, projectTag, project);
}

From source file:com.microfocus.application.automation.tools.octane.executor.TestExecutionJobCreatorService.java

private static String prepareMtbxData(List<TestExecutionInfo> tests) throws IOException {
    /*<Mtbx>//ww w.j  a v  a  2 s. c  o m
    <Test name="test1" path="${WORKSPACE}\${CHECKOUT_SUBDIR}\APITest1">
     <Parameter name="A" value="abc" type="string"/>
     <DataTable path="${WORKSPACE}\aa\bbb.xslx"/>
      .
     </Test>
     <Test name="test2" path="${WORKSPACE}\${CHECKOUT_SUBDIR}\test2">
    <Parameter name="p1" value="123" type="int"/>
    <Parameter name="p4" value="123.4" type="float"/>
     .
     </Test>
    </Mtbx>*/

    try {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.newDocument();
        Element rootElement = doc.createElement("Mtbx");
        doc.appendChild(rootElement);

        for (TestExecutionInfo test : tests) {
            Element testElement = doc.createElement("Test");
            String packageAndTestName = (StringUtils.isNotEmpty(test.getPackageName())
                    ? test.getPackageName() + "\\"
                    : "") + test.getTestName();
            testElement.setAttribute("name", packageAndTestName);
            String path = "${WORKSPACE}\\${CHECKOUT_SUBDIR}"
                    + (StringUtils.isEmpty(test.getPackageName()) ? ""
                            : SdkConstants.FileSystem.WINDOWS_PATH_SPLITTER + test.getPackageName())
                    + SdkConstants.FileSystem.WINDOWS_PATH_SPLITTER + test.getTestName();
            testElement.setAttribute("path", path);

            if (StringUtils.isNotEmpty(test.getDataTable())) {
                Element dataTableElement = doc.createElement("DataTable");
                dataTableElement.setAttribute("path", "${WORKSPACE}\\${CHECKOUT_SUBDIR}"
                        + SdkConstants.FileSystem.WINDOWS_PATH_SPLITTER + test.getDataTable());
                testElement.appendChild(dataTableElement);
            }

            rootElement.appendChild(testElement);
        }

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));

        return writer.toString();
    } catch (Exception e) {
        throw new IOException("Failed to build MTBX content : " + e.getMessage());
    }

}

From source file:Main.java

public static Element getElement(Document document, String[] path, String value, String translate,
        String module) {/*from w ww.  j  a v a  2s. c  o m*/
    Node node = document;
    NodeList list;
    for (int i = 0; i < path.length; ++i) {
        list = node.getChildNodes();
        boolean found = false;
        for (int j = 0; j < list.getLength(); ++j) {
            Node testNode = list.item(j);
            if (testNode.getNodeName().equals(path[i])) {
                found = true;
                node = testNode;
                break;
            }
        }
        if (found == false) {
            Element element = document.createElement(path[i]);
            node.appendChild(element);
            node = element;
        }
    }
    if (value != null) {
        Text text = document.createTextNode(value);
        list = node.getChildNodes();
        boolean found = false;
        for (int j = 0; j < list.getLength(); ++j) {
            Node testNode = list.item(j);
            if (testNode instanceof Text) {
                node.replaceChild(text, testNode);
                found = true;
                break;
            }
        }
        if (!found)
            node.appendChild(text);
    }
    if (node instanceof Element) {
        Element element = (Element) node;
        if (translate != null && element.getAttribute("translate").equals("")) {
            element.setAttribute("translate", translate);
        }
        if (module != null && element.getAttribute("module").equals("")) {
            element.setAttribute("module", module);
        }
    }
    return (Element) node;
}

From source file:com.connexta.arbitro.utils.PolicyUtils.java

/**
 * This method creates a policy element of the XACML policy
 * @param policyElementDTO  policy element data object
 * @param doc XML document//w  ww  .  ja  va2s .  c om
 * @return policyElement
 * @throws PolicyBuilderException if
 */

public static Element createPolicyElement(PolicyElementDTO policyElementDTO, Document doc)
        throws PolicyBuilderException {

    Element policyElement = doc.createElement(PolicyConstants.POLICY_ELEMENT);

    policyElement.setAttribute("xmlns", PolicyConstants.XACMLData.XACML3_POLICY_NAMESPACE);

    if (policyElementDTO.getPolicyName() != null && policyElementDTO.getPolicyName().trim().length() > 0) {
        policyElement.setAttribute(PolicyConstants.POLICY_ID, policyElementDTO.getPolicyName());
    } else {
        throw new PolicyBuilderException("Policy name can not be null");
    }

    if (policyElementDTO.getRuleCombiningAlgorithms() != null
            && policyElementDTO.getRuleCombiningAlgorithms().trim().length() > 0) {
        policyElement.setAttribute(PolicyConstants.RULE_ALGORITHM,
                policyElementDTO.getRuleCombiningAlgorithms());
    } else {
        policyElement.setAttribute(PolicyConstants.RULE_ALGORITHM,
                PolicyConstants.RuleCombiningAlog.DENY_OVERRIDE_ID); // TODO
        log.warn("Rule combining algorithm is not defined. Use default algorithm; Deny Override");
    }

    if (policyElementDTO.getVersion() != null && policyElementDTO.getVersion().trim().length() > 0) {
        policyElement.setAttribute(PolicyConstants.POLICY_VERSION, policyElementDTO.getVersion());
    } else {
        // policy version is can be handled by policy registry.  therefore we can ignore it, although it
        // is a required attribute
        policyElement.setAttribute(PolicyConstants.POLICY_VERSION, "1.0");
    }

    if (policyElementDTO.getPolicyDescription() != null
            && policyElementDTO.getPolicyDescription().trim().length() > 0) {

        Element descriptionElement = doc.createElement(PolicyConstants.DESCRIPTION_ELEMENT);
        descriptionElement.setTextContent(policyElementDTO.getPolicyDescription());
        policyElement.appendChild(descriptionElement);
    }

    TargetElementDTO targetElementDTO = policyElementDTO.getTargetElementDTO();
    List<RuleElementDTO> ruleElementDTOs = policyElementDTO.getRuleElementDTOs();
    List<ObligationElementDTO> obligationElementDTOs = policyElementDTO.getObligationElementDTOs();

    if (targetElementDTO != null) {
        policyElement.appendChild(createTargetElement(targetElementDTO, doc));
    } else {
        policyElement.appendChild(doc.createElement(PolicyConstants.TARGET_ELEMENT));
    }

    if (ruleElementDTOs != null && ruleElementDTOs.size() > 0) {
        for (RuleElementDTO ruleElementDTO : ruleElementDTOs) {
            policyElement.appendChild(createRuleElement(ruleElementDTO, doc));
        }
    } else {
        RuleElementDTO ruleElementDTO = new RuleElementDTO();
        ruleElementDTO.setRuleId(UUID.randomUUID().toString());
        ruleElementDTO.setRuleEffect(PolicyConstants.RuleEffect.DENY);
        policyElement.appendChild(createRuleElement(ruleElementDTO, doc));
    }

    if (obligationElementDTOs != null && obligationElementDTOs.size() > 0) {
        List<ObligationElementDTO> obligations = new ArrayList<ObligationElementDTO>();
        List<ObligationElementDTO> advices = new ArrayList<ObligationElementDTO>();
        for (ObligationElementDTO obligationElementDTO : obligationElementDTOs) {
            if (obligationElementDTO.getType() == ObligationElementDTO.ADVICE) {
                advices.add(obligationElementDTO);
            } else {
                obligations.add(obligationElementDTO);
            }
        }
        Element obligation = createObligationsElement(obligations, doc);
        Element advice = createAdvicesElement(advices, doc);
        if (obligation != null) {
            policyElement.appendChild(obligation);
        }
        if (advice != null) {
            policyElement.appendChild(advice);
        }
    }

    return policyElement;
}

From source file:com.connexta.arbitro.utils.PolicyUtils.java

/**
 * This method creates a policy set element of the XACML policy
 * @param policyElementDTO  policy element data object
 * @param doc XML document// w  w  w . j a  va 2 s .  c  o m
 * @return policyElement
 * @throws PolicyBuilderException if
 */

public static Element createPolicySetElement(PolicySetElementDTO policyElementDTO, Document doc)
        throws PolicyBuilderException {

    Element policyElement = doc.createElement(PolicyConstants.POLICY_SET_ELEMENT);

    policyElement.setAttribute("xmlns", PolicyConstants.XACMLData.XACML3_POLICY_NAMESPACE);

    if (policyElementDTO.getPolicySetId() != null && policyElementDTO.getPolicySetId().trim().length() > 0) {
        policyElement.setAttribute(PolicyConstants.POLICY_SET_ID, policyElementDTO.getPolicySetId());
    } else {
        throw new PolicyBuilderException("Policy name can not be null");
    }

    if (policyElementDTO.getPolicyCombiningAlgId() != null
            && policyElementDTO.getPolicyCombiningAlgId().trim().length() > 0) {
        policyElement.setAttribute(PolicyConstants.POLICY_ALGORITHM,
                policyElementDTO.getPolicyCombiningAlgId());
    } else {
        policyElement.setAttribute(PolicyConstants.POLICY_ALGORITHM,
                PolicyConstants.PolicyCombiningAlog.DENY_OVERRIDE_ID); // TODO
        log.warn("Rule combining algorithm is not defined. Use default algorithm; Deny Override");
    }

    if (policyElementDTO.getVersion() != null && policyElementDTO.getVersion().trim().length() > 0) {
        policyElement.setAttribute(PolicyConstants.POLICY_VERSION, policyElementDTO.getVersion());
    } else {
        // policy version is can be handled by policy registry.  therefore we can ignore it, although it
        // is a required attribute
        policyElement.setAttribute(PolicyConstants.POLICY_VERSION, "1.0");
    }

    if (policyElementDTO.getDescription() != null && policyElementDTO.getDescription().trim().length() > 0) {

        Element descriptionElement = doc.createElement(PolicyConstants.DESCRIPTION_ELEMENT);
        descriptionElement.setTextContent(policyElementDTO.getDescription());
        policyElement.appendChild(descriptionElement);
    }

    TargetElementDTO targetElementDTO = policyElementDTO.getTargetElementDTO();
    List<ObligationElementDTO> obligationElementDTOs = policyElementDTO.getObligationElementDTOs();

    if (targetElementDTO != null) {
        policyElement.appendChild(createTargetElement(targetElementDTO, doc));
    } else {
        policyElement.appendChild(doc.createElement(PolicyConstants.TARGET_ELEMENT));
    }

    List<String> policySets = policyElementDTO.getPolicySets();
    if (policySets != null && policySets.size() > 0) {
        // TODO 
    }

    List<String> policies = policyElementDTO.getPolicies();
    if (policies != null && policies.size() > 0) {
        // TODO    
    }

    List<String> policySetIds = policyElementDTO.getPolicySetIdReferences();
    if (policySetIds != null && policySetIds.size() > 0) {
        for (String policySetId : policySetIds) {
            Element element = doc.createElement(PolicyConstants.POLICY_SET_ID_REFERENCE_ELEMENT);
            element.setTextContent(policySetId);
            policyElement.appendChild(element);
        }
    }

    List<String> policyIds = policyElementDTO.getPolicyIdReferences();
    if (policyIds != null && policyIds.size() > 0) {
        for (String policyId : policyIds) {
            Element element = doc.createElement(PolicyConstants.POLICY_ID_REFERENCE_ELEMENT);
            element.setTextContent(policyId);
            policyElement.appendChild(element);
        }
    }

    if (obligationElementDTOs != null && obligationElementDTOs.size() > 0) {
        List<ObligationElementDTO> obligations = new ArrayList<ObligationElementDTO>();
        List<ObligationElementDTO> advices = new ArrayList<ObligationElementDTO>();
        for (ObligationElementDTO obligationElementDTO : obligationElementDTOs) {
            if (obligationElementDTO.getType() == ObligationElementDTO.ADVICE) {
                advices.add(obligationElementDTO);
            } else {
                obligations.add(obligationElementDTO);
            }
        }
        Element obligation = createObligationsElement(obligations, doc);
        Element advice = createAdvicesElement(advices, doc);
        if (obligation != null) {
            policyElement.appendChild(obligation);
        }
        if (advice != null) {
            policyElement.appendChild(advice);
        }
    }

    return policyElement;
}

From source file:com.connexta.arbitro.utils.PolicyUtils.java

/**
 * This creates XML representation of function Element using FunctionElementDTO object
 *
 * @param functionElementDTO  FunctionElementDTO
 * @param doc Document//from   w  w w  .j a  v  a  2s.  c om
 * @return DOM element
 */
public static Element createFunctionElement(FunctionElementDTO functionElementDTO, Document doc) {

    Element functionElement = doc.createElement(PolicyConstants.FUNCTION);

    if (functionElementDTO.getFunctionId() != null && functionElementDTO.getFunctionId().trim().length() > 0) {
        functionElement.setAttribute(PolicyConstants.FUNCTION_ID, functionElementDTO.getFunctionId());
    }

    return functionElement;
}

From source file:com.connexta.arbitro.utils.PolicyUtils.java

/**
 * This creates XML representation of rule element using RuleElementDTO object
 *
 * @param ruleElementDTO RuleElementDTO/*from   w  ww  .j  a v  a2s.  c om*/
 * @param doc Document
 * @return DOM element
 * @throws PolicyBuilderException throws
 */
public static Element createRuleElement(RuleElementDTO ruleElementDTO, Document doc)
        throws PolicyBuilderException {

    TargetElementDTO targetElementDTO = ruleElementDTO.getTargetElementDTO();
    ConditionElementDT0 conditionElementDT0 = ruleElementDTO.getConditionElementDT0();
    List<ObligationElementDTO> obligationElementDTOs = ruleElementDTO.getObligationElementDTOs();

    Element ruleElement = doc.createElement(PolicyConstants.RULE_ELEMENT);

    if (ruleElementDTO.getRuleId() != null && ruleElementDTO.getRuleId().trim().length() > 0) {
        ruleElement.setAttribute(PolicyConstants.RULE_ID, ruleElementDTO.getRuleId());
    }

    if (ruleElementDTO.getRuleEffect() != null && ruleElementDTO.getRuleEffect().trim().length() > 0) {
        ruleElement.setAttribute(PolicyConstants.RULE_EFFECT, ruleElementDTO.getRuleEffect());
    }

    if (ruleElementDTO.getRuleDescription() != null
            && ruleElementDTO.getRuleDescription().trim().length() > 0) {
        Element descriptionElement = doc.createElement(PolicyConstants.DESCRIPTION_ELEMENT);
        descriptionElement.setTextContent(ruleElementDTO.getRuleDescription());
        ruleElement.appendChild(descriptionElement);
    }

    if (targetElementDTO != null) {
        Element targetElement = createTargetElement(targetElementDTO, doc);
        ruleElement.appendChild(targetElement);
    }

    if (conditionElementDT0 != null) {
        ruleElement.appendChild(createConditionElement(conditionElementDT0, doc));
    }

    if (obligationElementDTOs != null && obligationElementDTOs.size() > 0) {
        List<ObligationElementDTO> obligations = new ArrayList<ObligationElementDTO>();
        List<ObligationElementDTO> advices = new ArrayList<ObligationElementDTO>();
        for (ObligationElementDTO obligationElementDTO : obligationElementDTOs) {
            if (obligationElementDTO.getType() == ObligationElementDTO.ADVICE) {
                advices.add(obligationElementDTO);
            } else {
                obligations.add(obligationElementDTO);
            }
        }
        Element obligation = createObligationsElement(obligations, doc);
        Element advice = createAdvicesElement(advices, doc);
        if (obligation != null) {
            ruleElement.appendChild(obligation);
        }
        if (advice != null) {
            ruleElement.appendChild(advice);
        }
    }

    return ruleElement;
}

From source file:com.connexta.arbitro.utils.PolicyUtils.java

/**
 * This creates XML representation of apply element using ApplyElementDTO object
 *
 * @param applyElementDTO ApplyElementDTO
 * @param doc Document/*  ww  w.  j a  v a 2 s.  c  o  m*/
 * @return DOM element
 * @throws PolicyBuilderException throws
 */
public static Element createApplyElement(ApplyElementDTO applyElementDTO, Document doc)
        throws PolicyBuilderException {

    Element applyElement = doc.createElement(PolicyConstants.APPLY_ELEMENT);

    if (applyElementDTO.getFunctionId() != null && applyElementDTO.getFunctionId().trim().length() > 0) {
        applyElement.setAttribute(PolicyConstants.FUNCTION_ID, applyElementDTO.getFunctionId());
    }

    if (applyElementDTO.getFunctionFunctionId() != null
            && applyElementDTO.getFunctionFunctionId().trim().length() > 0) {
        FunctionElementDTO functionElementDTO = new FunctionElementDTO();
        functionElementDTO.setFunctionId(applyElementDTO.getFunctionFunctionId());
        Element functionElement = createFunctionElement(functionElementDTO, doc);
        applyElement.appendChild(functionElement);
    }

    List<ApplyElementDTO> applyElementDTOs = applyElementDTO.getApplyElements();

    if (applyElementDTOs != null && applyElementDTOs.size() > 0) {

        for (ApplyElementDTO elementDTO : applyElementDTOs) {
            Element subApplyElement = createApplyElement(elementDTO, doc);
            applyElement.appendChild(subApplyElement);
        }
    }

    List<AttributeValueElementDTO> attributeValueElementDTOs = applyElementDTO.getAttributeValueElementDTOs();
    if (attributeValueElementDTOs != null && attributeValueElementDTOs.size() > 0) {

        for (AttributeValueElementDTO attributeValueElementDTO : attributeValueElementDTOs) {
            Element attributeValueElement = createAttributeValueElement(attributeValueElementDTO, doc);

            applyElement.appendChild(attributeValueElement);
        }
    }

    List<AttributeDesignatorDTO> attributeDesignatorDTOs = applyElementDTO.getAttributeDesignators();
    if (attributeDesignatorDTOs != null && attributeDesignatorDTOs.size() > 0) {

        for (AttributeDesignatorDTO attributeDesignatorDTO : attributeDesignatorDTOs) {
            Element attributeDesignatorElement = createAttributeDesignatorElement(attributeDesignatorDTO, doc);
            applyElement.appendChild(attributeDesignatorElement);
        }
    }

    List<AttributeSelectorDTO> attributeSelectorDTOs = applyElementDTO.getAttributeSelectors();
    if (attributeSelectorDTOs != null && attributeSelectorDTOs.size() > 0) {

        for (AttributeSelectorDTO attributeSelectorDTO : attributeSelectorDTOs) {
            Element attributeSelectorElement = createAttributeSelectorElement(attributeSelectorDTO, doc);
            applyElement.appendChild(attributeSelectorElement);
        }
    }
    return applyElement;
}