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

License:Open Source License

public void parseFromXML(Element elementRoot, Dictionary dictionary, ElementAbstract elemDico)
        throws Exception {
    //si non present dans le dico on parse le fichier xml
    if (elemDico == null) {
        String tagStr = elementRoot.attributeValue("identifier");
        if (tagStr == null) {
            tagStr = elementRoot.attributeValue("tag");
        }/*  w ww  .  j a va 2s  .co  m*/
        tagStr = tagStr.trim();
        int iPos = tagStr.indexOf(":");
        String label = null;
        String value = tagStr;
        if (iPos >= 0) {
            label = tagStr.substring(0, iPos);
            value = tagStr.substring(iPos + 1);
        }

        int tagInt = getTagValueFromBinary(value);
        this.tag = tagInt;
        this.label = label;
    }

    // for Q931 protocols
    String labelTag = elementRoot.attributeValue("name");
    if (labelTag != null) {
        this.label = labelTag;
    }

    String instances = elementRoot.attributeValue("instances");
    if (instances != null) {
        this.instances = Integer.parseInt(instances);
    }

    List<Element> listField = elementRoot.elements("field");
    for (Iterator<Element> it = listField.iterator(); it.hasNext();) {
        Element fieldRoot = it.next();
        String name = fieldRoot.attributeValue("name");
        FieldAbstract field = null;
        // Case if field is present in the dico
        if (elemDico != null) {
            field = elemDico.getFieldsByName(name);
        }
        if (field == null) {
            field = FieldAbstract.parseFromXML(fieldRoot);
        }
        this.fieldsByName.put(name, field);
        this.fields.add(field);
    }

    // initiate the Array containing the fields
    this.fieldsArray = new SupArray();
    Array emptyArray = new DefaultArray(getLengthElem() / 8);
    this.fieldsArray.addFirst(emptyArray);
    this.subelementsArray = new SupArray();

    // set the value for each fields
    listField = elementRoot.elements("field");
    //boucle pour setter tous les field de elemV
    int offset = 0;
    for (Iterator<Element> it = listField.iterator(); it.hasNext();) {
        Element element1 = it.next();
        String fieldName = element1.attributeValue("name");
        FieldAbstract field = this.fieldsByName.get(fieldName);
        if (field != null) {
            String value = element1.attributeValue("value");
            if (value != null) {
                field.setValue(value, offset, this.fieldsArray);
            } else {
                field.setOffset(offset);
            }
            int length = field.length;
            if (length != 0) {
                offset += length;
            } else {
                offset = this.fieldsArray.length * 8;
            }
        } else {
            throw new ExecutionException(
                    "The field \"" + fieldName + "\" is not found in the element : \"" + this.tag + "\"");
        }
    }

    //parse the sub-elements
    List<Element> listElement = elementRoot.elements("element");
    ElementAbstract subElemDico = null;
    ElementAbstract elem = null;
    for (Iterator<Element> it = listElement.iterator(); it.hasNext();) {
        Element elemElement = it.next();
        if (dictionary != null) {
            subElemDico = dictionary.getElementFromXML(elemElement);
            elem = (ElementAbstract) subElemDico.cloneAttribute();
            elem.parseFromXML(elemElement, dictionary, subElemDico);
            this.elements.add(elem);
        }
    }

}

From source file:com.devoteam.srit.xmlloader.core.coding.binary.EnumerationField.java

License:Open Source License

public EnumerationField(Element rootXML) {
    super(rootXML);

    List<Element> list = rootXML.elements("enum");
    for (Element elemEnum : list) {
        String valueStr = elemEnum.attributeValue("value");
        String nameStr = elemEnum.attributeValue("name");
        int iPos = valueStr.indexOf('-');
        if (iPos >= 0) {
            String beginStr = valueStr.substring(0, iPos);
            String endStr = valueStr.substring(iPos + 1);
            EnumRange range = new EnumRange(beginStr, endStr, nameStr);
            ranges.add(range);//from  www . j av  a2  s.  c o  m
            this.valuesByLabel.put(nameStr, range.getBegin());
        } else {
            byte[] valueBytes = Utils.parseBinaryString(valueStr);
            int value = (int) valueBytes[0] & 0xFF;
            this.valuesByLabel.put(nameStr, value);
            this.labelsByValue.put(value, nameStr);
        }
    }

}

