Example usage for org.dom4j Element createCopy

List of usage examples for org.dom4j Element createCopy

Introduction

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

Prototype

Element createCopy();

Source Link

Document

Creates a deep copy of this element The new element is detached from its parent, and getParent() on the clone will return null.

Usage

From source file:com.devoteam.srit.xmlloader.core.operations.functions.FunctionsRegistry.java

License:Open Source License

/**
 * return a copy of a dom tree (because it will be used to create a Function, and the
 * function will modify the dom tree with the replacers) or null if unknown
 *///from w  w  w . j a  v a  2s  .  co  m
public synchronized Element element(String name) {
    Element element = _registry.get(name);
    if (null == element) {
        return null;
    } else {
        return element.createCopy();
    }
}

From source file:com.devoteam.srit.xmlloader.core.utils.XMLElementAVPParser.java

License:Open Source License

public List<Element> replace(Element element, ParameterPool parameterPool) throws Exception {
    List<Element> result;

    if (element.getName().equalsIgnoreCase("header") || element.getName().startsWith("send")) {
        result = xmlElementDefaultParser.replace(element, parameterPool);
    } else // <avp .../>
    {//from  w w  w.j ava 2  s .com
        LinkedList list = new LinkedList<Element>();

        List<Attribute> attributes;
        attributes = element.attributes();

        int allowedParameterLength = -1;
        boolean hasParameter = false;

        for (Attribute attribute : attributes) {
            String value = attribute.getValue();
            Matcher matcher = Parameter.pattern.matcher(value);

            while (matcher.find()) {
                String variableStr = matcher.group();

                if (false == parameterPool.isConstant(variableStr)) {
                    Parameter variable = parameterPool.get(variableStr);
                    hasParameter = true;
                    if (variable != null) {
                        if (allowedParameterLength == -1 || allowedParameterLength == 1) {
                            allowedParameterLength = variable.length();
                        } else if (variable.length() != 1 && allowedParameterLength != variable.length()) {
                            throw new ExecutionException("Invalid length of variables : a variable of length "
                                    + allowedParameterLength + " has been found but " + variableStr
                                    + " has a length of " + variable.length());
                        }
                    }
                }
            }
        }

        if (!hasParameter) {
            allowedParameterLength = 1;
        }

        for (int i = 0; i < allowedParameterLength; i++) {
            Element newElement = element.createCopy();

            List<Attribute> newElementAttributes;
            newElementAttributes = newElement.attributes();

            for (Attribute newAttribute : newElementAttributes) {
                String value = newAttribute.getValue();

                Pattern pattern = Pattern.compile(Parameter.EXPRESSION);
                Matcher matcher = pattern.matcher(value);
                int offset = 0;
                while (matcher.find()) {
                    String before = value.substring(0, matcher.end() + offset - 1);
                    String after = value.substring(matcher.end() + offset - 1);

                    if (parameterPool.exists(matcher.group())) {
                        int index = i;
                        if (parameterPool.get(matcher.group()).length() == 1) {
                            index = 0;
                        }

                        value = before + "(" + index + ")" + after;
                        offset += ((String) "(" + index + ")").length();
                    }

                }
                newAttribute.setValue(value);
            }

            List<Element> tempList = xmlElementDefaultParser.replace(newElement, parameterPool);
            try {
                for (Element e : tempList) {
                    // MsgDiameterParser.getInstance().doDictionnary(e, "0", false);

                    list.addAll(xmlElementTextOnlyParser.replace(e, parameterPool));
                }
            } catch (Exception e) {
                throw new ExecutionException("Error while checking parsed variables against dictionary", e);
            }

        }
        result = list;
    }

    return result;
}

From source file:com.devoteam.srit.xmlloader.core.utils.XMLElementDefaultParser.java

License:Open Source License

public List<Element> replace(Element element, ParameterPool parameterPool) throws Exception {
    List<Element> list = new LinkedList<Element>();

    Element newElement = element.createCopy();
    list.add(newElement);//from  w w  w .  ja v  a 2 s  .co  m
    List<Attribute> attributes = newElement.attributes();

    for (Attribute attribute : attributes) {
        String value = attribute.getValue();

        LinkedList<String> parsedValue = parameterPool.parse(value);

        if (parsedValue.size() != 1) {
            throw new ExecutionException("Invalid size of variables in attribute " + value);
        }

        attribute.setValue(parsedValue.getFirst());
    }
    return list;
}

