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.core.coding.binary.q931.MessageQ931.java

License:Open Source License

public MessageQ931(Element root) throws Exception {
    this.syntax = root.attributeValue("syntax");
    initDictionary(syntax);/*from ww  w  .  j a  va2  s. c o m*/

    this.header = new HeaderQ931();
    this.header.parseFromXML(root.element("header"), dictionary);

    this.elements = new ArrayList<ElementAbstract>();

    List<Element> elementsInf = root.elements("element");
    ElementAbstract elemInfo = null;
    ElementAbstract elem = null;
    for (Element element : elementsInf) {
        element.addAttribute("coding", "Q931");
        elemInfo = this.dictionary.getElementFromXML(element);
        elem = (ElementQ931) elemInfo.cloneAttribute();
        // FH Manage a new Element like ElementQ931big for id = User-User:126
        elem.parseFromXML(element, this.dictionary, elemInfo);

        this.elements.add(elem);
    }
}

From source file:com.devoteam.srit.xmlloader.core.operations.basic.OperationSwitch.java

License:Open Source License

/**
 * Constructor/*ww  w.  j a v a2  s  .  c o m*/
 *
 * @param condition       boolean variable which represents a condition of the if statment
 * @param operationsThen    List of operations executed if the value of condition is true
 * @param operationsElse   List of operations executed if the value of condition is false
 */
public OperationSwitch(Element root, Scenario scenario) throws Exception {
    super(root, null);
    this.scenario = scenario;
    this.parameter = root.attributeValue("parameter");
    List<Element> list = root.elements("case");

    equalsValues = new String[list.size()];
    matchesValues = new String[list.size()];
    operationSequences = new OperationSequence[list.size()];

    int i = 0;
    for (Element element : list) {
        operationSequences[i] = new OperationSequence(element, scenario);
        equalsValues[i] = element.attributeValue("equals");
        matchesValues[i] = element.attributeValue("matches");

        boolean isEquals = null != equalsValues[i];
        boolean isMatches = null != matchesValues[i];

        if (!(isEquals ^ isMatches))
            throw new ParsingException("case element cannot contain both matches and equals attributes");

        i++;
    }

    Element defaultElement = root.element("default");
    if (null != defaultElement)
        defaultOperationSequence = new OperationSequence(defaultElement, scenario);
    else
        defaultOperationSequence = null;
}

From source file:com.devoteam.srit.xmlloader.core.RunProfile.java

License:Open Source License

