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.sctp.StackSctp.java

License:Open Source License

/** Creates a Channel specific to each Stack */
@Override/*from   w w  w  .  j a va2  s  . c o m*/
public Channel parseChannelFromXml(Element root, String protocol) throws Exception {
    String name = root.attributeValue("name");
    // deprecated part //
    if (name == null)
        name = root.attributeValue("connectionName");
    // deprecated part //
    String localHost = root.attributeValue("localHost");
    String localPort = root.attributeValue("localPort");
    String remoteHost = root.attributeValue("remoteHost");
    String remotePort = root.attributeValue("remotePort");
    String ostreams = root.attributeValue("ostreams");
    String instreams = root.attributeValue("instreams");
    String attempts = root.attributeValue("attempts");
    String timeo = root.attributeValue("timeo");

    sctp_initmsg im = new sctp_initmsg();
    if (ostreams != null)
        im.sinit_num_ostreams = (short) Integer.parseInt(ostreams);
    if (instreams != null)
        im.sinit_max_instreams = (short) Integer.parseInt(instreams);
    if (attempts != null)
        im.sinit_max_attempts = (short) Integer.parseInt(attempts);
    if (timeo != null)
        im.sinit_max_init_timeo = (short) Integer.parseInt(timeo);

    if (existsChannel(name)) {
        return getChannel(name);
    } else {
        return new ChannelSctp(name, localHost, localPort, remoteHost, remotePort, protocol, im);
    }
}

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

License:Open Source License

/** Creates a specific Msg */
@Override//from ww  w.  j  av a  2  s.  c o  m
public Msg parseMsgFromXml(Boolean request, Element root, Runner runner) throws Exception {
    String stream = root.attributeValue("stream");
    String ssn = root.attributeValue("ssn");
    String flags = root.attributeValue("flags");
    String ppid = root.attributeValue("ppid");
    String context = root.attributeValue("context");
    String ttl = root.attributeValue("ttl");
    String tsn = root.attributeValue("tsn");
    String cumtsn = root.attributeValue("cumtsn");
    String aid = root.attributeValue("aid");

    List<Element> elements = root.elements("data");
    List<byte[]> datas = new LinkedList<byte[]>();
    try {
        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));
            }
            }
        }
    } 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++;
        }
    }

    SCTPData sctpData = new SCTPData(data);

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

    MsgSctp msgSctp = new MsgSctp(sctpData);

    String channelName = root.attributeValue("channel");
    // deprecated part //
    if (channelName == null)
        channelName = root.attributeValue("connectionName");
    // deprecated part //

    // instanciates the channel
    Channel channel = getChannel(channelName);
    if (channel == null) {
        throw new ExecutionException("The channel <name=" + channelName + "> does not exist");
    }
    msgSctp.setChannel(channel);

    return msgSctp;
}

From source file:com.devoteam.srit.xmlloader.sigtran.fvo.FvoDictionary.java

License:Open Source License

/**
 * Parse the dictionary from the XML file
 * // ww  w .  j a va 2s . c  o m
 * @param stream      :fvoDictionary XML file
 * @throws Exception
 */
final void parseFile(InputStream stream) throws Exception {
    SAXReader reader = new SAXReader();
    Document document = reader.read(stream);

    //HEADER
    Node headerNode = document.selectSingleNode("/dictionary/header");
    if (headerNode != null) {
        Element root = (Element) headerNode;
        header = parseParameter(root);
        header.setMessageLength(Integer.decode(root.attributeValue("length")));
    }

    //MESSAGES
    List listMessages = document.selectNodes("/dictionary/messages/message");
    for (Object object : listMessages) {
        Element root = (Element) object;
        FvoMessage message = new FvoMessage(null, this);

        String typeName = root.attributeValue("typeName");
        message.setName(typeName);
        String typeCode = root.attributeValue("typeCode");
        message.setMessageType(Integer.decode(typeCode));
        List parameters = root.elements("parameter");
        //PARAMETER OF EACH MESSAGE
        for (int j = 0; j < parameters.size(); j++) {
            Element param = (Element) parameters.get(j);

            String name = param.attributeValue("name");

            FvoParameter parameter;
            // try to find a detailed definition of the parameter
            Element def = (Element) document
                    .selectSingleNode("/dictionary/parameters/parameter[@name=\"" + name + "\"]");
            if (null != def) {
                parameter = parseParameter(def);
            } else {
                parameter = new FvoParameter(null);
            }
            if (name != null) {
                parameter.setName(name);
            }
            String type = param.attributeValue("type");
            if (type != null) {
                parameter.setType(type);
            }
            String length = param.attributeValue("length");
            if (length != null) {
                parameter.setMessageLength(Integer.decode(length));
            }
            String littleEndian = param.attributeValue("littleEndian");
            if (littleEndian != null) {
                parameter.setLittleEndian(Boolean.parseBoolean(littleEndian));
            }

            if (parameter.getType().equalsIgnoreCase("F")) {
                message.getFparameters().add(parameter);
            }
            if (parameter.getType().equalsIgnoreCase("V")) {
                message.getVparameters().add(parameter);
            }
            if (parameter.getType().equalsIgnoreCase("O")) {
                message.getOparameters().add(parameter);
            }
        }
        this.codeToMessage.put(message.getMessageType(), message);
    }

    //ENUMERATIONS
    //TODO
}

