Example usage for org.dom4j Element elements

List of usage examples for org.dom4j Element elements

Introduction

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

Prototype

List<Element> elements(QName qName);

Source Link

Document

Returns the elements contained in this element with the given fully qualified name.

Usage

From source file:com.devoteam.srit.xmlloader.master.master.utils.DataMaster.java

License:Open Source License

private void parse() throws Exception {
    this.testDatas = new LinkedList<DataTest>();

    List<Element> parametersMaster = (List<Element>) root.elements("parameter");

    /* Duplicate the parameter elements from the <master> level and copy
     * them at the begining of the <test> element, before the <test>
     * element's own parameter elements.
     *//*from  w  ww .  j  av a2s.  com*/
    List<Element> tests = (List<Element>) root.elements("test");
    for (Element test : tests) {
        List<Element> parametersTest = (List<Element>) test.elements("parameter");
        for (Element parameter : parametersTest) {
            test.remove(parameter);
        }

        for (Element parameter : parametersMaster) {
            test.add(parameter.createCopy());
        }

        for (Element parameter : parametersTest) {
            test.add(parameter);
        }

        testDatas.add(new DataTest(test, this.xmlDocument.getXMLFile()));
    }

    //
    // Test for TestData names uniqueness
    HashSet hashSet = new HashSet();
    for (DataTest testData : testDatas) {
        if (hashSet.contains(testData.getName())) {
            throw new Exception("Test name should be unique (" + testData.getName() + ").");
        } else {
            hashSet.add(testData.getName());
        }
    }
}

From source file:com.devoteam.srit.xmlloader.radius.StackRadius.java

License:Open Source License

