Example usage for org.w3c.dom Element setPrefix

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

Introduction

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

Prototype

public void setPrefix(String prefix) throws DOMException;

Source Link

Document

The namespace prefix of this node, or null if it is unspecified.

Usage

From source file:org.ojbc.util.xml.subscription.SubscriptionNotificationDocumentBuilderUtils.java

private static Element getRootSubscribeElement(Document responseDoc) {

    Element root = responseDoc.createElementNS(OjbcNamespaceContext.NS_B2, "Subscribe");

    responseDoc.appendChild(root);/*from   w  ww  . ja  va  2 s  .  co  m*/
    root.setPrefix(OjbcNamespaceContext.NS_PREFIX_B2);

    return root;
}

From source file:org.ojbc.util.xml.subscription.SubscriptionNotificationDocumentBuilderUtils.java

public static Document createUnubscriptionRequest(Unsubscription unsubscription) throws Exception {

    String subscriptionIdentificationId = unsubscription.getSubscriptionId();
    boolean containsSubjectData = ((StringUtils.isNotBlank(unsubscription.getFirstName()))
            || (StringUtils.isNotBlank(unsubscription.getLastName()))
            || (StringUtils.isNotBlank(unsubscription.getSid())) || (unsubscription.getDateOfBirth() != null));

    if (containsSubjectData && StringUtils.isNotEmpty(subscriptionIdentificationId)) {
        throw new Exception(
                "Unsubscription message can have either subject data or Subscription Identification, but not both.");
    }//  w  w  w.j a  v  a2  s  . c  o  m

    Document doc = OJBCXMLUtils.createDocument();
    Element root = doc.createElementNS(OjbcNamespaceContext.NS_B2, "Unsubscribe");
    doc.appendChild(root);
    root.setPrefix(OjbcNamespaceContext.NS_PREFIX_B2);

    Element unsubscriptionMessage = XmlUtils.appendElement(root, OjbcNamespaceContext.NS_UNBSUB_MSG_EXCHANGE,
            "UnsubscriptionMessage");

    //        <smext:Subject>
    //           <nc20:PersonBirthDate>
    //               <nc20:Date>1998-01-11</nc20:Date>
    //           </nc20:PersonBirthDate>
    //           <nc20:PersonName>
    //               <nc20:PersonGivenName>John</nc20:PersonGivenName>
    //               <nc20:PersonSurName>Doe</nc20:PersonSurName>
    //           </nc20:PersonName>
    //           <jxdm41:PersonAugmentation>
    //              <jxdm41:PersonStateFingerprintIdentification>
    //                 <nc20:IdentificationID>A9999999</nc20:IdentificationID>
    //              </jxdm41:PersonStateFingerprintIdentification>
    //           </jxdm41:PersonAugmentation>        
    //       </smext:Subject>

    if (containsSubjectData) {
        Element subject = XmlUtils.appendElement(unsubscriptionMessage, OjbcNamespaceContext.NS_SUB_MSG_EXT,
                "Subject");

        if ((unsubscription.getDateOfBirth() != null)) {
            Element dob = XmlUtils.appendElement(subject, OjbcNamespaceContext.NS_NC, "PersonBirthDate");
            Element dobDate = XmlUtils.appendElement(dob, OjbcNamespaceContext.NS_NC, "Date");
            dobDate.setTextContent(unsubscription.getDateOfBirth().toString());
        }

        if (StringUtils.isNotBlank(unsubscription.getFirstName())
                || StringUtils.isNotBlank(unsubscription.getLastName())) {
            Element personName = XmlUtils.appendElement(subject, OjbcNamespaceContext.NS_NC, "PersonName");

            if (StringUtils.isNotBlank(unsubscription.getFirstName())) {
                Element personGivenName = XmlUtils.appendElement(personName, OjbcNamespaceContext.NS_NC,
                        "PersonGivenName");
                personGivenName.setTextContent(unsubscription.getFirstName());

            }

            if (StringUtils.isNotBlank(unsubscription.getLastName())) {
                Element personSurName = XmlUtils.appendElement(personName, OjbcNamespaceContext.NS_NC,
                        "PersonSurName");
                personSurName.setTextContent(unsubscription.getLastName());

            }

        }

        if (StringUtils.isNotBlank(unsubscription.getSid())) {
            Element personAugmentation = XmlUtils.appendElement(subject, OjbcNamespaceContext.NS_JXDM_41,
                    "PersonAugmentation");

            Element personStateFingerprintIdentification = XmlUtils.appendElement(personAugmentation,
                    OjbcNamespaceContext.NS_JXDM_41, "PersonStateFingerprintIdentification");

            Element identificationID = XmlUtils.appendElement(personStateFingerprintIdentification,
                    OjbcNamespaceContext.NS_NC, "IdentificationID");
            identificationID.setTextContent(unsubscription.getSid());

        }

    }

    //       <nc20:ContactEmailID>william.francis@maine.gov</nc20:ContactEmailID>
    //       <smext:SystemName>{http://maine.gov/ProbationCase/1.0}MaineDOC</smext:SystemName>
    //       <smext:SubscriptionQualifierIdentification>
    //           <nc20:IdentificationID>128799</nc20:IdentificationID>
    //       </smext:SubscriptionQualifierIdentification>
    if (unsubscription.getEmailAddresses() != null) {
        for (String emailAddress : unsubscription.getEmailAddresses()) {
            Element contactEmailID = XmlUtils.appendElement(unsubscriptionMessage, OjbcNamespaceContext.NS_NC,
                    "ContactEmailID");
            contactEmailID.setTextContent(emailAddress);
        }
    }

    if (StringUtils.isNotBlank(unsubscription.getSystemName())) {
        Element systemName = XmlUtils.appendElement(unsubscriptionMessage, OjbcNamespaceContext.NS_SUB_MSG_EXT,
                "SystemName");
        systemName.setTextContent(unsubscription.getSystemName());

    }

    if (StringUtils.isNotBlank(unsubscription.getSubscriptionQualifierIdentification())) {
        Element subscriptionQualifierIdentification = XmlUtils.appendElement(unsubscriptionMessage,
                OjbcNamespaceContext.NS_SUB_MSG_EXT, "SubscriptionQualifierIdentification");

        Element identificationID = XmlUtils.appendElement(subscriptionQualifierIdentification,
                OjbcNamespaceContext.NS_NC, "IdentificationID");
        identificationID.setTextContent(unsubscription.getSubscriptionQualifierIdentification());

    }

    if (StringUtils.isNotEmpty(subscriptionIdentificationId)) {
        Element subscriptionIdentification = XmlUtils.appendElement(unsubscriptionMessage,
                OjbcNamespaceContext.NS_SUB_MSG_EXT, "SubscriptionIdentification");

        Element identificationID = XmlUtils.appendElement(subscriptionIdentification,
                OjbcNamespaceContext.NS_NC, "IdentificationID");
        identificationID.setTextContent(subscriptionIdentificationId);
    }

    String reasonCode = unsubscription.getReasonCode();
    if (CIVIL_SUBSCRIPTION_REASON_CODE.equals(reasonCode)) {
        Element reasonCodeElement = XmlUtils.appendElement(unsubscriptionMessage,
                OjbcNamespaceContext.NS_SUB_MSG_EXT, "CivilSubscriptionReasonCode");
        reasonCodeElement.setTextContent(reasonCode);
    } else {
        Element reasonCodeElement = XmlUtils.appendElement(unsubscriptionMessage,
                OjbcNamespaceContext.NS_SUB_MSG_EXT, "CriminalSubscriptionReasonCode");
        reasonCodeElement.setTextContent(reasonCode);
    }

    Element topicExpNode = XmlUtils.appendElement(root, OjbcNamespaceContext.NS_B2, "TopicExpression");
    XmlUtils.addAttribute(topicExpNode, null, "Dialect", TOPIC_EXPRESSION_DIALECT);
    topicExpNode.setTextContent(unsubscription.getTopic());

    OjbcNamespaceContext ojbNamespaceCtxt = new OjbcNamespaceContext();
    ojbNamespaceCtxt.populateRootNamespaceDeclarations(doc.getDocumentElement());

    return doc;
}