From source file:com.devoteam.srit.xmlloader.sigtran.fvo.FvoDictionary.java

License:Open Source License

/**
 * Parse a parameter from the dictionary file
 * /*from w  ww  .j  av  a 2  s .  c o  m*/
 * @param node         : root of the parameter
 * @throws Exception
 */
FvoParameter parseParameter(Element node) throws Exception {

    //ATTRIBUTES
    FvoParameter parameter = new FvoParameter(null);
    String parameterName = node.attributeValue("name");
    parameter.setName(parameterName);
    String parameterId = node.attributeValue("id");
    if (parameterId != null) {
        parameter.setId(Integer.decode(parameterId));
    }
    String longParameter = node.attributeValue("longParameter");
    if (longParameter != null) {
        parameter.setLongParameter(Boolean.parseBoolean(longParameter));
    }
    String littleEndian = node.attributeValue("littleEndian");
    if (littleEndian != null) {
        parameter.setLittleEndian(Boolean.valueOf(littleEndian));
    }

    //FIELDS
    List listFields = node.elements("field");
    parameter.setFields(new LinkedList<FvoField>());
    for (int i = 0; i < listFields.size(); i++) {
        FvoField field = parseField((Element) listFields.get(i));

        // inherit littleEndian status from parameter
        field.setLittleEndian(parameter.isLittleEndian());

        parameter.getFields().add(field);
    }
    return parameter;
}

From source file:com.devoteam.srit.xmlloader.sigtran.fvo.FvoDictionary.java

License:Open Source License

/**
 * Parse a field from the dictionary file
 * //from   w w w.j  av a 2s . c  o  m
 * @param fieldNode      : root of the field to parse
 * @return            : the fvoField parsed
 * @throws Exception
 */
FvoField parseField(Element fieldNode) throws Exception {
    String fieldName = fieldNode.attributeValue("name");
    String fieldFormat = fieldNode.attributeValue("format");
    String fieldLengthBit = fieldNode.attributeValue("lengthBit");
    String fieldLength = fieldNode.attributeValue("length");
    String fieldValue = fieldNode.attributeValue("enumerationId");
    FvoField field = new FvoField(null);
    if (fieldLengthBit != null) {
        try {
            field.setLengthBit(0, Integer.decode(fieldLengthBit));
        } catch (Exception e) {
            throw new ExecutionException(
                    "a field fieldLengthBit must be an Integer\n" + fieldNode.asXML().replace("   ", ""));
        }
    }
    if (fieldLength != null) {
        try {
            field.setLength(Integer.decode(fieldLength));
        } catch (Exception e) {
            throw new ExecutionException(
                    "a field fieldLength must be an Integer\n" + fieldNode.asXML().replace("   ", ""));
        }
    }
    if (fieldFormat != null) {
        field.setFormat(fieldFormat);
    } else {
        field.setFormat("integer");
    }
    field.setValue(fieldValue);
    field.setName(fieldName);

    return field;
}

From source file:com.devoteam.srit.xmlloader.sigtran.fvo.FvoField.java

License:Open Source License

public void parseElement(Element root) throws Exception {
    String name = root.attributeValue("name");
    if (name == null) {
        //throw new ExecutionException("A name is required for fvo fields\n" + root.asXML());
    } else {/*from   w ww.j  ava2s .co  m*/
        _name = name;
    }
    String value = root.attributeValue("value");
    // test for errors (value is mandatory attribute)
    if (value == null) {
        throw new ExecutionException("A value is required for fvo fields\n" + root.asXML());
    } else {
        this._value = value;
    }

    String format = root.attributeValue("format");
    // field info should already have been completed from dicionary, using constructor that clones
    // test for errors (value is mandatory attribute)
    if (format != null && !format.equalsIgnoreCase(FvoField.formatBinary)
            && !format.equalsIgnoreCase(FvoField.formatInteger)
            && !format.equalsIgnoreCase(FvoField.formatString)) {
        throw new ExecutionException("The format of a FvoField must be one of " + FvoField.formatBinary + ", "
                + FvoField.formatInteger + ", " + FvoField.formatString + ". Error in XML :\n" + root.asXML());
    } else {
        this._format = format;
    }

    String length = root.attributeValue("length");
    String lengthBit = root.attributeValue("lengthBit");
    if (null != length || null != lengthBit) {
        _lengthBit = 0;
        if (null != length) {
            _lengthBit += Integer.decode(length) * 8;
        }

        if (null != lengthBit) {
            _lengthBit += Integer.decode(lengthBit);
        }
    }
}