From source file:com.devoteam.srit.xmlloader.core.utils.XMLElementRTPFLOWParser.java

License:Open Source License

public List<Element> replace(Element element, ParameterPool parameterPool) throws Exception {
    List<Element> result = new LinkedList();

    //do classic replacement of attribute and save it in result
    Element newElement = element.createCopy();
    result.add(newElement);//  ww  w  .ja  v  a  2s.c  om
    List<Attribute> attributes = newElement.attributes();

    for (Attribute attribute : attributes) {
        if (!attribute.getName().equalsIgnoreCase("timestamp")
                && !attribute.getName().equalsIgnoreCase("seqnum")
                && !attribute.getName().equalsIgnoreCase("deltaTime")
                && !attribute.getName().equalsIgnoreCase("mark")) {
            String value = attribute.getValue();

            LinkedList<String> parsedValue = parameterPool.parse(value);

            if (parsedValue.size() != 1) {
                throw new ExecutionException("Invalid size of variables in attribute " + value);
            }

            attribute.setValue(parsedValue.getFirst());
        }
    }

    return result;
}

From source file:com.devoteam.srit.xmlloader.core.utils.XMLElementTextOnlyParser.java

License:Open Source License

public List<Element> replace(Element element, ParameterPool variables) throws Exception {
    List<Element> result = new LinkedList();
    result.add(element.createCopy());
    element = result.get(0);//from   w  w  w  .  java 2 s .  c om

    Iterator nodesIterator = element.nodeIterator();
    HashMap<Node, Node> nodesToReplace = new HashMap<Node, Node>();

    boolean alreadyNext = false;

    while (nodesIterator.hasNext()) {
        Node node = null;
        if (!alreadyNext) {
            node = (Node) nodesIterator.next();
        }

        Node lastTextNode = null;
        String lastTextNodeText = "";
        alreadyNext = false;

        //
        // We put all successive TEXT Nodes into one node ( there is some fragmentation i don't understand )
        //
        while (null != node && node.getNodeType() == Node.TEXT_NODE) {
            alreadyNext = true;
            lastTextNode = (Text) node;
            lastTextNodeText += lastTextNode.getText();

            // this node will be deleted later ... if not overwritten in this hashmap
            nodesToReplace.put(lastTextNode, null);
            if (nodesIterator.hasNext()) {
                node = (Node) nodesIterator.next();
            } else {
                node = null;
            }
        }

        //
        // We process normally the CDATA Nodes
        // 
        if (null != node && node.getNodeType() == Node.CDATA_SECTION_NODE) {
            lastTextNode = (Node) node;
            lastTextNodeText = lastTextNode.getText();
        }

        //
        // We do nothing for the other type Nodes
        // 
        if (null == lastTextNode) {
            continue;
        }

        lastTextNode.setText(lastTextNodeText);

        //
        // Now that we have only one, complete, TEXT node or one CDATA node to proceed
        //
        Node textNode = (Node) lastTextNode;

        String text = textNode.getText();
        String out = "";

        int endOfLine;
        String line;

        //
        // Transform all \r\n, in \n
        //
        //text = Utils.replaceNoRegex(text, "\r", "");

        while (text.length() > 0) {
            //
            // Read a line
            //
            endOfLine = text.indexOf("\n");

            if (endOfLine == -1) {
                line = text;
                text = "";
            } else {
                line = text.substring(0, endOfLine + 1);
                text = text.substring(endOfLine + 1);
            }

            //
            // Replace line if it contains at least a variable
            //
            if (Parameter.containsParameter(line)) {
                List<String> results = variables.parse(line);

                for (String s : results) {
                    out += s;
                }
            } else {
                out += line;
            }
        }

        //
        // Set new text into new AVP
        //
        Text newTextNode = new DefaultText(out);
        nodesToReplace.put(textNode, newTextNode);
    }

    for (Node key : nodesToReplace.keySet()) {
        DefaultElementInterface.replaceNode((DefaultElement) element, key, nodesToReplace.get(key));
    }

    if (result.size() != 1) {
        throw new ExecutionException("Size of result for XMLElementTextOnlyParser should be 1");
    }

    return result;
}

From source file:com.devoteam.srit.xmlloader.core.utils.XMLTree.java

License:Open Source License

