Example usage for org.dom4j Element attributeValue

List of usage examples for org.dom4j Element attributeValue

Introduction

In this page you can find the example usage for org.dom4j Element attributeValue.

Prototype

String attributeValue(QName qName);

Source Link

Document

This returns the attribute value for the attribute with the given fully qualified name or null if there is no such attribute or the empty string if the attribute value is empty.

Usage

From source file:com.devoteam.srit.xmlloader.smpp.MsgSmpp.java

License:Open Source License

/** 
 * Parse the message from XML element //from   w w  w. j  a  v a  2s  . co  m
 */
@Override
public void parseFromXml(Boolean request, Element root, Runner runner) throws Exception {
    // header
    Element header = root.element("header");
    String msgName = header.attributeValue("name");
    String msgId = header.attributeValue("id");

    if ((msgId != null) && (msgName != null))
        throw new Exception("id and name of the message " + msgName + " must not be set both");

    if ((msgId == null) && (msgName == null))
        throw new Exception("One of the parameter id and name of the message header must be set");

    if (msgName != null) {
        smppMessage = ((StackSmpp) stack).smppDictionary.getMessageFromName(msgName);
        smppMessage.setId(((StackSmpp) stack).smppDictionary.getMessageIdFromName(msgName));
    }

    if (msgId != null)
        smppMessage = ((StackSmpp) stack).smppDictionary.getMessageFromId((int) Long.parseLong(msgId, 16));

    if ((smppMessage.getId() == 0) || (smppMessage.getName().equalsIgnoreCase("Unknown message")))
        smppMessage.setLogError("Message <" + msgName + "> is not present in the dictionary\r\n");

    String msgStatus = header.attributeValue("status");
    String msgSeqNum = header.attributeValue("sequence_number");

    if (msgStatus != null) {
        smppMessage.setStatus((int) Long.parseLong(msgStatus));
    }
    smppMessage.setSequenceNumber((int) Long.parseLong(msgSeqNum));

    parseAttributes(root, smppMessage);
    parseTLVs(root, smppMessage);

}

From source file:com.devoteam.srit.xmlloader.smpp.MsgSmpp.java

License:Open Source License