public synchronized void parse(Element root) throws Exception {
    long now = System.currentTimeMillis();

    this.executions = 0;
    this.points = new ArrayList();
    this.profilePeriod = PROFILE_INF;

    Element start = root.element("start");
    if (null != start) {
        if (null != start.attributeValue("time")) {
            String date = start.attributeValue("time");
            this.startTime = DateUtils.parseDate(date);
        } else if (null != start.attributeValue("delay")) {
            this.startTime = -(long) (Double.parseDouble(start.attributeValue("delay")) * 1000);
        } else {/*  ww w.  jav a2s.  c  o m*/

        }

        if (null != start.attributeValue("time") && null != start.attributeValue("delay")) {
            throw new ParsingException("only specify time or delay attributes in <start>, not both");
        }

    } else {
        this.startTime = 0;
    }

    for (Object object : root.elements("step")) {
        Element element = (Element) object;

        long time = (long) Double.parseDouble(element.attributeValue("time")) * 1000;

        if (time == this.profilePeriod)
            time += 1;
        else if (time < this.profilePeriod)
            throw new ParsingException("time isn't greater than previous time");

        double frequence;

        String periodStr = element.attributeValue("period");
        String frequencyStr = element.attributeValue("frequency");

        if (periodStr == null && frequencyStr == null)
            throw new ParsingException("missing period or frequence attribute");
        if (periodStr != null && frequencyStr != null)
            throw new ParsingException("please define only period or frequence attribute");

        if (null != periodStr && Double.parseDouble(periodStr) > 0) {
            frequence = (1 / Double.parseDouble(periodStr));
        } else {
            frequence = PROFILE_INF;
        }

        if (null != frequencyStr)
            frequence = Double.parseDouble(frequencyStr);
        if (frequence == 0)
            frequence = 0.000000001;
        this.points.add(new Point(time, frequence));
        this.profilePeriod = time;
    }

    // add steps if there is only one or no step at time 0
    if (this.points.size() == 0) {
        double period = Config.getConfigByName("tester.properties").getDouble("runprofile.PERIOD");
        if (0 == period)
            period = PROFILE_INF;
        this.points.add(new Point(0, period));
        // add point @ 0
    }

    // add steps if there is only one or no step at time 0
    if (this.points.get(0).date != 0) {
        this.points.add(0, new Point(0, 0.000000001));
        // add point @ 0
    }

    // if there is only one step at zero
    if (this.points.size() == 1) {
        // add point to make a constant, ten second later
        this.points.add(this.points.get(0).clone());

        if (this.points.get(0).frequency == -1) {
            this.points.get(1).date = 10000;
        } else {
            double delay = 1 / this.points.get(0).frequency;

            while (delay < 1)
                delay *= 10;

            delay = Math.round(delay);

            this.points.get(1).date = delay * 10000;
        }

        this.profilePeriod = (long) Math.round(this.points.get(1).date);
    }

    Element end = root.element("end");
    if (null != end) {
        boolean hasTime = true;
        boolean hasExecutions = true;

        if (null != end.attributeValue("time")) {
            String date = end.attributeValue("time");
            this.endTime = DateUtils.parseDate(date);
        } else if (null != end.attributeValue("delay")) {
            long delay = (long) (Double.parseDouble(end.attributeValue("delay")) * 1000);
            if (delay == 0)
                this.endTime = 0;
            else
                this.endTime = -delay;
        } else {
            hasTime = false;
        }

        if (null != end.attributeValue("iteration")) {
            this.executions = Long.parseLong(end.attributeValue("iteration"));
        } else {
            hasExecutions = false;
        }

        if (null != end.attributeValue("time") && null != end.attributeValue("delay")) {
            throw new ParsingException("only specify time or delay attributes in <end>, not both");
        }
        if (!hasTime && !hasExecutions) {
            // some error, must define end by time or iterations
        }
    } else {
        long delay = (long) Config.getConfigByName("tester.properties").getDouble("runprofile.DURATION", 0)
                * 1000;
        if (delay == 0)
            this.endTime = 0;
        else
            this.endTime = -delay;

        this.executions = Config.getConfigByName("tester.properties").getInteger("runprofile.NUMBER", 0);
    }

    long tempStartTime = 0;
    if (startTime < 0)
        tempStartTime = now - startTime;
    if (startTime > 0)
        tempStartTime = startTime;

    long tempEndTime = 0;
    if (endTime < 0)
        tempEndTime = now - endTime;
    if (endTime > 0)
        tempEndTime = endTime;

    if (0 != tempEndTime && tempEndTime <= tempStartTime)
        throw new ParsingException(
                "start time cannot be greater than end time (check start and end dates or delays?)");

    // if the profile is a zero-only, set executions to zero and endTime to startTime
    nothingToDo = true;
    for (int i = 0; i < points.size(); i++) {
        if (points.get(i).frequency != 0.000000001) {
            nothingToDo = false;
            break;
        }
    }

    this.profileSize = this.points.size();
}

From source file:com.devoteam.srit.xmlloader.diameter.dictionary.Application.java

License:Open Source License

public void parseApplication(Element root) throws ParsingException {
    List<Element> elements;

    elements = root.elements("vendor");
    for (Element element : elements) {
        parseVendor(element);//from   w w  w  .j a  v a 2 s . c  om
    }

    elements = root.elements("command");
    for (Element element : elements) {
        parseCommand(element);
    }

    elements = root.elements("typedefn");
    for (Element element : elements) {
        parseType(element);
    }

    elements = root.elements("avp");
    for (Element element : elements) {
        parseAvp(element);
    }
}

From source file:com.devoteam.srit.xmlloader.diameter.dictionary.Application.java

License:Open Source License