From source file:com.devoteam.srit.xmlloader.core.coding.binary.EnumLongField.java

License:Open Source License

@Override
public void parseFromXML(Element rootXML, boolean parseDico) {
    super.parseFromXML(rootXML, parseDico);

    List<Element> list = rootXML.elements("enum");
    for (Element elemEnum : list) {
        String valueStr = elemEnum.attributeValue("value");
        String nameStr = elemEnum.attributeValue("name");
        int iPos = valueStr.indexOf('-');
        if (iPos >= 0) {
            String beginStr = valueStr.substring(0, iPos);
            String endStr = valueStr.substring(iPos + 1);
            EnumRange range = new EnumRange(beginStr, endStr, nameStr);
            ranges.add(range);/*from  w  w  w.  j av  a  2 s  .  c  o  m*/
            this.valuesByLabel.put(nameStr, range.getBeginValue());
            this.labelsByValue.put(range.getBeginValue(), nameStr);
        } else {
            byte[] valueBytes = Utils.parseBinaryString(valueStr);
            long value = EnumRange.toLong(valueBytes);

            this.valuesByLabel.put(nameStr, value);
            this.labelsByValue.put(value, nameStr);
        }
    }

}

From source file:com.devoteam.srit.xmlloader.core.coding.binary.EnumStringField.java

License:Open Source License

@Override
public void parseFromXML(Element rootXML, boolean parseDico) {
    super.parseFromXML(rootXML, parseDico);

    List<Element> list = rootXML.elements("enum");
    for (Element elemEnum : list) {
        String valueStr = elemEnum.attributeValue("value");
        String nameStr = elemEnum.attributeValue("name");
        this.valuesByLabel.put(nameStr, valueStr);
        this.labelsByValue.put(valueStr, nameStr);
    }/* www.j a  v a2 s.co m*/

}

From source file:com.devoteam.srit.xmlloader.core.coding.binary.FieldAbstract.java

License:Open Source License

public FieldAbstract(Element rootXML) {
    name = rootXML.attributeValue("name");
    this.length = 0;
    String lengthBit = rootXML.attributeValue("lengthBit");
    if (lengthBit != null) {
        this.length = Integer.parseInt(lengthBit);
    }/*from w  w w.  jav a 2  s  . co  m*/
}

From source file:com.devoteam.srit.xmlloader.core.coding.binary.FieldAbstract.java

License:Open Source License

public static FieldAbstract parseFromXML(Element fieldRoot) throws Exception {
    String type = fieldRoot.attributeValue("type");
    String name = fieldRoot.attributeValue("name");
    FieldAbstract newField = null;//  ww w .j a v a2s.  com
    if (type == null) {
        throw new ExecutionException("ERROR : The type attribute for the field \"" + name
                + "\" is mandatory because the element he belongs to is not present in the dictionary.");
    }
    if (type.equalsIgnoreCase("integer")) {
        newField = new IntegerField(fieldRoot);
    } else if (type.equalsIgnoreCase("boolean")) {
        newField = new BooleanField(fieldRoot);
    } else if (type.equalsIgnoreCase("enumeration")) {
        newField = new EnumerationField(fieldRoot);
    } else if (type.equalsIgnoreCase("string")) {
        newField = new StringField(fieldRoot);
    } else if (type.equalsIgnoreCase("length_string")) {
        newField = new LengthStringField(fieldRoot);
    } else if (type.equalsIgnoreCase("length2_string")) {
        newField = new Length2StringField(fieldRoot);
    } else if (type.equalsIgnoreCase("binary")) {
        newField = new BinaryField(fieldRoot);
    } else if (type.equalsIgnoreCase("length2_binary")) {
        newField = new Length2BinaryField(fieldRoot);
    } else if (type.equalsIgnoreCase("number_bcd")) {
        newField = new NumberBCDField(fieldRoot);
    } else if (type.equalsIgnoreCase("number_mmc")) {
        newField = new NumberMMCField(fieldRoot);
    } else if (type.equalsIgnoreCase("ipv4_address")) {
        newField = new IPV4AddressField(fieldRoot);
    } else if (type.equalsIgnoreCase("ipv6_address")) {
        newField = new IPV6AddressField(fieldRoot);
    } else {
        throw new ExecutionException(
                "ERROR : The field type \"" + type + "\" is not supported in the field \"" + name + "\"");
    }
    return newField;
}

