Example usage for org.dom4j Element getText

List of usage examples for org.dom4j Element getText

Introduction

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

Prototype

String getText();

Source Link

Document

Returns the text value of this element without recursing through child elements.

Usage

From source file:com.devoteam.srit.xmlloader.smtp.MsgSmtp.java

License:Open Source License

/** 
 * Parse the message from XML element /* www. java2s  .  c  om*/
 */
@Override
public void parseFromXml(Boolean request, Element root, Runner runner) throws Exception {
    this.data = root.getText();
    this.data = Utils.replaceNoRegex(this.data, "\r\n", "\n");
    this.data = Utils.replaceNoRegex(this.data, "\n", "\r\n");
    if (!this.data.endsWith("\r\n")) {
        this.data += "\r\n";
    }
}

From source file:com.devoteam.srit.xmlloader.smtp.StackSmtp.java

License:Open Source License

public Msg parseMsgFromXml(Boolean request, Element root, Runner runner) throws Exception {
    String text = root.getText();
    MsgSmtp msgSmtp = new MsgSmtp(text);
    String transactionIdStr = root.attributeValue("transactionId");

    if (transactionIdStr != null) {
        TransactionId transactionId = new TransactionId(transactionIdStr);
        msgSmtp.setTransactionId(transactionId);
        Msg requestSmtp = StackFactory.getStack(StackFactory.PROTOCOL_SMTP).getInTransaction(transactionId)
                .getBeginMsg();/*from  ww  w  .j  a va2s.c  om*/
        msgSmtp.setType(requestSmtp.getType());
    } else {
        String channelName = root.attributeValue("channel");
        // deprecated part //
        if (channelName == null)
            channelName = root.attributeValue("sessionName");
        // deprecated part //
        Channel channel = getChannel(channelName);
        if (channel == null) {
            throw new ExecutionException("The channel <name=" + channelName + "> does not exist");
        }
        msgSmtp.setChannel(getChannel(channelName));
    }
    return msgSmtp;
}

From source file:com.devoteam.srit.xmlloader.tcp.MsgTcp.java

License:Open Source License