From source file:org.ojbc.util.xml.TestOjbcNamespaceContext.java

@Test
public void testRootNamespacePopulation() throws Exception {

    Document d = db.newDocument();
    Element root = d.createElementNS(OjbcNamespaceContext.NS_PERSON_SEARCH_RESULTS_EXT, "e1");
    root.setPrefix(OjbcNamespaceContext.NS_PREFIX_PERSON_SEARCH_RESULTS_EXT);
    d.appendChild(root);//from  w w  w  .  j a  v  a 2s  .c o m
    Element child = XmlUtils.appendElement(root, OjbcNamespaceContext.NS_JXDM_41, "foo");
    XmlUtils.addAttribute(child, OjbcNamespaceContext.NS_STRUCTURES, "id", "I1");
    //XmlUtils.printNode(d);
    XmlUtils.OJBC_NAMESPACE_CONTEXT.populateRootNamespaceDeclarations(root);
    //XmlUtils.printNode(d);
    assertNotNull(XmlUtils.xPathNodeSearch(root,
            "namespace::xmlns:" + OjbcNamespaceContext.NS_PREFIX_PERSON_SEARCH_RESULTS_EXT));
    assertEquals(
            root.getAttributeNS("http://www.w3.org/2000/xmlns/",
                    OjbcNamespaceContext.NS_PREFIX_PERSON_SEARCH_RESULTS_EXT),
            OjbcNamespaceContext.NS_PERSON_SEARCH_RESULTS_EXT);
    assertNotNull(
            XmlUtils.xPathNodeSearch(root, "namespace::xmlns:" + OjbcNamespaceContext.NS_PREFIX_STRUCTURES));
    assertEquals(
            root.getAttributeNS("http://www.w3.org/2000/xmlns/", OjbcNamespaceContext.NS_PREFIX_STRUCTURES),
            OjbcNamespaceContext.NS_STRUCTURES);
    assertNull(XmlUtils.xPathNodeSearch(root, "namespace::xmlns:" + OjbcNamespaceContext.NS_PREFIX_ANSI_NIST));
    assertNotNull(
            XmlUtils.xPathNodeSearch(child, "namespace::xmlns:" + OjbcNamespaceContext.NS_PREFIX_JXDM_41));
    assertNull(
            child.getAttributeNodeNS("http://www.w3.org/2000/xmlns/", OjbcNamespaceContext.NS_PREFIX_JXDM_41));
    assertNull(child.getAttributeNodeNS("http://www.w3.org/2000/xmlns/",
            OjbcNamespaceContext.NS_PREFIX_STRUCTURES));

}