From source file:com.devoteam.srit.xmlloader.sigtran.fvo.FvoParameter.java

License:Open Source License

public void parseElement(Element root) throws Exception {
    String id = root.attributeValue("id");
    String name = root.attributeValue("name");
    String longParameter = root.attributeValue("longParameter");
    String type = root.attributeValue("type");
    String littleEndian = root.attributeValue("littleEndian");

    List<Element> listFields = root.elements("field");

    // TODO: eventually complete data with information from dictionary

    // search for errors in XML
    if (type != null) {
        if (!(type.equalsIgnoreCase("F") || type.equalsIgnoreCase("V") || type.equalsIgnoreCase("O"))) {
            throw new ExecutionException(
                    "Parameter type value must be set at 'F', 'V' or 'O'\n" + root.asXML().replace("   ", ""));
        }//from  w w  w  .j  a  v a 2 s .  c om
        if ((type.equalsIgnoreCase("O")) && (id == null)) {
            throw new ExecutionException("O Parameters must have a name\n" + root.asXML().replace("   ", ""));
        }
        if (!(type.equalsIgnoreCase("V")) && (longParameter != null)) {
            throw new ExecutionException(
                    "Only V Parameter may have longParameter flag\n" + root.asXML().replace("   ", ""));
        }
    }

    //TODO case pointerLength>2
    //Create the FvoParameter
    if (id != null) {
        setName(name);
    }
    if (id != null) {
        setId(Integer.decode(id));
    }
    if (type != null) {
        setType(type);
    }
    if (id != null) {
        setId(Integer.decode(id));
    }
    if (littleEndian != null) {
        setLittleEndian(Boolean.parseBoolean(littleEndian));
    }

    if (!listFields.isEmpty()) {
        for (int cpt = 0; cpt < listFields.size(); cpt++) {
            Element element = listFields.get(cpt);
            FvoField field = new FvoField(_msg);
            field.setLittleEndian(isLittleEndian());
            field.parseElement(element);
            getFields().add(field);
        }
    }
}

From source file:com.devoteam.srit.xmlloader.sigtran.MsgSigtran.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> listAps = root.elements("ASN");
    Object[] tabAps = listAps.toArray();

    ASNMessage tcapMessage = null;
    if (tabAps.length >= 1) {
        Element elementTCAP = (Element) tabAps[tabAps.length - 1];
        tcapMessage = new BN_TCAPMessage("tcap/dictionary_TCAP.xml");
        tcapMessage.parseFromXML(elementTCAP);
        // TCAP layer (optional)
        this.setTCAPMessage((BN_TCAPMessage) tcapMessage);
    }

    if (tabAps.length >= 2) {
        Element elementAP = (Element) tabAps[0];
        String dictionary = elementAP.attributeValue("dictionary");
        ASNMessage apMessage = new BN_APMessage(dictionary);
        apMessage.parseFromXML(elementAP);
        // AP layer (optional)
        this.setAPMessage((BN_APMessage) apMessage);
    }

    // ISDN layer (optional)
    Element ie = root.element("ISDN");
    if (ie != null) {
        MessageQ931 ieMessage = new MessageQ931(ie);
        this.setIeMessage(ieMessage);
    }

    // SS7 layer (optional)
    Element fvo = root.element("SS7");
    if (fvo != null) {
        FvoDictionary fvoDictionnary = ((StackSigtran) this.stack)
                .getFvoDictionnary(fvo.attributeValue("file"));
        FvoMessage fvoMessage = new FvoMessage(this, fvoDictionnary);
        this.setFvoMessage(fvoMessage);
        fvoMessage.parseElement(fvo);
    }

    // UA layer (mandatory)
    Element tlv = root.element("UA");
    if (tlv != null) {

        TlvDictionary tlvDictionnary = ((StackSigtran) this.stack)
                .getTlvDictionnary(tlv.attributeValue("file"));
        TlvMessage tlvMessage = new TlvMessage(this, tlvDictionnary);
        tlvMessage.parseMsgFromXml(tlv);
        this.setTlvMessage(tlvMessage);
        this.setTlvProtocol(tlvDictionnary.getPpid());
    } else {
        // TODO throw some exception
    }
}