private void parseAttributes(Element root, SmppMessage msg) throws Exception {
    List<Element> attributes = root.elements("attribute");
    List<Element> imbricateAttributesInScenario = null;
    SmppAttribute att = null;//ww w.  jav  a 2  s .c  o m
    SmppAttribute att2 = null;
    SmppGroup attGroup = null;
    String value = null;
    String name = null;
    String ret = null;
    Element element2 = null;

    for (int cpt = 0; cpt < attributes.size(); cpt++) {
        Element element = attributes.get(cpt);
        att = msg.getAttribut(element.attributeValue("name"));

        if (att != null) {
            if (!(att.getValue() instanceof Vector))//simple attribute
            {
                value = element.attributeValue("value");

                //cas particulier pour sm_length
                if (att.getName().equalsIgnoreCase("sm_length")) {
                    if (value.equalsIgnoreCase("auto"))
                        value = "-1";//value is et to -1 to later set it automattically with the good length of the short message
                }

                ret = setAttributeValue(att, value, element.attributeValue("type"));
                if (ret != null)
                    msg.setLogError(ret);
            } else//attribute containing multiple occurence
            {
                //get vector values of att
                Vector vec = (Vector) att.getValue();

                int occurenceValue = 0;
                if (att.getOccurenceAttribute() != null)//if occurence is defined
                {
                    //get the occurence value
                    String occurence = att.getOccurenceAttribute();
                    occurenceValue = (Integer) msg.getAttribut(occurence).getValue();
                    //need to duplicate vector of imbricatte attribute in the occurence vector
                    for (int i = 1; i < occurenceValue; i++) {
                        //this way is used to the clone method
                        Vector<Attribute> newVec = new Vector<Attribute>();
                        for (int j = 0; j < ((Vector) vec.get(0)).size(); j++)
                            newVec.add(((Attribute) ((Vector) vec.get(0)).get(j)).clone());
                        vec.add(newVec);
                    }
                }

                for (int cptOc = 0; cptOc < occurenceValue; cptOc++)//run through multiple occurence
                {
                    //get imbricate attribute or attribute in choice for att
                    imbricateAttributesInScenario = element.selectNodes("attribute");

                    Vector vecImbricateAtt = (Vector) vec.get(cptOc);
                    //run through attribute in att,(list of attribute, or attribute and choice)
                    for (int i = 0; i < vecImbricateAtt.size(); i++) {
                        if (vecImbricateAtt.get(i) instanceof SmppChoice) {
                            SmppChoice choice = (SmppChoice) vecImbricateAtt.get(i);
                            //get choice parameter
                            String choiceName = choice.getChoiceAttribute().getName();

                            //search this choice parameter in the attribute list
                            for (int j = 0; j < vecImbricateAtt.size(); j++) {
                                att2 = (SmppAttribute) vecImbricateAtt.get(j);
                                if (choiceName.equals(att2.getName())) {
                                    //get its value
                                    value = Integer.toString((Integer) att2.getValue());
                                    //set choice attribute value in choice with this value
                                    choice.getChoiceAttribute().setValue((Integer) att2.getValue());
                                    break;
                                }
                            }

                            if ((value != null) && (value.length() != 0)) {
                                //search value of the choice parameter in the choice attribute group list
                                for (int j = 0; j < ((Vector<SmppGroup>) choice.getValue()).size(); j++) {
                                    attGroup = ((Vector<SmppGroup>) choice.getValue()).get(j);
                                    if (attGroup.getChoiceValue().equals(value)) {
                                        //set value of the group with value in the scenario
                                        for (int k = 0; k < ((Vector<SmppAttribute>) attGroup.getValue())
                                                .size(); k++) {
                                            //check name and then set value
                                            att2 = ((Vector<SmppAttribute>) attGroup.getValue()).get(k);
                                            element2 = imbricateAttributesInScenario.get(0);//get first following the order in scenario
                                            if (att2.getName()
                                                    .equalsIgnoreCase(element2.attributeValue("name"))) {
                                                ret = setAttributeValue(att2, element2.attributeValue("value"),
                                                        element2.attributeValue("type"));
                                                if (ret != null)
                                                    msg.setLogError(ret);
                                                //remove from the list to not retrieve it later
                                                imbricateAttributesInScenario.remove(0);
                                            } else {
                                                throw new Exception("Name of attribute " + att2.getName()
                                                        + "doesn't correspond with attribute "
                                                        + element2.attributeValue("name") + " in scenario");
                                            }
                                        }
                                        break;
                                    }
                                }
                            } else {
                                throw new Exception("value of attribute " + " must not be empty");
                            }
                        } else {
                            att2 = ((SmppAttribute) vecImbricateAtt.get(i));

                            //run through attribute in scenario to find att with the same name
                            for (int j = 0; j < imbricateAttributesInScenario.size(); j++) {
                                element2 = imbricateAttributesInScenario.get(j);
                                name = element2.attributeValue("name");

                                if (att2.getName().equalsIgnoreCase(name))//if found
                                {
                                    ret = setAttributeValue(att2, element2.attributeValue("value"),
                                            element2.attributeValue("type"));
                                    if (ret != null)
                                        msg.setLogError(ret);
                                    //remove from the list to not retrieve it later
                                    imbricateAttributesInScenario.remove(j);
                                    break;
                                }
                            }
                        }
                    }

                    //increment for next access to a new occurence:
                    if (((cptOc + 1) == (occurenceValue - 1)) && ((cpt + 1) < attributes.size())) {
                        cpt++;
                        element = attributes.get(cpt);
                    }
                }
            }
        }
        //add attribute to message even if unknown and if the message is not present in the dictionary
        else if (msg.getName().equals("UnknownMesage") || (msg.getId() == 0)) {
            value = element.attributeValue("value");
            att = new SmppAttribute();
            att.setName(element.attributeValue("name"));
            att.setFormat("OCTETSTRING");
            att.setValue(new DefaultArray(value.getBytes()));
            msg.addAttribut(att);
        } else {
            msg.setLogError("attribute <" + element.attributeValue("name")
                    + "> not added in message because it is unknown for this message\r\n");
        }
    }
}