public XMLTree(Element root, boolean duplicate) {
    elementsOrder = new LinkedList<Element>();
    elementsMap = new HashMap<Element, List<Element>>();
    lock = new ReentrantLock();
    if (duplicate) {
        this.root = root.createCopy();
    } else {/*from  w  w w .j  a va2s  .c  o  m*/
        this.root = root;
    }
}

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

License:Open Source License

public void doDictionnary(Element root, String applicationId, boolean recurse) throws ParsingException {
    Application application = Dictionary.getInstance().getApplication(applicationId);

    if (null == application) {
        throw new ParsingException("Unknown \"applicationId\" attribute in header: " + applicationId);
    }/*w  ww .  ja v  a  2  s  . com*/

    Element unmodifiedRoot = root.createCopy();

    if (root.getName().equalsIgnoreCase("header")) {
        //
        // ApplicationId
        //
        String attributeValue;

        attributeValue = root.attributeValue("applicationId");
        if (!Utils.isInteger(attributeValue)) {
            root.attribute("applicationId").setValue(Integer.toString(application.get_id()));
        }

        //
        // CommandCode
        //
        attributeValue = root.attributeValue("command");
        if (!Utils.isInteger(attributeValue)) {
            CommandDef commandDef = Dictionary.getInstance().getCommandDefByName(attributeValue, applicationId);
            if (commandDef == null) {
                throw (new ParsingException(
                        "Unknown \"command\" attribute in header: " + attributeValue + "skipp it"));
            }
            root.attribute("command").setValue(Integer.toString(commandDef.get_code()));
        }

    } else if (root.getName().equalsIgnoreCase("avp")) {
        boolean isTypeAppId = false;
        boolean isTypeVendorId = false;
        String attributeValue;

        attributeValue = root.attributeValue("code");
        //
        // Set default values implied by code in XMLTree from dictionnary
        //
        if (null != attributeValue) {
            AvpDef avpDef;
            if (!Utils.isInteger(attributeValue)) {
                avpDef = Dictionary.getInstance().getAvpDefByName(attributeValue, applicationId);
            } else {
                avpDef = Dictionary.getInstance().getAvpDefByCode(Integer.parseInt(attributeValue),
                        applicationId);
            }

            if (null == avpDef) {
                //
                // If the code value is an integer, we don't necessary have to know it in the dictionnary.
                // However, if it isn't, we have to.
                //
            }

            //
            // Handle the code attribute
            //
            if (null != avpDef) {
                root.addAttribute("code", Integer.toString(avpDef.get_code()));
            }

            //
            // Handle the type attribute
            //
            if (null == root.attribute("type") && null != avpDef) {
                TypeDef typeDef = avpDef.get_type();
                if (null != typeDef) {
                    while (null != typeDef.get_type_parent()) {
                        if (typeDef.get_type_name().equalsIgnoreCase("AppId"))
                            isTypeAppId = true;
                        if (typeDef.get_type_name().equalsIgnoreCase("VendorId"))
                            isTypeVendorId = true;
                        typeDef = typeDef.get_type_parent();
                    }
                    root.addAttribute("type", typeDef.get_type_name());
                }
            }

            //
            // Handle the vendorId attribute
            //
            if (null == root.attribute("vendorId") && null != avpDef) {
                VendorDef vendorDef = avpDef.get_vendor_id();
                if (null != vendorDef) {
                    root.addAttribute("vendorId", Integer.toString(vendorDef.get_code()));
                }
            }

            //
            // Handle the mandatory attribute
            //
            if (null == root.attribute("mandatory")) {
                if (null != avpDef && null != avpDef.get_mandatory()
                        && avpDef.get_mandatory().equals("mustnot")) {
                    root.addAttribute("mandatory", "false");
                } else {
                    root.addAttribute("mandatory", "true");
                }
            }

            //
            // Handle the private attribute
            //
            if (null == root.attribute("private") && null != avpDef) {
                if (null != avpDef && null != avpDef.get_protected()
                        && avpDef.get_protected().equals("mustnot")) {
                    root.addAttribute("private", "false");
                } else {
                    root.addAttribute("private", "true");
                }
            }

            //
            // Parse the enumerated value that could be present in "value"
            //
            if (null != root.attribute("value") && null != avpDef) {
                String enumName = root.attributeValue("value");
                long enumValue = avpDef.getEnumCodeByName(enumName);
                if (enumValue != -1) {
                    root.attribute("value").setValue(Long.toString(enumValue));
                }
            }
        } else {
            throw new ParsingException(
                    "in element: " + unmodifiedRoot + "\n" + "code is a mandatory attribute");
        }

        //
        // Set the vendorId code (in case it isn't referenced by the avp Code via dictionnary, or overwritten).
        //
        attributeValue = root.attributeValue("vendorId");
        if (null != attributeValue) {
            if (!Utils.isInteger(attributeValue)) {
                VendorDef vendorDef = Dictionary.getInstance().getVendorDefByName(attributeValue,
                        applicationId);
                if (null != vendorDef) {
                    root.attribute("vendorId").setValue(Integer.toString(vendorDef.get_code()));
                } else {
                    throw new ParsingException("in element: " + unmodifiedRoot + "\n" + attributeValue
                            + " is not a valid vendor id in element");
                }
            }
        }

        //
        // Set the top-parent type (in case it isn't referenced by the avp Code via dictionnary, or overwritten).
        //
        if (root.elements().size() > 0) {
            root.addAttribute("type", "grouped");
        }

        attributeValue = root.attributeValue("type");
        if (null != attributeValue) {
            if (!attributeValue.equalsIgnoreCase("grouped")) {
                if (null != attributeValue) {
                    TypeDef typeDef = Dictionary.getInstance().getTypeDefByName(attributeValue, applicationId);
                    if (null != typeDef) {
                        while (null != typeDef && null != typeDef.get_type_parent()) {
                            if (typeDef.get_type_name().equalsIgnoreCase("AppId"))
                                isTypeAppId = true;
                            if (typeDef.get_type_name().equalsIgnoreCase("VendorId"))
                                isTypeVendorId = true;
                            typeDef = typeDef.get_type_parent();
                        }
                        root.attribute("type").setValue(typeDef.get_type_name());
                    } else {
                        throw new ParsingException("In element: " + unmodifiedRoot + "\n" + attributeValue
                                + " is not a valid type");
                    }
                }

            }
        }

        //
        // Handle the value in case it is an appId or vendorId avp, enum should have already been handled at this point
        //
        attributeValue = root.attributeValue("value");
        if (null != attributeValue) {
            if (isTypeAppId) {
                Application anApplication = Dictionary.getInstance().getApplication(attributeValue);
                if (null != anApplication) {
                    root.attribute("value").setValue(Integer.toString(anApplication.get_id()));
                }
            }
            if (isTypeVendorId) {
                VendorDef vendorDef = Dictionary.getInstance().getVendorDefByName(attributeValue,
                        applicationId);
                if (null != vendorDef) {
                    root.attribute("value").setValue(Integer.toString(vendorDef.get_code()));
                }
            }
        } else {
            if (!root.attributeValue("type").equalsIgnoreCase("grouped")) {
                throw new ParsingException("in element: " + unmodifiedRoot + "\n"
                        + "value is a mandatory attribute for element <avp .../> if it is not a grouped avp");
            }
        }
    }

    if (recurse) {
        List<Element> list = root.elements();
        for (Element element : list) {
            doDictionnary(element, applicationId, recurse);
        }
    }
}