/** 
 * Parse the message from XML element //w w  w .jav  a2s .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[]>();
    ;
    try {
        for (Element element : elements) {
            if (element.attributeValue("format").equalsIgnoreCase("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 = Utils.replaceNoRegex(text, "\r\n", "\n");
                text = Utils.replaceNoRegex(text, "\n", "\r\n");
                datas.add(text.getBytes("UTF8"));
            } else if (element.attributeValue("format").equalsIgnoreCase("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++;
        }
    }

    decode(data);
}

From source file:com.devoteam.srit.xmlloader.tcp.StackTcp.java

License:Open Source License

/** Creates a specific Msg */
@Override/*from   ww w .  j a  v a  2  s.  c  o m*/
public Msg parseMsgFromXml(Boolean request, Element root, Runner runner) throws Exception {
    //
    // Parse all <data ... /> tags
    //
    List<Element> elements = root.elements("data");
    List<byte[]> datas = new LinkedList<byte[]>();
    ;
    try {
        for (Element element : elements) {
            if (element.attributeValue("format").equalsIgnoreCase("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 = Utils.replaceNoRegex(text, "\r\n", "\n");
                text = Utils.replaceNoRegex(text, "\n", "\r\n");
                datas.add(text.getBytes("UTF8"));
            } else if (element.attributeValue("format").equalsIgnoreCase("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++;
        }
    }

    MsgTcp msgTcp = new MsgTcp(data);

    // instanciates the channel
    String channelName = root.attributeValue("channel");
    // deprecated part //
    if (channelName == null)
        channelName = root.attributeValue("connectionName");
    // deprecated part //
    Channel channel = getChannel(channelName);
    if (channel == null) {
        throw new ExecutionException("The channel <name=" + channelName + "> does not exist");
    }
    msgTcp.setChannel(getChannel(channelName));

    return msgTcp;
}

From source file:com.devoteam.srit.xmlloader.tls.MsgTls.java

License:Open Source License

/** 
 * Parse the message from XML element //  w  ww  .  j  a  v  a2s .co 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) {
        if (element.attributeValue("format").equalsIgnoreCase("text")) {
            String text = element.getText();
            // change the \n caractere 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 = Utils.replaceNoRegex(text, "\r\n", "\n");
            text = Utils.replaceNoRegex(text, "\n", "\r\n");
            datas.add(text.getBytes("UTF8"));
        } else if (element.attributeValue("format").equalsIgnoreCase("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++;
        }
    }

    decode(data);
}

From source file:com.devoteam.srit.xmlloader.tls.StackTls.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 {
    //
    // Parse all <data ... /> tags
    //
    List<Element> elements = root.elements("data");
    List<byte[]> datas = new LinkedList<byte[]>();
    ;
    try {
        for (Element element : elements) {
            if (element.attributeValue("format").equalsIgnoreCase("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 = Utils.replaceNoRegex(text, "\r\n", "\n");
                text = Utils.replaceNoRegex(text, "\n", "\r\n");
                datas.add(text.getBytes("UTF8"));
            } else if (element.attributeValue("format").equalsIgnoreCase("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++;
        }
    }

    MsgTls msgtls = new MsgTls(data);

    // instanciates the conn
    String channelName = root.attributeValue("channel");
    // deprecated part //
    if (channelName == null)
        channelName = root.attributeValue("connectionName");
    // deprecated part //
    Channel channel = getChannel(channelName);
    if (channel == null) {
        throw new ExecutionException("The channel <name=" + channelName + "> does not exist");
    }
    msgtls.setChannel(getChannel(channelName));

    return msgtls;
}

From source file:com.devoteam.srit.xmlloader.udp.MsgUdp.java

License:Open Source License

/** 
 * Parse the message from XML element //from   w  w  w .  j ava2  s  .  co 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[]>();

    try {
        for (Element element : elements) {
            if (element.attributeValue("format").equalsIgnoreCase("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 = Utils.replaceNoRegex(text, "\r\n", "\n");
                text = Utils.replaceNoRegex(text, "\n", "\r\n");
                datas.add(text.getBytes("UTF8"));
            } else if (element.attributeValue("format").equalsIgnoreCase("binary")) {
                String text = element.getTextTrim();
                datas.add(Utils.parseBinaryString(text));
            }
        }
    } catch (Exception e) {
        throw new ExecutionException("StackUDP: Error while parsing data", e);
    }

    //
    // Compute total length
    //
    int dataLength = 0;
    for (byte[] data : datas) {
        dataLength += data.length;
    }

    this.data = new byte[dataLength];
    int i = 0;
    for (byte[] aData : datas) {
        for (int j = 0; j < aData.length; j++) {
            this.data[i] = aData[j];
            i++;
        }
    }

    String length = root.attributeValue("length");
    if (length != null) {
        dataLength = Integer.parseInt(length);
        GlobalLogger.instance().getApplicationLogger().debug(TextEvent.Topic.PROTOCOL,
                "fixed length of the datagramPacket to be sent:  ", dataLength);
        if (this.data.length != dataLength) {
            GlobalLogger.instance().getApplicationLogger().warn(TextEvent.Topic.PROTOCOL,
                    "data.length different from chosen fixed length");
        }
    }
}

From source file:com.devoteam.srit.xmlloader.udp.StackUdp.java

License:Open Source License

/** Creates a specific Msg */
@Override/* w  ww  .ja v  a2  s  . c om*/
public Msg parseMsgFromXml(Boolean request, Element root, Runner runner) throws Exception {
    //
    // Parse all <data ... /> tags
    //
    List<Element> elements = root.elements("data");
    List<byte[]> datas = new LinkedList<byte[]>();

    try {
        for (Element element : elements) {
            if (element.attributeValue("format").equalsIgnoreCase("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 = Utils.replaceNoRegex(text, "\r\n", "\n");
                text = Utils.replaceNoRegex(text, "\n", "\r\n");
                datas.add(text.getBytes("UTF8"));
            } else if (element.attributeValue("format").equalsIgnoreCase("binary")) {
                String text = element.getTextTrim();
                datas.add(Utils.parseBinaryString(text));
            }
        }
    } catch (Exception e) {
        throw new ExecutionException("StackUDP: Error while parsing data", e);
    }

    //
    // Compute total length
    //
    int dataLength = 0;
    for (byte[] data : datas) {
        dataLength += data.length;
    }

    byte[] data = new byte[dataLength];
    int i = 0;
    for (byte[] aData : datas) {
        for (int j = 0; j < aData.length; j++) {
            data[i] = aData[j];
            i++;
        }
    }

    String length = root.attributeValue("length");
    if (length != null) {
        dataLength = Integer.parseInt(length);
        GlobalLogger.instance().getApplicationLogger().debug(TextEvent.Topic.PROTOCOL,
                "fixed length of the datagramPacket to be sent:  ", dataLength);
        if (data.length != dataLength) {
            GlobalLogger.instance().getApplicationLogger().warn(TextEvent.Topic.PROTOCOL,
                    "data.length different from chosen fixed length");
        }
    }

    MsgUdp msgUdp = new MsgUdp(data, dataLength);

    String remoteHost = root.attributeValue("remoteHost");
    String remotePort = root.attributeValue("remotePort");

    // deprecated part //
    String name = root.attributeValue("socketName");
    if (name != null) {
        Channel channel = getChannel(name);
        if (channel == null) {
            throw new ExecutionException("StackUDP: The connection <name=" + name + "> does not exist");
        }

        if (remoteHost != null) {
            channel.setRemoteHost(remoteHost);
        }
        if (remotePort != null) {
            channel.setRemotePort(new Integer(remotePort).intValue());
        }
        msgUdp.setChannel(channel);
    } // deprecated part //
    else {
        name = root.attributeValue("listenpoint");
        Listenpoint listenpoint = getListenpoint(name);
        if (listenpoint == null) {
            throw new ExecutionException("StackUDP: The listenpoint <name=" + name + "> does not exist");
        }

        if (remoteHost != null) {
            msgUdp.setRemoteHost(remoteHost);
        }
        if (remotePort != null) {
            msgUdp.setRemotePort(new Integer(remotePort).intValue());
        }
        msgUdp.setListenpoint(listenpoint);
    }

    return msgUdp;
}

From source file:com.dockingsoftware.dockingpreference.NodeInfo.java

License:Apache License

/**
 * Constructor./*from  ww w.  ja  v  a 2s  .c o  m*/
 * 
 * @param element 
 */
public NodeInfo(Element element) {
    this.element = element;
    this.name = element.getName();
    this.clazz = element.attributeValue(GlobalConstant.CLASS);
    this.adapter = element.attributeValue(GlobalConstant.ADAPTER);
    this.value = element.getText().trim();// field value
    this.arrayType = element.attributeValue(GlobalConstant.ARRAY_TYPE);
    this.arrayLength = element.attributeValue(GlobalConstant.ARRAY_LENGTH);
}

From source file:com.dockingsoftware.dockingpreference.PreferenceProcessor.java

License:Apache License

/**
 * Loads user preference information./*  w  w w.  j a va2s .  c  o  m*/
 * 
 * @param obj
 * @param parent
 * @throws ClassNotFoundException
 * @throws InstantiationException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException 
 */
private void load(Object obj, Element parent) throws ClassNotFoundException, InstantiationException,
        IllegalArgumentException, IllegalAccessException, Exception {

    Class cls = obj.getClass();

    if (cls.isArray()) {
        List<NodeInfo> infos = new ArrayList<NodeInfo>();
        List<Element> lstChildren = parent.elements();
        for (int i = 0; i < lstChildren.size(); i++) {
            Element child = lstChildren.get(i);
            NodeInfo nodeInfo = new NodeInfo(child);
            infos.add(nodeInfo);
        }

        Object array = obj;

        for (int i = 0; i < infos.size(); i++) {
            NodeInfo info = infos.get(i);
            Class fieldClass = Class.forName(info.getClazz());
            if (!StringUtils.isBlank(info.getValue())) {

                PersistentAdapterIF adapter;
                if (info.getAdapter() == null) {
                    adapter = new DefaultPersistentAdapter();
                } else {
                    adapter = (PersistentAdapterIF) Class.forName(info.getAdapter()).newInstance();
                }
                //Gets the new value for the field of obj.
                Object fieldValue = adapter.restore(info.getValue(), fieldClass);
                Array.set(array, i, fieldValue);
            } else {

                Object fieldValue = fieldClass.newInstance();
                // Loading children
                load(fieldValue, info.getElement());
                Array.set(array, i, fieldValue);
            }
        }
    } else if (obj instanceof Collection) {

        List<NodeInfo> infos = new ArrayList<NodeInfo>();
        List<Element> lstChildren = parent.elements();
        for (int i = 0; i < lstChildren.size(); i++) {
            Element child = lstChildren.get(i);
            NodeInfo nodeInfo = new NodeInfo(child);
            infos.add(nodeInfo);
        }

        Collection coll = (Collection) obj;

        for (int i = 0; i < infos.size(); i++) {
            NodeInfo info = infos.get(i);
            Class fieldClass = Class.forName(info.getClazz());
            if (!StringUtils.isBlank(info.getValue())) {

                PersistentAdapterIF adapter = null;
                if (info.getAdapter() == null) {
                    adapter = new DefaultPersistentAdapter();
                } else {
                    adapter = (PersistentAdapterIF) Class.forName(info.getAdapter()).newInstance();
                }
                //Gets the new value for the field of obj.
                Object fieldValue = adapter.restore(info.getValue(), fieldClass);
                coll.add(fieldValue);
            } else {

                Object fieldValue = fieldClass.newInstance();
                // Loading children
                load(fieldValue, info.getElement());
                coll.add(fieldValue);
            }
        }
    } else if (obj instanceof Map) {
        // Key is always String type. 
        Map<String, NodeInfo> infos = new HashMap<String, NodeInfo>();
        List<Element> lstKeys = parent.elements();
        for (int i = 0; i < lstKeys.size(); i++) {
            Element keyEle = lstKeys.get(i);
            Element valueEle = (Element) keyEle.elements().get(0);
            NodeInfo valueInfo = new NodeInfo(valueEle);
            infos.put(keyEle.getText().trim(), valueInfo);
        }

        Map map = (Map) obj;

        Iterator<String> iter = infos.keySet().iterator();
        while (iter.hasNext()) {
            String key = iter.next();
            NodeInfo valueInfo = infos.get(key);
            Class fieldClass = Class.forName(valueInfo.getClazz());
            if (!StringUtils.isBlank(valueInfo.getValue())) {

                PersistentAdapterIF adapter = null;
                if (valueInfo.getAdapter() == null) {
                    adapter = new DefaultPersistentAdapter();
                } else {
                    adapter = (PersistentAdapterIF) Class.forName(valueInfo.getAdapter()).newInstance();
                }
                //Gets the new value for the field of obj.
                Object fieldValue = adapter.restore(valueInfo.getValue(), fieldClass);
                map.put(key, fieldValue);
            } else {

                Object fieldValue = fieldClass.newInstance();
                // Loading children
                load(fieldValue, valueInfo.getElement());
                map.put(key, fieldValue);
            }
        }
    } else {

        Map<String, NodeInfo> infos = new HashMap<String, NodeInfo>();
        List<Element> lstChildren = parent.elements();
        for (int i = 0; i < lstChildren.size(); i++) {
            Element child = lstChildren.get(i);
            NodeInfo nodeInfo = new NodeInfo(child);
            infos.put(nodeInfo.getName(), nodeInfo);
        }

        Field[] fields = cls.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            fields[i].setAccessible(true);
            NodeInfo info = infos.get(fields[i].getName());
            if (info != null) {

                Class fieldClass = Class.forName(info.getClazz());
                if (!StringUtils.isBlank(info.getValue())) {

                    PersistentAdapterIF adapter = null;
                    if (info.getAdapter() == null) {
                        adapter = new DefaultPersistentAdapter();
                    } else {
                        adapter = (PersistentAdapterIF) Class.forName(info.getAdapter()).newInstance();
                    }
                    //Gets the new value for the field of obj.
                    Object fieldValue = adapter.restore(info.getValue(), fieldClass);
                    fields[i].set(obj, fieldValue);

                } else {

                    if (fieldClass == java.lang.reflect.Array.class) {

                        Class arryCls = Class.forName(info.getArrayType());
                        Object fieldValue = Array.newInstance(arryCls, Integer.parseInt(info.getArrayLength()));
                        // Loading children
                        load(fieldValue, info.getElement());
                        fields[i].set(obj, fieldValue);
                    } else {

                        Object fieldValue = fieldClass.newInstance();
                        // Loading children
                        load(fieldValue, info.getElement());
                        fields[i].set(obj, fieldValue);
                    }
                }
            }
        }

    }
}