From source file:com.devoteam.srit.xmlloader.smpp.MsgSmpp.java

License:Open Source License

private void parseTLVs(Element root, SmppMessage msg) throws Exception {
    List<Element> tlvs = root.elements("tlv");
    SmppTLV tlv = null;/*  ww w.  j  av  a 2  s. c  o m*/
    String value = null;

    for (Element element : tlvs) {
        int length = 0;
        tlv = msg.getTLV(element.attributeValue("name"));
        if (tlv != null) {
            value = element.attributeValue("value");
            if (tlv.getFormat().equalsIgnoreCase("INT")) {
                tlv.setValue((int) Long.parseLong(value));
                int tmp = ((Integer) tlv.getValue());
                while (tmp != 0) {
                    tmp >>= 8;
                    length++;
                }
            } else if (tlv.getFormat().equalsIgnoreCase("OCTETSTRING")
                    && (element.attributeValue("type") != null)
                    && element.attributeValue("type").equalsIgnoreCase("binary")) {
                tlv.setValue(DefaultArray.fromHexString(value));// do conversion in string here to don't have pb with charset
                length = ((DefaultArray) tlv.getValue()).length;
            } else if (tlv.getFormat().equalsIgnoreCase("OCTETSTRING")) {
                tlv.setValue(new DefaultArray(GSMConversion.toGsmCharset(value)));
                length = ((SupArray) tlv.getValue()).length;
            } else {
                tlv.setValue(new String(GSMConversion.toGsmCharset(value)));
                length = ((String) tlv.getValue()).length();
            }

            value = element.attributeValue("length");
            if (value != null) {
                if (value.equalsIgnoreCase("auto"))
                    tlv.setLength(length);
                else
                    tlv.setLength(Integer.parseInt(value));
            }

            if ((tlv.getLength() > tlv.getSizeMax()) || (tlv.getLength() < tlv.getSizeMin())) {
                GlobalLogger.instance().getApplicationLogger().warn(TextEvent.Topic.PROTOCOL, "TLV length for ",
                        tlv.toString(), "is not according to size given in dictionary");
            }
        } else {
            //add tlv to message even if unknown
            value = element.attributeValue("value");
            tlv = new SmppTLV();
            tlv.setName(element.attributeValue("name"));
            tlv.setLength(Integer.parseInt(element.attributeValue("length")));
            tlv.setFormat("OCTETSTRING");
            tlv.setValue(new DefaultArray(GSMConversion.toGsmCharset(value)));
            msg.addTLV(tlv);
        }
    }
}

From source file:com.devoteam.srit.xmlloader.smpp.SmppDictionary.java

License:Open Source License