From source file:org.ojbc.util.xml.TestOjbcNamespaceContext.java

@Test
public void testRootNamespacePopulationWithEmbeddedNamespaceDeclarations() throws Exception {

    Document d = db.newDocument();
    Element root = d.createElementNS(OjbcNamespaceContext.NS_PERSON_SEARCH_RESULTS_EXT, "e1");
    root.setPrefix(OjbcNamespaceContext.NS_PREFIX_PERSON_SEARCH_RESULTS_EXT);
    d.appendChild(root);// w  ww.j  a v  a  2s  .c  om
    Element child = XmlUtils.appendElement(root, OjbcNamespaceContext.NS_JXDM_41, "foo");
    XmlUtils.addAttribute(child, OjbcNamespaceContext.NS_STRUCTURES, "id", "I1");
    child = XmlUtils.appendElement(child, OjbcNamespaceContext.NS_JXDM_41, "foo2");
    XmlUtils.addAttribute(child, OjbcNamespaceContext.NS_STRUCTURES, "id", "I2");
    XmlUtils.OJBC_NAMESPACE_CONTEXT.populateRootNamespaceDeclarations(child);
    //XmlUtils.printNode(d);
    XmlUtils.OJBC_NAMESPACE_CONTEXT.populateRootNamespaceDeclarations(root);
    //XmlUtils.printNode(d);
    NamedNodeMap attrs = root.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        assertFalse("null".equals(attrs.item(i).getLocalName()) && "xmlns".equals(attrs.item(i).getPrefix()));
    }

}

From source file:org.ojbc.util.xml.XmlUtils.java