From source file:com.devoteam.srit.xmlloader.sigtran.StackSigtran.java

License:Open Source License

/** Creates a specific Msg */
@Override/*  w ww .ja va2  s  .  c om*/
public Msg parseMsgFromXml(Boolean request, Element root, Runner runner) throws Exception {
    MsgSigtran msgSigtran = new MsgSigtran();

    List<Element> listAps = root.elements("AP");
    Object[] tabAps = listAps.toArray();

    ASNMessage tcapMessage = null;
    if (tabAps.length >= 1) {
        Element elementTCAP = (Element) tabAps[tabAps.length - 1];
        tcapMessage = new BN_TCAPMessage();
        tcapMessage.parseFromXML(elementTCAP);
        String className = tcapMessage.getClassName();
        // TCAP layer (optional)
        msgSigtran.setTCAPMessage((BN_TCAPMessage) tcapMessage);
    }

    if (tabAps.length >= 2) {
        Element elementAP = (Element) tabAps[0];
        ASNMessage apMessage = new BN_APMessage(tcapMessage);
        apMessage.parseFromXML(elementAP);
        String className = apMessage.getClassName();
        // AP layer (optional)
        msgSigtran.setAPMessage((BN_APMessage) apMessage);
    }

    // ISDN layer (optional)
    Element ie = root.element("ISDN");
    if (ie != null) {
        MessageQ931 ieMessage = new MessageQ931(ie);
        msgSigtran.setIeMessage(ieMessage);
    }

    // SS7 layer (optional)
    Element fvo = root.element("SS7");
    if (fvo != null) {
        FvoMessage fvoMessage = new FvoMessage(msgSigtran, getFvoDictionnary(fvo.attributeValue("file")));
        msgSigtran.setFvoMessage(fvoMessage);
        fvoMessage.parseElement(fvo);
    }

    // UA layer (mandatory)
    Element tlv = root.element("UA");
    if (tlv != null) {
        TlvDictionary tlvDictionnary = getTlvDictionnary(tlv.attributeValue("file"));
        TlvMessage tlvMessage = new TlvMessage(msgSigtran, tlvDictionnary);
        tlvMessage.parseMsgFromXml(tlv);
        msgSigtran.setTlvMessage(tlvMessage);
        msgSigtran.setTlvProtocol(tlvDictionnary.getPpid());
    } else {
        // TODO throw some exception
    }

    return msgSigtran;
}

From source file:com.devoteam.srit.xmlloader.sigtran.StackSigtranHybrid.java

License:Open Source License

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

    List<Element> listAps = root.elements("AP");
    Object[] tabAps = listAps.toArray();
    if (tabAps.length >= 1) {
        ASNMessage apMessage = new BN_TCAPMessage();
        apMessage.parseFromXML(((Element) tabAps[0]));
        String className = apMessage.getClassName();
        if (className.contains(".tcap.")) {
            // TCAP layer (optional)
            msgSigtran.setTCAPMessage(apMessage);
        } else {
            // generic AP layer (optional)
            msgSigtran.setAPMessage(apMessage);
        }
    }

    if (tabAps.length >= 2) {
        ASNMessage apMessage = new MobicentTCAPMessage();
        apMessage.parseFromXML(((Element) tabAps[1]));
        String className = apMessage.getClassName();
        if (className.contains(".tcap.")) {
            // TCAP layer (optional)
            msgSigtran.setTCAPMessage(apMessage);
        } else {
            // generic AP layer (optional)
            msgSigtran.setAPMessage(apMessage);
        }
    }

    // ISDN layer (optional)
    Element ie = root.element("ISDN");
    if (ie != null) {
        MessageQ931 ieMessage = new MessageQ931(ie);
        msgSigtran.setIeMessage(ieMessage);
    }

    // SS7 layer (optional)
    Element fvo = root.element("SS7");
    if (fvo != null) {
        FvoMessage fvoMessage = new FvoMessage(msgSigtran, getFvoDictionnary(fvo.attributeValue("file")));
        msgSigtran.setFvoMessage(fvoMessage);
        fvoMessage.parseElement(fvo);
    }

    // UA layer (mandatory)
    Element tlv = root.element("UA");
    if (tlv != null) {
        TlvDictionary tlvDictionnary = getTlvDictionnary(tlv.attributeValue("file"));
        TlvMessage tlvMessage = new TlvMessage(msgSigtran, tlvDictionnary);
        tlvMessage.parseMsgFromXml(tlv);
        msgSigtran.setTlvMessage(tlvMessage);
        msgSigtran.setTlvProtocol(tlvDictionnary.getPpid());
    } else {
        // TODO throw some exception
    }

    return msgSigtran;
}