/** Creates a specific Msg */
@Override//from  w  w  w  .  ja v a 2s  .  c o  m
public Msg parseMsgFromXml(Boolean request, Element root, Runner runner) throws Exception {
    RadiusMessage radiusMessage = new RadiusMessage();

    // header
    Element header = root.element("header");
    String codeAttribute = header.attributeValue("code");
    String authenticatorAttribute = header.attributeValue("authenticator");
    String identifierAttribute = header.attributeValue("identifier");

    if (Utils.isInteger(codeAttribute)) {
        radiusMessage.setCode(Integer.valueOf(codeAttribute));
    } else {
        RadiusCodes radiusCodes = this.radiusDictionary.getRadiusCodes();
        Integer code = radiusCodes.getCode(codeAttribute);
        if (null != code)
            radiusMessage.setCode(code);
        else
            throw new Exception("Unknown message code " + codeAttribute);
    }

    List<Element> avps = root.elements("avp");
    for (Element avp : avps) {
        radiusMessage.addAVP(this.parseAVP(-1, avp));
    }

    String name = root.attributeValue("listenpoint");
    boolean isListenpoint = true;
    // deprecated part //
    if (name == null) {
        name = root.attributeValue("socketName");
        isListenpoint = false;
    }
    // deprecated part //
    String remoteHost = root.attributeValue("remoteHost");
    String remotePort = root.attributeValue("remotePort");

    if ((null != remoteHost) && (null != remotePort)) {
        InetAddress inetAddress = InetAddress.getByName(remoteHost);
        InetSocketAddress inetSocketAddress = new InetSocketAddress(inetAddress, Integer.parseInt(remotePort));

        radiusMessage.setRemoteAddress(inetSocketAddress);
    }

    MsgRadius msgRadius = new MsgRadius(radiusMessage);
    ListenpointRadius listenpoint = null;
    ChannelRadius channel = null;

    if (null != identifierAttribute) {
        radiusMessage.setIdentifier(Integer.valueOf(identifierAttribute));
    }

    if (isListenpoint) {
        listenpoint = (ListenpointRadius) getListenpoint(name);
        if (listenpoint == null) {
            throw new ExecutionException("StackRadius: The listenpoint " + name + " does not exist");
        }
        if ((null == remoteHost) || (null == remotePort)) {
            throw new ExecutionException(
                    "StackRadius: The \"remoteXXX\" attributes are mandatory; please define them when sending the message.");
        }
        if (radiusMessage.getIdentifier() == 0)
            radiusMessage.setIdentifier(listenpoint.getIdentifierHandler().getIdentifier());
        msgRadius.setListenpoint(listenpoint);
    } else// deprecated part //
    {
        channel = (ChannelRadius) getChannel(name);
        if (channel == null) {
            throw new ExecutionException("StackRadius: The channel " + name + " does not exist");
        }
        if (radiusMessage.getIdentifier() == 0)
            radiusMessage.setIdentifier(channel.getIdentifierHandler().getIdentifier());
        msgRadius.setChannel(channel);
    }
    // deprecated part //

    if (isListenpoint) {
        radiusMessage.setSecret(listenpoint.getSecret());
        if (null == radiusMessage.getRemoteAddress() && null != listenpoint.getHost()
                && 0 != listenpoint.getPort()) {
            InetAddress inetAddress = InetAddress.getByName(listenpoint.getHost());
            InetSocketAddress inetSocketAddress = new InetSocketAddress(inetAddress, listenpoint.getPort());
            radiusMessage.setRemoteAddress(inetSocketAddress);
        }
    } else// deprecated part //
    {
        radiusMessage.setSecret(channel.getSecret());
        if (radiusMessage.getRemoteAddress() == null) {
            InetAddress inetAddress = InetAddress.getByName(channel.getRemoteHost());
            InetSocketAddress inetSocketAddress = new InetSocketAddress(inetAddress, channel.getRemotePort());
            radiusMessage.setRemoteAddress(inetSocketAddress);
        }
        if ((channel.getRemoteHost() == null) || (channel.getRemotePort() == 0)) {
            channel.setRemoteHost(radiusMessage.getRemoteAddress().getAddress().getHostAddress());
            channel.setRemotePort(radiusMessage.getRemoteAddress().getPort());
        }

    } // deprecated part //

    if (null != authenticatorAttribute) {
        radiusMessage.setAuthenticator(new DefaultArray(Utils.parseBinaryString(authenticatorAttribute)));
    } else {
        if (msgRadius.isRequest()) {
            radiusMessage.computeRequestAuthenticator();
        } else {
            MsgRadius msgRadiusRequest = (MsgRadius) this.getInTransaction(msgRadius.getTransactionId())
                    .getBeginMsg();

            radiusMessage.computeResponseAuthenticator(msgRadiusRequest.getRadiusMessage().getAuthenticator());
        }
    }

    // encode the User-Password AVPs if necessary
    if (this.getConfig().getBoolean("radius.ENCODE_USER_PASSWORD", true)) {
        if (1 == radiusMessage.getCode()) {
            GlobalLogger.instance().getApplicationLogger().debug(TextEvent.Topic.PROTOCOL,
                    "ChannelRadius, encode user-password avps");
            radiusMessage.encodeUserPasswordAvps();
        }
    }

    return msgRadius;
}

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.  jav a2  s. co  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.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  ww. j  av  a 2 s  .co 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.MsgRtp.java

License:Open Source License

/** 
 * Parse the message from XML element //from w  ww  .j a va 2 s  .c om
 */
@Override
public void parseFromXml(Boolean request, Element root, Runner runner) throws Exception {
    RTPPacket rtpPacket = parsePacket(root);
    if (rtpPacket != null) {
        add(rtpPacket);
    }
    boolean control = parseChannel(root);
    setControl(control);

    List<Element> listPackets = root.elements("packet");
    if (listPackets.size() > 0) {
        control = parseChannel(listPackets.get(0));
        setControl(control);
    }
    Iterator<Element> iter = listPackets.iterator();
    while (iter.hasNext()) {
        Element packet = iter.next();
        rtpPacket = parsePacket(packet);
        if (rtpPacket != null) {
            add(rtpPacket);
        }
    }
}

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  va  2 s.  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 specific RTP Msg */
public Msg parseMsgFromXml(Boolean request, Element root, Runner runner) throws Exception {
    MsgRtp msgRtp = new MsgRtp();

    // instanciates the channel        
    String channelName = root.attributeValue("sessionName");
    Channel channel = getChannel(channelName);
    if (channel != null) {
        msgRtp.setChannel(channel);/*from   w  w w . j a  v a 2  s  .com*/
    } else if ((channel == null) && (root.attribute("listenpoint") == null)) {
        throw new ExecutionException("The channel <name=" + channelName + "> does not exist");
    }

    if (root.element("flow") != null) {
        throw new Exception("The JMF RTP stack does not support <flow> tag, use the light RTP stack instead");
    }

    List<Element> listPackets = root.elements("packet");
    boolean control = parseChannel(listPackets.get(0));
    msgRtp.setControl(control);

    Iterator<Element> iter = listPackets.iterator();
    while (iter.hasNext()) {
        Element packet = iter.next();
        RTPPacket rtpPacket = parsePacket(packet);
        msgRtp.add(rtpPacket);
    }
    return msgRtp;
}

