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.rtp.flow.ListenpointRtpFlow.java

License:Open Source License

/** Creates a Listenpoint specific from XML tree*/
public ListenpointRtpFlow(Stack stack, Element root) throws Exception {
    super(stack, root);

    Element header = root.element("flow");
    if (root.element("srtpSender") != null)
        this.parseSrtp(root, 0);
    if (root.element("srtpReceiver") != null)
        this.parseSrtp(root, 1);

    this.endTimerNoPacket = ((StackRtpFlow) stack).endTimerNoPacket; // set the default value from config file
    if (header != null) {
        String endTimerNoPacketStr = header.attributeValue("endTimerNoPacket");
        //override value if given in listenpoint creation
        if (endTimerNoPacketStr != null) {
            this.endTimerNoPacket = Float.parseFloat(endTimerNoPacketStr); //set time given in attribute in seconds
        }/*w  w w . java  2 s  .co  m*/
    }

    this.endTimerSilentFlow = ((StackRtpFlow) stack).endTimerSilentFlow; // set the default value from config file
    if (header != null) {

        String endTimerSilentFlowStr = header.attributeValue("endTimerSilentFlow");
        if (endTimerSilentFlowStr != null) {
            endTimerSilentFlow = Float.parseFloat(endTimerSilentFlowStr); //set time given in attribute in seconds
        }
    }

    this.endTimerPeriodic = ((StackRtpFlow) stack).endTimerPeriodic; // set the default value from config file
    if (header != null) {
        String endTimerPeriodicStr = header.attributeValue("endTimerPeriodic");
        if (endTimerPeriodicStr != null) {
            endTimerPeriodic = Float.parseFloat(endTimerPeriodicStr);//set time given in attribute in seconds
        }
    }

    this.qosMeasurment = ((StackRtpFlow) stack).qosMeasurment; // set the default value from config file
    if (header != null) {
        String qosMeasurmentStr = header.attributeValue("qosMeasurment");
        if (qosMeasurmentStr != null) {
            qosMeasurment = Boolean.parseBoolean(qosMeasurmentStr);
        }
    }

    this.ignoreReceivedMessages = ((StackRtpFlow) stack).ignoreReceivedMessages; // set the default value from config file
    if (header != null) {
        String ignoreReceivedMessagesStr = header.attributeValue("ignoreReceivedMessages");
        if (ignoreReceivedMessagesStr != null) {
            ignoreReceivedMessages = Boolean.parseBoolean(ignoreReceivedMessagesStr);
        }
    }
}

From source file:com.devoteam.srit.xmlloader.rtp.flow.MsgRtpFlow.java

License:Open Source License