/**
 * Create a new element with the specified namespace and name, append it to the specified parent, and return it
 * //from ww w . j a  v a  2s.c o  m
 * @param parent
 *            the parent to contain the new element
 * @param ns
 *            the namespace URI of the new element
 * @param elementName
 *            the name of the new element
 * @return the new element
 */
public static final Element appendElement(Element parent, String ns, String elementName) {
    Document doc = parent.getOwnerDocument();
    Element ret = doc.createElementNS(ns, elementName);
    parent.appendChild(ret);
    ret.setPrefix(OJBC_NAMESPACE_CONTEXT.getPrefix(ns));
    return ret;
}

From source file:org.ojbc.util.xml.XmlUtils.java

/**
  * Create a new element with the specified namespace and name, insert it under the specified parent but before the specified sibling, and return it
  * // ww  w .j a v a 2 s  .com
  * @param parent
  *            the parent
  * @param sibling
  *            the parent's current child, in front of which the new element is to be inserted
  * @param ns
  *            the namespace URI of the new element
  * @param elementName
  *            the name of the new element
  * @return the new element
  */
public static final Element insertElementBefore(Element parent, Element sibling, String ns,
        String elementName) {
    Document doc = parent.getOwnerDocument();
    Element ret = doc.createElementNS(ns, elementName);
    ret.setPrefix(OJBC_NAMESPACE_CONTEXT.getPrefix(ns));
    parent.insertBefore(ret, sibling);
    return ret;
}

From source file:org.ojbc.web.util.RequestMessageBuilderUtilities.java

public static Document createSubscriptionSearchRequest() throws Exception {
    Document doc = OJBCXMLUtils.createDocument();

    Element root = doc.createElementNS(OjbcNamespaceContext.NS_SUBSCRIPTION_SEARCH_REQUEST,
            "SubscriptionSearchRequest");
    doc.appendChild(root);//from w  ww  .  j  a  v a  2  s. com
    root.setPrefix(OjbcNamespaceContext.NS_PREFIX_SUBSCRIPTION_SEARCH_REQUEST);

    OJBC_NAMESPACE_CONTEXT.populateRootNamespaceDeclarations(root);

    return doc;

}

From source file:org.ojbc.web.util.RequestMessageBuilderUtilities.java

public static Document createSubscriptionQueryRequest(DetailsRequest subscriptionQueryRequest)
        throws Exception {
    Document doc = OJBCXMLUtils.createDocument();

    Element root = doc.createElementNS(OjbcNamespaceContext.NS_SUBSCRIPTION_QUERY_REQUEST,
            "SubscriptionQueryRequest");
    doc.appendChild(root);/*from ww w.  j  a  v  a2  s.c om*/
    root.setPrefix(OjbcNamespaceContext.NS_PREFIX_SUBSCRIPTION_QUERY_REQUEST);

    Element subIdentification = XmlUtils.appendElement(root,
            OjbcNamespaceContext.NS_SUBSCRIPTION_QUERY_REQUEST_EXT, "SubscriptionIdentification");

    Element identificationId = XmlUtils.appendElement(subIdentification, OjbcNamespaceContext.NS_NC,
            "IdentificationID");
    identificationId.setTextContent(subscriptionQueryRequest.getIdentificationID());

    Element identificationSourceText = XmlUtils.appendElement(subIdentification, OjbcNamespaceContext.NS_NC,
            "IdentificationSourceText");
    identificationSourceText.setTextContent(subscriptionQueryRequest.getIdentificationSourceText());

    OJBC_NAMESPACE_CONTEXT.populateRootNamespaceDeclarations(root);

    return doc;
}

From source file:org.ojbc.web.util.RequestMessageBuilderUtilities.java

