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

License:Open Source License

/** Creates a specific Msg */
@Override//from  www .ja v  a2 s.c om
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 .jav  a  2 s . 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 w  w.ja v a2  s  .co 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.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 av  a2 s.com
        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 ww  w .  j  av a  2s.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//from  www. ja  va 2  s.co m
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.  j  a  v  a2  s.  co m*/
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;
}

From source file:com.devoteam.srit.xmlloader.sigtran.tlv.TlvDictionary.java

License:Open Source License

private void parseFile(InputStream stream) throws Exception {
    //      variables
    int i = 0;//from w  ww. j av a  2  s  .  co m
    int j = 0;
    int k = 0;

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

    Element root = (Element) document.selectSingleNode("/dictionary");
    String ppidStr = root.attributeValue("ppid");
    _ppid = Integer.parseInt(ppidStr);

    //Class and type
    List listClassType = document.selectNodes("/dictionary/classType/class");
    for (i = 0; i < listClassType.size(); i++) {
        Element classNode = (Element) listClassType.get(i);
        String classId = classNode.attributeValue("id");
        String className = classNode.attributeValue("name");

        List listType = classNode.elements("type");
        for (j = 0; j < listType.size(); j++) {
            Element typeNode = (Element) listType.get(j);
            String typeId = typeNode.attributeValue("id");
            String typeName = typeNode.attributeValue("name");
            String class_Type = classId;
            class_Type += ":" + typeId;

            messageTypeValue.put(typeName, typeId);
            messageTypeName.put(class_Type, typeName);
        }
        try {
            messageClassValue.put(className, (Integer.decode(classId)));
            messageClassName.put((Integer.decode(classId)), className);
        } catch (Exception e) {
            throw new ExecutionException(
                    "a class id must be an Integer\n" + classNode.asXML().replace("   ", ""));
        }

    }
    //parameters

    List listParameters = document.selectNodes("/dictionary/parameters/parameter");
    for (i = 0; i < listParameters.size(); i++) {
        Element parameterNode = (Element) listParameters.get(i);
        String parameterName = parameterNode.attributeValue("name");
        String parameterTag = parameterNode.attributeValue("tag");
        TlvParameter parameter = new TlvParameter(null, this);
        if (parameterTag != null) {
            try {
                parameter.setTag(Integer.decode(parameterTag));
            } catch (Exception e) {
                throw new ExecutionException(
                        "a tag of a parameter must be an Integer\n" + parameterNode.asXML().replace("   ", ""));
            }
        }
        parameter.setName(parameterName);
        this.parameter.put(parameterName, parameter);
        this.parameterName.put(parameter.getTag(), parameterName);

        List listFields = parameterNode.elements("field");
        for (j = 0; j < listFields.size(); j++) {
            TlvField field = parseField((Element) listFields.get(j));
            parameter.getFields().add(field);

        }
    }

    //enumerations
    List listEnumeration = document.selectNodes("/dictionary/enumerations/enumeration");
    for (i = 0; i < listEnumeration.size(); i++) {
        Element enumerationNode = (Element) listEnumeration.get(i);
        List listValue = enumerationNode.elements("value");
        String enumerationName = enumerationNode.attributeValue("name");

        for (j = 0; j < listValue.size(); j++) //-value-
        {
            Element valueNode = (Element) listValue.get(j);
            List listField = valueNode.elements("field");
            String valueName = valueNode.attributeValue("name");
            String valueCode = valueNode.attributeValue("code");

            if (valueCode != null) {
                try {
                    Integer.decode(valueCode);
                } catch (Exception e) {
                    throw new ExecutionException(
                            "a valueCode must be an Integer\n" + valueNode.asXML().replace("   ", ""));
                }
            }

            String enumerationName_code = enumerationName + ":" + valueCode;
            enumerationFromCode.put(enumerationName_code, valueName);
            String enumerationName_name = enumerationName + ":" + valueName;
            enumerationFromName.put(enumerationName_name, valueCode);
        }
    }

}

From source file:com.devoteam.srit.xmlloader.sigtran.tlv.TlvMessage.java

License:Open Source License