public void parseFlow(Element flow, Runner runner) throws Exception {
    StackRtp stackRtp = (StackRtp) StackFactory.getStack(StackFactory.PROTOCOL_RTP);
    MsgRtp msg = new MsgRtp(stackRtp);
    msg.parsePacketHeader(flow, runner);
    this.msgRtp = msg;

    this.payloadList = parsePacketPayload(flow, runner);
    this.packetNumber = payloadList.size();
    //prepare first packet now =>
    this.msgRtp.setData(payloadList.get(0));

    this.seqnumList = parsePacketAttribute(flow, runner, "seqnum");
    this.msgRtp.setSequenceNumber(this.seqnumList.get(0));

    this.timestampList = parsePacketAttribute(flow, runner, "timestamp");
    this.msgRtp.setTimestampRTP(this.timestampList.get(0));

    this.markList = parsePacketAttribute(flow, runner, "mark");
    if (this.markList.size() == 0) {
        this.msgRtp.setMarker(1);
    } else {/*  w  w w.j av a  2  s .c  om*/
        this.msgRtp.setMarker(this.markList.get(0));
    }
    if (this.markList.size() == 0) {
        this.markList = new ArrayList<Integer>();
        this.markList.add(0);
    }

    //construct it the first time
    this.msgRtp.encode();

    this.payloadType = this.msgRtp.getPayloadType();
    this.ssrc = this.msgRtp.getSsrc();

    this.packetsList = new LinkedList<MsgRtp>();
    if (this.payloadList.size() > 1) {
        this.maxNbPacketInList = this.payloadList.size();
    } else if (this.seqnumList.size() > 1) {
        this.maxNbPacketInList = this.seqnumList.size();
    } else if (this.timestampList.size() > 1) {
        this.maxNbPacketInList = this.timestampList.size();
    } else if (this.markList.size() > 1) {
        this.maxNbPacketInList = this.markList.size();
    } else {
        this.maxNbPacketInList = 1;
    }

    //WARNING: part above must be done before setting flow parameter

    String bitRate = flow.attributeValue("bitRate");
    String deltaTime = flow.attributeValue("deltaTime");
    String deltaTimestamp = flow.attributeValue("deltaTimestamp");
    if ((bitRate != null) && (deltaTime != null)) {
        throw new Exception("attribute <bitRate> and <deltaTime> of flow tag cannot be set in the same time");
    }

    if ((bitRate == null) && (deltaTime == null)) {
        throw new Exception("one of the attribute <bitRate> or <deltaTime> of flow tag must be set");
    }

    String duration = flow.attributeValue("duration");
    String packetNumber = flow.attributeValue("packetNumber");
    if ((packetNumber == null) && (duration == null)) {
        throw new Exception("one of the attribute <packetNumber> or <duration> of flow tag must be set");
    }

    if (packetNumber != null) {
        this.packetNumber = Integer.parseInt(packetNumber);
    }
    if (deltaTime != null) {
        LinkedList<String> listAttribute = runner.getParameterPool().parse(deltaTime);
        if (listAttribute.size() > 0)//if more than one element
        {
            ArrayList listAttributeData = new ArrayList();

            int delta;
            for (Iterator<String> it = listAttribute.iterator(); it.hasNext();) {
                delta = Integer.parseInt(it.next());
                if (delta < 0)//if delta is negatif, set 0 for the time to send
                {
                    listAttributeData.add(0);
                } else {
                    listAttributeData.add(delta);
                }

            }
            this.deltaTimeList = listAttributeData;
        }
    }

    if (duration != null) {
        this.duration = Float.parseFloat(duration);
    }

    if (bitRate != null) {
        this.bitRate = Float.parseFloat(bitRate);
    }
    if (deltaTimestamp != null) {
        this.deltaTimestamp = Integer.parseInt(deltaTimestamp);
    } else {
        this.deltaTimestamp = (int) this.getDataRTPLength();
    }
    String jitterDelay = flow.attributeValue("jitterDelay");
    if (jitterDelay != null) {
        this.jitterDelay = Float.parseFloat(jitterDelay);
    }

    //now compute missing values with values which are given in operation
    if (packetNumber == null && bitRate != null)//compute packetNumber in function of the bitrate
    {
        this.packetNumber = (int) (this.bitRate * 1024 * this.duration / (this.getDataRTPLength() * 8));
    } else if (packetNumber == null && bitRate == null)//compute packetNumber in function of deltaTimestamp
    {
        int newPacketNumber = (int) (this.duration * 1000 / this.getDeltaTime());
        this.packetNumber = newPacketNumber;
    }

    if (duration == null) {
        float dur = (float) this.packetNumber * this.getDeltaTime() / 1000;
        this.duration = dur;
    }

    String packetLost = flow.attributeValue("packetLost");
    if (packetLost != null) {
        float packetLostPercent = Float.parseFloat(packetLost);
        if ((packetLostPercent < 0) || (packetLostPercent > 100)) {
            throw new Exception("packetLost attribute is a percentage (between 0 and 100)");
        }

        this.percentagePacketLost = packetLostPercent;
        this.setPacketToBeLost();
    }

    if (bitRate != null)//compute deltaTime in function of bitrate, it is also mandatory to set packetNumber in this case
    {
        long size = this.getDataRTPLength() * this.packetNumber * 8;
        float timeToSend = size / (this.bitRate * 1024);

        int newDeltaTimestamp = (int) Math.round(timeToSend * 1000 / this.packetNumber);
        if (newDeltaTimestamp == 0) {
            newDeltaTimestamp = 1;
        }
        this.setDeltaTime(newDeltaTimestamp);
    }

    String synchronous = flow.attributeValue("synchronous");
    if (synchronous != null) {
        this.synchronous = Boolean.parseBoolean(synchronous);
    }
}

From source file:com.devoteam.srit.xmlloader.rtp.flow.MsgRtpFlow.java

License:Open Source License