private void parseFile(InputStream stream) throws Exception {
    Element messageNode = null;
    Element group = null;/*from   w ww  .  j av  a 2 s.  com*/
    SmppMessage msg = null;
    SmppAttribute att = null;
    SmppAttribute att2 = null;
    SmppGroup attGroup = null;
    SmppChoice attChoice = null;
    Vector<SmppGroup> choiceList = null;
    Vector<SmppAttribute> imbricateAtt = null;
    SmppTLV tlv = null;
    int i = 0;
    int j = 0;
    long id = 0;

    SAXReader reader = new SAXReader();
    Document document = reader.read(stream);

    //parsing des messages
    List listMessages = document.selectNodes("/dictionary/message");
    for (i = 0; i < listMessages.size(); i++) {
        messageNode = (Element) listMessages.get(i);

        msg = new SmppMessage();
        msg.setName(messageNode.attributeValue("name"));
        id = Long.parseLong(messageNode.attributeValue("id"), 16);
        msg.setId((int) id);

        for (Iterator it = messageNode.elementIterator(); it.hasNext();) {
            Element element = (Element) it.next();
            String name = element.getName();

            if (name.equalsIgnoreCase("attribute")) {
                att = setAttribute(element); //for simple attribute

                //check if attribute imbricate in attribute or choice in attribute
                List listAtt = element.selectNodes("//attribute[@name='" + element.attributeValue("name")
                        + "']/attribute | //attribute[@name='" + element.attributeValue("name") + "']/choice");
                if (!listAtt.isEmpty()) {
                    ((Vector) att.getValue()).add(new Vector<SmppAttribute>());//set vector of imbricate attributes into the vector of occurence
                }

                for (j = 0; j < listAtt.size(); j++) {
                    if (((Element) listAtt.get(j)).getName().equalsIgnoreCase("attribute")) {
                        //add imbricate attribute to att
                        att2 = setAttribute((Element) listAtt.get(j));
                        ((Vector<SmppAttribute>) ((Vector) att.getValue()).get(0)).add(att2);
                    } else if (((Element) listAtt.get(j)).getName().equalsIgnoreCase("choice")) {
                        List groupList = element.selectNodes(
                                "//attribute[@name='" + element.attributeValue("name") + "']/choice/group");
                        attChoice = new SmppChoice();
                        choiceList = new Vector<SmppGroup>();

                        //work because last imbricate att is the last attribute set and is the att base on choice
                        attChoice.setChoiceAttribute(att2);

                        //parse all group of the choice, could be only an attribute or a group of attribute
                        for (j = 0; j < groupList.size(); j++) {
                            group = (Element) groupList.get(j);
                            //create an attribute for each group
                            attGroup = new SmppGroup();

                            attGroup.setChoiceValue(group.attributeValue("value"));
                            imbricateAtt = new Vector<SmppAttribute>();

                            for (Iterator iter = group.elementIterator(); iter.hasNext();) {
                                imbricateAtt.add(setAttribute((Element) iter.next()));
                            }

                            attGroup.setValue(imbricateAtt);
                            choiceList.add(attGroup);
                        }
                        attChoice.setValue(choiceList);
                        ((Vector<SmppChoice>) ((Vector) att.getValue()).get(0)).add(attChoice);
                    }
                }
                msg.addAttribut(att);
            } else if (name.equalsIgnoreCase("tlv")) {
                tlv = setTLV(element, msg);
                if (tlv != null)
                    msg.addTLV(tlv);
            }
        }

        messagesList.put(msg.getName(), msg);
        messageIdToNameList.put(msg.getId(), msg.getName());
        messageNameToIdList.put(msg.getName(), msg.getId());
    }

    //        //affichage des message
    //        Collection<SmppMessage> collecMsg = messagesList.values();
    //        System.out.println("\r\nnb de message dans la collection: " + collecMsg.size());
    //        for(Iterator<SmppMessage> iter = collecMsg.iterator(); iter.hasNext();)
    //        {
    //            SmppMessage msgEle = (SmppMessage)iter.next();
    //            if(msgEle.getName().equals("submit_multi"))
    //            {
    //                System.out.println(msgEle.getName());
    //                //affichage des attributs
    //                System.out.println(msgEle.toString());
    //            }
    //        }
}

From source file:com.devoteam.srit.xmlloader.smpp.SmppDictionary.java

License:Open Source License

public SmppAttribute setAttribute(Element element) throws Exception {
    SmppAttribute att = new SmppAttribute();
    String value = null;//from www  .j av  a2  s  .c om

    att.setName(element.attributeValue("name"));
    att.setFormat(element.attributeValue("format"));
    value = element.attributeValue("sizeMin");
    if (value != null)
        att.setSizeMin(Integer.parseInt(value));
    value = element.attributeValue("sizeMax");
    if (value != null)
        att.setSizeMax(Integer.parseInt(value));
    value = element.attributeValue("value");//always an INT value in the dictionnary
    if (value != null)
        att.setValue(Integer.parseInt(value));

    value = element.attributeValue("numberOfTime");//number of occurence of this attribut depend on which parameter
    if (value != null) {
        att.setOccurenceAttribute(value);
        att.setValue(new Vector<SmppAttribute>());
    }

    return att;
}

From source file:com.devoteam.srit.xmlloader.smpp.SmppDictionary.java

License:Open Source License