From source file:com.devoteam.srit.xmlloader.core.coding.binary.q931.HeaderQ931.java

License:Open Source License

@Override
public void parseFromXML(Element header, Dictionary dictionary) throws Exception {
    this.dictionary = dictionary;

    if (header.attributeValue("type").startsWith("b")) {
        _typeArray = new Integer08Array((Utils.parseBinaryString(header.attributeValue("type")))[0]);
    } else {/*from w w  w. ja  v a2  s. co m*/
        EnumerationField field = (EnumerationField) dictionary.getHeaderFieldByName("Message type");
        _typeArray = new Integer08Array(field.getEnumValueByLabel(header.attributeValue("type")));
    }

    String discriminator = header.attributeValue("discriminator");
    try {
        _discrimArray = new Integer08Array((Utils.parseBinaryString(discriminator))[0]);
    } catch (Exception e) {
        EnumerationField field = (EnumerationField) dictionary.getHeaderFieldByName("Protocol discriminator");
        _discrimArray = new Integer08Array(field.getEnumValueByLabel(discriminator));
    }
    String callReference = header.attributeValue("callReference");
    if (callReference != null) {
        _callReferenceArray = new DefaultArray(Utils.parseBinaryString(callReference));
        if (_callReferenceArray.length > 2) {
            throw new ExecutionException(
                    "ISDN layer : The \"callReference\" attribute value for the header is too long [0...32767]");
        }
    }
    String layer3Address = header.attributeValue("layer3Address");
    if (layer3Address != null) {
        _layer3AddressArray = new DefaultArray(Utils.parseBinaryString(layer3Address));
    }
}

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);/* www  .j a  v a 2s .  co 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.newstats.StatPool.java

License:Open Source License

/**
 * add the static stat counters : specially for the editable parameters and test sections
 *///from w  ww.j a  va2  s. c o m
public void addStatsStaticTestParameters(Test test) {
    this.addValue(new StatKey(StatPool.PREFIX_TEST, test.getName(), "_name"), test.getName());
    this.addValue(new StatKey(StatPool.PREFIX_TEST, test.getName(), "_description"), test.getDescription());
    this.addValue(new StatKey(StatPool.PREFIX_TEST, test.getName(), "_filename"),
            test.getXMLDocument().getXMLFile().toASCIIString());
    this.addValue(new StatKey(StatPool.PREFIX_TEST, test.getName(), "_version"), Tester.getRelease());

    DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
    this.addValue(new StatKey(StatPool.PREFIX_TEST, "_currentTime"),
            dateFormat.format(this.getZeroTimestamp() + this.getLastTimestamp()));
    this.addValue(new StatKey(StatPool.PREFIX_TEST, "_currentDuration"),
            Helper.getElapsedTimeString(this.getLastTimestamp() / 1000));
    if (this.isActivate()) {
        this.addValue(new StatKey(StatPool.PREFIX_TEST, "_currentInterval"), 0);
        float lInterval = (float) ((StatCounter) this.getValue(
                new StatKey(StatPool.PREFIX_TEST, "_currentInterval"))).graphDataset.graphParameters.graphPeriod
                / 1000;
        String interval = lInterval + " s";
        this.addValue(new StatKey(StatPool.PREFIX_TEST, "_currentInterval"), interval);
    }

    // We add stats about configuration of the test -------------------------
    List<Element> list = test.getEditableParameters();
    for (Element elementEditable : list) {
        String name = elementEditable.attributeValue("name");
        name = name.substring(1, name.length() - 1);
        String description = elementEditable.attributeValue("description");
        String value = elementEditable.attributeValue("value");
        StatKey statKey = new StatKey(StatPool.PREFIX_PARAM, "value", name, "_value");
        addValue(statKey, value);
        // create the corresponding template
        CounterReportTemplate template = new CounterReportTemplate("<text>", statKey, null, name, name,
                description);
        StatCounterConfigManager.getInstance().addTemplateList(new StatKey(StatPool.PREFIX_PARAM), template);
    }
}

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

License:Open Source License

/**
 * Execute operation//ww  w  . j av  a2s .co m
 *
 * @param session Current session
 * @return Next operation or null by default
 */
