List of usage examples for org.dom4j Element elements
List<Element> elements(QName qName);
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;//from ww w . j a v a 2 s.c om 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;/*from w w w. j ava2s.co 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.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;//from www. j a v 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.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 va 2s . co 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.snmp.StackSnmp.java
License:Open Source License
@Override public Msg parseMsgFromXml(Boolean request, Element root, Runner runner) throws Exception { // header/* w w w.jav a 2 s . c om*/ Element header = root.element("header"); String version = header.attributeValue("version"); String community = header.attributeValue("community"); if (version == null)//get version given in config if not present in sendMessage version = getConfig().getString("protocol.version", "1"); if (!Utils.isInteger(version)) throw new Exception("attribute version must be integer"); Integer versionInt = Integer.parseInt(version); if (versionInt == 1) versionInt = SnmpConstants.version1; else if (versionInt == 2) versionInt = SnmpConstants.version2c; else if (versionInt == 3) versionInt = SnmpConstants.version3; else throw new Exception("possible version for SNMP are 1, 2 or 3 exclusively"); Element pdu = root.element("pdu"); String name = pdu.attributeValue("name"); String type = pdu.attributeValue("type"); String requestId = pdu.attributeValue("requestId"); String errorStatus = pdu.attributeValue("errorStatus"); String errorIndex = pdu.attributeValue("errorIndex"); String nonRepeaters = pdu.attributeValue("nonRepeaters"); String maxRepetitions = pdu.attributeValue("maxRepetitions"); if ((type != null) && (name != null)) throw new Exception("type and name of the message " + name + " must not be set both"); if ((type == null) && (name == null)) throw new Exception("One of the parameter type or name of the message header must be set"); if ((type != null) && !Utils.isInteger(type)) throw new Exception("attribute type must be integer"); Integer typeInt = null; if (type == null) { if (name.equalsIgnoreCase("trap") && (versionInt == SnmpConstants.version1)) typeInt = PDU.V1TRAP; else { typeInt = PDU.getTypeFromString(name.toUpperCase()); if (typeInt < -100) throw new Exception("the type " + name + " specified is unknown in the SNMP protocol"); } } else typeInt = Integer.parseInt(type); if (((errorStatus != null) || (errorIndex != null)) && ((nonRepeaters != null) || (maxRepetitions != null))) throw new Exception( "The attributes errorStatus or errorIndex must not be set with nonRepeaters or maxRepetitions"); if ((requestId == null) || !Utils.isInteger(requestId)) throw new Exception("attribute requestId must be integer"); if (((errorStatus != null) && !Utils.isInteger(errorStatus)) || ((errorIndex != null) && !Utils.isInteger(errorIndex))) throw new Exception("attribute errorStatus and errorIndex must be integer"); if (((nonRepeaters != null) && !Utils.isInteger(nonRepeaters)) || ((maxRepetitions != null) && !Utils.isInteger(maxRepetitions))) throw new Exception("attribute nonRepeaters and maxRepetitions must be integer"); Integer requestIdInt = (requestId != null) ? Integer.parseInt(requestId) : null; Integer errorStatusInt = (errorStatus != null) ? Integer.parseInt(errorStatus) : null; Integer errorIndexInt = (errorIndex != null) ? Integer.parseInt(errorIndex) : null; Integer nonRepeatersInt = (nonRepeaters != null) ? Integer.parseInt(nonRepeaters) : null; Integer maxRepetitionsInt = (maxRepetitions != null) ? Integer.parseInt(maxRepetitions) : null; MsgSnmp msgSmtp = new MsgSnmp(versionInt, community, typeInt, requestIdInt, errorStatusInt, errorIndexInt, nonRepeatersInt, maxRepetitionsInt); //specific parameter for snmpv1 Trap to check and add to pdu if ((msgSmtp.getPdu() instanceof PDUv1) && (typeInt == PDU.V1TRAP)) { String enterprise = pdu.attributeValue("enterprise"); String agentAddress = pdu.attributeValue("agentAddress"); String genericTrap = pdu.attributeValue("genericTrap"); String specificTrap = pdu.attributeValue("specificTrap"); String timestamp = pdu.attributeValue("timestamp"); if (genericTrap.equalsIgnoreCase("enterpriseSpecific") && !Utils.isInteger(specificTrap)) throw new Exception( "specificTrap attribute must be an integer when enterpriseSpecific if given for genereicTrap in SNMPV1 TRAP message"); if (!Utils.isInteger(timestamp)) throw new Exception("timestamp must be an integer"); int genericTrapInt = 0; if (genericTrap.equalsIgnoreCase("coldStart")) genericTrapInt = PDUv1.COLDSTART; else if (genericTrap.equalsIgnoreCase("warmStart")) genericTrapInt = PDUv1.WARMSTART; else if (genericTrap.equalsIgnoreCase("linkDown")) genericTrapInt = PDUv1.LINKDOWN; else if (genericTrap.equalsIgnoreCase("linkUp")) genericTrapInt = PDUv1.LINKUP; else if (genericTrap.equalsIgnoreCase("authenticationFailure")) genericTrapInt = PDUv1.AUTHENTICATIONFAILURE; else if (genericTrap.equalsIgnoreCase("egpNeighborLoss")) genericTrapInt = 5;//specified in rfc 1157, but not present in snmp4j stack else if (genericTrap.equalsIgnoreCase("enterpriseSpecific")) genericTrapInt = PDUv1.ENTERPRISE_SPECIFIC; else throw new Exception("genericTrap attribute is unknown"); ((PDUv1) msgSmtp.getPdu()).setEnterprise(new OID(enterprise)); ((PDUv1) msgSmtp.getPdu()).setAgentAddress(new IpAddress(agentAddress)); ((PDUv1) msgSmtp.getPdu()).setGenericTrap(genericTrapInt); ((PDUv1) msgSmtp.getPdu()).setSpecificTrap(Integer.parseInt(specificTrap)); ((PDUv1) msgSmtp.getPdu()).setTimestamp(Integer.parseInt(timestamp)); } List<Element> variables = pdu.elements("variableBinding"); for (Element var : variables) { parseVariableBinding(var, msgSmtp.getPdu()); } return msgSmtp; }
From source file:com.devoteam.srit.xmlloader.stun.MsgStun.java
License:Open Source License
MsgStun(Element root) { super();/* w w w . j av a 2s . c o m*/ header = new HeaderStun(); parseHeader(root.element("header")); List<Element> attributes = root.elements("attribute"); for (Element attribute : attributes) { if (attribute.attributeValue("type").equalsIgnoreCase("realm")) { this.longTermCredential = true; } parseAttribute(attribute); } }
From source file:com.devoteam.srit.xmlloader.tcp.MsgTcp.java
License:Open Source License
/** * Parse the message from XML element //from w w w.ja v a2s . c o m */ @Override public void parseFromXml(Boolean request, Element root, Runner runner) throws Exception { List<Element> elements = root.elements("data"); List<byte[]> datas = new LinkedList<byte[]>(); ; try { for (Element element : elements) { if (element.attributeValue("format").equalsIgnoreCase("text")) { String text = element.getText(); // change the \n caractre to \r\n caracteres because the dom librairy return only \n. // this could make some trouble when the length is calculated in the scenario text = Utils.replaceNoRegex(text, "\r\n", "\n"); text = Utils.replaceNoRegex(text, "\n", "\r\n"); datas.add(text.getBytes("UTF8")); } else if (element.attributeValue("format").equalsIgnoreCase("binary")) { String text = element.getTextTrim(); datas.add(Utils.parseBinaryString(text)); } } } catch (Exception e) { throw new ExecutionException(e); } // // Compute total length // int length = 0; for (byte[] data : datas) { length += data.length; } byte[] data = new byte[length]; int i = 0; for (byte[] aData : datas) { for (int j = 0; j < aData.length; j++) { data[i] = aData[j]; i++; } } decode(data); }
From source file:com.devoteam.srit.xmlloader.tcp.StackTcp.java
License:Open Source License
/** Creates a specific Msg */ @Override//from w ww . java2s . c om public Msg parseMsgFromXml(Boolean request, Element root, Runner runner) throws Exception { // // Parse all <data ... /> tags // List<Element> elements = root.elements("data"); List<byte[]> datas = new LinkedList<byte[]>(); ; try { for (Element element : elements) { if (element.attributeValue("format").equalsIgnoreCase("text")) { String text = element.getText(); // change the \n caractre to \r\n caracteres because the dom librairy return only \n. // this could make some trouble when the length is calculated in the scenario text = Utils.replaceNoRegex(text, "\r\n", "\n"); text = Utils.replaceNoRegex(text, "\n", "\r\n"); datas.add(text.getBytes("UTF8")); } else if (element.attributeValue("format").equalsIgnoreCase("binary")) { String text = element.getTextTrim(); datas.add(Utils.parseBinaryString(text)); } } } catch (Exception e) { throw new ExecutionException(e); } // // Compute total length // int length = 0; for (byte[] data : datas) { length += data.length; } byte[] data = new byte[length]; int i = 0; for (byte[] aData : datas) { for (int j = 0; j < aData.length; j++) { data[i] = aData[j]; i++; } } MsgTcp msgTcp = new MsgTcp(data); // instanciates the channel String channelName = root.attributeValue("channel"); // deprecated part // if (channelName == null) channelName = root.attributeValue("connectionName"); // deprecated part // Channel channel = getChannel(channelName); if (channel == null) { throw new ExecutionException("The channel <name=" + channelName + "> does not exist"); } msgTcp.setChannel(getChannel(channelName)); return msgTcp; }
From source file:com.devoteam.srit.xmlloader.tls.MsgTls.java
License:Open Source License
/** * Parse the message from XML element //from w w w . j a v a 2 s . c om */ @Override public void parseFromXml(Boolean request, Element root, Runner runner) throws Exception { List<Element> elements = root.elements("data"); List<byte[]> datas = new LinkedList<byte[]>(); for (Element element : elements) { if (element.attributeValue("format").equalsIgnoreCase("text")) { String text = element.getText(); // change the \n caractere to \r\n caracteres because the dom librairy return only \n. // this could make some trouble when the length is calculated in the scenario text = Utils.replaceNoRegex(text, "\r\n", "\n"); text = Utils.replaceNoRegex(text, "\n", "\r\n"); datas.add(text.getBytes("UTF8")); } else if (element.attributeValue("format").equalsIgnoreCase("binary")) { String text = element.getTextTrim(); datas.add(Utils.parseBinaryString(text)); } } // // Compute total length // int length = 0; for (byte[] data : datas) { length += data.length; } byte[] data = new byte[length]; int i = 0; for (byte[] aData : datas) { for (int j = 0; j < aData.length; j++) { data[i] = aData[j]; i++; } } decode(data); }
From source file:com.devoteam.srit.xmlloader.tls.StackTls.java
License:Open Source License
/** Creates a specific Msg */ @Override/*from w ww .ja va 2 s.c o m*/ public Msg parseMsgFromXml(Boolean request, Element root, Runner runner) throws Exception { // // Parse all <data ... /> tags // List<Element> elements = root.elements("data"); List<byte[]> datas = new LinkedList<byte[]>(); ; try { for (Element element : elements) { if (element.attributeValue("format").equalsIgnoreCase("text")) { String text = element.getText(); // change the \n caractre to \r\n caracteres because the dom librairy return only \n. // this could make some trouble when the length is calculated in the scenario text = Utils.replaceNoRegex(text, "\r\n", "\n"); text = Utils.replaceNoRegex(text, "\n", "\r\n"); datas.add(text.getBytes("UTF8")); } else if (element.attributeValue("format").equalsIgnoreCase("binary")) { String text = element.getTextTrim(); datas.add(Utils.parseBinaryString(text)); } } } catch (Exception e) { throw new ExecutionException(e); } // // Compute total length // int length = 0; for (byte[] data : datas) { length += data.length; } byte[] data = new byte[length]; int i = 0; for (byte[] aData : datas) { for (int j = 0; j < aData.length; j++) { data[i] = aData[j]; i++; } } MsgTls msgtls = new MsgTls(data); // instanciates the conn String channelName = root.attributeValue("channel"); // deprecated part // if (channelName == null) channelName = root.attributeValue("connectionName"); // deprecated part // Channel channel = getChannel(channelName); if (channel == null) { throw new ExecutionException("The channel <name=" + channelName + "> does not exist"); } msgtls.setChannel(getChannel(channelName)); return msgtls; }