public SmppTLV setTLV(Element element, SmppMessage msg) throws Exception {
    SmppTLV tlv = new SmppTLV();
    String value = null;/* www .ja  v a2s .c  om*/

    tlv.setName(element.attributeValue("name"));
    tlv.setTag(Integer.parseInt(element.attributeValue("tag"), 16));
    tlv.setFormat(element.attributeValue("format"));
    value = element.attributeValue("sizeMin");
    if (value != null)
        tlv.setSizeMin(Integer.parseInt(value));
    value = element.attributeValue("sizeMax");
    if (value != null) {
        if (value.equalsIgnoreCase("tlv_length")) {
            tlv.setSizeMax(65536);//size max of 2 octets in lenght field
        } else {
            tlv.setSizeMax(Integer.parseInt(value));
        }
    }
    value = element.attributeValue("mandatory");
    if (value != null) {
        tlv.setMandatory(Boolean.parseBoolean(value));
    }

    return tlv;
}

From source file:com.devoteam.srit.xmlloader.smpp.StackSmpp.java

License:Open Source License

/** Creates a Channel specific to each Stack */
@Override// ww w  .  j a  v a  2  s  . co m
public Channel parseChannelFromXml(Element root, String protocol) throws Exception {
    String name = root.attributeValue("name");
    String localHost = root.attributeValue("localHost");
    String localPort = root.attributeValue("localPort");
    String remoteHost = root.attributeValue("remoteHost");
    String remotePort = root.attributeValue("remotePort");

    if (existsChannel(name)) {
        return getChannel(name);
    } else {
        if (null != localHost)
            localHost = InetAddress.getByName(localHost).getHostAddress();
        else
            localHost = "0.0.0.0";

        if (null != remoteHost)
            remoteHost = InetAddress.getByName(remoteHost).getHostAddress();

        return new ChannelSmpp(name, localHost, localPort, remoteHost, remotePort, protocol);
    }
}

From source file:com.devoteam.srit.xmlloader.smpp.StackSmpp.java

License:Open Source License

/** Creates a specific Msg */
@Override/*from w  ww.  j  av a2 s.  co m*/
public Msg parseMsgFromXml(Boolean request, Element root, Runner runner) throws Exception {
    SmppMessage smppMessage = null;

    // header
    Element header = root.element("header");
    String msgName = header.attributeValue("name");
    String msgId = header.attributeValue("id");

    if ((msgId != null) && (msgName != null))
        throw new Exception("id and name of the message " + msgName + " must not be set both");

    if ((msgId == null) && (msgName == null))
        throw new Exception("One of the parameter id and name of the message header must be set");

    if (msgName != null) {
        smppMessage = smppDictionary.getMessageFromName(msgName);
        smppMessage.setId(smppDictionary.getMessageIdFromName(msgName));
    }

    if (msgId != null)
        smppMessage = smppDictionary.getMessageFromId((int) Long.parseLong(msgId, 16));

    if ((smppMessage.getId() == 0) || (smppMessage.getName().equalsIgnoreCase("Unknown message")))
        smppMessage.setLogError("Message <" + msgName + "> is not present in the dictionary\r\n");

    String msgStatus = header.attributeValue("status");
    String msgSeqNum = header.attributeValue("sequence_number");

    if (msgStatus != null)
        smppMessage.setStatus((int) Long.parseLong(msgStatus));
    smppMessage.setSequenceNumber((int) Long.parseLong(msgSeqNum));

    parseAttributes(root, smppMessage);
    parseTLVs(root, smppMessage);

    return new MsgSmpp(smppMessage);
}

From source file:com.devoteam.srit.xmlloader.smpp.StackSmpp.java

License:Open Source License