public void parseMsgFromXml(Element root) throws Exception {
    //header//from ww  w .j av a  2  s . co  m
    Element header = root.element("header");
    String version = header.attributeValue("version");
    String reserved = header.attributeValue("reserved");
    String messageClass = header.attributeValue("messageClass");
    String messageType = header.attributeValue("messageType");
    String messageLength = header.attributeValue("messageLength");

    if (version != null) {
        setVersion(Integer.decode(version));
    }

    if (reserved != null) {
        setReserved(Integer.decode(reserved));
    }

    if (messageClass != null) {
        try {
            setMessageClass(Integer.decode(messageClass));
        } catch (Exception e) {
            try {
                setMessageClass(_dictionary.messageClassValue(messageClass));
            } catch (Exception e2) {
                throw new ExecutionException("The messageClass of the message header is unrecognized\n"
                        + root.asXML().replace("   ", ""));
            }
        }
    } else {
        throw new ExecutionException(
                "The messageClass of the message header must be set\n" + root.asXML().replace("   ", ""));
    }

    if (messageType != null) {
        try {
            setMessageType(Integer.decode(messageType));
        } catch (Exception e) {
            try {
                setMessageType(_dictionary.messageTypeValue(messageType));
            } catch (Exception e2) {
                throw new ExecutionException("The messageType of the message header is unrecognized\n"
                        + root.asXML().replace("   ", ""));
            }
        }
    } else {
        throw new ExecutionException(
                "The messageType of the message header must be set\n" + root.asXML().replace("   ", ""));
    }

    if (messageLength != null) {
        setMessageLength(Integer.decode(messageLength));
    }

    List<Element> parameters = root.elements("parameter");
    for (int cpt = 0; cpt < parameters.size(); cpt++) {
        Element element = parameters.get(cpt);
        TlvParameter parameter = new TlvParameter(_msg, _dictionary);
        parameter.parseElement(element);
        getParameters().add(parameter);
    }
}

From source file:com.devoteam.srit.xmlloader.sigtran.tlv.TlvParameter.java

License:Open Source License

/**
 * Parse the TLV parameter from the XML element
 *//*from   ww  w.  j  av  a2 s.c om*/
public void parseElement(Element root) throws Exception {
    String tagStr = root.attributeValue("tag");
    List<Element> listParameters = root.elements("parameter");
    List<Element> listFields = root.elements("field");

    // try to get parameter def from dictionary
    TlvParameter dictionaryParameter;
    int tagInt;
    try {
        tagInt = Integer.decode(tagStr);

        // the tag is already an integer
        dictionaryParameter = _dictionary.parameter(tagInt, _msg);
    } catch (Exception e) {
        // the tag was not an integer
        dictionaryParameter = _dictionary.parameter(tagStr, _msg);
        tagInt = dictionaryParameter.getTag();
        if (null == dictionaryParameter) {
            throw new ParsingException("Tag was not an integer and was not found in dictionary.\n", e);
        }
    }

    // set the tag
    _tag.setValue(tagInt);

    // if possible, copy human readable name from dictionary
    if (null != dictionaryParameter) {
        _name = dictionaryParameter._name;
    }

    // parse the inner fields
    if (!listFields.isEmpty()) {
        for (Element parameter : listFields) {
            TlvField tlvField = new TlvField(_msg, _dictionary);

            // parse a first time (to init name...)
            tlvField.parseElement(parameter);

            // if the parameter is defined in the dictionary, then try to find definition of the field
            if (null != dictionaryParameter) {
                for (TlvField dictionaryField : dictionaryParameter.getFields()) {
                    if (dictionaryField.getName().equalsIgnoreCase(tlvField.getName())) {
                        tlvField.init(dictionaryField);
                    }
                }
            }

            // parse a second time (to override dictionary values...)
            tlvField.parseElement(parameter);

            // check the length has been defined
            if (("integer".equalsIgnoreCase(tlvField.getFormat())
                    || "spare".equalsIgnoreCase(tlvField.getFormat()))
                    && (-1 == tlvField.getLength() && -1 == tlvField.getLengthBit())) {
                throw new ParsingException(
                        "length attributes are not defined, neither in dictionary or in scenario\n"
                                + root.asXML());
            }

            _fields.add(tlvField);
        }
    } else if (!listParameters.isEmpty()) {
        // parse the inner parameters
        for (Element parameter : listParameters) {
            TlvParameter tlvParameter = new TlvParameter(_msg, _dictionary);
            tlvParameter.parseElement(parameter);
            _parameters.add(tlvParameter);
        }
    }
}