From source file:com.devoteam.srit.xmlloader.rtp.jmf.StackRtp.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));
        }/*w  w w .j  a v  a2  s  .  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");
    String ssrc = header.attributeValue("ssrc");
    rtpPacket.ssrc = Integer.parseInt(ssrc);
    String seqnum = header.attributeValue("seqnum");
    rtpPacket.seqnum = Integer.parseInt(seqnum);
    String timestamp = header.attributeValue("timestamp");
    rtpPacket.timestamp = Integer.parseInt(timestamp);
    String payloadType = header.attributeValue("payloadType");
    rtpPacket.payloadType = Integer.parseInt(payloadType);
    rtpPacket.payloadoffset = 12;
    rtpPacket.payloadlength = data.length - rtpPacket.payloadoffset;
    rtpPacket.calcLength();
    rtpPacket.assemble(1, false);

    return rtpPacket;
}

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

License:Open Source License

public ArrayList<Array> parsePacketPayload(Element packet, Runner runner) throws Exception {
    List<Element> payloads = packet.elements("payload");
    SupArray data = new SupArray();
    ArrayList<Array> listPayloadData = new ArrayList<Array>();
    String format = null;/*from  w w w .  j av a 2  s  .  com*/
    String text = null;

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

        if (format.equalsIgnoreCase("text")) {
            data.addLast(new DefaultArray(text.getBytes()));
        } else if (format.equalsIgnoreCase("binary")) {
            data.addLast(new DefaultArray(Utils.parseBinaryString(text)));
        } else {
            throw new Exception("format of payload <" + format + "> is unknown");
        }
    }
    listPayloadData.add(data);
    return listPayloadData;
}

From source file:com.devoteam.srit.xmlloader.sctp.MsgSctp.java

License:Open Source License

/** 
 * Parse the message from XML element //from   ww w. j a  v a2  s.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[]>();
    for (Element element : elements) {
        switch (DataType.valueOf(element.attributeValue("format"))) {
        case 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 = text.replace("\r\n", "\n");
            text = text.replace("\n", "\r\n");
            datas.add(text.getBytes("UTF8"));
            break;
        }
        case 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++;
        }
    }

    SCTPData sctpData = new SCTPData(data);

    String stream = root.attributeValue("stream");
    if (stream != null) {
        sctpData.sndrcvinfo.sinfo_stream = (short) Integer.parseInt(stream);
    }
    String ssn = root.attributeValue("ssn");
    if (ssn != null) {
        sctpData.sndrcvinfo.sinfo_ssn = (short) Integer.parseInt(ssn);
    }
    String flags = root.attributeValue("flags");
    if (flags != null) {
        sctpData.sndrcvinfo.sinfo_flags = (short) Integer.parseInt(flags);
    }
    String ppid = root.attributeValue("ppid");
    if (ppid != null) {
        sctpData.sndrcvinfo.sinfo_ppid = Integer.parseInt(ppid);
    }
    String context = root.attributeValue("context");
    if (context != null) {
        sctpData.sndrcvinfo.sinfo_context = Integer.parseInt(context);
    }
    String ttl = root.attributeValue("ttl");
    if (ttl != null) {
        sctpData.sndrcvinfo.sinfo_timetolive = Integer.parseInt(ttl);
    }
    String tsn = root.attributeValue("tsn");
    if (tsn != null) {
        sctpData.sndrcvinfo.sinfo_tsn = Integer.parseInt(tsn);
    }
    String cumtsn = root.attributeValue("cumtsn");
    if (cumtsn != null) {
        sctpData.sndrcvinfo.sinfo_cumtsn = Integer.parseInt(cumtsn);
    }
    String aid = root.attributeValue("aid");
    if (aid != null) {
        sctpData.sndrcvinfo.sinfo_assoc_id = new AssociationId(Long.parseLong(aid));
    }

    this.sctpData = new SCTPData(sctpData.sndrcvinfo, sctpData.getData());
    this.type = "DATA";
}