public void parseAttributes(Element root, SmppMessage msg) throws Exception {
    List<Element> attributes = root.elements("attribute");
    List<Element> imbricateAttributesInScenario = null;
    SmppAttribute att = null;//  w  w  w .  j  av  a2  s  . co  m
    SmppAttribute att2 = null;
    SmppGroup attGroup = null;
    String value = null;
    String name = null;
    String ret = null;
    Element element2 = null;

    for (int cpt = 0; cpt < attributes.size(); cpt++) {
        Element element = attributes.get(cpt);
        att = msg.getAttribut(element.attributeValue("name"));

        if (att != null) {
            if (!(att.getValue() instanceof Vector))//simple attribute
            {
                value = element.attributeValue("value");

                //cas particulier pour sm_length
                if (att.getName().equalsIgnoreCase("sm_length")) {
                    if (value.equalsIgnoreCase("auto"))
                        value = "-1";//value is et to -1 to later set it automattically with the good length of the short message
                }

                ret = setAttributeValue(att, value, element.attributeValue("type"));
                if (ret != null)
                    msg.setLogError(ret);
            } else//attribute containing multiple occurence
            {
                //get vector values of att
                Vector vec = (Vector) att.getValue();

                int occurenceValue = 0;
                if (att.getOccurenceAttribute() != null)//if occurence is defined
                {
                    //get the occurence value
                    String occurence = att.getOccurenceAttribute();
                    occurenceValue = (Integer) msg.getAttribut(occurence).getValue();
                    //need to duplicate vector of imbricatte attribute in the occurence vector
                    for (int i = 1; i < occurenceValue; i++) {
                        //this way is used to the clone method
                        Vector<Attribute> newVec = new Vector<Attribute>();
                        for (int j = 0; j < ((Vector) vec.get(0)).size(); j++)
                            newVec.add(((Attribute) ((Vector) vec.get(0)).get(j)).clone());
                        vec.add(newVec);
                    }
                }

                for (int cptOc = 0; cptOc < occurenceValue; cptOc++)//run through multiple occurence
                {
                    //get imbricate attribute or attribute in choice for att
                    imbricateAttributesInScenario = element.selectNodes("attribute");

                    Vector vecImbricateAtt = (Vector) vec.get(cptOc);
                    //run through attribute in att,(list of attribute, or attribute and choice)
                    for (int i = 0; i < vecImbricateAtt.size(); i++) {
                        if (vecImbricateAtt.get(i) instanceof SmppChoice) {
                            SmppChoice choice = (SmppChoice) vecImbricateAtt.get(i);
                            //get choice parameter
                            String choiceName = choice.getChoiceAttribute().getName();

                            //search this choice parameter in the attribute list
                            for (int j = 0; j < vecImbricateAtt.size(); j++) {
                                att2 = (SmppAttribute) vecImbricateAtt.get(j);
                                if (choiceName.equals(att2.getName())) {
                                    //get its value
                                    value = Integer.toString((Integer) att2.getValue());
                                    //set choice attribute value in choice with this value
                                    choice.getChoiceAttribute().setValue((Integer) att2.getValue());
                                    break;
                                }
                            }

                            if ((value != null) && (value.length() != 0)) {
                                //search value of the choice parameter in the choice attribute group list
                                for (int j = 0; j < ((Vector<SmppGroup>) choice.getValue()).size(); j++) {
                                    attGroup = ((Vector<SmppGroup>) choice.getValue()).get(j);
                                    if (attGroup.getChoiceValue().equals(value)) {
                                        //set value of the group with value in the scenario
                                        for (int k = 0; k < ((Vector<SmppAttribute>) attGroup.getValue())
                                                .size(); k++) {
                                            //check name and then set value
                                            att2 = ((Vector<SmppAttribute>) attGroup.getValue()).get(k);
                                            element2 = imbricateAttributesInScenario.get(0);//get first following the order in scenario
                                            if (att2.getName()
                                                    .equalsIgnoreCase(element2.attributeValue("name"))) {
                                                ret = setAttributeValue(att2, element2.attributeValue("value"),
                                                        element2.attributeValue("type"));
                                                if (ret != null)
                                                    msg.setLogError(ret);
                                                //remove from the list to not retrieve it later
                                                imbricateAttributesInScenario.remove(0);
                                            } else {
                                                throw new Exception("Name of attribute " + att2.getName()
                                                        + "doesn't correspond with attribute "
                                                        + element2.attributeValue("name") + " in scenario");
                                            }
                                        }
                                        break;
                                    }
                                }
                            } else {
                                throw new Exception("value of attribute " + " must not be empty");
                            }
                        } else {
                            att2 = ((SmppAttribute) vecImbricateAtt.get(i));

                            //run through attribute in scenario to find att with the same name
                            for (int j = 0; j < imbricateAttributesInScenario.size(); j++) {
                                element2 = imbricateAttributesInScenario.get(j);
                                name = element2.attributeValue("name");

                                if (att2.getName().equalsIgnoreCase(name))//if found
                                {
                                    ret = setAttributeValue(att2, element2.attributeValue("value"),
                                            element2.attributeValue("type"));
                                    if (ret != null)
                                        msg.setLogError(ret);
                                    //remove from the list to not retrieve it later
                                    imbricateAttributesInScenario.remove(j);
                                    break;
                                }
                            }
                        }
                    }

                    //increment for next access to a new occurence:
                    if (((cptOc + 1) == (occurenceValue - 1)) && ((cpt + 1) < attributes.size())) {
                        cpt++;
                        element = attributes.get(cpt);
                    }
                }
            }
        }
        //add attribute to message even if unknown and if the message is not present in the dictionary
        else if (msg.getName().equals("UnknownMesage") || (msg.getId() == 0)) {
            value = element.attributeValue("value");
            att = new SmppAttribute();
            att.setName(element.attributeValue("name"));
            att.setFormat("OCTETSTRING");
            att.setValue(new DefaultArray(value.getBytes()));
            msg.addAttribut(att);
        } else {
            msg.setLogError("attribute <" + element.attributeValue("name")
                    + "> not added in message because it is unknown for this message\r\n");
        }
    }
}