private void parseAvp(Element root) throws ParsingException {
    String name = null;/*from ww w .  j  a  v a2  s. c  o m*/
    String description = null;
    String may_encrypt = null;
    String mandatory = null;
    String protected_ = null;
    String vendor_bit = null;

    if (null != root.attributeValue("name"))
        name = root.attributeValue("name");
    else {
        Dictionary.traceWarning("Invalid avp.name, skipping");
        return;
    }
    if (null != root.attributeValue("description"))
        description = root.attributeValue("description");
    if (null != root.attributeValue("may-encrypt"))
        may_encrypt = root.attributeValue("may-encrypt");
    if (null != root.attributeValue("mandatory"))
        mandatory = root.attributeValue("mandatory");
    if (null != root.attributeValue("protected"))
        protected_ = root.attributeValue("protected");
    if (null != root.attributeValue("vendor-bit"))
        vendor_bit = root.attributeValue("vendor-bit");

    int code;
    try {
        code = Integer.parseInt(root.attributeValue("code"));
    } catch (Exception e) {
        code = -1;
    }

    if (code == -1) {
        Dictionary.traceWarning("Missing avp.code, skipping");
        return;
    }

    boolean constrained = false;
    if (null != root.attributeValue("constrained"))
        constrained = Boolean.parseBoolean(root.attributeValue("constrained"));

    VendorDef vendor_id = null;
    if (null != root.attributeValue("vendor-id"))
        vendor_id = Dictionary.getInstance().getVendorDefByName(root.attributeValue("vendor-id"), _name);

    TypeDef type = null;
    {
        List<Element> elements;
        elements = root.elements("type");
        for (Element element : elements) {
            String type_name = element.attributeValue("type-name");
            type = Dictionary.getInstance().getTypeDefByName(type_name, _name);
            if (type == null) {
                Dictionary.traceWarning("Invalid avp.type (" + type_name + "), skipping");
            }
        }
    }

    AvpDef avpDef = new AvpDef(name, code, description, may_encrypt, protected_, vendor_bit, mandatory,
            constrained, type, vendor_id);

    // parse enums
    {
        List<Element> elements;
        elements = root.elements("enum");
        for (Element element : elements) {
            String enum_name = null;
            if (null != element.attribute("name")) {
                enum_name = element.attributeValue("name");
            } else {
                Dictionary.traceWarning("No avp.enum.name in " + avpDef.get_name() + ", skipping this enum");
                continue;
            }

            int enum_code = -1;
            if (null != element.attribute("code")) {
                try {
                    enum_code = Integer.parseInt(element.attributeValue("code"));
                } catch (Exception e) {
                    Dictionary.traceWarning(
                            "Invalid avp.enum.code in " + avpDef.get_name() + ", skipping this enum");
                    continue;
                }
            } else {
                Dictionary.traceWarning("No avp.enum.code in " + avpDef.get_name() + ", skipping this enum");
                continue;
            }

            avpDef.addEnum(enum_name, enum_code);
        }
    }

    // parse grouped
    {
        Element elementGrouped = root.element("grouped");
        if (null != elementGrouped) {
            List<Element> elements;
            elements = elementGrouped.elements("gavp");

            for (Element element : elements) {
                String gavp_name = element.attributeValue("name");
                avpDef.addGroupedAvpName(gavp_name);
            }
        }
    }

    if (null != getAvpDefByCode(avpDef.get_code()))
        Dictionary.traceWarning("AvpDef of code " + avpDef.get_code() + " already exists, overwriting");
    if (null != getAvpDefByName(avpDef.get_name()))
        Dictionary.traceWarning("AvpDef of name " + avpDef.get_name() + " already exists, overwriting");

    avpDefByName.put(avpDef.get_name(), avpDef);
    avpDefByCode.put(Integer.toString(avpDef.get_code()), avpDef);
}

From source file:com.devoteam.srit.xmlloader.diameter.MsgDiameterParser.java

License:Open Source License

/** Parses then returns all the AVPs from the XML root element */
public void parseAllAVPs(Message message, Element root) throws Exception {
    List<Element> avpList = root.elements("avp");
    for (Element avpElement : avpList) {
        AVP avp = parseAvp(avpElement);/*w ww . ja  va2  s .  c o m*/
        if (avp != null) {
            message.add(avp);
        }
    }
}

From source file:com.devoteam.srit.xmlloader.diameter.MsgDiameterParser.java

License:Open Source License