public static Document createUnubscriptionRequest(Unsubscription unsubscription) throws Exception {

    String subscriptionIdentificationId = unsubscription.getSubscriptionId();

    Document doc = OJBCXMLUtils.createDocument();
    Element root = doc.createElementNS(OjbcNamespaceContext.NS_B2, "Unsubscribe");
    doc.appendChild(root);/*  w  ww  .  ja  va2  s.co m*/
    root.setPrefix(OjbcNamespaceContext.NS_PREFIX_B2);

    Element unsubscriptionMessage = XmlUtils.appendElement(root, OjbcNamespaceContext.NS_UNBSUB_MSG_EXCHANGE,
            "UnsubscriptionMessage");

    Element subscriptionIdentification = XmlUtils.appendElement(unsubscriptionMessage,
            OjbcNamespaceContext.NS_SUB_MSG_EXT, "SubscriptionIdentification");

    Element identificationID = XmlUtils.appendElement(subscriptionIdentification, OjbcNamespaceContext.NS_NC,
            "IdentificationID");
    identificationID.setTextContent(subscriptionIdentificationId);

    String reasonCode = unsubscription.getReasonCode();
    if (CIVIL_SUBSCRIPTION_REASON_CODE.equals(reasonCode)) {
        Element reasonCodeElement = XmlUtils.appendElement(unsubscriptionMessage,
                OjbcNamespaceContext.NS_SUB_MSG_EXT, "CivilSubscriptionReasonCode");
        reasonCodeElement.setTextContent(reasonCode);
    } else {
        Element reasonCodeElement = XmlUtils.appendElement(unsubscriptionMessage,
                OjbcNamespaceContext.NS_SUB_MSG_EXT, "CriminalSubscriptionReasonCode");
        reasonCodeElement.setTextContent(reasonCode);
    }

    Element topicExpNode = XmlUtils.appendElement(root, OjbcNamespaceContext.NS_B2, "TopicExpression");
    XmlUtils.addAttribute(topicExpNode, null, "Dialect", TOPIC_EXPRESSION_DIALECT);
    topicExpNode.setTextContent(unsubscription.getTopic());

    OjbcNamespaceContext ojbNamespaceCtxt = new OjbcNamespaceContext();
    ojbNamespaceCtxt.populateRootNamespaceDeclarations(doc.getDocumentElement());

    return doc;
}

From source file:org.ojbc.web.util.RequestMessageBuilderUtilities.java

public static Document createValidateSubscriptionRequest(String subscriptionId, String topic, String reasonCode)
        throws Exception {

    Document doc = OJBCXMLUtils.createDocument();
    Element rootElement = doc.createElementNS(OjbcNamespaceContext.NS_B2, "Validate");
    doc.appendChild(rootElement);/*w  ww  .  j  av  a2s.  c  o m*/
    rootElement.setPrefix(OjbcNamespaceContext.NS_PREFIX_B2);

    Element subValidMsgElement = XmlUtils.appendElement(rootElement, OjbcNamespaceContext.NS_SUB_VALID_MESSAGE,
            "SubscriptionValidationMessage");

    Element subIdElement = XmlUtils.appendElement(subValidMsgElement, OjbcNamespaceContext.NS_SUB_MSG_EXT,
            "SubscriptionIdentification");

    Element identificationIDElement = XmlUtils.appendElement(subIdElement, OjbcNamespaceContext.NS_NC,
            "IdentificationID");

    identificationIDElement.setTextContent(subscriptionId);

    if (StringUtils.isNotBlank(reasonCode)) {
        if (OjbcWebConstants.CIVIL_SUBSCRIPTION_REASON_CODE.equals(reasonCode)) {
            Element civilSubscriptionReasonCode = XmlUtils.appendElement(subValidMsgElement,
                    OjbcNamespaceContext.NS_SUB_MSG_EXT, "CivilSubscriptionReasonCode");
            civilSubscriptionReasonCode.setTextContent(reasonCode);
        } else {
            Element criminalSubscriptionReasonCode = XmlUtils.appendElement(subValidMsgElement,
                    OjbcNamespaceContext.NS_SUB_MSG_EXT, "CriminalSubscriptionReasonCode");
            criminalSubscriptionReasonCode.setTextContent(reasonCode);
        }
    }

    Element topicElement = XmlUtils.appendElement(rootElement, OjbcNamespaceContext.NS_B2, "TopicExpression");
    XmlUtils.addAttribute(topicElement, null, "Dialect", TOPIC_EXPRESSION_DIALECT);

    topicElement.setTextContent(topic);

    return doc;
}