public ArrayList<Array> parsePacketPayload(Element packet, Runner runner) throws Exception {
    List<Element> payloads = packet.elements("payload");
    ArrayList<Array> listPayloadData = new ArrayList<Array>();
    LinkedList<String> listPayload;
    String format = null;//from ww  w.j  a  v  a 2s .c  om
    String text = null;

    for (Element element : payloads) {
        format = element.attributeValue("format");
        text = element.getTextTrim();

        if (Parameter.matchesParameter(text) && payloads.size() == 1) {
            // optimisation, use cache
            Parameter parameter = runner.getParameterPool().get(text);
            if (format.equalsIgnoreCase("text")) {
                listPayloadData = (ArrayList<Array>) ParameterCache.getAsAsciiArrayList(parameter);
            } else if (format.equalsIgnoreCase("binary")) {
                listPayloadData = (ArrayList<Array>) ParameterCache.getAsHexArrayList(parameter);
            } else {
                throw new Exception("format of payload <" + format + "> is unknown");
            }
        } else {
            listPayload = runner.getParameterPool().parse(text);
            if (format.equalsIgnoreCase("text")) {
                for (Iterator<String> it = listPayload.iterator(); it.hasNext();) {
                    listPayloadData.add(new DefaultArray(it.next().getBytes()));
                }
            } else if (format.equalsIgnoreCase("binary")) {
                for (Iterator<String> it = listPayload.iterator(); it.hasNext();) {
                    listPayloadData.add(Array.fromHexString(it.next()));
                }
            } else {
                throw new Exception("format of payload <" + format + "> is unknown");
            }
        }
    }
    return listPayloadData;
}

From source file:com.devoteam.srit.xmlloader.rtp.flow.MsgRtpFlow.java

License:Open Source License

public ArrayList parsePacketAttribute(Element packet, Runner runner, String attName) throws Exception {
    Element header = packet.element("header");
    ArrayList listAttributeData = new ArrayList();
    String attribute = header.attributeValue(attName);

    if (attribute != null) {
        if (Parameter.matchesParameter(attribute)) {
            // optimisation, use cache
            Parameter parameter = runner.getParameterPool().get(attribute);
            if (attName.equals("seqnum") || attName.equals("mark")) {
                listAttributeData = (ArrayList) ParameterCache.getAsIntegerList(parameter);
            } else if (attName.equals("timestamp")) {
                listAttributeData = (ArrayList) ParameterCache.getAsLongList(parameter);
            }//  www.  j  a va2  s .co  m
        } else {
            LinkedList<String> listAttribute = runner.getParameterPool().parse(attribute);
            if (attName.equals("seqnum") || attName.equals("mark")) {
                for (Iterator<String> it = listAttribute.iterator(); it.hasNext();) {
                    listAttributeData.add(Integer.parseInt(it.next()));
                }
            } else if (attName.equals("timestamp")) {
                for (Iterator<String> it = listAttribute.iterator(); it.hasNext();) {
                    listAttributeData.add(Long.parseLong(it.next()));
                }
            }
        }
    }
    return listAttributeData;
}

From source file:com.devoteam.srit.xmlloader.rtp.flow.StackRtpFlow.java

License:Open Source License