/** Parses an element <avp>; recursively if it contains other AVPs, used by parseMessage */
private AVP parseAvp(Element element) throws Exception {
    AVP avp;//w w  w .jav a  2 s  . c o m

    // avp attributes
    String code_str = element.attributeValue("code");
    String value = element.attributeValue("value");
    String type = element.attributeValue("type");
    String vendor_id_str = element.attributeValue("vendorId");
    String mandatory_str = element.attributeValue("mandatory");
    String private_str = element.attributeValue("private");
    String state = element.attributeValue("state");

    boolean bState = true;
    if (state != null && state.equalsIgnoreCase("false")) {
        bState = false;
    }
    if (!bState) {
        return null;
    }

    int vendor_id = 0;
    int code = 0;

    boolean mandatory_bool = false;
    boolean private_bool = false;

    // error if no code is specified
    if (null == code_str)
        throw new ParsingException("In element :" + element + "\n" + "no avp code");

    // flags attribute is not used
    if (null != element.attributeValue("flags"))
        throw new ParsingException(
                "In element :" + element + "\n" + "flags attribute is currently not supported in avp tag");

    // vendorSpecific attribute is not used
    if (null != element.attributeValue("vendorSpecific"))
        throw new ParsingException("In element :" + element + "\n"
                + "VendorSpecific attribute is currently not supported in avp tag");

    if (null == type) {
        type = "OctetString";
    }

    //
    // AVP Code
    //
    code = Integer.parseInt(code_str);

    //
    // AVP Mandatory Flag
    //
    if (null != mandatory_str) {
        mandatory_bool = Boolean.parseBoolean(mandatory_str);
    }

    //
    // AVP Private Flag
    //
    if (null != private_str) {
        private_bool = Boolean.parseBoolean(private_str);
    }

    //
    // AVP Vendor ID
    //
    if (null != vendor_id_str) {
        vendor_id = Integer.parseInt(vendor_id_str);
    }

    //
    // Grouped AVP
    //
    if (type.equalsIgnoreCase("grouped")) {
        //
        // Parse child avps
        //
        List<AVP> avpList = new LinkedList<AVP>();
        List<Element> list = element.elements("avp");
        for (Element e : list) {
            AVP subAvp = parseAvp(e);
            if (subAvp != null) {
                avpList.add(subAvp);
            }
        }

        // create the AVP
        // add parsed childs to grouped AVP
        AVP_Grouped gAvp = new AVP_Grouped(code, avpList.toArray(new AVP[0]));
        avp = gAvp;
    } else {
        //
        // Error if no value is specified
        //
        if (null == value)
            throw new ParsingException("In element :" + element + "\n" + "AVP of code " + code_str
                    + " should have a value since it is not a grouped AVP");
        //
        // Create the AVP
        //
        if (type.equalsIgnoreCase("OctetString")) {
            try {
                avp = new AVP_OctetString(code, Utils.parseBinaryString(value));
            } catch (Exception e) {
                avp = new AVP_OctetString(code, value.getBytes());
            }
        } else if (type.equalsIgnoreCase("IPAddress")) {
            avp = new AVP_OctetString(code, InetAddress.getByName(value).getAddress());
        } else if (type.equalsIgnoreCase("UTF8String")) {
            avp = new AVP_OctetString(code, value.getBytes());
        } else if (type.equalsIgnoreCase("Integer32")) {
            avp = new AVP_Integer32(code, Integer.parseInt(value));
        } else if (type.equalsIgnoreCase("Integer64")) {
            avp = new AVP_Integer64(code, Long.parseLong(value));
        } else if (type.equalsIgnoreCase("Unsigned32")) {
            UnsignedInt32 unsignedInt32 = new UnsignedInt32(value);
            avp = new AVP_Unsigned32(code, unsignedInt32.intValue());
        } else if (type.equalsIgnoreCase("Unsigned64")) {
            UnsignedInt64 unsignedInt64 = new UnsignedInt64(value);
            avp = new AVP_Unsigned64(code, unsignedInt64.longValue());
        } else {
            throw new ParsingException("no matching avp type in protocol stack for type " + type);
        }
    }

    avp.vendor_id = vendor_id;
    avp.setMandatory(mandatory_bool);
    avp.setPrivate(private_bool);

    return avp;
}

From source file:com.devoteam.srit.xmlloader.gtp.data.MessageGTP.java

License:Open Source License

public MessageGTP(Element root) throws Exception {
    Element elementHeader;/* w w w  . ja v  a 2s. co m*/
    elementHeader = root.element("headerPrime");
    if (elementHeader != null) {
        this.header = new HeaderGTPPrime();
        this.syntax = header.getSyntax();
        initDictionary(this.syntax);
        this.header.parseFromXML(elementHeader, dictionary);
    }
    elementHeader = root.element("headerV1");
    if (elementHeader != null) {
        this.header = new HeaderGTPV1();
        this.syntax = header.getSyntax();
        initDictionary(this.syntax);
        this.header.parseFromXML(elementHeader, dictionary);
    }
    elementHeader = root.element("headerV2");
    if (elementHeader != null) {
        this.header = new HeaderGTPV2();
        this.syntax = header.getSyntax();
        initDictionary(this.syntax);
        this.header.parseFromXML(elementHeader, dictionary);
    }

    this.elements = new ArrayList<ElementAbstract>();
    List<Element> elementsInf = root.elements("element");
    ElementAbstract elemDico = null;
    ElementAbstract newElement = null;
    for (Element elementRoot : elementsInf) {
        elemDico = this.dictionary.getElementFromXML(elementRoot);
        newElement = (ElementAbstract) elemDico.cloneAttribute();
        newElement.parseFromXML(elementRoot, this.dictionary, elemDico);

        this.elements.add(newElement);

    }
    // Header GTPv1 plus data field -> get raw datas to be sent as T-PDU
    this.tpdu = new DataPDU();
    this.tpdu.parseFromXML(root, dictionary, elemDico);
}