@Override
public Operation execute(Runner runner) throws Exception {
    GlobalLogger.instance().getSessionLogger().info(runner, TextEvent.Topic.CORE, this);

    // get the function
    String name = getRootElement().attributeValue("name");

    Function function = FunctionsCache.instance().getFunction(name,
            ((ScenarioRunner) runner).getScenarioReference());

    // prepare input arguments (copy parameters in hashmap)
    HashMap<String, Parameter> inputs = new HashMap();
    for (Object object : getRootElement().selectNodes("./input/parameter")) {
        Element element = (Element) object;
        String inputName = element.attributeValue("name");

        if (inputName.contains("function:")) {
            inputName = Utils.replaceNoRegex(inputName, "function:", "");
        }

        String inputValue = element.attributeValue("value");

        if (!Parameter.matchesParameter(inputName)) {
            throw new ExecutionException("invalid input name format in function call\r\n" + element.asXML()
                    + "\r\nfrom\r\n" + getRootElement().asXML(), null);
        }

        if (Parameter.matchesParameter(inputValue)) {
            LinkedList<String> inputValueParsed = runner.getParameterPool()
                    .parse(ParameterPool.unbracket(inputValue));
            if (inputValueParsed.size() != 1) {
                throw new ExecutionException("invalid parameter name size\r\n" + element.asXML()
                        + "\r\nfrom\r\n" + getRootElement().asXML(), null);
            }

            Parameter inputValueParameter = runner.getParameterPool()
                    .get(ParameterPool.bracket(inputValueParsed.getFirst()));

            Parameter inputParameter = new Parameter();
            inputParameter.addAll(inputValueParameter.getArray());
            inputs.put(inputName, inputParameter);
        } else {
            LinkedList<String> inputValueParsed = runner.getParameterPool().parse(inputValue);
            Parameter inputParameter = new Parameter();
            inputParameter.addAll(inputValueParsed);
            inputs.put(inputName, inputParameter);
        }
    }

    if (null == function) {
        throw new ExecutionException("could not find function " + name);
    }

    // execute the function
    HashMap<String, Parameter> outputs = function.execute(runner, inputs);

    // put back the function into the cache
    FunctionsCache.instance().freeFunction(function);

    // copy back output parameters to parameter pool
    for (Object object : getRootElement().selectNodes("./output/parameter")) {
        Element element = (Element) object;
        String outputName = element.attributeValue("name");
        String outputValue = element.attributeValue("value");

        if (null != outputName && outputName.contains("function:")) {
            outputName = Utils.replaceNoRegex(outputName, "function:", "");
        }

        if (null != outputValue && outputValue.contains("function:")) {
            outputValue = Utils.replaceNoRegex(outputValue, "function:", "");
        }

        if (!Parameter.matchesParameter(outputName)) {
            throw new ExecutionException("invalid output name format in function call\r\n" + element.asXML()
                    + "\r\nfrom\r\n" + getRootElement().asXML(), null);
        }

        if (null != outputValue && !Parameter.matchesParameter(outputValue)) {
            throw new ExecutionException("invalid output value format in function call\r\n" + element.asXML()
                    + "\r\nfrom\r\n" + getRootElement().asXML(), null);
        }

        if (null == outputValue) {
            if (!outputs.containsKey(outputName)) {
                throw new ExecutionException("invalid output name; not present in function output\r\n"
                        + element.asXML() + "\r\nfrom\r\n" + getRootElement().asXML(), null);
            }

            runner.getParameterPool().set(outputName, outputs.get(outputName));
            GlobalLogger.instance().getSessionLogger().info(runner, TextEvent.Topic.CORE, "setted output ",
                    outputValue, " as ", outputName, " in parameter pool");
        } else {
            if (!outputs.containsKey(outputValue)) {
                throw new ExecutionException("invalid output value; not present in function output\r\n"
                        + element.asXML() + "\r\nfrom\r\n" + getRootElement().asXML(), null);
            }

            runner.getParameterPool().set(outputName, outputs.get(outputValue));
            GlobalLogger.instance().getSessionLogger().info(runner, TextEvent.Topic.CORE, "setted output ",
                    outputName, " as ", outputName, " in parameter pool");
        }

    }

    // some logs
    GlobalLogger.instance().getSessionLogger().info(runner, TextEvent.Topic.CORE,
            "<function> executed " + name);
    return null;
}