public MsgRtpFlow parseFlow(Element flow, Runner runner) throws Exception {
    MsgRtp msg = parsePacketHeader(flow, runner);

    List<Array> listPayload = parsePacketPayload(flow, runner);
    List<Integer> listSequm = parsePacketAttribute(flow, runner, "seqnum");
    List<Long> listTimestamp = parsePacketAttribute(flow, runner, "timestamp");
    List<Integer> listMarker = parsePacketAttribute(flow, runner, "mark");

    MsgRtpFlow flowRtp = new MsgRtpFlow(dico, listPayload, listSequm, listTimestamp, listMarker, msg);
    //WARNING: part above must be done before setting flow parameter

    String duration = flow.attributeValue("duration");
    String packetNumber = flow.attributeValue("packetNumber");
    String bitRate = flow.attributeValue("bitRate");
    String deltaTime = flow.attributeValue("deltaTime");
    String deltaTimestamp = flow.attributeValue("deltaTimestamp");
    String jitterDelay = flow.attributeValue("jitterDelay");
    String packetLost = flow.attributeValue("packetLost");
    String synchronous = flow.attributeValue("synchronous");

    if ((bitRate != null) && (deltaTime != null)) {
        throw new Exception("attribute <bitRate> and <deltaTime> of flow tag cannot be set in the same time");
    }/*ww w .  j a v  a  2  s  .  co m*/

    if ((bitRate == null) && (deltaTime == null)) {
        throw new Exception("one of the attribute <bitRate> or <deltaTime> of flow tag must be set");
    }

    if ((packetNumber == null) && (duration == null)) {
        throw new Exception("one of the attribute <packetNumber> or <duration> of flow tag must be set");
    }

    if ((listPayload.size() > 1) && (packetNumber != null)
            && (Integer.parseInt(packetNumber) > listPayload.size())
            && ((listSequm.size() > 1) || (listTimestamp.size() > 1))) {
        throw new Exception(
                "cannot use greater <packetNumber> than number of payload given when use in coordination with list of sequence number or timestamp");
    }

    if (packetNumber != null) {
        flowRtp.setPacketNumber(Integer.parseInt(packetNumber));
    }
    if (deltaTime != null) {
        LinkedList<String> listAttribute = runner.getParameterPool().parse(deltaTime);
        if (listAttribute.size() > 0)//if more than one element
        {
            ArrayList listAttributeData = new ArrayList();

            int delta;
            for (Iterator<String> it = listAttribute.iterator(); it.hasNext();) {
                delta = Integer.parseInt(it.next());
                if (delta < 0)//if delta is negatif, set 0 for the time to send
                {
                    listAttributeData.add(0);
                } else {
                    listAttributeData.add(delta);
                }

            }
            flowRtp.setDeltaTimeList(listAttributeData);
        }
    }

    if (duration != null) {
        flowRtp.setDuration(Float.parseFloat(duration));
    }
    if (bitRate != null) {
        flowRtp.setBitRate(Float.parseFloat(bitRate));
    }
    if (deltaTimestamp != null) {
        flowRtp.setDeltaTimestamp(Integer.parseInt(deltaTimestamp));
    } else {
        flowRtp.setDeltaTimestamp((int) flowRtp.getDataRTPLength());
    }
    if (jitterDelay != null) {
        flowRtp.setJitterDelay(Float.parseFloat(jitterDelay));
    }

    //now compute missing values with values which are given in operation
    if (packetNumber == null && bitRate != null)//compute packetNumber in function of the bitrate
    {
        flowRtp.setPacketNumber(
                (int) (flowRtp.getBitRate() * 1024 * flowRtp.getDuration() / (flowRtp.getDataRTPLength() * 8)));
    } else if (packetNumber == null && bitRate == null)//compute packetNumber in function of deltaTimestamp
    {
        int newPacketNumber = (int) (flowRtp.getDuration() * 1000 / flowRtp.getDeltaTime());
        if ((packetNumber == null) || (newPacketNumber < flowRtp.getPacketNumber())) {
            flowRtp.setPacketNumber(newPacketNumber);
        }
    }

    if (packetLost != null) {
        float packetLostPercent = Float.parseFloat(packetLost);
        if ((packetLostPercent < 0) || (packetLostPercent > 100)) {
            throw new Exception("packetLost attribute is a percentage (between 0 and 100)");
        }

        flowRtp.setPacketLost(packetLostPercent);
        flowRtp.setPacketToBeLost();
    }

    if (bitRate != null)//compute deltaTime in function of bitrate, it is also mandatory to set packetNumber in this case
    {
        long size = flowRtp.getDataRTPLength() * flowRtp.getPacketNumber() * 8;
        float timeToSend = size / (flowRtp.getBitRate() * 1024);

        int newDeltaTimestamp = (int) Math.round(timeToSend * 1000 / flowRtp.getPacketNumber());
        if (newDeltaTimestamp == 0) {
            newDeltaTimestamp = 1;
        }
        flowRtp.setDeltaTime(newDeltaTimestamp);
    }

    if (synchronous != null) {
        flowRtp.setSynchronous(Boolean.parseBoolean(synchronous));
    }
    return flowRtp;
}

From source file:com.devoteam.srit.xmlloader.rtp.flow.StackRtpFlow.java

License:Open Source License

@Override
public ArrayList<Array> parsePacketPayload(Element packet, Runner runner) throws Exception {
    List<Element> payloads = packet.elements("payload");
    ArrayList<Array> listPayloadData = new ArrayList<Array>();
    LinkedList<String> listPayload;
    String format = null;//from  w  w w  . j ava  2  s  . c o  m
    String text = null;

    for (Element element : payloads) {
        format = element.attributeValue("format");
        text = element.getTextTrim();

        if (Parameter.matchesParameter(text) && payloads.size() == 1) {
            // optimisation, use cache
            Parameter parameter = runner.getParameterPool().get(text);
            if (format.equalsIgnoreCase("text")) {
                listPayloadData = (ArrayList<Array>) ParameterCache.getAsAsciiArrayList(parameter);
            } else if (format.equalsIgnoreCase("binary")) {
                listPayloadData = (ArrayList<Array>) ParameterCache.getAsHexArrayList(parameter);
            } else {
                throw new Exception("format of payload <" + format + "> is unknown");
            }
        } else {
            listPayload = runner.getParameterPool().parse(text);
            if (format.equalsIgnoreCase("text")) {
                for (Iterator<String> it = listPayload.iterator(); it.hasNext();) {
                    listPayloadData.add(new DefaultArray(it.next().getBytes()));
                }
            } else if (format.equalsIgnoreCase("binary")) {
                for (Iterator<String> it = listPayload.iterator(); it.hasNext();) {
                    listPayloadData.add(Array.fromHexString(it.next()));
                }
            } else {
                throw new Exception("format of payload <" + format + "> is unknown");
            }
        }
    }
    return listPayloadData;
}