From source file:com.devoteam.srit.xmlloader.smpp.StackSmpp.java

License:Open Source License

public void parseTLVs(Element root, SmppMessage msg) throws Exception {
    List<Element> tlvs = root.elements("tlv");
    SmppTLV tlv = null;/*from w w  w.ja v  a2s .  com*/
    String value = null;

    for (Element element : tlvs) {
        int length = 0;
        tlv = msg.getTLV(element.attributeValue("name"));
        if (tlv != null) {
            value = element.attributeValue("value");
            if (tlv.getFormat().equalsIgnoreCase("INT")) {
                tlv.setValue((int) Long.parseLong(value));
                int tmp = ((Integer) tlv.getValue());
                while (tmp != 0) {
                    tmp >>= 8;
                    length++;
                }
            } else if (tlv.getFormat().equalsIgnoreCase("OCTETSTRING")
                    && (element.attributeValue("type") != null)
                    && element.attributeValue("type").equalsIgnoreCase("binary")) {
                tlv.setValue(DefaultArray.fromHexString(value));// do conversion in string here to don't have pb with charset
                length = ((DefaultArray) tlv.getValue()).length;
            } else if (tlv.getFormat().equalsIgnoreCase("OCTETSTRING")) {
                tlv.setValue(new DefaultArray(GSMConversion.toGsmCharset(value)));
                length = ((SupArray) tlv.getValue()).length;
            } else {
                tlv.setValue(new String(GSMConversion.toGsmCharset(value)));
                length = ((String) tlv.getValue()).length();
            }

            value = element.attributeValue("length");
            if (value != null) {
                if (value.equalsIgnoreCase("auto"))
                    tlv.setLength(length);
                else
                    tlv.setLength(Integer.parseInt(value));
            }

            if ((tlv.getLength() > tlv.getSizeMax()) || (tlv.getLength() < tlv.getSizeMin())) {
                GlobalLogger.instance().getApplicationLogger().warn(TextEvent.Topic.PROTOCOL, "TLV length for ",
                        tlv.toString(), "is not according to size given in dictionary");
            }
        } else {
            //add tlv to message even if unknown
            value = element.attributeValue("value");
            tlv = new SmppTLV();
            tlv.setName(element.attributeValue("name"));
            tlv.setLength(Integer.parseInt(element.attributeValue("length")));
            tlv.setFormat("OCTETSTRING");
            tlv.setValue(new DefaultArray(GSMConversion.toGsmCharset(value)));
            msg.addTLV(tlv);
        }
    }
}