From source file:com.devoteam.srit.xmlloader.gui.frames.JFrameEditableParameters.java

License:Open Source License

@Override
public void setVisible(boolean b) {
    if (b) {/*  w  w w .  ja  va  2 s  .  co  m*/
        valueChanged = false;
        for (Element e : this.elements)
            elementsOrigin.add(e.createCopy());
    } else {
        elementsOrigin.clear();
        valueChanged = false;
    }
    super.setVisible(b);
}

From source file:com.devoteam.srit.xmlloader.gui.frames.JFrameEditableParameters.java

License:Open Source License

private void jButtonDefaultsActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonDefaultsActionPerformed
{//GEN-HEADEREND:event_jButtonDefaultsActionPerformed
    this.elements.clear();
    for (Element e : elementsDefault) {
        this.elements.add(e.createCopy());
    }//from  w  w  w  . j ava 2  s.  c om
    changeTable();
    this.jButtonOK.setEnabled(true);
    this.jButtonDefaults.setEnabled(false);
}

From source file:com.devoteam.srit.xmlloader.gui.frames.JFrameEditableParameters.java

License:Open Source License

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton1ActionPerformed
{//GEN-HEADEREND:event_jButton1ActionPerformed
    this.elements.clear();
    for (Element e : elementsOrigin)
        this.elements.add(e.createCopy());
    changeTable();/*from w ww .  j  a  va 2s. co  m*/
    this.setVisible(false);
}