From source file:com.devoteam.srit.xmlloader.gtppr.StackGtpp.java

License:Open Source License

private void parseAtt(Attribute att, List<Element> attributes) throws Exception {
    LinkedList<Object> listAtt = null;
    String value = null;/*from  w w w  .j a  v a2s  .c  om*/

    for (Element elementAtt : attributes) {
        value = elementAtt.attributeValue("name");
        listAtt = ((LinkedList) att.getValue());
        for (int i = 0; i < listAtt.size(); i++) {
            if (listAtt.get(i) instanceof GtppAttribute) {
                GtppAttribute attribute = (GtppAttribute) listAtt.get(i);
                if (attribute.getName().equalsIgnoreCase(value)) {
                    if (!attribute.getFormat().equalsIgnoreCase("list")) {
                        if (attribute.getValueQuality())//duplicate att in case it is already set
                        {
                            GtppAttribute duplicateAtt = attribute.clone();
                            attribute = duplicateAtt;
                            listAtt.add(attribute);
                        }

                        value = elementAtt.attributeValue("value");
                        if (attribute.getValueQuality())//if an attribute already exist with the same name, duplicate it in the list
                        {
                            listAtt.add(i + 1, attribute.clone());
                            attribute = (GtppAttribute) listAtt.get(i + 1);
                        }
                        setAttributeValue(attribute, value);
                        break;
                    } else//if this attribute is also a list => recursive call
                    {
                        parseAtt(attribute, elementAtt.elements("attribute"));
                    }
                }
            } else if (listAtt.get(i) instanceof LinkedList) {
                LinkedList list = (LinkedList) listAtt.get(i);
                for (int j = 0; j < list.size(); j++)
                    parseAtt((Attribute) list.get(j), elementAtt.elements("attribute"));
            }

            if (i == listAtt.size())//insert attribute at the place given in this list
            {
                GtppAttribute newAtt = new GtppAttribute();
                newAtt.setName(value);
                value = elementAtt.attributeValue("value");
                newAtt.setValue(value.getBytes());
                listAtt.add(attributes.indexOf(elementAtt), newAtt);
                break;
            }
        }
    }
}

From source file:com.devoteam.srit.xmlloader.h323.h225cs.XmlToAsn1.java

License:Open Source License

public void initField(Object objClass, Element element, Field field, String ClasseName)
        throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, InstantiationException,
        InvocationTargetException, NoSuchMethodException {
    // si le champ est priv, pour y accder
    field.setAccessible(true);//from w  ww. j  av a  2  s .  c  o  m
    // pour ne pas traiter les static
    if (field.toGenericString().contains("static")) {
        return;
    }
    if (field.getType().getCanonicalName().contains("Collection")) {
        // type DANS la collection

        // Rcuprer le type des lements de la collection
        Type[] elementParamTypeTab = ((ParameterizedType) field.getGenericType()).getActualTypeArguments();

        // Exception si la collection n'a pas un seul argument
        if (elementParamTypeTab.length != 1) {
            throw new RuntimeException("Message d'erreur");
        }

        Class collectionElementType = (Class) elementParamTypeTab[0];

        // creer la collection
        ArrayList<Object> listInstance = new ArrayList<Object>();

        // parcourir les enfants <instance> de element
        List<Element> children = element.elements("instance");
        for (Element elementInstance : children) {
            // pour chaque <instance>
            listInstance.add(parseField(elementInstance, collectionElementType.getCanonicalName(), ClasseName));
        }
        // set la collection dans le field
        field.set(objClass, listInstance);
    } else {
        field.set(objClass, parseField(element, field.getType().getCanonicalName(), ClasseName));
    }
}