From source file:com.devoteam.srit.xmlloader.rtp.jmf.ChannelRtp.java

License:Open Source License

/** 
 * Parse the message from XML element //from   w ww . j a  v a2  s . c o  m
 */
@Override
public void parseFromXml(Element root, Runner runner, String protocol) throws Exception {
    super.parseFromXml(root, runner, protocol);

    // DEPRECATED begin
    String name = root.attributeValue("sessionName");
    if (name != null) {
        this.name = name;
    }
    // DEPRECATED end
}

From source file:com.devoteam.srit.xmlloader.rtp.jmf.MsgRtp.java

License:Open Source License

/** Parses then returns the channel from the XML root element */
private boolean parseChannel(Element root) throws Exception {
    Element header = root.element("header");
    if (header != null) {
        String channel = header.attributeValue("channel");
        boolean control = false;
        if (channel != null) {
            if (channel.equalsIgnoreCase("control")) {
                control = true;/*  ww  w  .  jav a2s.  co  m*/
            } else if (channel.equalsIgnoreCase("data")) {
            } else {
                Exception e = new Exception();
                throw new ExecutionException("Bad channel attribute " + channel
                        + " for the <header> tag : possible values are <data> or <control>", e);
            }
        }
        return control;
    }
    return false;
}

From source file:com.devoteam.srit.xmlloader.rtp.jmf.MsgRtp.java

License:Open Source License

/** Parses then returns an RTP Packet from the XML root element */
private RTPPacket parsePacket(Element root) throws Exception {
    List<Element> elements = root.elements("payload");
    List<byte[]> datas = new LinkedList<byte[]>();

    for (Element element : elements) {
        if (element.attributeValue("format").equalsIgnoreCase("text")) {
            String text = element.getTextTrim();
            datas.add(text.getBytes("UTF8"));
        } else if (element.attributeValue("format").equalsIgnoreCase("binary")) {
            String text = element.getTextTrim();
            datas.add(Utils.parseBinaryString(text));
        }/*from  w w w.  j  a v a 2s .c o  m*/
    }

    //
    // Compute total length
    //
    int length = 0;
    for (byte[] data : datas) {
        length += data.length;
    }

    byte[] data = new byte[length + 12];

    int i = 0;
    for (byte[] aData : datas) {
        for (int j = 0; j < aData.length; j++) {
            data[i + 12] = aData[j];
            i++;
        }
    }

    Packet packet = new Packet();
    packet.data = data;

    RTPPacket rtpPacket = new RTPPacket(packet);

    //
    // Parse header tag
    //
    Element header = root.element("header");
    if (header != null) {
        String ssrc = header.attributeValue("ssrc");
        rtpPacket.ssrc = Integer.parseInt(ssrc);
        String payloadType = header.attributeValue("payloadType");
        rtpPacket.payloadType = Integer.parseInt(payloadType);
        String seqnum = header.attributeValue("seqnum");
        rtpPacket.seqnum = Integer.parseInt(seqnum);
        String timestamp = header.attributeValue("timestamp");
        rtpPacket.timestamp = Integer.parseInt(timestamp);
        String marker = header.attributeValue("mark");
        if (marker != null) {
            rtpPacket.marker = Integer.parseInt(marker);
        }
        rtpPacket.payloadoffset = 12;
        rtpPacket.payloadlength = data.length - rtpPacket.payloadoffset;
        rtpPacket.calcLength();
        rtpPacket.assemble(1, false);

        return rtpPacket;
    }
    return null;
}

From source file:com.devoteam.srit.xmlloader.rtp.jmf.StackRtp.java

License:Open Source License

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

    ChannelRtp channel = new ChannelRtp(name, localHost, localPort, remoteHost, remotePort, protocol);
    return channel;
}